forked from uktrade/sqlite-s3vfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite_ddbvfs.py
561 lines (504 loc) · 20.6 KB
/
sqlite_ddbvfs.py
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import uuid
import apsw
from apsw import BusyError
from boto3.dynamodb.conditions import Key
from boto3.dynamodb.conditions import Attr
from botocore.exceptions import ClientError
from boto3.dynamodb.conditions import Attr
class DDBVFS(apsw.VFS):
def __init__(self, table, block_size=4060):
self.name = f'ddbvfs-{str(uuid.uuid4())}'
self._table = table
self._block_size = block_size
super().__init__(name=self.name, base='')
def xAccess(self, pathname, flags):
#check that ACCESS with range pathname exists
result = self._table.query(KeyConditionExpression=Key('key').eq('ACCESS') & Key('range').eq(pathname),
ConsistentRead = True)
return (
flags == apsw.mapping_access["SQLITE_ACCESS_EXISTS"]
and ( result['Count'] > 0 )
) or (
flags != apsw.mapping_access["SQLITE_ACCESS_EXISTS"]
)
def xFullPathname(self, filename):
return filename
def xDelete(self, pathname, syncdir):
lek = None
while True:
if lek:
response = self._table.query( KeyConditionExpression=Key('key').eq(f'BL_{pathname}'),
ConsistentRead = True,
ExclusiveStartKey = lek )
else:
response = self._table.query(KeyConditionExpression=Key('key').eq(f'BL_{pathname}'),
ConsistentRead = True)
for obj in response['Items']:
#delete
self._table.delete_item(Key={'key': obj['key'], 'range': obj['range']})
if 'LastEvaluatedKey' not in response:
break
lek = response['LastEvaluatedKey']
self._table.delete_item(Key={'key': 'ACCESS', 'range': pathname})
def xOpen(self, pathname, flags):
return DDBVFSFile(pathname, flags, self._table, self._block_size)
def serialize_iter(self, pathname):
lek = None
while True:
if lek:
response = self._table.query( KeyConditionExpression=Key('key').eq(f'BL_{pathname}'),
ConsistentRead = True,
ExclusiveStartKey = lek )
else:
response = self._table.query(KeyConditionExpression=Key('key').eq(f'BL_{pathname}'),
ConsistentRead = True)
for obj in response['Items']:
yield obj['data'].value
if 'LastEvaluatedKey' not in response:
break
lek = response['LastEvaluatedKey']
def serialize_fileobj(self, key_prefix):
chunk = b''
offset = 0
it = iter(self.serialize_iter(key_prefix))
def up_to_iter(num):
nonlocal chunk, offset
while num:
if offset == len(chunk):
try:
chunk = next(it)
except StopIteration:
break
else:
offset = 0
to_yield = min(num, len(chunk) - offset)
offset = offset + to_yield
num -= to_yield
yield chunk[offset - to_yield:offset]
class FileLikeObj:
def read(self, n=-1):
n = \
n if n != -1 else \
4294967294 * 65536 # max size of SQLite file
return b''.join(up_to_iter(n))
return FileLikeObj()
def deserialize_iter(self, key_prefix, bytes_iter):
chunk = b''
offset = 0
it = iter(bytes_iter)
def up_to_iter(num):
nonlocal chunk, offset
while num:
if offset == len(chunk):
try:
chunk = next(it)
except StopIteration:
break
else:
offset = 0
to_yield = min(num, len(chunk) - offset)
offset = offset + to_yield
num -= to_yield
yield chunk[offset - to_yield:offset]
def block_bytes_iter():
while True:
block = b''.join(up_to_iter(self._block_size))
if not block:
break
yield block
total = 0
for block, block_bytes in enumerate(block_bytes_iter()):
size =len(block_bytes)
total += size
self._table.put_item(
Item={
'key': f'BL_{key_prefix}',
'range': f'{block:010d}',
'data': block_bytes,
'size': size
},
ReturnValues='ALL_OLD'
)
#set totals
self._table.put_item(
Item={
'key': f'FSIZE',
'range': key_prefix,
'size': total
}
)
class DDBVFSFile:
def __init__(self, name, flags, table, block_size):
if name == None:
name = f'temp-{str(uuid.uuid4())}'
self._key_prefix = \
self._key_prefix = name.filename() if isinstance(name, apsw.URIFilename) else \
name
#add random uuid for name if name is none
self._table = table
self._block_size = block_size
self._attained_log_level = 0
self._client_id = uuid.uuid4()
self._table.put_item(
Item={
'key': f'ACCESS',
'range': self._key_prefix
},
)
def _blocks(self, offset, amount):
while amount > 0:
block = offset // self._block_size # which block to get
start = offset % self._block_size # place in block to start
consume = min(self._block_size - start, amount)
yield (block, start, consume)
amount -= consume
offset += consume
def _block_get_bytes(self, block):
result = self._table.get_item(
Key={
'key': f'BL_{self._key_prefix}',
'range': f'{block:010d}'
},
AttributesToGet=[
'data'
],
ConsistentRead=True
)
ret = b''
if 'Item' in result:
ret = result['Item']['data'].value
return ret
def _block_put_bytes(self, block, bytes):
size =len(bytes)
result = self._table.put_item(
Item={
'key': f'BL_{self._key_prefix}',
'range': f'{block:010d}',
'data': bytes,
'size': size
},
ReturnValues='ALL_OLD'
)
assert(result['ResponseMetadata']['HTTPStatusCode'] == 200)
dsize = size
if 'Attributes' in result:
dsize = size-int(result['Attributes']['size'])
return dsize
def _block_delete(self, block):
result = self._table.delete_item(
Key={
'key': f'BL_{self._key_prefix}',
'range': f'{block:010d}'
},
ReturnValues='ALL_OLD'
)
assert(result['ResponseMetadata']['HTTPStatusCode'] == 200)
dsize = -int(result['Attributes']['size'])
return dsize
def _update_total_size(self, dsize):
result = self._table.update_item(
Key={
'key': f'FSIZE',
'range': self._key_prefix
},
UpdateExpression='ADD size :dsize',
ExpressionAttributeValues={
':dsize': dsize
},
ReturnValues='UPDATED_NEW'
)
assert(result['ResponseMetadata']['HTTPStatusCode'] == 200)
def _get_total_size(self):
result = self._table.get_item(
Key={
'key': f'FSIZE',
'range': self._key_prefix
},
AttributesToGet=[
'size'
],
ConsistentRead=True
)
assert(result['ResponseMetadata']['HTTPStatusCode'] == 200)
if 'Item' in result:
return int(result['Item']['size'])
else:
return 0
def xRead(self, amount, offset):
try:
def _read():
for block, start, consume in self._blocks(offset, amount):
block_bytes = self._block_get_bytes(block)
yield block_bytes[start:start+consume]
return b"".join(_read())
except Exception as ex:
print("ERROR READING ", ex )
raise ex
def xSectorSize(self):
return 0
def xFileControl(self, *args):
return False
SQLITE_LOCK_NONE = 0
SQLITE_LOCK_SHARED = 1
SQLITE_LOCK_RESERVED = 2
SQLITE_LOCK_PENDING = 3
SQLITE_LOCK_EXCLUSIVE = 4
def _get_lock_state(self):
response = self._table.get_item(
Key={
'key': f'LK',
'range': self._key_prefix
},
AttributesToGet=[
'level',
'owner'
],
ConsistentRead=True
)
assert(response['ResponseMetadata']['HTTPStatusCode'] == 200)
if 'Item' in response and 'level' in response['Item']:
return int(response['Item']['level'])
if 'Item' in response and 'count' in response['Item'] and int(response['Item']['count'])>0:
return 1
return 0
def xCheckReservedLock(self):
return self._get_lock_state() >= self.SQLITE_LOCK_RESERVED
def xLock(self, level):
assert level != self.SQLITE_LOCK_NONE
assert level != self.SQLITE_LOCK_PENDING
if level == self._attained_log_level:
return
if level == self.SQLITE_LOCK_SHARED:
try:
response = self._table.update_item(
Key={
'key': f'LK',
'range': self._key_prefix
},
UpdateExpression='SET #cnt = if_not_exists(#cnt, :initial) + :increment',
ConditionExpression=Attr('level').not_exists() | Attr('level').eq(self.SQLITE_LOCK_RESERVED),
ExpressionAttributeValues={
':initial': 0,
':increment': 1
},
ExpressionAttributeNames={
'#cnt': 'count'
}
)
self._attained_log_level = self.SQLITE_LOCK_SHARED
return
except ClientError as e:
if e.response['Error']['Code'] == 'ConditionalCheckFailedException':
#someone else has reserved the lock
raise BusyError()
else:
raise e
else:
#start with a pending lock if we don't already have one
if self._attained_log_level != self.SQLITE_LOCK_PENDING:
try:
response = self._table.update_item(
Key={
'key': f'LK',
'range': self._key_prefix,
},
UpdateExpression = 'SET #lvl = :level, #own = :owner ',
ConditionExpression= (Attr('owner').not_exists() | Attr('owner').eq(f'{self._client_id}')),
ExpressionAttributeValues={
':level': self.SQLITE_LOCK_PENDING,
':owner': f'{self._client_id}'
},
ExpressionAttributeNames={
'#lvl': 'level',
'#own': 'owner'
}
)
self._attained_log_level = self.SQLITE_LOCK_PENDING
except ClientError as e:
if e.response['Error']['Code'] == 'ConditionalCheckFailedException':
#someone has a strong lock already
raise BusyError()
else:
raise e
#we're pending, try to upgrade to a hard lock
try:
if level == self.SQLITE_LOCK_RESERVED:
condition=Attr('owner').eq(f'{self._client_id}')
else:
#only go to exclusive lock after readers have drained (count = 1)
#if here we always at least own a pending lock
condition=Attr('owner').eq(f'{self._client_id}') & Attr('count').eq(1)
response =self._table.update_item(
Key={
'key': f'LK',
'range': self._key_prefix,
},
UpdateExpression = 'SET #lvl = :level, #own = :owner ',
ConditionExpression= condition,
ExpressionAttributeValues={
':level': level,
':owner': f'{self._client_id}'
},
ExpressionAttributeNames={
'#lvl': 'level',
'#own': 'owner'
}
)
self._attained_log_level = level
except ClientError as e:
if e.response['Error']['Code'] == 'ConditionalCheckFailedException':
#someone has a strong lock already
raise BusyError()
else:
raise e
def xUnlock(self, level):
if level == self._attained_log_level:
return
if level >= self.SQLITE_LOCK_RESERVED:
#downgrading lock from exclusive, but keeping exclusive
response = self._table.put_item(
Item={
'key': f'LK',
'range': self._key_prefix,
'level' : level,
'owner' : f'{self._client_id}',
'count': 1
},
ConditionExpression=Attr('owner').eq(f'{self._client_id}')
)
self._attained_log_level = level
if level == self.SQLITE_LOCK_SHARED:
#downgrading lock from exclusive, releasing exclusive
response = self._table.update_item(
Key={
'key': f'LK',
'range': self._key_prefix,
},
UpdateExpression='REMOVE #lvl, #own',
ConditionExpression=Attr('owner').eq(f'{self._client_id}'),
ExpressionAttributeNames={
'#lvl': 'level',
'#own': 'owner'
}
)
self._attained_log_level = level
if level == self.SQLITE_LOCK_NONE:
if self._attained_log_level > self.SQLITE_LOCK_SHARED:
try:
response = self._table.update_item(
Key={
'key': f'LK',
'range': self._key_prefix,
},
UpdateExpression='SET #cnt = #cnt - :increment REMOVE #lvl, #own',
ConditionExpression=Attr('owner').eq(f'{self._client_id}'),
ExpressionAttributeValues={
':increment': 1
},
ExpressionAttributeNames={
'#cnt': 'count',
'#lvl': 'level',
'#own': 'owner'
}
)
except Exception as e:
raise e
else:
increment = 1 if self._attained_log_level > 0 else 0
response = self._table.update_item(
Key={
'key': f'LK',
'range': self._key_prefix
},
UpdateExpression='SET #cnt = #cnt - :increment',
ExpressionAttributeValues={
':increment': increment
},
ExpressionAttributeNames={
'#cnt': 'count'
}
)
self._attained_log_level = level
def xClose(self):
pass
def xFileSize(self):
return self._get_total_size()
total = 0
lek = None
while True:
if lek:
response = self._table.query( KeyConditionExpression=Key('key').eq(f'BL_{self._key_prefix}'),
ExclusiveStartKey = lek,
ConsistentRead = True )
else:
response = self._table.query(KeyConditionExpression=Key('key').eq(f'BL_{self._key_prefix}'),
ConsistentRead = True)
for obj in response['Items']:
total += int(obj['size'])
if 'LastEvaluatedKey' not in response:
break
lek = response['LastEvaluatedKey']
stored_total = self._get_total_size()
return total
def xSync(self, flags):
return True
def xTruncate(self, newsize):
total = 0
dsize = 0
lek = None
while True:
if lek:
response = self._table.query( KeyConditionExpression=Key('key').eq(f'BL_{self._key_prefix}'),
ConsistentRead = True,
ExclusiveStartKey = lek )
else:
response = self._table.query(KeyConditionExpression=Key('key').eq(f'BL_{self._key_prefix}'),
ConsistentRead = True)
for obj in response['Items']:
total += int(obj['size'])
to_keep = max(int(obj['size']) - total + newsize, 0)
if to_keep == 0:
dsize += self._block_delete(int(obj['range']))
elif to_keep < int(obj['size']):
bytes = self._block_get_bytes(int(obj['range']))
dsize += self._block_put_bytes(int(obj['range']), bytes[:to_keep])
if 'LastEvaluatedKey' not in response:
break
lek = response['LastEvaluatedKey']
self._update_total_size(dsize)
return True
def xWrite(self, data, offset):
dsize = 0
try:
lock_page_offset = 1073741824
page_size = len(data)
if offset == lock_page_offset + page_size:
# Ensure the previous blocks have enough bytes for size calculations and serialization.
# SQLite seems to always write pages sequentially, except that it skips the byte-lock
# page, so we only check previous blocks if we know we're just after the byte-lock
# page.
data_first_block = offset // self._block_size
lock_page_block = lock_page_offset // self._block_size
for block in range(data_first_block - 1, lock_page_block - 1, -1):
original_block_bytes = self._block_get_bytes(block)
if len(original_block_bytes) == self._block_size:
break
dsize += self._block_put_bytes(block, original_block_bytes + bytes(
self._block_size - len(original_block_bytes)
))
data_offset = 0
for block, start, write in self._blocks(offset, len(data)):
data_to_write = data[data_offset:data_offset+write]
if start != 0 or len(data_to_write) != self._block_size:
original_block_bytes = self._block_get_bytes(block)
original_block_bytes = original_block_bytes + bytes(max(start - len(original_block_bytes), 0))
data_to_write = \
original_block_bytes[0:start] + \
data_to_write + \
original_block_bytes[start+write:]
data_offset += write
dsize += self._block_put_bytes(block, data_to_write)
self._update_total_size(dsize)
except Exception as ex:
print("ERROR WRITING ", ex)
raise ex