-
-
Save mikez/dcd8cd65319049e434820c3c9459cc4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
USAGE | |
leveldb-keys.py file | |
DESCRIPTION | |
Output keys of an ldb or log file. | |
Requires the folders.py file in same directory as this file; | |
available at https://git.io/folders | |
LOG file: fresh writes of LevelDB go here; | |
this is *NOT* sorted by keys, rather by most-recent write at the end. | |
LDB files: older writes of LevelDB are here; | |
this is sorted by the keys according to a comparator function; this | |
function may be alphanumeric, but could also be something custom whose | |
internals are not visible directly from the database files. | |
EXAMPLES | |
python leveldb-dump.py primary.ldb/000127.ldb | |
python leveldb-dump.py primary.ldb/000125.log | |
""" | |
import sys | |
from folders import ( | |
BytesReader, | |
LogReader, | |
TableData, | |
TableFooter, | |
TableIndex, | |
TableReader, | |
filesize, | |
) | |
def main(filepath): | |
if filepath.endswith(".ldb"): | |
print_ldb_keys(filepath) | |
elif filepath.endswith(".log"): | |
print_log_keys(filepath) | |
else: | |
raise ValueError("Unsupported file extension.") | |
def print_ldb_keys(filepath): | |
with open(filepath, "rb") as file: | |
reader = BytesReader(file, filesize(filepath)) | |
footer = TableFooter(reader) | |
for internal_key, handle in TableIndex(footer.index_handle, reader): | |
for internal_key, value in TableData(handle, reader): | |
print_bytes(internal_key.user_key) | |
def print_log_keys(filepath): | |
with open(filepath, "rb") as file: | |
for batch in LogReader(file, filesize(filepath)): | |
for command, args in batch: | |
if command != "put": | |
continue | |
key, value = args | |
print_bytes(key) | |
def print_bytes(bytestring): | |
print(repr(bytestring)[2:-1]) | |
if __name__ == "__main__": | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment