Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksey Lim <alsroot@sugarlabs.org>2012-10-07 14:49:40 (GMT)
committer Aleksey Lim <alsroot@sugarlabs.org>2012-10-07 14:49:40 (GMT)
commit334359b1cabcd99afbcc34658c2e3f82523a5d50 (patch)
treec47756698f027d2fb5e182d0ac4dc20d70463a2a
parent9f2405f21601ca59b281f13e829ec74ab468a314 (diff)
Shift index mtime only if populate happened
-rw-r--r--active_document/directory.py1
-rw-r--r--active_document/index.py10
-rwxr-xr-xtests/units/document.py2
-rwxr-xr-xtests/units/index.py30
4 files changed, 35 insertions, 8 deletions
diff --git a/active_document/directory.py b/active_document/directory.py
index f03e401..c0fd242 100644
--- a/active_document/directory.py
+++ b/active_document/directory.py
@@ -245,6 +245,7 @@ class Directory(object):
guid, self.metadata.name)
record.invalidate()
+ self._index.checkpoint()
if found:
self._save_layout()
self.commit()
diff --git a/active_document/index.py b/active_document/index.py
index 3ed5997..d0a0f98 100644
--- a/active_document/index.py
+++ b/active_document/index.py
@@ -45,6 +45,7 @@ class IndexReader(object):
self._props = {}
self._path = root
self._mtime_path = join(self._path, 'mtime')
+ self._dirty = True
self._commit_cb = commit_cb
for name, prop in self.metadata.items():
@@ -376,6 +377,11 @@ class IndexWriter(IndexReader):
# Trigger condition to reset waiting for `index_flush_timeout` timeout
self._commit_cond.set()
+ def checkpoint(self):
+ with file(self._mtime_path, 'w'):
+ pass
+ self._dirty = False
+
def _do_open(self):
try:
self._db = xapian.WritableDatabase(self._path,
@@ -399,8 +405,8 @@ class IndexWriter(IndexReader):
self._db.commit()
else:
self._db.flush()
- with file(self._mtime_path, 'w'):
- pass
+ if not self._dirty:
+ self.checkpoint()
self._pending_updates = 0
_logger.debug('Commit %r changes took %s seconds',
diff --git a/tests/units/document.py b/tests/units/document.py
index 869c03e..879c5ea 100755
--- a/tests/units/document.py
+++ b/tests/units/document.py
@@ -270,8 +270,10 @@ class DocumentTest(tests.Test):
directory = Directory(tests.tmpdir, Document, IndexWriter)
+ self.assertEqual(0, directory._index.mtime)
for i in directory.populate():
pass
+ self.assertNotEqual(0, directory._index.mtime)
doc = directory.get('1')
self.assertEqual(1, doc['ctime'])
diff --git a/tests/units/index.py b/tests/units/index.py
index bfd0b4f..709b167 100755
--- a/tests/units/index.py
+++ b/tests/units/index.py
@@ -371,22 +371,40 @@ class IndexTest(tests.Test):
self.assertEqual(1, len(deleted))
def test_mtime(self):
+ # No index at start; checkpoint didn't happen
db = Index({})
self.assertEqual(0, db.mtime)
+ db.store('1', {}, True)
+ db.commit()
db.close()
+ # Index exists at start; checkpoint didn't happen
db = Index({})
- db.store('1', {}, True)
+ self.assertEqual(0, db.mtime)
+ db.store('2', {}, True)
db.commit()
- self.assertNotEqual(0, db.mtime)
- mtime = db.mtime
+ self.assertEqual(0, db.mtime)
+ db.close()
- time.sleep(1)
+ # Index exists at start; mtime exists at start; checkpoint didn't happen
+ self.touch('index/mtime')
+ os.utime('index/mtime', (1, 1))
+ db = Index({})
+ self.assertEqual(1, db.mtime)
+ db.store('3', {}, True)
+ db.commit()
+ self.assertEqual(1, db.mtime)
+ db.close()
+ # Index exists at start; checkpoint happened
db = Index({})
- db.store('2', {}, True)
+ db.checkpoint()
+ self.assertNotEqual(1, db.mtime)
+ os.utime('index/mtime', (1, 1))
+ self.assertEqual(1, db.mtime)
+ db.store('4', {}, True)
db.commit()
- self.assertNotEqual(mtime, db.mtime)
+ self.assertNotEqual(1, db.mtime)
db.close()
def test_find_OrderByGUIDAllTime(self):