hat.event.backends.lmdb.convert.version

 1from pathlib import Path
 2import contextlib
 3import enum
 4import platform
 5import struct
 6
 7import lmdb
 8
 9from hat import json
10
11
12_max_size = (1024 * 1024 * 1024 * 1024
13             if platform.architecture()[0] == '64bit'
14             else 2 * 1024 * 1024 * 1024)
15
16_max_dbs = 9
17
18
19class Version(enum.Enum):
20    v06 = '0.6'
21    v07 = '0.7'
22    v09 = '0.9'
23
24
25def get_version(path: Path) -> Version:
26    with lmdb.Environment(str(path),
27                          map_size=_max_size,
28                          subdir=False,
29                          max_dbs=_max_dbs,
30                          readonly=True) as env:
31        with contextlib.suppress(Exception):
32            db = env.open_db(b'SYSTEM_SETTINGS', create=False)
33
34            with env.begin(buffers=True) as txn:
35                key = _encode_uint(0)
36                value = txn.get(key, db=db)
37
38                return Version(_decode_json(value))
39
40        with contextlib.suppress(Exception):
41            for db_name in [b'SYSTEM', b'LATEST_DATA', b'LATEST_TYPE',
42                            b'ORDERED_DATA', b'ORDERED_PARTITION',
43                            b'ORDERED_COUNT', b'REF']:
44                env.open_db(db_name, create=False)
45
46            return Version.v07
47
48        with contextlib.suppress(Exception):
49            for db_name in [b'system', b'latest', b'ordered_data',
50                            b'ordered_partition', b'ordered_count']:
51                env.open_db(db_name, create=False)
52
53            return Version.v06
54
55    raise Exception('unsupported version')
56
57
58def _encode_uint(value):
59    return struct.pack(">Q", value)
60
61
62def _decode_json(data_bytes):
63    return json.decode(str(data_bytes, encoding='utf-8'))
class Version(enum.Enum):
20class Version(enum.Enum):
21    v06 = '0.6'
22    v07 = '0.7'
23    v09 = '0.9'

An enumeration.

v06 = <Version.v06: '0.6'>
v07 = <Version.v07: '0.7'>
v09 = <Version.v09: '0.9'>
def get_version(path: pathlib.Path) -> Version:
26def get_version(path: Path) -> Version:
27    with lmdb.Environment(str(path),
28                          map_size=_max_size,
29                          subdir=False,
30                          max_dbs=_max_dbs,
31                          readonly=True) as env:
32        with contextlib.suppress(Exception):
33            db = env.open_db(b'SYSTEM_SETTINGS', create=False)
34
35            with env.begin(buffers=True) as txn:
36                key = _encode_uint(0)
37                value = txn.get(key, db=db)
38
39                return Version(_decode_json(value))
40
41        with contextlib.suppress(Exception):
42            for db_name in [b'SYSTEM', b'LATEST_DATA', b'LATEST_TYPE',
43                            b'ORDERED_DATA', b'ORDERED_PARTITION',
44                            b'ORDERED_COUNT', b'REF']:
45                env.open_db(db_name, create=False)
46
47            return Version.v07
48
49        with contextlib.suppress(Exception):
50            for db_name in [b'system', b'latest', b'ordered_data',
51                            b'ordered_partition', b'ordered_count']:
52                env.open_db(db_name, create=False)
53
54            return Version.v06
55
56    raise Exception('unsupported version')