basic_ds.go 1.99 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 44 45 46 47 48
}

// 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
49 50
func NewNullDatastore() *NullDatastore {
	return &NullDatastore{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51 52 53
}

func (d *NullDatastore) Put(key Key, value interface{}) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
54
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55 56 57
}

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

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

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

// LogDatastore logs all accesses through the datastore.
type LogDatastore struct {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
71
	Child Datastore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72 73
}

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
74 75
func NewLogDatastore(ds Datastore) *LogDatastore {
	return &LogDatastore{Child: ds}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76 77 78
}

func (d *LogDatastore) Put(key Key, value interface{}) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
79 80
	log.Printf("LogDatastore: Put %s", key)
	return d.Child.Put(key, value)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81 82 83
}

func (d *LogDatastore) Get(key Key) (value interface{}, err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
84 85
	log.Printf("LogDatastore: Get %s", key)
	return d.Child.Get(key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
86 87 88
}

func (d *LogDatastore) Has(key Key) (exists bool, err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
89 90
	log.Printf("LogDatastore: Has %s", key)
	return d.Child.Has(key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91 92 93
}

func (d *LogDatastore) Delete(key Key) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
94 95
	log.Printf("LogDatastore: Delete %s", key)
	return d.Child.Delete(key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96
}