File size: 4,931 Bytes
2261eaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168


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 <outer key> <trim length> <ttl> <value> [<value> ...]
    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 <outer key> <count> <element> [<count> <element> ...]
    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 <outer key> <start> <stop> <ttl> [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 <outer key> <trim length> <ttl> <value> [<value> ...]
    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 <outer key> <ttl> [<inner key> <value>] 
    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")