Show More
@@ -40,6 +40,9 b' import UserDict' | |||
|
40 | 40 | import warnings |
|
41 | 41 | import glob |
|
42 | 42 | |
|
43 | def gethashfile(key): | |
|
44 | return ("%02x" % abs(hash(key) % 256))[-2:] | |
|
45 | ||
|
43 | 46 | class PickleShareDB(UserDict.DictMixin): |
|
44 | 47 | """ The main 'connection' object for PickleShare database """ |
|
45 | 48 | def __init__(self,root): |
@@ -82,6 +85,35 b' class PickleShareDB(UserDict.DictMixin):' | |||
|
82 | 85 | if e.errno != 2: |
|
83 | 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 | 117 | def __delitem__(self,key): |
|
86 | 118 | """ del db["key"] """ |
|
87 | 119 | fil = self.root / key |
@@ -192,6 +224,11 b' def test():' | |||
|
192 | 224 | db['hello'] = 15 |
|
193 | 225 | db['aku ankka'] = [1,2,313] |
|
194 | 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 | 232 | print db.keys() |
|
196 | 233 | print db.keys('paths/nest/ok/k*') |
|
197 | 234 | print dict(db) # snapsot of whole db |
@@ -207,7 +244,7 b' def stress():' | |||
|
207 | 244 | db = PickleShareDB('~/fsdbtest') |
|
208 | 245 | import time,sys |
|
209 | 246 | for i in range(1000): |
|
210 |
for j in range( |
|
|
247 | for j in range(1000): | |
|
211 | 248 | if i % 15 == 0 and i < 200: |
|
212 | 249 | if str(j) in db: |
|
213 | 250 | del db[str(j)] |
@@ -217,6 +254,8 b' def stress():' | |||
|
217 | 254 | time.sleep(0.02) |
|
218 | 255 | |
|
219 | 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 | 259 | print i, |
|
221 | 260 | sys.stdout.flush() |
|
222 | 261 | if i % 10 == 0: |
General Comments 0
You need to be logged in to leave comments.
Login now