-
Notifications
You must be signed in to change notification settings - Fork 11
/
structures.go
77 lines (63 loc) · 2.6 KB
/
structures.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Package diskio implements basic operations for saving SKS related information
// on disk.
//
// Copyright (c) Facebook, Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package diskio
import (
"sync"
"github.com/peterbourgon/diskv"
)
// KeyValueStore is a thin wrapper around Diskv. It requires only the functions
// actually used here to allow us to mock calls. Accordingly, the documentation
// for the interface methods is basically the Diskv documentation, since that is
// the contract to be fulfilled.
type KeyValueStore interface {
// Read reads the key and returns the value.
Read(key string) ([]byte, error)
// Write synchronously writes the key-value pair to disk, making it
// immediately available for reads. Write relies on the filesystem to
// perform an eventual sync to physical media.
Write(key string, val []byte) error
// Has returns true if the given key exists.
Has(key string) bool
// Erase synchronously erases the given key from the disk (and the cache, if
// one is in use).
Erase(key string) error
// Keys returns a channel that will yield every key accessible by the store,
// in undefined order. If a cancel channel is provided, closing it will
// terminate and close the keys channel.
Keys(cancel <-chan struct{}) <-chan string
}
// Database defines an interface for a database utility
type Database interface {
// Load loads the data from the DB with the specified key.
Load(key string) ([]byte, error)
// Save puts bytes in the bucket. For a new item, set key to the empty
// string if you want a key automatically chosen for you. Returns the key
// used for saving data and any error that occurred.
Save(key string, value []byte) (string, error)
// HasKey returns true if the key is in the DB, false if not
HasKey(key string) bool
// Delete deletes a key from the database
Delete(key string) error
// List returns all keys
List() ([]string, error)
// Visit visits all keys in undefined order, calling visitor for each key-value pair.
Visit(visitor func(key string, val []byte) error) error
}
type diskKVStore struct {
db *diskv.Diskv
mux sync.RWMutex
}