-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory-p.js
More file actions
287 lines (233 loc) · 6.98 KB
/
Copy pathmemory-p.js
File metadata and controls
287 lines (233 loc) · 6.98 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const crypto = require('crypto')
const md5 = string => crypto.createHash('md5').update(string, 'binary').digest('hex')
// Memory P
//
// This is an example `this._store` object:
//
// {
// ids: {
// "mydoc": {
// winner: "ghi",
// branches: {
// "def": 2,
// "ghi": 2
// }
// }
// },
// revs: {
// "abc": {
// body: {
// foo: 'bar'
// }
// },
// "def": {
// parent: "abc"
// },
// "ghi": {
// body: {
// foo: 'qux'
// },
// parent: "abc"
// }
// }
// }
// Get a document by id from store.
// TODO: support find by rev
// Options can be
// * `revs`: include `_revisions` object
// * `conflicts`: include `_conflicts` array
const fromStore = (store, _id, { revs, conflicts }) => {
const info = store.ids[_id]
const revId = info.winner
const revPos = info.branches[info.winner]
const _rev = `${revPos}-${revId}`
// get the document body
const body = store.revs[revId].body
const doc = {
...body,
_id,
_rev
}
// get additional information if asked for
if (revs) doc._revisions = getRevisions(store.revs, info, revId)
if (conflicts) doc._conflicts = getConflicts(info, revId)
return doc
}
// return a revisions object including start and revision tree
const getRevisions = (revs, info, revId) => {
return {
start: info.branches[revId],
ids: getRevTree(revs, revId)
}
}
// if there are conflicts, get conflicts from branches
// and return full revisions (including position)
const getConflicts = (info, rev) => {
if (typeof info.branches !== 'object') return
if (Object.keys(info.branches).length <= 1) return
return Object.keys(info.branches)
.filter(id => id !== rev)
.map(rev => `${info.branches[rev]}-${rev}`)
.reverse()
}
const insertAsNewEdits = store => doc => {
// we need a doc id, otherwise we throw.
// in the future, we can generate an id if none is set.
if (typeof doc._id !== 'string') throw (new Error('No _id given'))
// get existing rev from store
const existingRevId = doc._id in store.ids && store.ids[doc._id].winner
// parse given document revision into position and id
const [givenRevPos, givenRevId] = parseRevision(doc._rev)
// ensure revision matches
if (existingRevId && existingRevId !== givenRevId) {
return {
id: doc._id,
error: 'conflict',
reason: 'Document update conflict.'
}
}
// strip doc from metadata
const body = stripMeta(doc)
// generate new rev
const newRevId = generateRevId(body, existingRevId)
const newRevPos = givenRevPos + 1
// calculate new rev
doc._rev = `${newRevPos}-${newRevId}`
// store the revision
store.revs[newRevId] = {
body,
parent: existingRevId
}
// store winner
store.ids[doc._id] = store.ids[doc._id] || { branches: {} }
store.ids[doc._id].winner = newRevId
store.ids[doc._id].branches[newRevId] = newRevPos
// delete previous winner branch
delete store.ids[doc._id].branches[givenRevId]
return {
ok: true,
id: doc._id,
rev: doc._rev
}
}
const insert = store => doc => {
// we need a doc id, otherwise we throw.
// in the future, we can generate an id if none is set.
if (typeof doc._id !== 'string') throw (new Error('No _id given'))
// parse given document revision into position and id
const [givenRevPos, givenRevId] = parseRevision(doc._rev)
// enforce presence of revision
if (!givenRevId) throw (new Error('no _rev given'))
// store the revision
store.revs[givenRevId] = {
body: doc
}
// loop over each node and create a revision if not existent
if (doc._revisions && doc._revisions.ids.length > 1) {
var previousRevId
var currentRevId
for (var i = 1; i < doc._revisions.ids.length; i++) {
previousRevId = doc._revisions.ids[i - 1]
currentRevId = doc._revisions.ids[i]
store.revs[previousRevId] = store.revs[previousRevId] || {}
// Possible optimisation:
// check for a conflict with current parent,
// see what couch does
// and maybe throw if parent does not match
store.revs[previousRevId].parent = currentRevId
// Another possible optimisation:
// stop walking the tree if parent is known
// delete branch
if (store.ids[doc._id].branches && currentRevId in store.ids[doc._id].branches) {
delete store.ids[doc._id].branches[currentRevId]
}
}
}
// get existing rev from store
const existingRevId = doc._id in store.ids && store.ids[doc._id].winner
const existingRevPos = (existingRevId && store.ids[doc._id].branches[existingRevId]) || 0
// store / update branches
store.ids[doc._id] = store.ids[doc._id] || { branches: {} }
store.ids[doc._id].branches[givenRevId] = givenRevPos
// choose winning rev
const winningRevId = chooseWinningRevId(
[givenRevPos, givenRevId],
[existingRevPos, existingRevId]
)
// store winner
store.ids[doc._id].winner = winningRevId
// return value with ok, id and new revision
return {
ok: true,
id: doc._id,
rev: doc._rev
}
}
// Mutate `store`, insert document
// Options can be
// * `new_edits`: if set to `false` do not generate new rev
const intoStore = (store, { new_edits = true }) => {
return new_edits ? insertAsNewEdits(store) : insert(store)
}
// walk up the parents and build array of ancestor rev ids
const getRevTree = (revs, rev) => rev in revs && revs[rev].parent ? [rev].concat(getRevTree(revs, revs[rev].parent)) : [rev]
// calculate winning rev id based on rev pos and alphabetical order of rev id
const chooseWinningRevId = ([aPos, aId], [bPos, bId]) => {
if (aPos > bPos) return aId
if (aPos < bPos) return bId
return aId > bId ? aId : bId
}
// generate revision id,
// that is a checksum over doc and rev
// without toplevel properties keys starting with `_`
const stripMeta = doc => {
const payload = {
...doc
}
Object.keys(payload)
.filter(key => key[0] === '_')
.forEach(key => delete payload[key])
return payload
}
const generateRevId = (doc, _rev) => md5(JSON.stringify({ ...doc, _rev }))
const parseRevision = rev => {
if (!rev) return [0]
const [pos, id] = rev.split('-')
return [
parseInt(pos, 10) || 0,
id
]
}
module.exports = class MemoryP {
constructor () {
this._store = { ids: {}, revs: {} }
}
reset (done) {
this._store = { ids: {}, revs: {} }
done(null)
}
// get documents
read (options = {}, done) {
if (typeof options === 'function') {
done = options
options = {}
}
const response = Object.keys(this._store.ids)
.sort()
.map(id => fromStore(this._store, id, options))
done(null, response)
}
// write documents
//
// options can be
// * new_edits: if set to false, do not generate new revisions, use provided rev
write (docs, options = {}, done) {
if (typeof options === 'function') {
done = options
options = {}
}
if (!Array.isArray(docs)) docs = [docs]
const response = docs.map(intoStore(this._store, options))
done(null, response)
}
}