[PATCH 20 of 22] obsstore: make markerindex support serialization

Jun Wu quark at fb.com
Sun Jun 4 19:59:32 EDT 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1496615642 25200
#      Sun Jun 04 15:34:02 2017 -0700
# Node ID edae716e97c441aff52688f3be4750b34b199901
# Parent  160567b62f74e8e02fa2e9a7b2cb4076b105e528
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r edae716e97c4
obsstore: make markerindex support serialization

This allows us to load and save index state to disk.

Note this is the first user of .hg/store/cache. The indexes are purely
depending on .hg/store/obsstore and .hg/store/cache looks reasonable - if
store is shared, the indexes won't need to be duplicated.

diff --git a/mercurial/obsolete.py b/mercurial/obsolete.py
--- a/mercurial/obsolete.py
+++ b/mercurial/obsolete.py
@@ -532,9 +532,11 @@ class markerindex(radixlink.radixlink):
     def __init__(self, obsstore, name, keyfunc):
         """keyfunc: rawmarker -> [key]"""
-        super(markerindex, self).__init__()
+        path = 'cache/obsindex-%s' % name
+        data = obsstore.svfs.tryread(path)
+        super(markerindex, self).__init__(data)
+        self._path = path
         self._keyfunc = keyfunc
         self._obsstore = weakref.proxy(obsstore)
         self.name = name
-        self.sourceoftruthsize = 0
         self._readmarker = formats[obsstore._version][0]
         self.update()
@@ -567,4 +569,18 @@ class markerindex(radixlink.radixlink):
             return default
 
+    def flush(self):
+        """write index state to disk"""
+        svfs = self._obsstore.svfs
+        # fast path: no need to write if file size matches
+        try:
+            if svfs.stat(self._path).st_size == len(self):
+                return
+        except OSError as inst:
+            if inst.errno != errno.ENOENT:
+                raise
+        svfs.makedirs('cache')
+        with svfs(self._path, 'w', atomictemp=True) as f:
+            f.write(self.data)
+
 class markerreader(object):
     """read a range of markers with proper caching"""


More information about the Mercurial-devel mailing list