8 from logrecord
import LogRecord
9 from struct
import pack, unpack
13 TIME_EPSILON = sys.float_info.epsilon
33 def decomposeBinary(bin):
35 t, se = unpack(
'fI', bin)
44 (t,e,s) = PKey.decomposeBinary(bin)
48 def compareBinary(s1,s2):
49 d1 = PKey.decomposeBinary(s1)
50 d2 = PKey.decomposeBinary(s2)
76 return unpack(
'I',bin)[0]
79 return unpack(
'B',bin)[0]
82 return unpack(
'f',bin)[0]
87 def extract_sys(key, data):
88 return PKey.fromBinary(key).system_id
90 def extract_time(key, data):
91 return PKey.fromBinary(key).time
93 def extract_evt(key, data):
94 return PKey.fromBinary(key).event_id
96 def compare_time(l, r):
104 lf = PKey.unpackTime(l)
105 rf = PKey.unpackTime(r)
114 def compare_sys(l, r):
122 lf = PKey.unpackSys(l)
123 rf = PKey.unpackSys(r)
131 def compare_evt(l, r):
139 lf = PKey.unpackEvt(l)
140 rf = PKey.unpackEvt(r)
148 def iter_cursor(c, mode = DB_NEXT):
157 def iter_secondary_cursor(c, mode = DB_NEXT):
183 fileFormatVersion =
"1"
190 CACHESIZE = 1024*1024*64 ;
192 e.set_cachesize(0,CACHESIZE,0)
194 e.open(os.path.dirname(pathName),DB_INIT_CDB | DB_INIT_MPOOL)
195 fn = os.path.basename(pathName);
199 m.open(fn, dbname=
"metadata", flags=DB_RDONLY)
202 p.set_bt_compare(PKey.compareBinary)
203 p.open(fn, dbname=
"primary", flags=DB_RDONLY)
206 si.set_bt_compare(compare_sys)
207 si.set_dup_compare(PKey.compareBinary)
208 si.open(fn, dbname=
"system_idx", flags=DB_RDONLY)
211 ti.set_bt_compare(compare_time)
212 ti.set_dup_compare(PKey.compareBinary)
213 ti.open(fn, dbname=
"time_idx", flags=DB_RDONLY)
216 ei.set_bt_compare(compare_evt)
217 ei.set_dup_compare(PKey.compareBinary)
218 ei.open(fn, dbname=
"event_idx", flags=DB_RDONLY)
220 p.associate(si, extract_sys )
221 p.associate(ti, extract_time )
222 p.associate(ei, extract_evt )
242 return self.metadata.get(name)
244 def validateVersionInfo(self):
247 raise RuntimeError(
"Mismatching file format version: {0}, required {1}".format(v, self.
fileFormatVersion))
256 c = self.primary.cursor()
257 for k,l
in iter_cursor(c):
258 yield IndexedLogDB.decodeKVP((k,l))
267 c = self.time_idx.cursor()
268 k = PKey.packTime(t0)
271 for k, p, l
in iter_secondary_cursor(c, DB_NEXT_NODUP):
272 t = PKey.unpackTime(k)
282 c = self.system_idx.cursor()
286 for k, p, l
in iter_secondary_cursor(c):
287 s = PKey.unpackSys(k)
289 yield IndexedLogDB.decodeKVP((p,l))
297 c = self.time_idx.cursor()
298 k = PKey.packTime(t0)
301 for k, p, l
in iter_secondary_cursor(c):
302 t = PKey.unpackTime(k)
304 yield IndexedLogDB.decodeKVP((p,l))
313 c = self.system_idx.cursor()
314 k = PKey.packSys(sysid)
315 pk =
PKey(t0, 1, sysid).toBinary()
316 _, pk, _ = c.pget(k, pk, DB_GET_BOTH_RANGE)
318 for k, pk, l
in iter_secondary_cursor(c):
319 kk = PKey.fromBinary(pk)
320 if t0 <= kk.time <= t1
and kk.system_id == sysid :
321 yield IndexedLogDB.decodeKVP((pk,l))
337 c = self.system_idx.cursor()
339 if system_range.isUniversal() :
344 s0, s1 = system_range.ulPair()
352 sysid = PKey.unpackSys(ks)
353 yield sysid, LogRecord.from_binary(l)
356 r = c.get(DB_NEXT_NODUP)
370 c = self.system_idx.cursor()
372 if system_range.isUniversal() :
377 s0, s1 = system_range.ulPair()
382 while sysid < s1
and c.next() !=
None :
383 if c.get(DB_NEXT_NODUP) ==
None :
388 sysid = PKey.unpackSys(ks)
389 yield sysid, LogRecord.from_binary(l)
395 return (PKey.fromBinary(r[0]),LogRecord.from_binary(r[1]))
397 def system_range_for_time_event(self,time,event_id,system_range):
398 c = self.primary.cursor()
399 s0, s1 = system_range
401 k =
PKey(time, event_id, s0)
402 c.set_range(k.toBinary())
405 for r
in iter_cursor(c):
406 k,l = IndexedLogDB.decodeKVP(r)
407 if s0 <= k.system_id <= s1
and k.time == time
and k.event_id == event_id:
421 def filterEventID(q,er):
423 if(er.contains(k.event_id)):
426 q0 = d.queryInternal(tr,sr)
428 if(
not er.isUniversal()):
429 q = filterEventID(q0,er)
435 def queryInternal(d, tr, sr):
436 if(tr.isUniversal()):
437 if(sr.isUniversal()):
438 for k,l
in d.all_records():
441 for k,l
in d.system_range_records(sr.ulPair()):
444 if(sr.isUniversal()):
445 for k,l
in d.time_range_records(tr.ulPair()):
448 for t
in d.time_sequence(tr.ulPair()):
449 for k,l
in d.system_range_for_time_event(t,1,sr.ulPair()):