forked from ipfs/go-datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_ds.go
248 lines (207 loc) · 6.34 KB
/
basic_ds.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package datastore
import (
"context"
"log"
dsq "github.com/ipfs/go-datastore/query"
)
// Here are some basic datastore implementations.
// MapDatastore uses a standard Go map for internal storage.
type MapDatastore struct {
values map[Key][]byte
}
var _ Datastore = (*MapDatastore)(nil)
var _ Batching = (*MapDatastore)(nil)
// NewMapDatastore constructs a MapDatastore. It is _not_ thread-safe by
// default, wrap using sync.MutexWrap if you need thread safety (the answer here
// is usually yes).
func NewMapDatastore() (d *MapDatastore) {
return &MapDatastore{
values: make(map[Key][]byte),
}
}
// Put implements Datastore.Put
func (d *MapDatastore) Put(ctx context.Context, key Key, value []byte) (err error) {
d.values[key] = value
return nil
}
// Sync implements Datastore.Sync
func (d *MapDatastore) Sync(ctx context.Context, prefix Key) error {
return nil
}
// Get implements Datastore.Get
func (d *MapDatastore) Get(ctx context.Context, key Key) (value []byte, err error) {
val, found := d.values[key]
if !found {
return nil, ErrNotFound
}
return val, nil
}
// Has implements Datastore.Has
func (d *MapDatastore) Has(ctx context.Context, key Key) (exists bool, err error) {
_, found := d.values[key]
return found, nil
}
// GetSize implements Datastore.GetSize
func (d *MapDatastore) GetSize(ctx context.Context, key Key) (size int, err error) {
if v, found := d.values[key]; found {
return len(v), nil
}
return -1, ErrNotFound
}
// Delete implements Datastore.Delete
func (d *MapDatastore) Delete(ctx context.Context, key Key) (err error) {
delete(d.values, key)
return nil
}
// Query implements Datastore.Query
func (d *MapDatastore) Query(ctx context.Context, q dsq.Query) (dsq.Results, error) {
re := make([]dsq.Entry, 0, len(d.values))
for k, v := range d.values {
e := dsq.Entry{Key: k.String(), Size: len(v)}
if !q.KeysOnly {
e.Value = v
}
re = append(re, e)
}
r := dsq.ResultsWithEntries(q, re)
r = dsq.NaiveQueryApply(q, r)
return r, nil
}
func (d *MapDatastore) Batch(ctx context.Context) (Batch, error) {
return NewBasicBatch(d), nil
}
func (d *MapDatastore) Close() error {
return nil
}
// LogDatastore logs all accesses through the datastore.
type LogDatastore struct {
Name string
child Datastore
}
var _ Datastore = (*LogDatastore)(nil)
var _ Batching = (*LogDatastore)(nil)
var _ GCDatastore = (*LogDatastore)(nil)
var _ PersistentDatastore = (*LogDatastore)(nil)
var _ ScrubbedDatastore = (*LogDatastore)(nil)
var _ CheckedDatastore = (*LogDatastore)(nil)
var _ Shim = (*LogDatastore)(nil)
// Shim is a datastore which has a child.
type Shim interface {
Datastore
Children() []Datastore
}
// NewLogDatastore constructs a log datastore.
func NewLogDatastore(ds Datastore, name string) *LogDatastore {
if len(name) < 1 {
name = "LogDatastore"
}
return &LogDatastore{Name: name, child: ds}
}
// Children implements Shim
func (d *LogDatastore) Children() []Datastore {
return []Datastore{d.child}
}
// Put implements Datastore.Put
func (d *LogDatastore) Put(ctx context.Context, key Key, value []byte) (err error) {
log.Printf("%s: Put %s\n", d.Name, key)
// log.Printf("%s: Put %s ```%s```", d.Name, key, value)
return d.child.Put(ctx, key, value)
}
// Sync implements Datastore.Sync
func (d *LogDatastore) Sync(ctx context.Context, prefix Key) error {
log.Printf("%s: Sync %s\n", d.Name, prefix)
return d.child.Sync(ctx, prefix)
}
// Get implements Datastore.Get
func (d *LogDatastore) Get(ctx context.Context, key Key) (value []byte, err error) {
log.Printf("%s: Get %s\n", d.Name, key)
return d.child.Get(ctx, key)
}
// Has implements Datastore.Has
func (d *LogDatastore) Has(ctx context.Context, key Key) (exists bool, err error) {
log.Printf("%s: Has %s\n", d.Name, key)
return d.child.Has(ctx, key)
}
// GetSize implements Datastore.GetSize
func (d *LogDatastore) GetSize(ctx context.Context, key Key) (size int, err error) {
log.Printf("%s: GetSize %s\n", d.Name, key)
return d.child.GetSize(ctx, key)
}
// Delete implements Datastore.Delete
func (d *LogDatastore) Delete(ctx context.Context, key Key) (err error) {
log.Printf("%s: Delete %s\n", d.Name, key)
return d.child.Delete(ctx, key)
}
// DiskUsage implements the PersistentDatastore interface.
func (d *LogDatastore) DiskUsage(ctx context.Context) (uint64, error) {
log.Printf("%s: DiskUsage\n", d.Name)
return DiskUsage(ctx, d.child)
}
// Query implements Datastore.Query
func (d *LogDatastore) Query(ctx context.Context, q dsq.Query) (dsq.Results, error) {
log.Printf("%s: Query\n", d.Name)
log.Printf("%s: q.Prefix: %s\n", d.Name, q.Prefix)
log.Printf("%s: q.KeysOnly: %v\n", d.Name, q.KeysOnly)
log.Printf("%s: q.Filters: %d\n", d.Name, len(q.Filters))
log.Printf("%s: q.Orders: %d\n", d.Name, len(q.Orders))
log.Printf("%s: q.Offset: %d\n", d.Name, q.Offset)
return d.child.Query(ctx, q)
}
// LogBatch logs all accesses through the batch.
type LogBatch struct {
Name string
child Batch
}
var _ Batch = (*LogBatch)(nil)
func (d *LogDatastore) Batch(ctx context.Context) (Batch, error) {
log.Printf("%s: Batch\n", d.Name)
if bds, ok := d.child.(Batching); ok {
b, err := bds.Batch(ctx)
if err != nil {
return nil, err
}
return &LogBatch{
Name: d.Name,
child: b,
}, nil
}
return nil, ErrBatchUnsupported
}
// Put implements Batch.Put
func (d *LogBatch) Put(ctx context.Context, key Key, value []byte) (err error) {
log.Printf("%s: BatchPut %s\n", d.Name, key)
// log.Printf("%s: Put %s ```%s```", d.Name, key, value)
return d.child.Put(ctx, key, value)
}
// Delete implements Batch.Delete
func (d *LogBatch) Delete(ctx context.Context, key Key) (err error) {
log.Printf("%s: BatchDelete %s\n", d.Name, key)
return d.child.Delete(ctx, key)
}
// Commit implements Batch.Commit
func (d *LogBatch) Commit(ctx context.Context) (err error) {
log.Printf("%s: BatchCommit\n", d.Name)
return d.child.Commit(ctx)
}
func (d *LogDatastore) Close() error {
log.Printf("%s: Close\n", d.Name)
return d.child.Close()
}
func (d *LogDatastore) Check(ctx context.Context) error {
if c, ok := d.child.(CheckedDatastore); ok {
return c.Check(ctx)
}
return nil
}
func (d *LogDatastore) Scrub(ctx context.Context) error {
if c, ok := d.child.(ScrubbedDatastore); ok {
return c.Scrub(ctx)
}
return nil
}
func (d *LogDatastore) CollectGarbage(ctx context.Context) error {
if c, ok := d.child.(GCDatastore); ok {
return c.CollectGarbage(ctx)
}
return nil
}