-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync_tt_llkv.py
171 lines (141 loc) · 3.92 KB
/
sync_tt_llkv.py
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
169
170
import llkv
import re
import ctypes
import struct
import pdb
import io
import os
list_server = ("192.168.1.194", 8000)
dp_server = ("192.168.1.194", 8001)
dp_pattern = re.compile("[0-9]+\t([0-9]+)\t([\-0-9]+)\t")
list_pattern = re.compile("([0-9\-]+)\t<p>([0-9\-]+)</p><s>([0-9\-]+)</s>")
def dp_store(to, item):
site_id, crc = int(item[0]), int(item[1])
if site_id > 0xffff or site_id < 0:
print "overflow", item
return
key = (int(site_id) << 48) |ctypes.c_uint(int(crc)).value
to[key] = 0
def list_store(to, item):
key, price, stock = item
try:
key = key_to_ulong(key)
value = price_stock_ulong(price, stock)
except ValueError:
print "garbage", repr(item)
return
if not key or not value:
return
to[key] = value
def key_to_ulong(key):
u = key.rfind("-")
site_id = int(key[u+1:])
crc = int(key[:u])
if crc > 0xffffffffffff or site_id > 0xffff:
print "overflow: %s" % key
return
return (site_id << 48) | ctypes.c_uint(crc).value
def price_stock_ulong(price, stock):
try:
price = int(price)
stock = int(stock)
except ValueError:
return
if stock > 0:
stock = 1
if stock < 0:
stock = 0
if price < 0:
price = 0
if price > 0x7fffffffffffffff:
print "overflow", price , stock
return
value = price | (int(stock) << 63)
return value
def reset_file_ptr(fileobj):
if os.path.exists("llkv.dp.ptr"):
fileobj.seek(int(open("llkv.dp.ptr").read()))
def load(path, server, pat, store_fn):
m10 = 1024 * 1024 * 100
cnt = 0
fobj = open(path)
#reset_file_ptr(fobj)
con = llkv.Connection(*server)
while True:
data = fobj.read(m10)
lastline = data.rfind("\n")
if lastline <= 0:
break
fobj.seek(lastline - len(data), io.SEEK_CUR)
result = pat.findall(data)
print "got items: ", len(result)
to = {}
pack = struct.pack
for item in result:
store_fn(to, item)
if len(to) == 1000:
cnt += 1000
con.multi_set(to)
to = {}
if to:
cnt += len(to)
con.multi_set(to)
print "current:", cnt
ptr = open("llkv.dp.ptr", "w+")
ptr.write(str(fobj.tell()))
ptr.close()
con.close()
fobj.close()
def get_key(server, key):
key = key_to_ulong(key)
con = llkv.Connection(*llkv_server)
value = con.get(key)
if not value:
return key, None, None
stock = value >> 63
price = value & 0x7fffffffffffffff
return key, price, stock
def sync_dp_idx():
import MySQLdb
db = {
"host": "192.168.1.192",
"port": 3306,
"db": "dp_idx",
"user": "minishop",
"passwd": "MiniShop!@#"
}
ll = llkv.Connection(*dp_server)
con = MySQLdb.connect(**db)
cur = con.cursor()
print "loading from dp_idx"
cur.execute("select url_crc, site_id from idx")
from ctypes import c_uint
keys = []
cnt = 0
for crc, site_id in cur.fetchall():
key = (site_id << 48) | c_uint(crc).value
keys.append((key, 0))
if len(keys) > 1000:
cnt += len(keys)
print "current", cnt
ll.multi_set(dict(keys))
keys = []
ll.close()
con.close()
def main():
import sys
if sys.argv[1] == "dp":
sync_dp_idx()
load("/pub_file/last_url.txt", dp_server, dp_pattern, dp_store)
elif sys.argv[1] == "list":
load("/tmp/llkv.list", list_server, list_pattern, list_store)
elif sys.argv[1] == "get":
print get_key(sys.argv[2])
else:
print """
dp load /tmp/llkv.dp
list load /tmp/llkv.list
get key
"""
if __name__ == "__main__":
main()