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

Jeromy's avatar
Jeromy committed
3
import "log"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4 5 6 7

// Here are some basic datastore implementations.

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

// MapDatastore uses a standard Go map for internal storage.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
type MapDatastore struct {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
11
	values keyMap
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13
}

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

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

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

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

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
42
// Delete implements Datastore.Delete
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43
func (d *MapDatastore) Delete(key Key) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
44 45
	delete(d.values, key)
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46 47
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
48
// KeyList implements Datastore.KeyList
49
func (d *MapDatastore) KeyList() ([]Key, error) {
Jeromy's avatar
Jeromy committed
50
	var keys []Key
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
51
	for k := range d.values {
Jeromy's avatar
Jeromy committed
52 53
		keys = append(keys, k)
	}
54
	return keys, nil
Jeromy's avatar
Jeromy committed
55 56
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57 58 59 60 61
// 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
62
// NewNullDatastore constructs a null datastoe
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
63 64
func NewNullDatastore() *NullDatastore {
	return &NullDatastore{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65 66
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
67
// Put implements Datastore.Put
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68
func (d *NullDatastore) Put(key Key, value interface{}) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
69
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70 71
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
72
// Get implements Datastore.Get
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73
func (d *NullDatastore) Get(key Key) (value interface{}, err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
74
	return nil, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75 76
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
77
// Has implements Datastore.Has
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78
func (d *NullDatastore) Has(key Key) (exists bool, err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
79
	return false, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
82
// Delete implements Datastore.Delete
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83
func (d *NullDatastore) Delete(key Key) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
84
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85 86
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
87
// KeyList implements Datastore.KeyList
88 89
func (d *NullDatastore) KeyList() ([]Key, error) {
	return nil, nil
Jeromy's avatar
Jeromy committed
90 91
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92 93
// LogDatastore logs all accesses through the datastore.
type LogDatastore struct {
Jeromy's avatar
Jeromy committed
94
	Name  string
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
95 96 97 98 99 100 101 102
	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
103 104
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
105 106
// NewLogDatastore constructs a log datastore.
func NewLogDatastore(ds Datastore, name string) Shim {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
107 108 109
	if len(name) < 1 {
		name = "LogDatastore"
	}
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
110 111 112 113 114 115
	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
116 117
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
118
// Put implements Datastore.Put
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
119
func (d *LogDatastore) Put(key Key, value interface{}) (err error) {
Jeromy's avatar
Jeromy committed
120
	log.Printf("%s: Put %s\n", d.Name, key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
121
	// log.Printf("%s: Put %s ```%s```", d.Name, key, value)
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
122
	return d.child.Put(key, value)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
123 124
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
125
// Get implements Datastore.Get
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
126
func (d *LogDatastore) Get(key Key) (value interface{}, err error) {
Jeromy's avatar
Jeromy committed
127
	log.Printf("%s: Get %s\n", d.Name, key)
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
128
	return d.child.Get(key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
129 130
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
131
// Has implements Datastore.Has
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
132
func (d *LogDatastore) Has(key Key) (exists bool, err error) {
Jeromy's avatar
Jeromy committed
133
	log.Printf("%s: Has %s\n", d.Name, key)
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
134
	return d.child.Has(key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
135 136
}

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

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