zlmdb 25.10.1__cp311-cp311-macosx_10_9_universal2.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of zlmdb might be problematic. Click here for more details.

Files changed (87) hide show
  1. flatbuffers/__init__.py +19 -0
  2. flatbuffers/_version.py +17 -0
  3. flatbuffers/builder.py +776 -0
  4. flatbuffers/compat.py +86 -0
  5. flatbuffers/encode.py +42 -0
  6. flatbuffers/flexbuffers.py +1527 -0
  7. flatbuffers/number_types.py +181 -0
  8. flatbuffers/packer.py +42 -0
  9. flatbuffers/reflection/AdvancedFeatures.py +10 -0
  10. flatbuffers/reflection/BaseType.py +24 -0
  11. flatbuffers/reflection/Enum.py +169 -0
  12. flatbuffers/reflection/EnumVal.py +96 -0
  13. flatbuffers/reflection/Field.py +208 -0
  14. flatbuffers/reflection/KeyValue.py +56 -0
  15. flatbuffers/reflection/Object.py +175 -0
  16. flatbuffers/reflection/RPCCall.py +131 -0
  17. flatbuffers/reflection/Schema.py +206 -0
  18. flatbuffers/reflection/SchemaFile.py +77 -0
  19. flatbuffers/reflection/Service.py +145 -0
  20. flatbuffers/reflection/Type.py +98 -0
  21. flatbuffers/reflection/__init__.py +0 -0
  22. flatbuffers/table.py +129 -0
  23. flatbuffers/util.py +43 -0
  24. zlmdb/__init__.py +312 -0
  25. zlmdb/_database.py +990 -0
  26. zlmdb/_errors.py +31 -0
  27. zlmdb/_meta.py +27 -0
  28. zlmdb/_pmap.py +1667 -0
  29. zlmdb/_schema.py +137 -0
  30. zlmdb/_transaction.py +181 -0
  31. zlmdb/_types.py +1596 -0
  32. zlmdb/_version.py +27 -0
  33. zlmdb/cli.py +41 -0
  34. zlmdb/flatbuffers/__init__.py +5 -0
  35. zlmdb/flatbuffers/reflection/AdvancedFeatures.py +10 -0
  36. zlmdb/flatbuffers/reflection/BaseType.py +25 -0
  37. zlmdb/flatbuffers/reflection/Enum.py +252 -0
  38. zlmdb/flatbuffers/reflection/EnumVal.py +144 -0
  39. zlmdb/flatbuffers/reflection/Field.py +325 -0
  40. zlmdb/flatbuffers/reflection/KeyValue.py +84 -0
  41. zlmdb/flatbuffers/reflection/Object.py +260 -0
  42. zlmdb/flatbuffers/reflection/RPCCall.py +195 -0
  43. zlmdb/flatbuffers/reflection/Schema.py +301 -0
  44. zlmdb/flatbuffers/reflection/SchemaFile.py +112 -0
  45. zlmdb/flatbuffers/reflection/Service.py +213 -0
  46. zlmdb/flatbuffers/reflection/Type.py +148 -0
  47. zlmdb/flatbuffers/reflection/__init__.py +0 -0
  48. zlmdb/flatbuffers/reflection.fbs +152 -0
  49. zlmdb/lmdb/__init__.py +37 -0
  50. zlmdb/lmdb/__main__.py +25 -0
  51. zlmdb/lmdb/_config.py +10 -0
  52. zlmdb/lmdb/_lmdb_cffi.cpython-311-darwin.so +0 -0
  53. zlmdb/lmdb/cffi.py +2606 -0
  54. zlmdb/lmdb/tool.py +670 -0
  55. zlmdb/tests/lmdb/__init__.py +0 -0
  56. zlmdb/tests/lmdb/address_book.py +287 -0
  57. zlmdb/tests/lmdb/crash_test.py +339 -0
  58. zlmdb/tests/lmdb/cursor_test.py +333 -0
  59. zlmdb/tests/lmdb/env_test.py +919 -0
  60. zlmdb/tests/lmdb/getmulti_test.py +92 -0
  61. zlmdb/tests/lmdb/iteration_test.py +258 -0
  62. zlmdb/tests/lmdb/package_test.py +70 -0
  63. zlmdb/tests/lmdb/test_lmdb.py +188 -0
  64. zlmdb/tests/lmdb/testlib.py +185 -0
  65. zlmdb/tests/lmdb/tool_test.py +60 -0
  66. zlmdb/tests/lmdb/txn_test.py +575 -0
  67. zlmdb/tests/orm/MNodeLog.py +853 -0
  68. zlmdb/tests/orm/__init__.py +0 -0
  69. zlmdb/tests/orm/_schema_fbs.py +215 -0
  70. zlmdb/tests/orm/_schema_mnode_log.py +1201 -0
  71. zlmdb/tests/orm/_schema_py2.py +250 -0
  72. zlmdb/tests/orm/_schema_py3.py +307 -0
  73. zlmdb/tests/orm/_test_flatbuffers.py +144 -0
  74. zlmdb/tests/orm/_test_serialization.py +144 -0
  75. zlmdb/tests/orm/test_basic.py +217 -0
  76. zlmdb/tests/orm/test_etcd.py +275 -0
  77. zlmdb/tests/orm/test_pmap_indexes.py +466 -0
  78. zlmdb/tests/orm/test_pmap_types.py +90 -0
  79. zlmdb/tests/orm/test_pmaps.py +295 -0
  80. zlmdb/tests/orm/test_select.py +619 -0
  81. zlmdb-25.10.1.dist-info/METADATA +264 -0
  82. zlmdb-25.10.1.dist-info/RECORD +87 -0
  83. zlmdb-25.10.1.dist-info/WHEEL +5 -0
  84. zlmdb-25.10.1.dist-info/entry_points.txt +2 -0
  85. zlmdb-25.10.1.dist-info/licenses/LICENSE +137 -0
  86. zlmdb-25.10.1.dist-info/licenses/NOTICE +41 -0
  87. zlmdb-25.10.1.dist-info/top_level.txt +2 -0
@@ -0,0 +1,144 @@
1
+ ###############################################################################
2
+ #
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) typedef int GmbH
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ # THE SOFTWARE.
24
+ #
25
+ ###############################################################################
26
+
27
+ import os
28
+ import sys
29
+ import timeit
30
+ import uuid
31
+ import platform
32
+ import logging
33
+
34
+ import humanize
35
+
36
+ import txaio
37
+
38
+ txaio.use_twisted()
39
+
40
+ from zlmdb import _types # noqa
41
+ from _schema_fbs import User as UserFbs # noqa
42
+
43
+ sys.path.append(os.path.dirname(os.path.abspath(__file__)))
44
+
45
+ if sys.version_info >= (3, 6):
46
+ from _schema_py3 import User
47
+ else:
48
+ from _schema_py2 import User
49
+
50
+ _TEST = {"oid": 0, "uuid": None, "bytes": 0, "serializer": None}
51
+
52
+
53
+ def _serializer_run_fbs():
54
+ serialize = _TEST["serializer"]._serialize_value
55
+ user = UserFbs.create_test_user()
56
+ data = serialize(user)
57
+ _TEST["bytes"] += len(data)
58
+
59
+
60
+ def _serializer_run():
61
+ serialize = _TEST["serializer"]._serialize_value
62
+ user = User.create_test_user()
63
+ data = serialize(user)
64
+ _TEST["bytes"] += len(data)
65
+
66
+
67
+ def _serialization_speed(serializer, testfun):
68
+ N = 10
69
+ M = 10000
70
+
71
+ samples = []
72
+
73
+ logging.info("running on:")
74
+ logging.info(sys.version)
75
+ logging.info(platform.uname())
76
+
77
+ _TEST["oid"] = 0
78
+ _TEST["uuid"] = uuid.uuid4()
79
+ _TEST["bytes"] = 0
80
+ _TEST["bytes"] = 0
81
+ _TEST["serializer"] = serializer
82
+
83
+ for i in range(N):
84
+ secs = timeit.timeit(testfun, number=M)
85
+ ops = round(float(M) / secs, 1)
86
+ samples.append(ops)
87
+ logging.info(
88
+ "{} objects/sec {}".format(ops, humanize.naturalsize(_TEST["bytes"]))
89
+ )
90
+
91
+ ops_max = max(samples)
92
+ bytes_per_obj = float(_TEST["bytes"]) / float(N * M)
93
+ logging.info(
94
+ "{} objects/sec max, {} bytes total, {} bytes/obj".format(
95
+ ops_max,
96
+ humanize.naturalsize(_TEST["bytes"]),
97
+ humanize.naturalsize(bytes_per_obj),
98
+ )
99
+ )
100
+
101
+ return ops_max, _TEST["bytes"]
102
+
103
+
104
+ def test_json_serialization_speed():
105
+ ser = _types._JsonValuesMixin(marshal=User.marshal, unmarshal=User.parse)
106
+ ops_max, total = _serialization_speed(ser, _serializer_run)
107
+ # cpy36: 19564.6 objects/sec max, 135456153 bytes total
108
+ assert ops_max > 1000
109
+ assert total > 1000000
110
+
111
+
112
+ def test_cbor_serialization_speed():
113
+ ser = _types._CborValuesMixin(marshal=User.marshal, unmarshal=User.parse)
114
+ ops_max, total = _serialization_speed(ser, _serializer_run)
115
+ # cpy36: 7787.4 objects/sec max, 97815364 bytes total
116
+ assert ops_max > 1000
117
+ assert total > 1000000
118
+
119
+
120
+ def test_pickle_serialization_speed():
121
+ ser = _types._PickleValuesMixin()
122
+ ops_max, total = _serialization_speed(ser, _serializer_run)
123
+ # cpy36: 33586.0 objects/sec max, 137738869 bytes total
124
+ assert ops_max > 1000
125
+ assert total > 1000000
126
+
127
+
128
+ def test_flatbuffer_serialization_speed():
129
+ ser = _types._FlatBuffersValuesMixin(build=UserFbs.build, cast=UserFbs.cast)
130
+ ops_max, total = _serialization_speed(ser, _serializer_run_fbs)
131
+ assert ops_max > 1000
132
+ assert total > 1000000
133
+
134
+
135
+ if __name__ == "__main__":
136
+ from typing import List
137
+
138
+ sers: List[object] = []
139
+ sers.append(_types._JsonValuesMixin(marshal=User.marshal, unmarshal=User.parse))
140
+ sers.append(_types._CborValuesMixin(marshal=User.marshal, unmarshal=User.parse))
141
+ sers.append(_types._PickleValuesMixin())
142
+ sers.append(_types._FlatBuffersValuesMixin(build=UserFbs.build, cast=UserFbs.cast))
143
+ for ser in sers:
144
+ logging.info(_serialization_speed(ser, _serializer_run))
@@ -0,0 +1,217 @@
1
+ ###############################################################################
2
+ #
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) typedef int GmbH
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ # THE SOFTWARE.
24
+ #
25
+ ###############################################################################
26
+
27
+ import sys
28
+ import os
29
+ import pytest
30
+ import logging
31
+
32
+ import txaio
33
+
34
+ txaio.use_twisted()
35
+
36
+ import zlmdb # noqa
37
+
38
+ try:
39
+ from tempfile import TemporaryDirectory
40
+ except ImportError:
41
+ from backports.tempfile import TemporaryDirectory # type:ignore
42
+
43
+ sys.path.append(os.path.dirname(os.path.abspath(__file__)))
44
+
45
+ if sys.version_info >= (3, 6):
46
+ from _schema_py3 import User, Schema2
47
+ else:
48
+ from _schema_py2 import User, Schema2
49
+
50
+
51
+ @pytest.fixture(scope="module")
52
+ def testset1():
53
+ users = []
54
+ for i in range(1000):
55
+ user = User.create_test_user(i)
56
+ users.append(user)
57
+ return users
58
+
59
+
60
+ def test_open1():
61
+ with TemporaryDirectory() as dbpath:
62
+ with zlmdb.Database(dbpath) as db:
63
+ assert db.maxsize == 10485760
64
+ assert db.is_sync
65
+ assert db.is_open
66
+ assert not db.is_readonly
67
+ assert not db.is_writemap
68
+
69
+
70
+ def test_open2():
71
+ with TemporaryDirectory() as dbpath:
72
+ with zlmdb.Database(dbpath) as db:
73
+ assert db.is_open
74
+ with zlmdb.Database(dbpath) as db:
75
+ assert db.is_open
76
+
77
+
78
+ def test_open3():
79
+ with TemporaryDirectory() as dbpath:
80
+ db = zlmdb.Database(dbpath)
81
+ assert db.is_open
82
+ try:
83
+ zlmdb.Database(dbpath)
84
+ assert False, (
85
+ "opening same dbpath twice in same process did not throw an exception"
86
+ )
87
+ except RuntimeError as e:
88
+ # RuntimeError: tried to open same dbpath "/tmp/tmpwc1dw5c8" twice within same process
89
+ assert "twice within same process" in str(e), (
90
+ "exception did not contain text we excepted: {}".format(e)
91
+ )
92
+ except Exception as e:
93
+ assert False, "unexpected exception {} raised".format(e)
94
+
95
+
96
+ def test_open4():
97
+ with TemporaryDirectory() as dbpath:
98
+ db1 = zlmdb.Database.open(dbpath)
99
+ assert db1.is_open
100
+
101
+ db2 = zlmdb.Database.open(dbpath)
102
+ assert db2.is_open
103
+
104
+ assert db1 == db2
105
+
106
+
107
+ def test_transaction():
108
+ with TemporaryDirectory() as dbpath:
109
+ logging.info("Using temporary directory {} for database".format(dbpath))
110
+
111
+ with zlmdb.Database(dbpath) as db:
112
+ with db.begin() as txn:
113
+ logging.info("transaction open {}".format(txn.id()))
114
+ logging.info("transaction committed")
115
+ logging.info("database closed")
116
+
117
+
118
+ def test_save_load():
119
+ with TemporaryDirectory() as dbpath:
120
+ logging.info("Using temporary directory {} for database".format(dbpath))
121
+
122
+ schema = Schema2()
123
+
124
+ user = User.create_test_user()
125
+
126
+ with zlmdb.Database(dbpath) as db:
127
+ with db.begin(write=True) as txn:
128
+ schema.users[txn, user.oid] = user
129
+ logging.info("user saved")
130
+
131
+ _user = schema.users[txn, user.oid]
132
+ assert _user
133
+ assert user == _user
134
+ logging.info("user loaded")
135
+
136
+ logging.info("transaction committed")
137
+
138
+ logging.info("database closed")
139
+
140
+
141
+ def test_save_load_many_1(testset1):
142
+ with TemporaryDirectory() as dbpath:
143
+ logging.info("Using temporary directory {} for database".format(dbpath))
144
+
145
+ schema = Schema2()
146
+
147
+ with zlmdb.Database(dbpath) as db:
148
+ with db.begin(write=True) as txn:
149
+ for user in testset1:
150
+ schema.users[txn, user.oid] = user
151
+
152
+ cnt = schema.users.count(txn)
153
+ logging.info("user saved: {}".format(cnt))
154
+ assert cnt == len(testset1)
155
+
156
+ with db.begin() as txn:
157
+ cnt = schema.users.count(txn)
158
+ assert cnt == len(testset1)
159
+
160
+ with zlmdb.Database(dbpath) as db:
161
+ with db.begin() as txn:
162
+ cnt = schema.users.count(txn)
163
+ assert cnt == len(testset1)
164
+
165
+
166
+ def test_save_load_many_2(testset1):
167
+ with TemporaryDirectory() as dbpath:
168
+ logging.info("Using temporary directory {} for database".format(dbpath))
169
+
170
+ schema = Schema2()
171
+
172
+ oids = []
173
+
174
+ with zlmdb.Database(dbpath) as db:
175
+ # write records in a 1st transaction
176
+ with db.begin(write=True) as txn:
177
+ c = 0
178
+ for user in testset1:
179
+ schema.users[txn, user.oid] = user
180
+ oids.append(user.oid)
181
+ c += 1
182
+ assert c == len(testset1)
183
+ logging.info("[1] successfully stored {} records".format(c))
184
+
185
+ # in the same transaction, read back records
186
+ c = 0
187
+ for oid in oids:
188
+ user = schema.users[txn, oid]
189
+ if user:
190
+ c += 1
191
+ assert c == len(testset1)
192
+ logging.info("[1] successfully loaded {} records".format(c))
193
+
194
+ # in a new transaction, read back records
195
+ c = 0
196
+ with db.begin() as txn:
197
+ for oid in oids:
198
+ user = schema.users[txn, oid]
199
+ if user:
200
+ c += 1
201
+ assert c == len(testset1)
202
+ logging.info("[2] successfully loaded {} records".format(c))
203
+
204
+ # in a new database environment (and hence new transaction), read back records
205
+ with zlmdb.Database(dbpath) as db:
206
+ with db.begin() as txn:
207
+ count = schema.users.count(txn)
208
+ assert count == len(testset1)
209
+ logging.info("total records: {}".format(count))
210
+
211
+ c = 0
212
+ for oid in oids:
213
+ user = schema.users[txn, oid]
214
+ if user:
215
+ c += 1
216
+ assert c == len(testset1)
217
+ logging.info("[3] successfully loaded {} records".format(c))
@@ -0,0 +1,275 @@
1
+ ###############################################################################
2
+ #
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) typedef int GmbH
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ # THE SOFTWARE.
24
+ #
25
+ ###############################################################################
26
+
27
+ import os
28
+ import sys
29
+ import pytest
30
+ import logging
31
+
32
+ import txaio
33
+
34
+ txaio.use_twisted()
35
+
36
+ import zlmdb # noqa
37
+
38
+ try:
39
+ from tempfile import TemporaryDirectory
40
+ except ImportError:
41
+ from backports.tempfile import TemporaryDirectory # type:ignore
42
+
43
+ sys.path.append(os.path.dirname(os.path.abspath(__file__)))
44
+
45
+ if sys.version_info >= (3, 6):
46
+ from _schema_py3 import User, Schema1, Schema3, Schema4
47
+ else:
48
+ from _schema_py2 import User, Schema1, Schema3, Schema4
49
+
50
+
51
+ @pytest.fixture(scope="module")
52
+ def testset1():
53
+ users = []
54
+ for i in range(1000):
55
+ user = User.create_test_user(i)
56
+ users.append(user)
57
+ return users
58
+
59
+
60
+ def test_truncate_table():
61
+ with TemporaryDirectory() as dbpath:
62
+ logging.info("Using temporary directory {} for database".format(dbpath))
63
+
64
+ schema = Schema1()
65
+
66
+ stats = zlmdb.TransactionStats()
67
+ tabs = [
68
+ schema.tab_oid_json,
69
+ schema.tab_str_json,
70
+ schema.tab_uuid_json,
71
+ schema.tab_oid_cbor,
72
+ schema.tab_str_cbor,
73
+ schema.tab_uuid_cbor,
74
+ schema.tab_oid_pickle,
75
+ schema.tab_str_pickle,
76
+ schema.tab_uuid_pickle,
77
+ ]
78
+
79
+ with zlmdb.Database(dbpath) as db:
80
+ with db.begin(write=True, stats=stats) as txn:
81
+ for tab in tabs:
82
+ tab.truncate(txn)
83
+
84
+ logging.info(stats.puts)
85
+ logging.info(stats.dels)
86
+
87
+
88
+ def test_fill_check(testset1):
89
+ with TemporaryDirectory() as dbpath:
90
+ logging.info("Using temporary directory {} for database".format(dbpath))
91
+
92
+ schema = Schema3()
93
+
94
+ with zlmdb.Database(dbpath) as db:
95
+ with db.begin(write=True) as txn:
96
+ for user in testset1:
97
+ schema.users[txn, user.authid] = user
98
+
99
+ with zlmdb.Database(dbpath) as db:
100
+ with db.begin() as txn:
101
+ for user in testset1:
102
+ _user = schema.users[txn, user.authid]
103
+ assert _user
104
+ assert _user == user
105
+
106
+
107
+ def test_select(testset1):
108
+ testset1_keys = set([user.authid for user in testset1])
109
+
110
+ with TemporaryDirectory() as dbpath:
111
+ logging.info("Using temporary directory {} for database".format(dbpath))
112
+
113
+ schema = Schema3()
114
+
115
+ with zlmdb.Database(dbpath) as db:
116
+ with db.begin(write=True) as txn:
117
+ for user in testset1:
118
+ schema.users[txn, user.authid] = user
119
+
120
+ with zlmdb.Database(dbpath) as db:
121
+ with db.begin() as txn:
122
+ i = 0
123
+ for authid, user in schema.users.select(txn):
124
+ i += 1
125
+ assert user
126
+ assert authid == user.authid
127
+ assert authid in testset1_keys
128
+
129
+
130
+ def test_count_all(testset1):
131
+ with TemporaryDirectory() as dbpath:
132
+ logging.info("Using temporary directory {} for database".format(dbpath))
133
+
134
+ schema = Schema3()
135
+
136
+ with zlmdb.Database(dbpath) as db:
137
+ # count on empty table
138
+ with db.begin() as txn:
139
+ cnt = schema.users.count(txn)
140
+ assert cnt == 0
141
+
142
+ # fill (and count on each insert)
143
+ with db.begin(write=True) as txn:
144
+ i = 0
145
+ for user in testset1:
146
+ schema.users[txn, user.authid] = user
147
+ i += 1
148
+
149
+ # table count within filling transaction
150
+ cnt = schema.users.count(txn)
151
+ assert cnt == i
152
+
153
+ # table count within transaction
154
+ cnt = schema.users.count(txn)
155
+ assert cnt == len(testset1)
156
+
157
+ # table count in new transaction
158
+ with db.begin() as txn:
159
+ cnt = schema.users.count(txn)
160
+ assert cnt == len(testset1)
161
+
162
+ # table count in new connection
163
+ with zlmdb.Database(dbpath) as db:
164
+ with db.begin() as txn:
165
+ cnt = schema.users.count(txn)
166
+ assert cnt == len(testset1)
167
+
168
+
169
+ def test_count_prefix(testset1):
170
+ with TemporaryDirectory() as dbpath:
171
+ logging.info("Using temporary directory {} for database".format(dbpath))
172
+
173
+ schema = Schema3()
174
+
175
+ with zlmdb.Database(dbpath) as db:
176
+ with db.begin(write=True) as txn:
177
+ for user in testset1:
178
+ schema.users[txn, user.authid] = user
179
+
180
+ n = len(testset1)
181
+ tests = [
182
+ (None, n),
183
+ ("", n),
184
+ ("test-", n),
185
+ ("test-1", 111),
186
+ ("test-11", 11),
187
+ ("test-111", 1),
188
+ ]
189
+ with zlmdb.Database(dbpath) as db:
190
+ with db.begin() as txn:
191
+ for prefix, num in tests:
192
+ cnt = schema.users.count(txn, prefix)
193
+ assert cnt == num
194
+
195
+
196
+ def test_fill_with_indexes(testset1):
197
+ with TemporaryDirectory() as dbpath:
198
+ logging.info("Using temporary directory {} for database".format(dbpath))
199
+
200
+ schema = Schema4()
201
+
202
+ with zlmdb.Database(dbpath) as db:
203
+ stats = zlmdb.TransactionStats()
204
+
205
+ with db.begin(write=True, stats=stats) as txn:
206
+ for user in testset1:
207
+ schema.users[txn, user.oid] = user
208
+
209
+ # check indexes has been written to (in addition to the table itself)
210
+ num_indexes = len(schema.users.indexes())
211
+ assert stats.puts == len(testset1) * (1 + num_indexes)
212
+
213
+
214
+ def test_truncate_table_with_index(testset1):
215
+ with TemporaryDirectory() as dbpath:
216
+ logging.info("Using temporary directory {} for database".format(dbpath))
217
+
218
+ schema = Schema4()
219
+
220
+ with zlmdb.Database(dbpath) as db:
221
+ with db.begin(write=True) as txn:
222
+ for user in testset1:
223
+ schema.users[txn, user.oid] = user
224
+
225
+ stats = zlmdb.TransactionStats()
226
+
227
+ with zlmdb.Database(dbpath) as db:
228
+ with db.begin(write=True, stats=stats) as txn:
229
+ records = schema.users.truncate(txn)
230
+ logging.info("table truncated: {}".format(records))
231
+
232
+ logging.info(stats.puts)
233
+ logging.info(stats.dels)
234
+
235
+
236
+ def test_rebuild_index(testset1):
237
+ with TemporaryDirectory() as dbpath:
238
+ logging.info("Using temporary directory {} for database".format(dbpath))
239
+
240
+ schema = Schema4()
241
+
242
+ with zlmdb.Database(dbpath) as db:
243
+ with db.begin(write=True) as txn:
244
+ for user in testset1:
245
+ schema.users[txn, user.oid] = user
246
+
247
+ with zlmdb.Database(dbpath) as db:
248
+ with db.begin(write=True) as txn:
249
+ records = schema.users.rebuild_index(txn, "idx1")
250
+ logging.info(
251
+ '\nrebuilt specific index "idx1" on "users": {} records affected'.format(
252
+ records
253
+ )
254
+ )
255
+
256
+
257
+ def test_rebuild_all_indexes(testset1):
258
+ with TemporaryDirectory() as dbpath:
259
+ logging.info("Using temporary directory {} for database".format(dbpath))
260
+
261
+ schema = Schema4()
262
+
263
+ with zlmdb.Database(dbpath) as db:
264
+ with db.begin(write=True) as txn:
265
+ for user in testset1:
266
+ schema.users[txn, user.oid] = user
267
+
268
+ with zlmdb.Database(dbpath) as db:
269
+ with db.begin(write=True) as txn:
270
+ records = schema.users.rebuild_indexes(txn)
271
+ logging.info(
272
+ '\nrebuilt all indexes on "users": {} records affected'.format(
273
+ records
274
+ )
275
+ )