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

import (
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
4
	"log"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5 6 7 8 9 10 11
)

// 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
12
	values keyMap
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14 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 21
}

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

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

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52 53 54 55 56
// 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
57 58
func NewNullDatastore() *NullDatastore {
	return &NullDatastore{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59 60 61
}

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

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

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

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

Jeromy's avatar
Jeromy committed
77 78 79 80
func (d *NullDatastore) KeyList() []Key {
	return nil
}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87 88 89 90 91
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
92 93 94
}

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

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

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

func (d *LogDatastore) Delete(key Key) (err error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
111
	log.Printf("%s: Delete %s", d.Name, key)
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
112
	return d.Child.Delete(key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
113
}
Jeromy's avatar
Jeromy committed
114 115 116 117 118

func (d *LogDatastore) KeyList() []Key {
	log.Printf("%s: Get KeyList.", d.Name)
	return d.Child.KeyList()
}