-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3stuff.py
204 lines (178 loc) · 6.19 KB
/
s3stuff.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
"""
s3stuff - s3 facing bits of an AWS backend
"""
import boto
from boto.key import Key
from ZODB.utils import u64, p64, z64
import time
from boto.exception import S3ResponseError
from boto.bucket import Bucket
RETRIES = 5
SLEEPTIME = 0.1
DEBUG = True
class S3Aspect:
'''A storage for Amazon S3 web service
'''
def __init__(self, aws_access_key_id, aws_secret_access_key, bucket_name):
conn = boto.connect_s3(aws_access_key_id, aws_secret_access_key)
self._bucket = Bucket(conn, bucket_name)
def lastCommit(self):
'''
Get the last committed tid and serial according to S3.
This may not be current
'''
prefix = 'type:index_serial,'
rs = self._bucket.get_all_keys(prefix=prefix, maxkeys=1)
if not rs:
# umm not created yet
return None, None
else:
info = dict_from_key(rs[0].key)
tid = tid_unrepr(info['tid'])
serial = serial_unrepr(info['serial'])
return tid, serial
def highestOid(self):
'''
Get the highest oid value
'''
prefix = 'type:record,'
rs = self._bucket.get_all_keys(prefix=prefix, maxkeys=1)
if not rs:
# umm not created yet
return None
else:
info = dict_from_key(rs[0].key)
oid = oid_unrepr(info['oid'])
return oid
def getSerialForTid(self, tid):
'''
get the serial for a tid
'''
prefix = 'type:index_tid,tid:%s,' % tid_repr(tid)
rs = self._bucket.get_all_keys(prefix=prefix, maxkeys=1)
if not rs:
#either it hasn't propogated yet or has been packed away
return None
info = dict_from_key(rs[0].key)
serial = serial_unrepr(info['serial'])
return serial
def loadPickle(self, oid, tid):
"""Actually get the data from s3"""
k = Key(self._bucket)
k.key = 'type:record,oid:%s,tid:%s' % (oid_repr(oid), tid_repr(tid))
if DEBUG: print 'LOAD %s' % k.key
for n in xrange(RETRIES):
try:
return k.get_contents_as_string()
except S3ResponseError:
if DEBUG: print 'RETRY %s' % n
time.sleep(SLEEPTIME)
return k.get_contents_as_string()
def storePickle(self, oid, tid, prev_tid, data):
'''Save the data to s3'''
k = Key(self._bucket)
k.key = 'type:record,oid:%s,tid:%s' % (oid_repr(oid), tid_repr(tid))
if DEBUG: print 'STORE %s' % k.key
for n in xrange(RETRIES):
try:
return k.set_contents_from_string(data)
except:
if DEBUG: print 'RETRY %s' % n
time.sleep(SLEEPTIME)
return k.set_contents_from_string(data)
def findLastCommit(self, oid, serial):
'''
Look for the last committed record for oid committed on or before serial
'''
oid_key = oid_repr(oid)
serial_key = serial_repr(serial)
prefix = 'type:index_oid,oid:%s,' % oid_key
marker = 'type:index_oid,oid:%s,serial:%s,' % (oid_key, serial_key)
rs = self._bucket.get_all_keys(prefix=prefix, marker=marker, maxkeys=1)
if not rs:
return None # no commits yet
key = rs[0]
return dict_from_key(key.key)
def getTrailingTransactions(self, serial):
'''Get commits after serial'''
#
# Lots of reads here. slow!
#
prefix = 'type:transaction,'
# use a / as it is later than a ,
marker = 'type:transaction,order:%s/' % order_repr(serial)
rs = self._bucket.get_all_keys(prefix=prefix, marker=marker)
data = []
for key in rs:
info = dict_from_key(key.key)
packed = key.get_contents_as_string()
assert len(packed) % 8 == 0 # 8 byte packed oids
oids = set(packed[n:n+8] for n in xrange(0, len(packed), 8))
assert len(oids) == len(packed) / 8
data.append((info, oids))
return data
def writeTransaction(self, tid, serial, oids, ude):
packed = ''.join(oid for oid in oids)
k = Key(self._bucket)
k.key = 'type:transaction,order:%s,tid:%s' % (order_repr(serial),
tid_repr(tid))
if DEBUG: print 'COMMIT %s' %k.key
k.set_contents_from_string(packed)
def indexTransaction(self, tid, serial, oids):
for oid in oids:
k = Key(self._bucket)
k.key = 'type:index_oid,oid:%s,serial:%s,tid:%s' %(
oid_repr(oid),
serial_repr(serial),
tid_repr(tid))
k.set_contents_from_string('')
k = Key(self._bucket)
# Index tid to serial
k.key = 'type:index_tid,tid:%s,serial:%s' %(
tid_repr(tid),
serial_repr(serial))
k.set_contents_from_string('')
# Index the highest committed serial
k = Key(self._bucket)
k.key = 'type:index_serial,serial:%s,tid:%s' %(
serial_repr(serial),
tid_repr(tid))
k.set_contents_from_string('')
# Convert tids, oids, serials, orders from packed 8 byte strings to an a string
# representation suitable for storing in S3
MAXINT = 0xffffffffffffffff
assert MAXINT == 2**64 -1
def invertid(id):
assert id >= 0
return MAXINT - id
def hexrepr(id):
return '%016x' % id
def unhexrepr(s):
'''
convert an hexrepr string back to an integer
'''
assert len(s) == 16
return int(s, 16)
def serial_repr(id):
'''
string representation of a serial, so that numerically higher values are
alphabetically lower.
'''
return hexrepr(invertid(u64(id)))
def serial_unrepr(s):
return p64(invertid(unhexrepr(s)))
def tid_repr(tid):
return hexrepr(u64(tid))
def tid_unrepr(tid_key):
return p64(unhexrepr(tid_key))
order_repr = tid_repr
order_unrepr = tid_unrepr
oid_repr = serial_repr
oid_unrepr = serial_unrepr
def dict_from_key(s):
items = s.split(',')
d = dict()
for i in items:
k, v = i.split(':')
d[k] = v
return d