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

3 4 5 6 7
import (
	"log"

	query "github.com/jbenet/go-datastore/query"
)
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) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
48 49
	delete(d.values, key)
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50 51
}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
63 64 65 66 67
// 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
68
// NewNullDatastore constructs a null datastoe
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
69 70
func NewNullDatastore() *NullDatastore {
	return &NullDatastore{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71 72
}

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

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

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

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
88
// Delete implements Datastore.Delete
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
89
func (d *NullDatastore) Delete(key Key) (err error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
90
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91 92
}

93 94 95
// Query implements Datastore.Query
func (d *NullDatastore) Query(q query.Query) (*query.Results, error) {
	return query.ResultsWithEntries(q, nil), nil
Jeromy's avatar
Jeromy committed
96 97
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98 99
// LogDatastore logs all accesses through the datastore.
type LogDatastore struct {
Jeromy's avatar
Jeromy committed
100
	Name  string
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
101 102 103 104 105 106 107 108
	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
109 110
}

Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
111 112
// NewLogDatastore constructs a log datastore.
func NewLogDatastore(ds Datastore, name string) Shim {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
113 114 115
	if len(name) < 1 {
		name = "LogDatastore"
	}
Juan Batiz-Benet's avatar
linting  
Juan Batiz-Benet committed
116 117 118 119 120 121
	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
122 123
}

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

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

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

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

149 150 151 152
// Query implements Datastore.Query
func (d *LogDatastore) Query(q query.Query) (*query.Results, error) {
	log.Printf("%s: Query\n", d.Name)
	return d.child.Query(q)
Jeromy's avatar
Jeromy committed
153
}