import os, sys import time import struct import subprocess import shutil import base64 OP_MAPPING = { } def _pack_line_bin(op, ttl, key_size, val_size, ): # first eight bit is op, the last 24 bit is ttl op = op << 24 ttl = min(req.ttl, 8000000) & (0x01000000-1) op_ttl = op | ttl # first 10 bit is key size, last 22 bit is value size assert req.value_size < 0x00400000-1, "value size too large " + str(req.value_size) ksz, vsz = req.key_size << 22, req.value_size & (0x00400000-1) kv_size = ksz | vsz self.ofile.write(self.mystruct.pack(req.real_time, req.obj_id, kv_size, op_ttl)) def _count_keylen(key): """ count the bytes in hex """ len0 = len(key) key = key.replace("\\x", "") # count of 0x in the key n_0x = (len0 - len(key))//2 cnt = len(key) - n_0x - key.count("\\") # print(len(key), cnt, key) return cnt def _remove_topology(key): pos = 0 n_bytes = 8 while n_bytes > 0: if key[pos] == "\\": if key[pos+1] == "x": pos += 4 else: pos += 2 else: pos += 1 n_bytes -=1 assert(key[pos] == "\\") return key[pos:] op_set = set(("llen", "ttl", "type", "scan", "ping", "info", "command", "select", "dbsize")) def _convert_line(line_split): info1 = line_split[0].split() ts = float(info1[0]) - 1604000000 db = int(info1[1][1:]) op = info1[3].strip('"') if op.lower() in op_set: return None pkey = _remove_topology(line_split[1].strip('"')) pklen = _count_keylen(pkey) # pkey = hash(pkey) other_fields = [] if op[:12] == "RPUSHXTRIMEX" or op == "RPUSHTRIMEX": # RPUSHXTRIMEX [ ...] trim = int(line_split[2].strip('"')) ttl = int(line_split[3].strip('"')) # val = [hash(line_split[i].strip('"')) for i in range(4, len(line_split))] val = [_count_keylen(line_split[i].strip('"')) for i in range(4, len(line_split))] other_fields = [trim, ttl, *val] # if len(val) != 1: # print(val) elif op == "LMREM": # LMREM (multi-LREM) : Remove multiple count, element pairs # LMREM [ ...] i = 2 while len(line_split) > i + 1: count = int(line_split[i].strip('"')) elem = hash(line_split[i+1].strip('"')) other_fields.append(count) other_fields.append(elem) i += 2 elif op[:6] == "LRANGE": start = int(line_split[2].strip('"')) stop = int(line_split[3].strip('"')) ttl, incr, max_ttl = "", "", "" if op[:8] == "LRANGEEX": # LRANGEEX [incr] [max ttl] ttl = int(line_split[4].strip('"')) if len(line_split) > 5: incr = line_split[5].strip('"') if len(line_split) > 6: max_ttl = int(line_split[6].strip('"')) other_fields = [start, stop, ttl, incr, max_ttl] elif op == "LRESET": # Create a new list, append values, and set TTL # LRESET [ ...] trim = int(line_split[2].strip('"')) ttl = int(line_split[3].strip('"')) val = [hash(line_split[i].strip('"')) for i in range(4, len(line_split))] other_fields = [trim, ttl, *val] elif op == "DEL": # other_fields = [hash(line_split[i].strip('"')) for i in range(2, len(line_split))] other_fields = [_count_keylen(line_split[i].strip('"')) for i in range(2, len(line_split))] if len(other_fields) != 0: print(other_fields) elif op == "HMGET": raise RuntimeError("not suuported {}".format(line_split)) elif op == "HMERGEEX": # HMERGEEX [ ] raise RuntimeError("not suuported {}".format(line_split)) elif op == "HMSETEX": raise RuntimeError("not suuported {}".format(line_split)) elif op.lower() == "ttl" or op.lower() == "type": return None else: raise RuntimeError("unknown command {}".format(line_split)) return ts, db, op, pkey, pklen, other_fields def convert(input_file): ifile = open(input_file) ofile = open(input_file + ".clean", "w") for line in ifile: if line.startswith("OK"): continue line_split = line.replace('\\\\"', 'a"').replace('\\"', 'a').strip("\n").split('" "') try: req = _convert_line(line_split) if req is None: continue ts, db, op, pkey, pklen, other_fields = req ofile.write("{:10.2f}\t{:2}\t{}\t{}\t{}\t{}\n".format(ts, db, op, pkey, pklen, ",".join(str(i) for i in other_fields))) except Exception as e: line = line.lower() # if "info" in line or "command" in line or 'select' in line or 'dbsize' in line: # continue print(input_file, line) for i, x in enumerate(line_split): print(i, x) print(e) # raise RuntimeError(e) if __name__ == "__main__": time.sleep(1) convert(sys.argv[1]+".1") shutil.move(sys.argv[1]+".1.clean", sys.argv[1]+".1")