/
test2.py
217 lines (156 loc) · 4.94 KB
/
test2.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
##
## generic part ("library")
##
import os
import shutil
import random
import lmdb
from collections.abc import MutableMapping
import struct
import cbor2
import pickle
import datetime
class PersistentMap(MutableMapping):
def __init__(self, table, txn):
MutableMapping.__init__(self)
self._table = table
self._txn = txn
def _serialize_key(self, key):
raise Exception('not implemented')
def _serialize_value(self, value):
raise Exception('not implemented')
def _deserialize_value(self, data):
raise Exception('not implemented')
def __getitem__(self, key):
_key = struct.pack('<H', self._table) + self._serialize_key(key)
_data = self._txn._txn.get(_key)
if _data:
return self._deserialize_value(_data)
else:
return None
def __setitem__(self, key, value):
_key = struct.pack('<H', self._table) + self._serialize_key(key)
_data = self._serialize_value(value)
self._txn._txn.put(_key, _data)
def __delitem__(self, key):
_key = struct.pack('<H', self._table) + self._serialize_key(key)
self._txn._txn.delete(_key)
def __iter__(self):
raise Exception('not implemented')
def __len__(self):
raise Exception('not implemented')
class MapStringOid(PersistentMap):
def _serialize_key(self, key):
return key.encode('utf8')
def _serialize_value(self, value):
return struct.pack('<Q', value)
def _deserialize_value(self, data):
return struct.unpack('<Q', data)[0]
class MapOidCbor(PersistentMap):
def _serialize_key(self, key):
return struct.pack('<Q', key)
def _serialize_value(self, value):
return cbor2.dumps(value)
def _deserialize_value(self, data):
return cbor2.loads(data)
class MapOidPickle(PersistentMap):
def _serialize_key(self, key):
return struct.pack('<Q', key)
def _serialize_value(self, value):
return pickle.dumps(value, protocol=4)
def _deserialize_value(self, data):
return pickle.loads(data)
class Transaction(object):
def __init__(self, write=False):
self._write = write
self._txn = None
def __enter__(self):
self._txn = lmdb.Transaction(env, write=self._write)
return self
def __exit__(self, exc_type, exc_value, traceback):
# https://docs.python.org/3/reference/datamodel.html#object.__exit__
# If the context was exited without an exception, all three arguments will be None.
if exc_type is None:
self._txn.commit()
print('LMDB transaction committed')
else:
self._txn.abort()
print('LMDB transaction aborted', exc_type, exc_value, traceback)
##
## app code: variant 1, using CBOR
##
class AppTx(Transaction):
"""
Definition of application database schema
"""
def __init__(self, write=False):
Transaction.__init__(self, write=write)
self.users_by_authid = MapStringOid(1, self)
self.users = MapOidCbor(2, self)
env = lmdb.open('.db3')
user_oid = 1
user_authid = 'joe50xyz'
user = {
'name': 'joe',
'authid': user_authid,
'email': 'joe.doe@digital.com',
'birthday': {
'year': 1950,
'month': 12,
'day': 24
},
'ratings': {
'dawn-of-the-dead': 6.9,
'day-of-the-dead': 7.5,
'land-of-the-dead': 8.9
},
'tags': ['geek', 'vip']
}
with AppTx(write=True) as txn:
_user_oid = txn.users_by_authid[user_authid]
if _user_oid:
user = txn.users[_user_oid]
print('user loaded', user)
else:
txn.users_by_authid[user_authid] = user_oid
txn.users[user_oid] = user
print('user stored', user)
##
## app code: variant 2, using native Python object
##
class User(object):
def __init__(self, user_authid):
self.name = 'joe'
self.authid = user_authid
self.email = 'joe.doe@digital.com'
self.birthday = datetime.date(1950, 12, 24)
self.is_friendly = True
self.ratings = {
'dawn-of-the-dead': 6.9,
'day-of-the-dead': 7.5,
'land-of-the-dead': 8.9
}
self.tags = ('geek', 'vip')
class AppTransaction(Transaction):
"""
Definition of application database schema
"""
def __init__(self, write=False):
Transaction.__init__(self, write=write)
# index: string -> oid
self.users_by_authid = MapStringOid(1, self)
# table: oid -> user
self.users = MapOidPickle(2, self)
env = lmdb.open('.db4')
user_oid = 1
user_authid = 'joe50xyz'
user = User(user_authid)
with AppTransaction(write=True) as txn:
_user_oid = txn.users_by_authid[user_authid]
if _user_oid:
user = txn.users[_user_oid]
print('user loaded', user)
else:
txn.users_by_authid[user_authid] = user_oid
txn.users[user_oid] = user
print('user stored', user)