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 pathdb.go
179 lines (157 loc) · 5.42 KB
/
db.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package memorydb
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"regexp"
"strings"
"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
)
var notYetImplemented = statusError{status: http.StatusNotImplemented, error: errors.New("kivik: not yet implemented in memory driver")}
// database is an in-memory database representation.
type db struct {
*client
dbName string
db *database
}
func (d *db) Query(context.Context, string, string, driver.Options) (driver.Rows, error) {
// FIXME: Unimplemented
return nil, notYetImplemented
}
func (d *db) Get(ctx context.Context, docID string, options driver.Options) (*driver.Document, error) {
if exists, _ := d.client.DBExists(ctx, d.dbName, nil); !exists {
return nil, statusError{status: http.StatusPreconditionFailed, error: errors.New("database does not exist")}
}
if !d.db.docExists(docID) {
return nil, statusError{status: http.StatusNotFound, error: errors.New("missing")}
}
opts := map[string]interface{}{}
options.Apply(opts)
if rev, ok := opts["rev"].(string); ok {
if doc, found := d.db.getRevision(docID, rev); found {
return &driver.Document{
Rev: rev,
Body: io.NopCloser(bytes.NewReader(doc.data)),
}, nil
}
return nil, statusError{status: http.StatusNotFound, error: errors.New("missing")}
}
last, _ := d.db.latestRevision(docID)
if last.Deleted {
return nil, statusError{status: http.StatusNotFound, error: errors.New("missing")}
}
return &driver.Document{
Rev: fmt.Sprintf("%d-%s", last.ID, last.Rev),
Body: io.NopCloser(bytes.NewReader(last.data)),
}, nil
}
func (d *db) CreateDoc(ctx context.Context, doc interface{}, _ driver.Options) (docID, rev string, err error) {
if exists, _ := d.client.DBExists(ctx, d.dbName, nil); !exists {
return "", "", statusError{status: http.StatusPreconditionFailed, error: errors.New("database does not exist")}
}
couchDoc, err := toCouchDoc(doc)
if err != nil {
return "", "", err
}
if id, ok := couchDoc["_id"].(string); ok {
docID = id
} else {
docID = randStr()
}
rev, err = d.Put(ctx, docID, doc, nil)
return docID, rev, err
}
func (d *db) Put(ctx context.Context, docID string, doc interface{}, _ driver.Options) (rev string, err error) {
if exists, _ := d.client.DBExists(ctx, d.dbName, nil); !exists {
return "", statusError{status: http.StatusPreconditionFailed, error: errors.New("database does not exist")}
}
isLocal := strings.HasPrefix(docID, "_local/")
if !isLocal && docID[0] == '_' && !strings.HasPrefix(docID, "_design/") {
return "", statusError{status: http.StatusBadRequest, error: errors.New("only reserved document ids may start with underscore")}
}
couchDoc, err := toCouchDoc(doc)
if err != nil {
return "", err
}
couchDoc["_id"] = docID
// TODO: Add support for storing attachments.
delete(couchDoc, "_attachments")
if last, ok := d.db.latestRevision(docID); ok {
if !last.Deleted && !isLocal && couchDoc.Rev() != fmt.Sprintf("%d-%s", last.ID, last.Rev) {
return "", statusError{status: http.StatusConflict, error: errors.New("document update conflict")}
}
return d.db.addRevision(couchDoc), nil
}
if couchDoc.Rev() != "" {
// Rev should not be set for a new document
return "", statusError{status: http.StatusConflict, error: errors.New("document update conflict")}
}
return d.db.addRevision(couchDoc), nil
}
var revRE = regexp.MustCompile("^[0-9]+-[a-f0-9]{32}$")
func validRev(rev string) bool {
return revRE.MatchString(rev)
}
func (d *db) Delete(ctx context.Context, docID string, options driver.Options) (newRev string, err error) {
if exists, _ := d.client.DBExists(ctx, d.dbName, kivik.Params(nil)); !exists {
return "", statusError{status: http.StatusPreconditionFailed, error: errors.New("database does not exist")}
}
opts := map[string]interface{}{}
options.Apply(opts)
rev, _ := opts["rev"].(string)
if !strings.HasPrefix(docID, "_local/") && !validRev(rev) {
return "", statusError{status: http.StatusBadRequest, error: errors.New("invalid rev format")}
}
if !d.db.docExists(docID) {
return "", statusError{status: http.StatusNotFound, error: errors.New("missing")}
}
return d.Put(ctx, docID, map[string]interface{}{
"_id": docID,
"_rev": rev,
"_deleted": true,
}, nil)
}
func (d *db) Stats(_ context.Context) (*driver.DBStats, error) {
return &driver.DBStats{
Name: d.dbName,
// DocCount: 0,
// DeletedCount: 0,
// UpdateSeq: "",
// DiskSize: 0,
// ActiveSize: 0,
// ExternalSize: 0,
}, nil
}
func (c *client) Compact(_ context.Context) error {
// FIXME: Unimplemented
return notYetImplemented
}
func (d *db) CompactView(_ context.Context, _ string) error {
// FIXME: Unimplemented
return notYetImplemented
}
func (d *db) ViewCleanup(_ context.Context) error {
// FIXME: Unimplemented
return notYetImplemented
}
func (d *db) Changes(context.Context, driver.Options) (driver.Changes, error) {
// FIXME: Unimplemented
return nil, notYetImplemented
}
func (d *db) PutAttachment(context.Context, string, *driver.Attachment, driver.Options) (string, error) {
// FIXME: Unimplemented
return "", notYetImplemented
}
func (d *db) GetAttachment(context.Context, string, string, driver.Options) (*driver.Attachment, error) {
// FIXME: Unimplemented
return nil, notYetImplemented
}
func (d *db) DeleteAttachment(context.Context, string, string, driver.Options) (newRev string, err error) {
// FIXME: Unimplemented
return "", notYetImplemented
}
func (d *db) Close() error { return nil }