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

3 4 5
import (
	"log"

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

// Here are some basic datastore implementations.

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

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

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

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

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

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

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

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

Jeromy's avatar
Jeromy committed
66 67 68 69
func (d *MapDatastore) StartBatchOp() Transaction {
	return NewBasicTransaction(d)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70 71 72 73 74
// 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
75
// NewNullDatastore constructs a null datastoe
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
76 77
func NewNullDatastore() *NullDatastore {
	return &NullDatastore{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78 79
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
80
// Put implements Datastore.Put
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81
func (d *NullDatastore) Put(key Key, value interface{}) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
82
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83 84
}

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

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

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

100
// Query implements Datastore.Query
101 102
func (d *NullDatastore) Query(q dsq.Query) (dsq.Results, error) {
	return dsq.ResultsWithEntries(q, nil), nil
Jeromy's avatar
Jeromy committed
103 104
}

Jeromy's avatar
Jeromy committed
105 106 107 108
func (d *NullDatastore) StartBatchOp() Transaction {
	return NewBasicTransaction(d)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
109 110
// LogDatastore logs all accesses through the datastore.
type LogDatastore struct {
Jeromy's avatar
Jeromy committed
111
	Name  string
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
112 113 114 115 116 117 118 119
	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
120 121
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
122 123
// NewLogDatastore constructs a log datastore.
func NewLogDatastore(ds Datastore, name string) Shim {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
124 125 126
	if len(name) < 1 {
		name = "LogDatastore"
	}
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
127 128 129 130 131 132
	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
133 134
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
135
// Put implements Datastore.Put
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
136
func (d *LogDatastore) Put(key Key, value interface{}) (err error) {
Jeromy's avatar
Jeromy committed
137
	log.Printf("%s: Put %s\n", d.Name, key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
138
	// log.Printf("%s: Put %s ```%s```", d.Name, key, value)
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
139
	return d.child.Put(key, value)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140 141
}

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

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
148
// Has implements Datastore.Has
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
149
func (d *LogDatastore) Has(key Key) (exists bool, err error) {
Jeromy's avatar
Jeromy committed
150
	log.Printf("%s: Has %s\n", d.Name, key)
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
151
	return d.child.Has(key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
152 153
}

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

160
// Query implements Datastore.Query
161
func (d *LogDatastore) Query(q dsq.Query) (dsq.Results, error) {
162 163
	log.Printf("%s: Query\n", d.Name)
	return d.child.Query(q)
Jeromy's avatar
Jeromy committed
164
}
Jeromy's avatar
Jeromy committed
165 166 167 168 169

func (d *LogDatastore) StartBatchOp() Transaction {
	log.Printf("%s: StartBatchOp\n", d.Name)
	return d.child.StartBatchOp()
}