File: cursor_test.py

package info (click to toggle)
py-lmdb 1.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,832 kB
  • sloc: ansic: 11,932; python: 4,646; makefile: 128
file content (331 lines) | stat: -rw-r--r-- 11,215 bytes parent folder | download | duplicates (2)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#
# Copyright 2013-2021 The py-lmdb authors, all rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted only as authorized by the OpenLDAP
# Public License.
#
# A copy of this license is available in the file LICENSE in the
# top-level directory of the distribution or, alternatively, at
# <http://www.OpenLDAP.org/license.html>.
#
# OpenLDAP is a registered trademark of the OpenLDAP Foundation.
#
# Individual files and/or contributed packages may be copyright by
# other parties and/or subject to additional restrictions.
#
# This work also contains materials derived from public sources.
#
# Additional information about OpenLDAP can be obtained at
# <http://www.openldap.org/>.
#

# test delete(dupdata)

from __future__ import absolute_import
from __future__ import with_statement
import sys
import unittest

import lmdb

import testlib
from testlib import B
from testlib import BT

class ContextManagerTest(unittest.TestCase):
    def tearDown(self):
        testlib.cleanup()

    def test_ok(self):
        path, env = testlib.temp_env()
        txn = env.begin(write=True)
        with txn.cursor() as curs:
            curs.put(B('foo'), B('123'))
        self.assertRaises(Exception, lambda: curs.get(B('foo')))

    def test_crash(self):
        path, env = testlib.temp_env()
        txn = env.begin(write=True)

        try:
            with txn.cursor() as curs:
                curs.put(123, 123)
        except Exception:
            pass
        self.assertRaises(Exception, lambda: curs.get(B('foo')))


class CursorTestBase(unittest.TestCase):
    def tearDown(self):
        testlib.cleanup()

    def setUp(self):
        self.path, self.env = testlib.temp_env()
        self.txn = self.env.begin(write=True)
        self.c = self.txn.cursor()


class CursorTest(CursorTestBase):
    def testKeyValueItemEmpty(self):
        self.assertEqual(B(''), self.c.key())
        self.assertEqual(B(''), self.c.value())
        self.assertEqual(BT('', ''), self.c.item())

    def testFirstLastEmpty(self):
        self.assertEqual(False, self.c.first())
        self.assertEqual(False, self.c.last())

    def testFirstFilled(self):
        testlib.putData(self.txn)
        self.assertEqual(True, self.c.first())
        self.assertEqual(testlib.ITEMS[0], self.c.item())

    def testLastFilled(self):
        testlib.putData(self.txn)
        self.assertEqual(True, self.c.last())
        self.assertEqual(testlib.ITEMS[-1], self.c.item())

    def testSetKey(self):
        self.assertRaises(Exception, (lambda: self.c.set_key(B(''))))
        self.assertEqual(False, self.c.set_key(B('missing')))
        testlib.putData(self.txn)
        self.assertEqual(True, self.c.set_key(B('b')))
        self.assertEqual(False, self.c.set_key(B('ba')))

    def testSetRange(self):
        self.assertEqual(False, self.c.set_range(B('x')))
        testlib.putData(self.txn)
        self.assertEqual(False, self.c.set_range(B('x')))
        self.assertEqual(True, self.c.set_range(B('a')))
        self.assertEqual(B('a'), self.c.key())
        self.assertEqual(True, self.c.set_range(B('ba')))
        self.assertEqual(B('baa'), self.c.key())
        self.c.set_range(B(''))
        self.assertEqual(B('a'), self.c.key())

    def testDeleteEmpty(self):
        self.assertEqual(False, self.c.delete())

    def testDeleteFirst(self):
        testlib.putData(self.txn)
        self.assertEqual(False, self.c.delete())
        self.c.first()
        self.assertEqual(BT('a', ''), self.c.item())
        self.assertEqual(True, self.c.delete())
        self.assertEqual(BT('b', ''), self.c.item())
        self.assertEqual(True, self.c.delete())
        self.assertEqual(BT('baa', ''), self.c.item())
        self.assertEqual(True, self.c.delete())
        self.assertEqual(BT('d', ''), self.c.item())
        self.assertEqual(True, self.c.delete())
        self.assertEqual(BT('', ''), self.c.item())
        self.assertEqual(False, self.c.delete())
        self.assertEqual(BT('', ''), self.c.item())

    def testDeleteLast(self):
        testlib.putData(self.txn)
        self.assertEqual(True, self.c.last())
        self.assertEqual(BT('d', ''), self.c.item())
        self.assertEqual(True, self.c.delete())
        self.assertEqual(BT('', ''), self.c.item())
        self.assertEqual(False, self.c.delete())
        self.assertEqual(BT('', ''), self.c.item())

    def testCount(self):
        self.assertRaises(Exception, (lambda: self.c.count()))
        testlib.putData(self.txn)
        self.c.first()
        # TODO: complete dup key support.
        #self.assertEqual(1, self.c.count())

    def testPut(self):
        pass

class CursorTest2(unittest.TestCase):
    def tearDown(self):
        testlib.cleanup()

    def setUp(self):
        self.path, self.env = testlib.temp_env()
        self.db = self.env.open_db(b'foo', dupsort=True)
        self.txn = self.env.begin(write=True, db=self.db)
        self.c = self.txn.cursor()

    def testIterWithDeletes(self):
        ''' A problem identified in LMDB 0.9.27 '''
        self.c.put(b'\x00\x01', b'hehe', dupdata=True)
        self.c.put(b'\x00\x02', b'haha', dupdata=True)
        self.c.set_key(b'\x00\x02')
        it = self.c.iternext()
        self.assertEqual((b'\x00\x02', b'haha'), next(it))
        self.txn.delete(b'\x00\x01', b'hehe', db=self.db)
        self.assertRaises(StopIteration, next, it)

class PutmultiTest(CursorTestBase):
    def test_empty_seq(self):
        consumed, added = self.c.putmulti(())
        assert consumed == added == 0

    def test_2list(self):
        l = [BT('a', ''), BT('a', '')]
        consumed, added = self.c.putmulti(l)
        assert consumed == added == 2

        li = iter(l)
        consumed, added = self.c.putmulti(li)
        assert consumed == added == 2

    def test_2list_preserve(self):
        l = [BT('a', ''), BT('a', '')]
        consumed, added = self.c.putmulti(l, overwrite=False)
        assert consumed == 2
        assert added == 1

        assert self.c.set_key(B('a'))
        assert self.c.delete()

        li = iter(l)
        consumed, added = self.c.putmulti(li, overwrite=False)
        assert consumed == 2
        assert added == 1

    def test_bad_seq1(self):
        self.assertRaises(Exception,
                          lambda: self.c.putmulti(range(2)))

    def test_dupsort(self):
        _, env = testlib.temp_env()
        db1 = env.open_db(B('db1'), dupsort=True)
        txn = env.begin(write=True, db=db1)
        with txn.cursor() as c:
            tups = [BT('a', 'value1'), BT('b', 'value1'), BT('b', 'value2')]
            assert (3, 3) == c.putmulti(tups)

    def test_dupsort_putmulti_append(self):
        _, env = testlib.temp_env()
        db1 = env.open_db(B('db1'), dupsort=True)
        txn = env.begin(write=True, db=db1)
        with txn.cursor() as c:
            tups = [BT('a', 'value1'), BT('b', 'value1'), BT('b', 'value2')]
            assert (3, 3) == c.putmulti(tups, append=True)

    def test_dupsort_put_append(self):
        _, env = testlib.temp_env()
        db1 = env.open_db(B('db1'), dupsort=True)
        txn = env.begin(write=True, db=db1)
        with txn.cursor() as c:
            assert c.put(B('a'), B('value1'), append=True)
            assert c.put(B('b'), B('value1'), append=True)
            assert c.put(B('b'), B('value2'), append=True)

class ReplaceTest(CursorTestBase):
    def test_replace(self):
        assert None is self.c.replace(B('a'), B(''))
        assert B('') == self.c.replace(B('a'), B('x'))
        assert B('x') == self.c.replace(B('a'), B('y'))


class ContextManagerTest2(CursorTestBase):
    def test_enter(self):
        with self.c as c:
            assert c is self.c
            c.put(B('a'), B('a'))
            assert c.get(B('a')) == B('a')
        self.assertRaises(Exception,
            lambda: c.get(B('a')))

    def test_exit_success(self):
        with self.txn.cursor() as c:
            c.put(B('a'), B('a'))
        self.assertRaises(Exception,
            lambda: c.get(B('a')))

    def test_exit_failure(self):
        try:
            with self.txn.cursor() as c:
                c.put(B('a'), B('a'))
            raise ValueError
        except ValueError:
            pass
        self.assertRaises(Exception,
            lambda: c.get(B('a')))

    def test_close(self):
        self.c.close()
        self.assertRaises(Exception,
            lambda: c.get(B('a')))

    def test_double_close(self):
        self.c.close()
        self.c.close()
        self.assertRaises(Exception,
            lambda: self.c.put(B('a'), B('a')))

GiB = 1024 * 1024 * 1024

class PreloadTest(CursorTestBase):

    def setUp(self, redo=False):
        env_args = {'writemap': True, 'map_size': GiB}
        if not redo:
            self.path, self.env = testlib.temp_env(**env_args)
        else:
            self.path, self.env = testlib.temp_env(path=self.path, **env_args)
        self.txn = self.env.begin(write=True)
        self.c = self.txn.cursor()

    @unittest.skipIf(not sys.platform.startswith('linux'), "test only works on Linux")
    def test_preload(self):
        """
        Test that reading just the key doesn't prefault the value contents, but
        reading the data does.
        """

        import resource
        self.c.put(B('a'), B('a') * (256 * 1024 * 1024))
        self.txn.commit()
        self.env.close()
        # Just reading the data is obviously going to fault the value in.  The
        # point is to fault it in while the GIL is unlocked.  We use the buffers
        # API so that we're not actually copying the data in.  This doesn't
        # actually show that we're prefaulting with the GIL unlocked, but it
        # does prove we prefault at all, and in 2 correct places.
        self.path, self.env = testlib.temp_env(path=self.path, writemap=True)
        self.txn = self.env.begin(write=True, buffers=True)
        self.c = self.txn.cursor()
        minflts_before = resource.getrusage(resource.RUSAGE_SELF)[6]
        self.c.set_key(B('a'))
        assert bytes(self.c.key()) == B('a')
        minflts_after_key = resource.getrusage(resource.RUSAGE_SELF)[6]

        self.c.value()
        minflts_after_value = resource.getrusage(resource.RUSAGE_SELF)[6]

        epsilon = 60

        # Setting the position doesn't prefault the data
        assert minflts_after_key - minflts_before < epsilon

        # Getting the value does prefault the data, even if we only get it by pointer
        assert minflts_after_value - minflts_after_key > 1000

class CursorReadOnlyTest(unittest.TestCase):
    def tearDown(self):
        testlib.cleanup()

    def test_cursor_readonly(self):
        '''
        Tests whether you can open a cursor on a sub-db at all in a read-only environment.
        '''
        path, env = testlib.temp_env(max_dbs=10)
        env.open_db(b'foo')
        env.close()
        with lmdb.open(path, max_dbs=10, readonly=True) as env:
            db2 = env.open_db(b'foo')
            with env.begin(db=db2) as txn:
                with txn.cursor(db=db2):
                    pass

if __name__ == '__main__':
    unittest.main()