basic_ds.go 2.45 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 8 9

// Here are some basic datastore implementations.

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

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

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

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

func (d *MapDatastore) Has(key Key) (exists bool, err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
33 34
	_, found := d.values[key]
	return found, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35 36 37
}

func (d *MapDatastore) Delete(key Key) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
38 39
	delete(d.values, key)
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40 41
}

42
func (d *MapDatastore) KeyList() ([]Key, error) {
Jeromy's avatar
Jeromy committed
43 44 45 46
	var keys []Key
	for k, _ := range d.values {
		keys = append(keys, k)
	}
47
	return keys, nil
Jeromy's avatar
Jeromy committed
48 49
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50 51 52 53 54
// NullDatastore stores nothing, but conforms to the API.
// Useful to test with.
type NullDatastore struct {
}

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
55 56
func NewNullDatastore() *NullDatastore {
	return &NullDatastore{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57 58 59
}

func (d *NullDatastore) Put(key Key, value interface{}) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
60
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61 62 63
}

func (d *NullDatastore) Get(key Key) (value interface{}, err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
64
	return nil, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65 66 67
}

func (d *NullDatastore) Has(key Key) (exists bool, err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
68
	return false, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69 70 71
}

func (d *NullDatastore) Delete(key Key) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
72
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74
}

75 76
func (d *NullDatastore) KeyList() ([]Key, error) {
	return nil, nil
Jeromy's avatar
Jeromy committed
77 78
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79 80
// LogDatastore logs all accesses through the datastore.
type LogDatastore struct {
Jeromy's avatar
Jeromy committed
81
	Name  string
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
82
	Child Datastore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83 84
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85 86 87 88 89
func NewLogDatastore(ds Datastore, name string) *LogDatastore {
	if len(name) < 1 {
		name = "LogDatastore"
	}
	return &LogDatastore{Name: name, Child: ds}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90 91 92
}

func (d *LogDatastore) Put(key Key, value interface{}) (err error) {
Jeromy's avatar
Jeromy committed
93
	log.Printf("%s: Put %s\n", d.Name, key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
94
	// log.Printf("%s: Put %s ```%s```", d.Name, key, value)
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
95
	return d.Child.Put(key, value)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96 97 98
}

func (d *LogDatastore) Get(key Key) (value interface{}, err error) {
Jeromy's avatar
Jeromy committed
99
	log.Printf("%s: Get %s\n", d.Name, key)
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
100
	return d.Child.Get(key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
101 102 103
}

func (d *LogDatastore) Has(key Key) (exists bool, err error) {
Jeromy's avatar
Jeromy committed
104
	log.Printf("%s: Has %s\n", d.Name, key)
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
105
	return d.Child.Has(key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
106 107 108
}

func (d *LogDatastore) Delete(key Key) (err error) {
Jeromy's avatar
Jeromy committed
109
	log.Printf("%s: Delete %s\n", d.Name, key)
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
110
	return d.Child.Delete(key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
111
}
Jeromy's avatar
Jeromy committed
112

113
func (d *LogDatastore) KeyList() ([]Key, error) {
Jeromy's avatar
Jeromy committed
114
	log.Printf("%s: Get KeyList\n", d.Name)
Jeromy's avatar
Jeromy committed
115 116
	return d.Child.KeyList()
}