-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARCdb.py
102 lines (85 loc) · 2.75 KB
/
ARCdb.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
#!/usr/bin/python
datadirs = ["data"]
from marshal import dump, load
import os, os.path, time
from ARCive import ARCive
from pprint import pprint
dbDir = {}
def createDatabaseFromScratch():
global dbDir
dbDir = {}
for d in datadirs:
if not os.path.exists(d):
continue
for basefn in os.listdir(d):
fn = os.path.join(d, basefn)
if os.path.isfile(fn) and fn.endswith(".arc.bz2"):
print fn
start = time.time()
arcDir = arcDirGetCacheForDb(d, basefn)
if not arcDir:
print "rebuilding cache for %r" % fn
a = ARCive(fn, 'r')
arcDir = a.getDir()
saveCacheForDb(arcDir, d, basefn)
print "updating dbdir"
for url in arcDir:
if url not in dbDir:
dbDir[url] = []
for x in arcDir[url]:
dbDir[url].append((x["checksum"], x["date"], x["offset"], fn))
delta = time.time() - start
print "dbdirlen=%d, %d urls in %fs, %f url/s" % (len(dbDir), len(arcDir), delta, len(arcDir)/delta)
def saveCacheForDb(arcDir, d, basefn):
if not os.path.exists(os.path.join(d, ".cache")):
os.makedirs(os.path.join(d, ".cache"))
fn = os.path.join(d, ".cache", basefn)
fd = open("%s.marshal" % fn, "w")
dump(arcDir, fd)
fd.close()
def arcDirGetCacheForDb(d, basefn):
cachn = "%s.marshal" % os.path.join(d, ".cache", basefn)
arcn = os.path.join(d, basefn)
if os.path.exists(cachn) and os.path.exists(arcn) and \
(os.path.getmtime(cachn) >= os.path.getmtime(arcn)):
return load(open(cachn))
else:
return None
def updateMemoryDb():
if dbDir == {}:
createDatabaseFromScratch()
def getEntries(url):
updateMemoryDb()
if url in dbDir:
return dbDir[url]
else:
return None
def getURLsBySubstring(urlfragment):
updateMemoryDb()
ret = []
for x in dbDir.keys():
if urlfragment in x:
ret.append(x)
return ret
def getFile(pos, file, url):
print url, pos, file
a = ARCive(file)
ret = a.readRawDocAtPos(pos)
a.close()
return ret
def getURLs():
updateMemoryDb()
return dbDir.keys()
import hotshot, hotshot.stats
if __name__ == "__main__":
prof = hotshot.Profile("ARCdb.prof")
prof.runcall(getEntries, "http://blogs.23.nu/")
prof.close()
stats = hotshot.stats.load("ARCdb.prof")
stats.strip_dirs()
stats.sort_stats('time', 'calls')
stats.print_stats(20)
pprint(getEntries("http://blogs.23.nu/"))
# getEntries("http://blogs.23.nu/")
print len(dbDir.keys())
#pprint(dbDir)