/
memory.go
109 lines (94 loc) · 2.84 KB
/
memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Package memory provides a memory-backed Kivik driver, intended for testing.
// This version of the package is deprecated, and no longer receiving updates.
// Please use github.com/go-kivik/memorydb instead.
package memory
import (
"context"
"net/http"
"regexp"
"sync"
"github.com/flimzy/kivik"
"github.com/flimzy/kivik/driver"
"github.com/flimzy/kivik/driver/common"
"github.com/flimzy/kivik/errors"
)
type memDriver struct{}
var _ driver.Driver = &memDriver{}
func init() {
kivik.Register("memory", &memDriver{})
}
type client struct {
*common.Client
mutex sync.RWMutex
dbs map[string]*database
}
var _ driver.Client = &client{}
// Identifying constants
const (
Version = "0.0.1"
Vendor = "Kivik Memory Adaptor"
)
func (d *memDriver) NewClient(_ context.Context, name string) (driver.Client, error) {
return &client{
Client: common.NewClient(Version, Vendor),
dbs: make(map[string]*database),
}, nil
}
func (c *client) AllDBs(_ context.Context, _ map[string]interface{}) ([]string, error) {
dbs := make([]string, 0, len(c.dbs))
for k := range c.dbs {
dbs = append(dbs, k)
}
return dbs, nil
}
func (c *client) DBExists(_ context.Context, dbName string, _ map[string]interface{}) (bool, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
_, ok := c.dbs[dbName]
return ok, nil
}
// Copied verbatim from http://docs.couchdb.org/en/2.0.0/api/database/common.html#head--db
var validDBName = regexp.MustCompile("^[a-z][a-z0-9_$()+/-]*$")
var validNames = map[string]struct{}{
"_users": struct{}{},
"_replicator": struct{}{},
}
func (c *client) CreateDB(ctx context.Context, dbName string, options map[string]interface{}) error {
if exists, _ := c.DBExists(ctx, dbName, options); exists {
return errors.Status(http.StatusPreconditionFailed, "database exists")
}
if _, ok := validNames[dbName]; !ok {
if !validDBName.MatchString(dbName) {
return errors.Status(kivik.StatusBadRequest, "invalid database name")
}
}
c.mutex.Lock()
defer c.mutex.Unlock()
c.dbs[dbName] = &database{
docs: make(map[string]*document),
security: &driver.Security{},
}
return nil
}
func (c *client) DestroyDB(ctx context.Context, dbName string, options map[string]interface{}) error {
if exists, _ := c.DBExists(ctx, dbName, options); !exists {
return errors.Status(http.StatusNotFound, "database does not exist")
}
c.mutex.Lock()
defer c.mutex.Unlock()
c.dbs[dbName].mu.Lock()
defer c.dbs[dbName].mu.Unlock()
c.dbs[dbName].deleted = true // To invalidate any outstanding db handles
delete(c.dbs, dbName)
return nil
}
func (c *client) DB(ctx context.Context, dbName string, options map[string]interface{}) (driver.DB, error) {
if exists, _ := c.DBExists(ctx, dbName, options); !exists {
return nil, errors.Status(http.StatusNotFound, "database does not exist")
}
return &db{
client: c,
dbName: dbName,
db: c.dbs[dbName],
}, nil
}