summaryrefslogtreecommitdiff
path: root/rdiff-backup/rdiff_backup/rorpiter.py
diff options
context:
space:
mode:
authorbescoto <bescoto@2b77aa54-bcbc-44c9-a7ec-4f6cf2b41109>2003-02-12 07:28:33 +0000
committerbescoto <bescoto@2b77aa54-bcbc-44c9-a7ec-4f6cf2b41109>2003-02-12 07:28:33 +0000
commitadc9d7a9a9cd90c8d078b1276cfdad98d0303d07 (patch)
treeafa49da6e1ebc85a0d16aad52591e55bda0c70f2 /rdiff-backup/rdiff_backup/rorpiter.py
parent9613406fbab1949f66fe7858590cab990c7b4b25 (diff)
downloadrdiff-backup-adc9d7a9a9cd90c8d078b1276cfdad98d0303d07.tar.gz
Fixed selection bug, renamed metadata files to ".snapshot"
git-svn-id: http://svn.savannah.nongnu.org/svn/rdiff-backup/trunk@278 2b77aa54-bcbc-44c9-a7ec-4f6cf2b41109
Diffstat (limited to 'rdiff-backup/rdiff_backup/rorpiter.py')
-rw-r--r--rdiff-backup/rdiff_backup/rorpiter.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/rdiff-backup/rdiff_backup/rorpiter.py b/rdiff-backup/rdiff_backup/rorpiter.py
index 3027fd1..f75d8e8 100644
--- a/rdiff-backup/rdiff_backup/rorpiter.py
+++ b/rdiff-backup/rdiff_backup/rorpiter.py
@@ -403,3 +403,42 @@ class ITRBranch:
(index and os.path.join(*index) or '()',), 2)
+class CacheIndexable:
+ """Cache last few indexed elements in iterator
+
+ This class should be initialized with an iterator yielding
+ .index'd objects. It looks like it is just the same iterator as
+ the one that initialized it. Luckily, it does more, caching the
+ last few elements iterated, which can be retrieved using the
+ .get() method.
+
+ If the index is not in the cache, return None.
+
+ """
+ def __init__(self, indexed_iter, cache_size = None):
+ """Make new CacheIndexable. Cache_size is max cache length"""
+ self.cache_size = cache_size
+ self.iter = indexed_iter
+ self.cache_dict = {}
+ self.cache_indicies = []
+
+ def next(self):
+ """Return next elem, add to cache. StopIteration passed upwards"""
+ next_elem = self.iter.next()
+ next_index = next_elem.index
+ self.cache_dict[next_index] = next_elem
+ self.cache_indicies.append(next_index)
+
+ if len(self.cache_indicies) > self.cache_size:
+ del self.cache_dict[self.cache_indicies[0]]
+ del self.cache_indicies[0]
+
+ return next_elem
+
+ def __iter__(self): return self
+
+ def get(self, index):
+ """Return element with index index from cache"""
+ try: return self.cache_dict[index]
+ except KeyError: return None
+