##// END OF EJS Templates
pickleshare: hget, hset, hdict (for write efficient hash bucket file storage)
vivainio -
Show More
@@ -40,6 +40,9 b' import UserDict'
40 import warnings
40 import warnings
41 import glob
41 import glob
42
42
43 def gethashfile(key):
44 return ("%02x" % abs(hash(key) % 256))[-2:]
45
43 class PickleShareDB(UserDict.DictMixin):
46 class PickleShareDB(UserDict.DictMixin):
44 """ The main 'connection' object for PickleShare database """
47 """ The main 'connection' object for PickleShare database """
45 def __init__(self,root):
48 def __init__(self,root):
@@ -82,6 +85,35 b' class PickleShareDB(UserDict.DictMixin):'
82 if e.errno != 2:
85 if e.errno != 2:
83 raise
86 raise
84
87
88 def hset(self, hashroot, key, value):
89 hroot = self.root / hashroot
90 if not hroot.isdir():
91 hroot.makedirs()
92 hfile = hroot / gethashfile(key)
93 d = self.get(hfile, {})
94 d.update( {key : value})
95 self[hfile] = d
96
97 def hget(self, hashroot, key, default = None):
98 hroot = self.root / hashroot
99 hfile = hroot / gethashfile(key)
100 d = self.get(hfile, None)
101 #print "got dict",d,"from",hfile
102 if d is None:
103 return default
104 return d.get(key, default)
105
106 def hdict(self, hashroot):
107 buckets = self.keys(hashroot + "/*")
108 hfiles = [f for f in buckets]
109 all = {}
110 for f in hfiles:
111 # print "using",f
112 all.update(self[f])
113 self.uncache(f)
114
115 return all
116
85 def __delitem__(self,key):
117 def __delitem__(self,key):
86 """ del db["key"] """
118 """ del db["key"] """
87 fil = self.root / key
119 fil = self.root / key
@@ -192,6 +224,11 b' def test():'
192 db['hello'] = 15
224 db['hello'] = 15
193 db['aku ankka'] = [1,2,313]
225 db['aku ankka'] = [1,2,313]
194 db['paths/nest/ok/keyname'] = [1,(5,46)]
226 db['paths/nest/ok/keyname'] = [1,(5,46)]
227 db.hset('hash', 'aku', 12)
228 db.hset('hash', 'ankka', 313)
229 print "12 =",db.hget('hash','aku')
230 print "313 =",db.hget('hash','ankka')
231 print "all hashed",db.hdict('hash')
195 print db.keys()
232 print db.keys()
196 print db.keys('paths/nest/ok/k*')
233 print db.keys('paths/nest/ok/k*')
197 print dict(db) # snapsot of whole db
234 print dict(db) # snapsot of whole db
@@ -207,7 +244,7 b' def stress():'
207 db = PickleShareDB('~/fsdbtest')
244 db = PickleShareDB('~/fsdbtest')
208 import time,sys
245 import time,sys
209 for i in range(1000):
246 for i in range(1000):
210 for j in range(300):
247 for j in range(1000):
211 if i % 15 == 0 and i < 200:
248 if i % 15 == 0 and i < 200:
212 if str(j) in db:
249 if str(j) in db:
213 del db[str(j)]
250 del db[str(j)]
@@ -217,6 +254,8 b' def stress():'
217 time.sleep(0.02)
254 time.sleep(0.02)
218
255
219 db[str(j)] = db.get(str(j), []) + [(i,j,"proc %d" % os.getpid())]
256 db[str(j)] = db.get(str(j), []) + [(i,j,"proc %d" % os.getpid())]
257 db.hset('hash',j, db.hget('hash',j,15) + 1 )
258
220 print i,
259 print i,
221 sys.stdout.flush()
260 sys.stdout.flush()
222 if i % 10 == 0:
261 if i % 10 == 0:
General Comments 0
You need to be logged in to leave comments. Login now