This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.go
116 lines (100 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
110
111
112
113
114
115
116
// Package memorydb provides a memory-backed Kivik driver, intended for testing.
package memorydb
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
"sync"
"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
)
type memDriver struct{}
var _ driver.Driver = &memDriver{}
func init() {
kivik.Register("memory", &memDriver{})
}
type client struct {
version *driver.Version
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(name string, _ driver.Options) (driver.Client, error) {
return &client{
version: &driver.Version{
Version: Version,
Vendor: Vendor,
RawResponse: json.RawMessage(fmt.Sprintf(`{"version":"%s","vendor":{"name":"%s"}}`, Version, Vendor)),
},
dbs: make(map[string]*database),
}, nil
}
func (c *client) AllDBs(context.Context, driver.Options) ([]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, _ driver.Options) (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_$()+/-]*$")
validNames = map[string]struct{}{
"_users": {},
"_replicator": {},
}
)
func (c *client) CreateDB(ctx context.Context, dbName string, options driver.Options) error {
if exists, _ := c.DBExists(ctx, dbName, options); exists {
return statusError{status: http.StatusPreconditionFailed, error: errors.New("database exists")}
}
if _, ok := validNames[dbName]; !ok {
if !validDBName.MatchString(dbName) {
return statusError{status: http.StatusBadRequest, error: errors.New("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 driver.Options) error {
if exists, _ := c.DBExists(ctx, dbName, options); !exists {
return statusError{status: http.StatusNotFound, error: errors.New("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(dbName string, options driver.Options) (driver.DB, error) {
return &db{
client: c,
dbName: dbName,
db: c.dbs[dbName],
}, nil
}
// Version returns the configured server info.
func (c *client) Version(_ context.Context) (*driver.Version, error) {
return c.version, nil
}