Skip to content

Commit 025f8d0

Browse files
Add a function to repair a database
This is automatically called if the database fails to open but this allows a caller to manually run it.
1 parent a43e443 commit 025f8d0

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

src/leveldb/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
LevelDBEncrypted,
55
LevelDBIteratorException,
66
Iterator,
7+
repair_db
78
)
89

910
from . import _version

src/leveldb/_leveldb.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,6 @@ class LevelDB:
7171
def __setitem__(self, key: bytes, value: bytes) -> None: ...
7272
def __delitem__(self, key: bytes) -> None: ...
7373
def __iter__(self) -> PyIterator[bytes]: ...
74+
75+
76+
def repair_db(path: str) -> None: ...

src/leveldb/_leveldb.pyx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ cdef inline bint _check_db(DB *db) except -1 nogil:
162162
raise LevelDBException("The database has been closed.")
163163

164164

165+
cdef inline Options* _create_options(bint create_if_missing = False) except NULL nogil:
166+
cdef Options * options = new Options()
167+
options.create_if_missing = create_if_missing
168+
options.filter_policy = NewBloomFilterPolicy(10)
169+
options.block_cache = NewLRUCache(40 * 1024 * 1024)
170+
options.write_buffer_size = 4 * 1024 * 1024
171+
options.info_log = new NullLogger()
172+
options.compressors[0] = new ZlibCompressorRaw(-1)
173+
options.compressors[1] = new ZlibCompressor(-1)
174+
options.block_size = 163840
175+
return options
176+
177+
165178
cdef class LevelDB:
166179
cdef DB *db
167180
cdef ReadOptions read_options
@@ -188,16 +201,7 @@ cdef class LevelDB:
188201
else:
189202
raise LevelDBException(f"No database exists to open at {path}")
190203

191-
cdef Options* options = new Options()
192-
options.create_if_missing = create_if_missing
193-
options.filter_policy = NewBloomFilterPolicy(10)
194-
options.block_cache = NewLRUCache(40 * 1024 * 1024)
195-
options.write_buffer_size = 4 * 1024 * 1024
196-
options.info_log = new NullLogger()
197-
options.compressors[0] = new ZlibCompressorRaw(-1)
198-
options.compressors[1] = new ZlibCompressor(-1)
199-
options.block_size = 163840
200-
cdef const Options* const_options = options
204+
cdef const Options* const_options = _create_options(True)
201205

202206
self.read_options.decompress_allocator = new DecompressAllocator()
203207

@@ -393,3 +397,9 @@ cdef class LevelDB:
393397

394398
def __iter__(self) -> IteratorT[bytes]:
395399
return self.keys()
400+
401+
402+
def repair_db(str path):
403+
cdef string s_path = path.encode()
404+
cdef const Options* const_options = _create_options()
405+
RepairDB(s_path, dereference(const_options))

0 commit comments

Comments
 (0)