basic_ds.go 4.19 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2
package datastore

3
import (
Jeromy's avatar
Jeromy committed
4
	"io"
5 6
	"log"

7
	dsq "github.com/jbenet/go-datastore/query"
8
)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9 10 11 12

// Here are some basic datastore implementations.

type keyMap map[Key]interface{}
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
13 14

// MapDatastore uses a standard Go map for internal storage.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
type MapDatastore struct {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
16
	values keyMap
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17 18
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
19
// NewMapDatastore constructs a MapDatastore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20
func NewMapDatastore() (d *MapDatastore) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
21 22 23
	return &MapDatastore{
		values: keyMap{},
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24 25
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
26
// Put implements Datastore.Put
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27
func (d *MapDatastore) Put(key Key, value interface{}) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
28 29
	d.values[key] = value
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
32
// Get implements Datastore.Get
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
func (d *MapDatastore) Get(key Key) (value interface{}, err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
34 35 36 37 38
	val, found := d.values[key]
	if !found {
		return nil, ErrNotFound
	}
	return val, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
41
// Has implements Datastore.Has
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42
func (d *MapDatastore) Has(key Key) (exists bool, err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
43 44
	_, found := d.values[key]
	return found, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45 46
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
47
// Delete implements Datastore.Delete
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48
func (d *MapDatastore) Delete(key Key) (err error) {
49 50 51
	if _, found := d.values[key]; !found {
		return ErrNotFound
	}
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
52 53
	delete(d.values, key)
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54 55
}

56
// Query implements Datastore.Query
57 58
func (d *MapDatastore) Query(q dsq.Query) (dsq.Results, error) {
	re := make([]dsq.Entry, 0, len(d.values))
59
	for k, v := range d.values {
60
		re = append(re, dsq.Entry{Key: k.String(), Value: v})
Jeromy's avatar
Jeromy committed
61
	}
62 63
	r := dsq.ResultsWithEntries(q, re)
	r = dsq.NaiveQueryApply(q, r)
64
	return r, nil
Jeromy's avatar
Jeromy committed
65 66
}

Jeromy's avatar
Jeromy committed
67 68
func (d *MapDatastore) Batch() (Batch, error) {
	return NewBasicBatch(d), nil
Jeromy's avatar
Jeromy committed
69 70
}

Jeromy's avatar
Jeromy committed
71 72 73 74
func (d *MapDatastore) Close() error {
	return nil
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75 76 77 78 79
// NullDatastore stores nothing, but conforms to the API.
// Useful to test with.
type NullDatastore struct {
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
80
// NewNullDatastore constructs a null datastoe
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
81 82
func NewNullDatastore() *NullDatastore {
	return &NullDatastore{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83 84
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
85
// Put implements Datastore.Put
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
86
func (d *NullDatastore) Put(key Key, value interface{}) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
87
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
88 89
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
90
// Get implements Datastore.Get
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91
func (d *NullDatastore) Get(key Key) (value interface{}, err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
92
	return nil, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93 94
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
95
// Has implements Datastore.Has
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96
func (d *NullDatastore) Has(key Key) (exists bool, err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
97
	return false, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98 99
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
100
// Delete implements Datastore.Delete
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
101
func (d *NullDatastore) Delete(key Key) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
102
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
103 104
}

105
// Query implements Datastore.Query
106 107
func (d *NullDatastore) Query(q dsq.Query) (dsq.Results, error) {
	return dsq.ResultsWithEntries(q, nil), nil
Jeromy's avatar
Jeromy committed
108 109
}

Jeromy's avatar
Jeromy committed
110 111
func (d *NullDatastore) Batch() (Batch, error) {
	return NewBasicBatch(d), nil
Jeromy's avatar
Jeromy committed
112 113
}

Jeromy's avatar
Jeromy committed
114 115 116 117
func (d *NullDatastore) Close() error {
	return nil
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
118 119
// LogDatastore logs all accesses through the datastore.
type LogDatastore struct {
Jeromy's avatar
Jeromy committed
120
	Name  string
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
121 122 123 124 125 126 127 128
	child Datastore
}

// Shim is a datastore which has a child.
type Shim interface {
	Datastore

	Children() []Datastore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
129 130
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
131
// NewLogDatastore constructs a log datastore.
Jeromy's avatar
Jeromy committed
132
func NewLogDatastore(ds Datastore, name string) *LogDatastore {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
133 134 135
	if len(name) < 1 {
		name = "LogDatastore"
	}
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
136 137 138 139 140 141
	return &LogDatastore{Name: name, child: ds}
}

// Children implements Shim
func (d *LogDatastore) Children() []Datastore {
	return []Datastore{d.child}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
142 143
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
144
// Put implements Datastore.Put
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
145
func (d *LogDatastore) Put(key Key, value interface{}) (err error) {
Jeromy's avatar
Jeromy committed
146
	log.Printf("%s: Put %s\n", d.Name, key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
147
	// log.Printf("%s: Put %s ```%s```", d.Name, key, value)
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
148
	return d.child.Put(key, value)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
149 150
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
151
// Get implements Datastore.Get
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
152
func (d *LogDatastore) Get(key Key) (value interface{}, err error) {
Jeromy's avatar
Jeromy committed
153
	log.Printf("%s: Get %s\n", d.Name, key)
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
154
	return d.child.Get(key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
155 156
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
157
// Has implements Datastore.Has
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
158
func (d *LogDatastore) Has(key Key) (exists bool, err error) {
Jeromy's avatar
Jeromy committed
159
	log.Printf("%s: Has %s\n", d.Name, key)
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
160
	return d.child.Has(key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
161 162
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
163
// Delete implements Datastore.Delete
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
164
func (d *LogDatastore) Delete(key Key) (err error) {
Jeromy's avatar
Jeromy committed
165
	log.Printf("%s: Delete %s\n", d.Name, key)
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
166
	return d.child.Delete(key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
167
}
Jeromy's avatar
Jeromy committed
168

169
// Query implements Datastore.Query
170
func (d *LogDatastore) Query(q dsq.Query) (dsq.Results, error) {
171 172
	log.Printf("%s: Query\n", d.Name)
	return d.child.Query(q)
Jeromy's avatar
Jeromy committed
173
}
Jeromy's avatar
Jeromy committed
174

Jeromy's avatar
Jeromy committed
175 176
func (d *LogDatastore) Batch() (Batch, error) {
	log.Printf("%s: Batch\n", d.Name)
Jeromy's avatar
Jeromy committed
177 178
	if bds, ok := d.child.(Batching); ok {
		return bds.Batch()
Jeromy's avatar
Jeromy committed
179
	}
Jeromy's avatar
Jeromy committed
180 181 182 183 184 185 186 187 188
	return nil, ErrBatchUnsupported
}

func (d *LogDatastore) Close() error {
	log.Printf("%s: Close\n", d.Name)
	if cds, ok := d.child.(io.Closer); ok {
		return cds.Close()
	}
	return nil
Jeromy's avatar
Jeromy committed
189
}