/
amazon.coffee
375 lines (332 loc) · 11.9 KB
/
amazon.coffee
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# This is an implementation of the OT data backend for Amazon Web Services.
#
# It uses Dynamo as the metadata store and it uses S3 for snapshot storage as
# Dynamo objects are limited to 64KB in size.
#
# In order to use this backend you must require the 'aws2js' and the 'dynamo'
# npm packages.
#
# Example usage:
#
# var connect = require('connect');
# var share = require('share').server;
#
# var server = connect(connect.logger());
#
# var options = {
# db: {
# type: 'amazon',
# amazon_region: 'us-east-1',
# amazon_access_key: '',
# amazon_secret_key: '',
# amazon_s3_snapshots_bucket_name: 'a-bucket-just-for-snapshots',
# amazon_dynamo_snapshots_table_name: 'a-dynamo-table-for-snapshots',
# amazon_dynamo_operations_table_name: 'a-dynamo-table-for-operations',
# }
# };
#
# share.attach(server, options);
# server.listen(9000);
#
# You can run bin/setup_amazon (after editing bin/options.js) to provision the
# required resources.
util = require('util')
async = require('async')
defaultOptions =
amazon_region: 'us-east-1'
module.exports = AmazonDb = (options) ->
return new Db if !(this instanceof AmazonDb)
options ?= {}
options[k] ?= v for k, v of defaultOptions
dynamo = require('dynamo')
s3 = require('aws2js').load('s3', options.amazon_access_key, options.amazon_secret_key);
client = dynamo.createClient({
accessKeyId: options.amazon_access_key,
secretAccessKey: options.amazon_secret_key
})
db = client.get(options.amazon_region)
snapshots_table = options.amazon_dynamo_snapshots_table_name
snapshots_bucket = options.amazon_s3_snapshots_bucket_name
operations_table = options.amazon_dynamo_operations_table_name
# Public: Creates a new document.
#
# docName - The unique name of the new document.
# docData = {snapshot, type:typename, [meta]}
#
# FIXME: What should happen if it fails to save?
#
# Calls callback(true) if the document already exists. Calls callback() on
# success.
@create = (docName, docData, callback) ->
async.auto(
write_metadata: (callback) ->
request =
TableName: snapshots_table,
Item:
doc: { S: docName },
v: { N: docData.v.toString() },
meta: { S: JSON.stringify(docData.meta) },
type: { S: docData.type },
created_at: { N: new Date().getTime().toString() }
Expected:
doc:
Exists: false
db.putItem(request, callback)
write_data: (callback) ->
path = options.amazon_s3_snapshots_bucket_name+'/'+docName+'-'+docData.v+'.snapshot'
headers = {}
data = JSON.stringify(docData.snapshot)
s3.put(path, headers, data, callback)
(error, result) ->
if error? and error.message.match 'The conditional request failed'
callback?('Document already exists')
else if error?
console.log('Failed to save Snapshot('+docName+'-'+docData.v+'): '+util.inspect(error))
callback?('Failure')
else
callback?()
)
# Public: Permanently deletes a document.
#
# docName - The name of the document to delete.
# dbMeta - ?
#
# Calls callback which takes a single argument which is null if something was
# deleted and the error message if something went wrong.
@delete = (docName, dbMeta, callback) ->
async.auto(
list_snapshots: (callback) ->
request =
TableName: snapshots_table
HashKeyValue: { S: docName }
ScanIndexForward: false
ConsistentRead: true
# TODO: This will only return the latest 1 MB of results, so if there
# are more keys additional requests must be made.
db.query(request, callback)
list_operations: (callback) ->
request =
TableName: operations_table
HashKeyValue: { S: docName }
ScanIndexForward: false
ConsistentRead: true
# TODO: This will only return the latest 1 MB of results, so if there
# are more keys additional requests must be made.
db.query(request, callback)
delete_snapshots: ['list_snapshots', (callback, results) ->
return callback("Document does not exist", null) if results.list_snapshots.Count == 0
async.mapSeries(results.list_snapshots.Items,
(item, cb) ->
request =
TableName: snapshots_table
Key:
HashKeyElement: { S : item.doc.S }
RangeKeyElement: { N : item.v.N }
Expected:
doc:
Value: { S: item.doc.S }
db.deleteItem(request, cb)
(error, result)->
if error?
callback(error, null)
else
callback(null, true)
)
]
delete_s3_snapshots: ['list_snapshots', (callback, results) ->
return callback(null, {}) if results.list_snapshots.Count == 0
async.forEachSeries(results.list_snapshots.Items,
(item, cb) ->
s3.del('/'+snapshots_bucket+'/'+item.doc.S+'-'+item.v.N+'.snapshot', cb)
(error)->
if error?
callback(error, null)
else
callback(null, true)
)
]
delete_operations: ['list_operations', (callback, results) ->
return callback(null, {}) if results.list_operations.Count == 0
async.forEachSeries(results.list_operations.Items,
(item, cb) ->
request =
TableName: operations_table
Key:
HashKeyElement: { S : item.doc.S }
RangeKeyElement: { N : item.v.N }
Expected:
doc:
Value: { S: item.doc.S }
db.deleteItem(request, cb)
(error)->
if error?
callback(error, null)
else
callback(null, true)
)
]
(error, result) ->
if error?
if error.toString().match "The conditional request failed"
error = "Document does not exist"
else if error != 'Document does not exist'
console.log('Failed to delete snapshots or operations from Document('+docName+'): '+util.inspect(error))
callback?(error)
else
callback?(null)
)
# Public: Retrieves the most recent snapshot
#
# docName - The name of the document to retrieve.
#
# data = {v, snapshot, type}. Snapshot == null and v = 0 if the document doesn't exist.
#
# Calls callback with (null, data) upon sucesss. Otherwise it calls callback with (error)
@getSnapshot = (docName, callback) ->
async.auto(
get_snapshot: (cb) ->
request =
TableName: snapshots_table
HashKeyValue: { S: docName }
ScanIndexForward: false
Limit: 1
ConsistentRead: true
db.query(request, cb)
get_data: ['get_snapshot', (cb, results) ->
return callback('Document does not exist', null) unless results.get_snapshot.Count == 1
item = results.get_snapshot.Items[0]
s3.get('/'+snapshots_bucket+'/'+item.doc.S+'-'+item.v.N+'.snapshot', 'buffer', cb)
]
(error, results) ->
if error?
if error == 'Document does not exist'
callback?(error)
if results.get_snapshot?
item = results.get_snapshot.Items[0]
callback?('Failed to get snapshot data for Document('+item.doc.S+'-'+item.v.N+'): '+util.inspect(error))
else
callback?('Failed to get snapshot metadata for Document('+docName+')')
else
item = results.get_snapshot.Items[0]
data =
v: parseInt(item.v.N)
snapshot: JSON.parse(results.get_data.buffer.toString())
type: item.type.S
meta: JSON.parse(item.meta.S)
callback?(null, data)
)
# Public: Write new snapshot data to the database.
#
# docName - Name of document.
# docData - Document snapshot data. {snapshot:s, type:t, meta}
# dbMeta - ?
#
# The callback just takes an optional error.
#
# This function has UNDEFINED BEHAVIOUR if you call append before calling create().
@writeSnapshot = (docName, docData, dbMeta, callback) ->
async.auto(
write_metadata: (callback) ->
request =
TableName: snapshots_table,
Item:
doc: { S: docName },
v: { N: docData.v.toString() },
meta: { S: JSON.stringify(docData.meta) },
type: { S: docData.type },
created_at: { N: new Date().getTime().toString() }
Expected:
doc:
Exists: false
db.putItem(request, callback)
write_data: (callback) ->
path = options.amazon_s3_snapshots_bucket_name+'/'+docName+'-'+docData.v+'.snapshot'
headers = {}
data = JSON.stringify(docData.snapshot)
s3.put(path, headers, data, callback)
(error, result) ->
if error? and error.message.match 'The conditional request failed'
callback?('Document already exists')
else if error?
console.log('Failed to save Snapshot('+docName+'-'+docData.v+'): '+util.inspect(error))
callback?('Failure')
else
callback?()
)
# Public: Get all ops with version = start to version = end. Noninclusive.
# end is trimmed to the size of the document.
#
# If any documents are passed to the callback, the first one has v = start
# end can be null. If so, returns all documents from start onwards.
#
# Each document returned is in the form {op:o, meta:m, v:version}.
#
# docName - Name of the document
# start - The noninclusive starting version.
# end - The noninclusive ending version.
#
# Calls callback(null, data) on success and otherwise calls the callback with
# just the error.
@getOps = (docName, start, end, callback) ->
end = 2147483648 unless end?
return callback("Start must be less than end", []) if start >= end
end = end - 1
async.auto(
get_metadata: (cb) ->
request =
TableName: operations_table
HashKeyValue: { S: docName }
ConsistentRead: true
RangeKeyCondition:
ComparisonOperator: 'BETWEEN'
AttributeValueList: [{ N: start.toString() }, { N: end.toString() }]
# TODO: This is limited to returning 1MB of data at a time, we should
# handle getting more.
db.query(request, cb)
(error, result) ->
if error?
console.log('Failed to fetch Operations('+docName+'-'+start+'..'+end+'): '+util.inspect(error))
callback?('Failure')
else
data = []
for op in result.get_metadata.Items
item = {
op: JSON.parse(op.op.S)
meta: JSON.parse(op.meta.S)
}
data.push(item)
callback? null, data
)
# Public: Write an operation to a document.
#
# opData = { op:the op to append, v:version, meta:optional }
# callback = callback when operation committed
#
# opData.v MUST be the subsequent version for the document.
#
# This function has UNDEFINED BEHAVIOUR if you call append before calling create().
# (its either that, or I have _another_ check when you append an op that the document already exists
# ... and that would slow it down a bit.)
@writeOp = (docName, opData, callback) ->
async.auto(
write_metadata: (cb) ->
request =
TableName: operations_table,
Item:
doc: { S: docName },
v: { N: opData.v.toString() },
op: { S: JSON.stringify(opData.op) },
meta: { S: JSON.stringify(opData.meta) },
db.putItem(request, cb)
(error, result) ->
if error?
console.log('Failed to save Operation('+docName+'-'+opData.v+'): '+util.inspect(error))
callback?('Failure')
else
callback?()
)
# Public: Call on close
#
# It's a noop here.
@close = () ->
this