Commits

painyeph committed 5b23aef

version 0.22 增加 LazyDeltaListEntry

  • Participants
  • Parent commits 00d89cb

Comments (0)

Files changed (1)

File immutabase.py

 # 120328: version 0.19 修正 DiskHashIndex 在 entry.serialtype 为 int 时出错的 bug
 # 120402: version 0.20 实现 SingleBlobStore
 # 120423: version 0.21 增加 DeltaListEntry
+# 120423: version 0.22 增加 LazyDeltaListEntry
 
 
 """Immutabase - lightweight read-only key-value database.
         6. ListEntry : for lists of entry
            LazyListEntry : similar to ListEntry, but faster for large lists
         7. DeltaListEntry : for list of increasing nonnegative integers
+           LazyDeltaListEntry : similar to DeltaListEntry, but with lazy evaluation
         8. ModelEntry : for GAE-like data modeling
 
     Stores:
     [1, 3, 5, 10, 1000, 10000, 10001]
     """
 
+    def load(self, pos=None):
+        num, = struct.unpack('I', self.store.read(pos, False, 4))
+        value = []
+        d = p = 0
+        for c in self.store.read(None, False, num):
+            c = ord(c)
+            if c & 0x80:
+                p += d | (c & 0x7F)
+                value.append(p)
+                d = 0
+            else:
+                d = (d | c) << 7
+        return value
+
+    def serialize(self, value):
+        data = []
+        p = 0
+        for i in value:
+            if not i >= p: raise ValueError('must be list of increasing nonnegative integers')
+            d = i - p ; p = i
+            part = [ chr(0x80 | (d & 0x7F)) ]
+            while d > 0x7F:
+                d >>= 7
+                part.append(chr(d & 0x7F))
+            data.extend(reversed(part))
+        return struct.pack('I', len(data)) + ''.join(data)
+
+
+class LazyDeltaListEntry(EntryBase):
+    """List of increasing nonnegative integers.
+
+    >>> db = Immutabase('delta.db', 'w', entry=LazyDeltaListEntry())
+    >>> db[0] = [1, 3, 5, 10, 1000, 10000, 10001]
+    >>> db.close()
+
+    >>> db = Immutabase('delta.db', entry=LazyDeltaListEntry())
+    >>> list(db[0])
+    [1, 3, 5, 10, 1000, 10000, 10001]
+    """
+
     def __generator(self, pos):
         d = p = 0
         for pos in xrange(pos + 4, pos + 4 + struct.unpack('I', self.store.read(pos, False, 4))[0]):