Skip to content

Commit 59ea45f

Browse files
committed
add conversor to 64-bit format [skip ci]
1 parent 4508869 commit 59ea45f

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

test/convert-to-64bit.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#
2+
# Copyright defined in LICENSE.txt
3+
#
4+
import os
5+
import sys
6+
import lmdb
7+
import struct
8+
import varint
9+
10+
11+
def delete_file(filepath):
12+
if os.path.exists(filepath):
13+
os.remove(filepath)
14+
15+
16+
def convert_db(filename1):
17+
18+
if not os.path.exists(filename1):
19+
print 'the file does not exist'
20+
quit()
21+
22+
filename2 = filename1 + '-converted'
23+
delete_file(filename2)
24+
print 'converting', filename, 'to', filename2, '...'
25+
26+
env1 = lmdb.open(filename1, subdir=False, max_dbs=1024)
27+
env2 = lmdb.open(filename2, subdir=False, max_dbs=1024)
28+
29+
pages_db1 = [None] # the first element (0) stores None
30+
maxpg_db1 = [None]
31+
32+
pages_db2 = [None] # the first element (0) stores None
33+
maxpg_db2 = [None]
34+
35+
with env1.begin(buffers=True) as txn1:
36+
37+
with env2.begin(buffers=True) as txn2:
38+
39+
value = txn1.get('last_branch_id')
40+
num_branches = struct.unpack('i', value)[0]
41+
print 'branches:', num_branches
42+
43+
for branch_id in range(1, num_branches + 1):
44+
dbname = 'b' + str(branch_id) + '-pages'
45+
pages_db1.append(env1.open_db(dbname))
46+
pages_db2.append(env2.open_db(dbname))
47+
dbname = 'b' + str(branch_id) + '-maxpage'
48+
maxpg_db1.append(env1.open_db(dbname, integerkey=True))
49+
maxpg_db2.append(env2.open_db(dbname))
50+
51+
52+
with env1.begin(buffers=True) as txn1:
53+
54+
with env2.begin(write=True, buffers=True) as txn2:
55+
56+
txn2.put('last_branch_id', varint.encode(num_branches))
57+
58+
value = txn1.get('change_counter')
59+
value = struct.unpack('i', value)[0]
60+
value = varint.encode(value)
61+
txn2.put('change_counter', value)
62+
63+
for branch_id in range(1, num_branches + 1):
64+
prefix = 'b' + str(branch_id)
65+
66+
key = prefix + '.name'
67+
name = txn1.get(key)
68+
print 'processing branch:', name
69+
txn2.put(key, name)
70+
71+
key = prefix + '.visible'
72+
value = txn1.get(key)
73+
value = struct.unpack('i', value)[0]
74+
value = varint.encode(value)
75+
txn2.put(key, value)
76+
77+
key = prefix + '.source_branch'
78+
value = txn1.get(key)
79+
value = struct.unpack('i', value)[0]
80+
value = varint.encode(value)
81+
txn2.put(key, value)
82+
83+
key = prefix + '.source_commit'
84+
value = txn1.get(key)
85+
value = struct.unpack('i', value)[0]
86+
value = varint.encode(value)
87+
txn2.put(key, value)
88+
89+
key = prefix + '.last_commit'
90+
value = txn1.get(key)
91+
value = struct.unpack('i', value)[0]
92+
value = varint.encode(value)
93+
txn2.put(key, value)
94+
95+
# iterate all the keys from the sub-db
96+
db1 = pages_db1[branch_id]
97+
db2 = pages_db2[branch_id]
98+
for key, value in txn1.cursor(db=db1):
99+
# read the key
100+
pgno = struct.unpack('>i', key[0:4])[0]
101+
commit = struct.unpack('>i', key[4:8] )[0]
102+
print 'page', pgno, 'commit', commit
103+
# write the new key
104+
key2 = varint.encode(pgno) + varint.encode(commit)
105+
txn2.put(key2, value, db=db2)
106+
107+
# iterate all the keys from the sub-db
108+
db1 = maxpg_db1[branch_id]
109+
db2 = maxpg_db2[branch_id]
110+
for key, value in txn1.cursor(db=db1):
111+
# read the key
112+
commit = struct.unpack('i', key)[0]
113+
print 'commit', commit
114+
# write the new key
115+
key2 = varint.encode(commit)
116+
txn2.put(key2, value, db=db2)
117+
118+
env1.close()
119+
env2.close()
120+
121+
print 'done. you can open it with the command: sqlite3 "file:' + filename2 + '?branches=on"'
122+
123+
124+
125+
if len(sys.argv) == 1:
126+
print 'usage: python', sys.argv[0], '<db_file>'
127+
quit()
128+
129+
filename = sys.argv[1]
130+
131+
convert_db(filename)

0 commit comments

Comments
 (0)