-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_model.py
296 lines (253 loc) · 8.58 KB
/
db_model.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from argparse import _ActionsContainer
import sqlite3 as sql
from sqlite3 import IntegrityError
import time
DATABASE_FILE = "ds2015.db"
CHUNK_SIZE = 250000
def init():
cur, conn = getCursor()
cur.execute('''
CREATE TABLE IF NOT EXISTS hashes
(
id INTEGER PRIMARY KEY ASC,
hash TEXT UNIQUE,
created_time REAL,
started_time REAL,
solved_time REAL,
solved INTEGER DEFAULT 0,
solution TEXT,
solvable INTEGER DEFAULT 1
)
''')
cur.execute('''
CREATE TABLE IF NOT EXISTS progress
(
hash_id INTEGER REFERENCES hashes(id) ON DELETE CASCADE,
character TEXT,
row REAL
)
''')
cur.execute('''
CREATE TABLE IF NOT EXISTS clients
(
id TEXT UNIQUE,
hash_working INTEGER,
join_time REAL,
last_ping REAL
)
''')
cur.execute('''
CREATE TABLE IF NOT EXISTS work
(
id INTEGER PRIMARY KEY ASC,
hash_id INTEGER REFERENCES hashes(id) ON DELETE CASCADE,
character TEXT,
progress REAL,
done INTEGER DEFAULT 0
)
''')
def addWork(target, character, progress):
cur, conn = getCursor()
cur.execute("SELECT id FROM hashes WHERE hash = '" + str(target) + "'")
for row in cur:
hash_id = row[0]
cur.execute("INSERT INTO work (hash_id, character, progress) VALUES ("+str(hash_id)+",'"+str(character)+"','"+str(progress)+"')")
conn.commit()
def executeWork(target, character, progress):
print "executing work " + str(target) + str(character) + str(progress)
if len(target) == 0:
return 0
cur, conn = getCursor()
cur.execute("SELECT id FROM hashes WHERE hash = '" + str(target) + "'")
for row in cur:
hash_id = row[0]
cur.execute("UPDATE work SET done = 1 WHERE hash_id = '" + str(hash_id) + "' AND character = '"+str(character)+"' AND progress='"+str(progress)+"'")
conn.commit()
def getUnfinishedWork():
cur, conn = getCursor()
cur.execute("SELECT hashes.hash, character, progress FROM work JOIN hashes ON hashes.id = work.hash_id WHERE done = 0")
for row in cur:
return str(row[0]), str(row[1]), row[2]
return None, None, None
def getClients():
result = []
cur, conn = getCursor()
cur.execute('''
SELECT * FROM clients
''')
for row in cur:
result.append(row)
return result
def getUnsolvedHashes():
result = []
cur, conn = getCursor()
cur.execute("SELECT * FROM hashes WHERE solved = 0 AND solvable = 1")
for row in cur:
result.append(row)
return result
def getSolvedHashes():
result = []
cur, conn = getCursor()
cur.execute("SELECT * FROM hashes WHERE solved = 1")
for row in cur:
result.append(row)
return result
def getProgress(client_id):
###print "getprogress"
hash_id = 0
target = ""
cur, conn = getCursor()
sql = "SELECT clients.hash_working FROM clients JOIN hashes ON hashes.id = clients.hash_working WHERE clients.id = '" + str(client_id) + "' AND hashes.solvable = 1"
cur.execute(sql)
found = False
for row in cur:
hash_id = row[0]
if not hash_id:
return None, None, None
if not found:
return getNextHash()
def getNextHash():
cur, conn = getCursor()
cur.execute("SELECT id, hash FROM hashes WHERE solved = 0 AND solvable = 1 ORDER BY created_time LIMIT 1")
found = False
for row in cur:
#print row[0]
hash_id = row[0]
target = row[1]
found = True
if not found:
return None, None, None
sql = "SELECT hashes.solvable, progress.character, progress.row FROM progress JOIN hashes ON hashes.id = progress.hash_id WHERE hash_id = '" + str(hash_id) + "'"
cur.close()
cur, conn = getCursor()
cur.execute(sql)
for row in cur:
updateClient(hash_id, row[1], row[2])
nextChunk(hash_id)
#print "returning " + target + str(row[1]) + str(row[2])
return target, row[1], row[2]
# update client progress
def updateClient(hash_id, character, row):
cur, conn = getCursor()
cur.execute("UPDATE progress SET character = '" + str(character) + "', row = '" + str(row)+ "' WHERE hash_id = '" + str(hash_id) + "'")
conn.commit()
# set client to work on hash x
def setClientWorking(target, client_id):
cur, conn = getCursor()
cur.execute("SELECT id FROM hashes WHERE hash = '" + str(target) + "'")
for row in cur:
hash_id = row[0]
cur.execute("UPDATE clients SET hash_working = '" + str(hash_id) + "' WHERE id = '" + str(client_id) + "'")
conn.commit()
ping(client_id)
def nextChunk(hash_id):
###print "nextchunk"
cur, conn = getCursor()
cur.execute("SELECT row FROM progress WHERE hash_id = '" + str(hash_id) + "'")
found = False
for row in cur:
curRow = row[0]
found = True
###print "found"
if not found:
return 0
cur.execute("UPDATE progress SET row = " + str(curRow + CHUNK_SIZE))
conn.commit()
cur.close()
def nextCharacter(target_hash):
cur, conn = getCursor()
cur.execute("SELECT id FROM hashes WHERE hash = '" + target_hash + "' AND solvable = 1 AND solved = 0")
for row in cur:
hash_id = row[0]
cur.execute("SELECT character FROM progress WHERE hash_id = '" + str(hash_id) + "'")
for row in cur:
curChar = row[0]
new_char = ""
## if not end of alphabets
if ord(curChar) < 122:
new_char = chr(ord(curChar) + 1)
else:
target, character, progress = getUnfinishedWork()
if not target:
couldNotSolve(hash_id)
return target, character, progress
cur.execute("UPDATE progress SET character = '" + str(new_char) + "', row = 0 WHERE hash_id = '" + str(hash_id) + "'")
conn.commit()
return None, None, None
def solve(target, solution):
cur, conn = getCursor()
cur.execute("UPDATE hashes SET solved = 1, solution = '" + str(solution) + "' WHERE hash = '" + target + "'")
conn.commit()
def couldNotSolve(hash_id):
cur, conn = getCursor()
print "setting " + str(hash_id) + " to unsolvable"
cur.execute("UPDATE hashes SET solvable = 0 WHERE id = '" + str(hash_id) + "'")
conn.commit()
def post_hash(hash):
cur, conn = getCursor()
sql = "INSERT INTO hashes (hash, created_time) VALUES ('" + str(hash) + "'," + str(time.time()) + ")"
try:
cur.execute(sql)
conn.commit()
lastid = cur.lastrowid
except IntegrityError:
conn.close()
return "Hash already exists"
cur.execute("INSERT INTO progress VALUES (" + str(lastid) + ",'a', 1)")
conn.commit()
cur.execute("SELECT * FROM hashes WHERE id = " + str(lastid))
for row in cur:
return row
def connectClient(client_id):
cur, conn = getCursor()
#print "connecting " + client_id
try:
cur.execute("INSERT INTO clients (id, join_time, last_ping) VALUES ('"+str(client_id)+"', "+str(time.time())+"," +str(time.time())+")")
conn.commit()
except Exception:
return "FAILURE, not unique"
sql = "SELECT last_ping FROM clients WHERE id = '" + str(client_id) + "'"
cur.execute(sql)
for row in cur:
conn.close()
return "OK"
conn.close()
return "FAILURE"
def disconnect(client_id):
cur, conn = getCursor()
cur.execute("DELETE FROM clients WHERE id = '" + str(client_id) + "'")
conn.commit()
conn.close()
#print "Disconnected " + str(client_id)
return "Deleted"
def ping(client_id):
cur, conn = getCursor()
now = time.time()
#print "ping : " + str(now)
#print "client : " + str(client_id)
cur.execute("UPDATE clients SET last_ping = " + str(now) + " WHERE id = '" + str(client_id) + "'")
conn.commit()
cur.close()
return str(now)
def getPing(client_id):
cur, conn = getCursor()
cur.execute("SELECT last_ping FROM clients WHERE id = '" + str(client_id) + "'")
for row in cur:
return str(row[0])
# remove nonresponding clients
def killThemDead():
cur, conn = getCursor()
now = time.time()
#print "killing, now = " + str(now)
cur.execute("SELECT id, last_ping FROM clients")
for row in cur:
#print "client last_ping = " + str(row[1])
#print "diff = " + str(now - float(row[1]))
# if client not responed for 60 seconds
if (now - float(row[1])) > 90.0:
cur.execute("DELETE FROM clients WHERE id ='" + str(row[0]) + "'")
conn.commit()
def getCursor():
c = sql.connect(DATABASE_FILE)
c.cursor().execute("PRAGMA foreign_keys = ON")
return c.cursor(), c