Last active
February 8, 2018 09:10
-
-
Save soasme/8617918 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
# -*- coding: utf-8 -*- | |
import os | |
from datetime import datetime | |
from binascii import crc32 | |
import cPickle as pickle | |
from glob import glob | |
TABLE = {} | |
current_active = max([int(f.replace('.sst', '')) for f in glob('*.sst')] or [1]) | |
THRESHOLD = 10000 | |
def io_read(filename, start, length): | |
with open(filename, 'rb') as f: | |
f.seek(start) | |
return f.read(length) | |
def io_write(crc, ts, key_size, value_size, key, value): | |
global current_active | |
active_filename = '%s.sst' % current_active | |
try: | |
if os.path.getsize(active_filename) >= THRESHOLD: | |
current_active += 1 | |
active_filename = '%s.sst' % current_active | |
except: | |
pass | |
with open(active_filename, 'a') as f: | |
data = pickle.dumps((crc, ts, key_size, value_size, key, value)) | |
start = f.tell() | |
length = len(data) | |
f.write(data) | |
return active_filename, start, length | |
def get(key): | |
info = TABLE.get(key) | |
if not info: | |
raise Exception('not found.') | |
pickled_data = io_read(*info) | |
data = pickle.loads(pickled_data) | |
# check tuple, crc, the same key? | |
return data[5] | |
def put(key, value): | |
key_size = len(key) | |
value_size = len(value) | |
ts = datetime.now() | |
crc = crc32('{0}{1}{2}{3}{4}'.format( | |
ts, key_size, value_size, key, value)) | |
info = io_write(crc, ts, key_size, value_size, key, value) | |
TABLE[key] = info | |
return True | |
def delete(key): | |
pass | |
def list_keys(): | |
pass | |
if __name__ == '__main__': | |
put('test', 'hello world') | |
print get('test') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment