-
Notifications
You must be signed in to change notification settings - Fork 41
/
store.go
167 lines (150 loc) · 3.31 KB
/
store.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
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package memorydb
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"
"strings"
"sync"
"time"
"github.com/go-kivik/kivik/v4/driver"
)
type file struct {
ContentType string
Data []byte
}
type document struct {
revs []*revision
}
type revision struct {
data []byte
ID int64
Rev string
Deleted bool
Attachments map[string]file
}
type database struct {
mu sync.RWMutex
docs map[string]*document
deleted bool
security *driver.Security
}
var (
rnd *rand.Rand
rndMU = &sync.Mutex{}
)
func init() {
rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
}
func (d *database) getRevision(docID, rev string) (*revision, bool) {
d.mu.RLock()
defer d.mu.RUnlock()
doc, ok := d.docs[docID]
if !ok {
return nil, false
}
for _, r := range doc.revs {
if rev == fmt.Sprintf("%d-%s", r.ID, r.Rev) {
return r, true
}
}
return nil, false
}
func (d *database) docExists(docID string) bool {
d.mu.RLock()
defer d.mu.RUnlock()
_, ok := d.docs[docID]
return ok
}
func (d *database) latestRevision(docID string) (*revision, bool) {
d.mu.RLock()
defer d.mu.RUnlock()
doc, ok := d.docs[docID]
if ok {
last := doc.revs[len(doc.revs)-1]
return last, true
}
return nil, false
}
type couchDoc map[string]interface{}
func (d couchDoc) ID() string {
id, _ := d["_id"].(string)
return id
}
func (d couchDoc) Rev() string {
rev, _ := d["_rev"].(string)
return rev
}
func toCouchDoc(i interface{}) (couchDoc, error) {
if d, ok := i.(couchDoc); ok {
return d, nil
}
asJSON, err := json.Marshal(i)
if err != nil {
return nil, statusError{status: http.StatusBadRequest, error: err}
}
var m couchDoc
if e := json.Unmarshal(asJSON, &m); e != nil {
return nil, statusError{status: http.StatusInternalServerError, error: errors.New("THIS IS A BUG: failed to decode encoded document")}
}
return m, nil
}
func (d *database) addRevision(doc couchDoc) string {
d.mu.Lock()
defer d.mu.Unlock()
id, ok := doc["_id"].(string)
if !ok {
panic("_id missing or not a string")
}
isLocal := strings.HasPrefix(id, "_local/")
if d.docs[id] == nil {
d.docs[id] = &document{
revs: make([]*revision, 0, 1),
}
}
var revID int64
var revStr string
if isLocal {
revID = 1
revStr = "0"
} else {
l := len(d.docs[id].revs)
if l == 0 {
revID = 1
} else {
revID = d.docs[id].revs[l-1].ID + 1
}
revStr = randStr()
}
rev := fmt.Sprintf("%d-%s", revID, revStr)
doc["_rev"] = rev
data, err := json.Marshal(doc)
if err != nil {
panic(err)
}
deleted, _ := doc["_deleted"].(bool)
newRev := &revision{
data: data,
ID: revID,
Rev: revStr,
Deleted: deleted,
}
if isLocal {
d.docs[id].revs = []*revision{newRev}
} else {
d.docs[id].revs = append(d.docs[id].revs, newRev)
}
return rev
}