Skip to content

Commit

Permalink
use ujson/cStringIO/python-lzf if they're available
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Owen committed Jan 19, 2016
1 parent d39c8e5 commit b2640bd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ tests/dumps/dump_dealers_vins.rdb
tests/dumps/dump_random_lists.rdb
tests/dumps/dump_sorted_sets.rdb

.idea/*
7 changes: 5 additions & 2 deletions rdbtools/cli/redis_memory_for_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import os
import sys

try :
from StringIO import StringIO
try:
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
except ImportError:
from io import StringIO

Expand Down
5 changes: 4 additions & 1 deletion rdbtools/memprofiler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from collections import namedtuple
import random
import json
try:
import ujson as json
except:
import json

from rdbtools.parser import RdbCallback
from rdbtools.callbacks import encode_key
Expand Down
78 changes: 45 additions & 33 deletions rdbtools/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
import datetime
import re

try :
from StringIO import StringIO
try:
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
except ImportError:
from io import StringIO

try:
import lzf
HAS_PYTHON_LZF = True
except ImportError:
HAS_PYTHON_LZF = False

REDIS_RDB_6BITLEN = 0
REDIS_RDB_14BITLEN = 1
Expand Down Expand Up @@ -653,38 +662,41 @@ def get_logical_type(self, data_type):
return DATA_TYPE_MAPPING[data_type]

def lzf_decompress(self, compressed, expected_length):
in_stream = bytearray(compressed)
in_len = len(in_stream)
in_index = 0
out_stream = bytearray()
out_index = 0

while in_index < in_len :
ctrl = in_stream[in_index]
if not isinstance(ctrl, int) :
raise Exception('lzf_decompress', 'ctrl should be a number %s for key %s' % (str(ctrl), self._key))
in_index = in_index + 1
if ctrl < 32 :
for x in xrange(0, ctrl + 1) :
out_stream.append(in_stream[in_index])
#sys.stdout.write(chr(in_stream[in_index]))
in_index = in_index + 1
out_index = out_index + 1
else :
length = ctrl >> 5
if length == 7 :
length = length + in_stream[in_index]
in_index = in_index + 1

ref = out_index - ((ctrl & 0x1f) << 8) - in_stream[in_index] - 1
if HAS_PYTHON_LZF:
return lzf.decompress(compressed, expected_length)
else:
in_stream = bytearray(compressed)
in_len = len(in_stream)
in_index = 0
out_stream = bytearray()
out_index = 0

while in_index < in_len :
ctrl = in_stream[in_index]
if not isinstance(ctrl, int) :
raise Exception('lzf_decompress', 'ctrl should be a number %s for key %s' % (str(ctrl), self._key))
in_index = in_index + 1
for x in xrange(0, length + 2) :
out_stream.append(out_stream[ref])
ref = ref + 1
out_index = out_index + 1
if len(out_stream) != expected_length :
raise Exception('lzf_decompress', 'Expected lengths do not match %d != %d for key %s' % (len(out_stream), expected_length, self._key))
return str(out_stream)
if ctrl < 32 :
for x in xrange(0, ctrl + 1) :
out_stream.append(in_stream[in_index])
#sys.stdout.write(chr(in_stream[in_index]))
in_index = in_index + 1
out_index = out_index + 1
else :
length = ctrl >> 5
if length == 7 :
length = length + in_stream[in_index]
in_index = in_index + 1

ref = out_index - ((ctrl & 0x1f) << 8) - in_stream[in_index] - 1
in_index = in_index + 1
for x in xrange(0, length + 2) :
out_stream.append(out_stream[ref])
ref = ref + 1
out_index = out_index + 1
if len(out_stream) != expected_length :
raise Exception('lzf_decompress', 'Expected lengths do not match %d != %d for key %s' % (len(out_stream), expected_length, self._key))
return str(out_stream)

def skip(f, free):
if free :
Expand Down

0 comments on commit b2640bd

Please sign in to comment.