Selaa lähdekoodia

gzip compress persisted data

Malte Kraus 8 vuotta sitten
vanhempi
sitoutus
b84cfcb6e6
1 muutettua tiedostoa jossa 10 lisäystä ja 7 poistoa
  1. 10 7
      src/persistence.py

+ 10 - 7
src/persistence.py

@@ -4,6 +4,8 @@ import json
 import os
 import os.path
 import tempfile
+import gzip
+from io import TextIOWrapper
 from threading import Condition, Thread
 
 
@@ -30,8 +32,8 @@ class Persistence:
         """ Loads data from disk. """
         self._loading = True
         try:
-            with open(self.path, "r") as f:
-                obj = json.load(f)
+            with gzip.open(self.path, "r") as f:
+                obj = json.load(TextIOWrapper(f))
             for block in obj['blocks']:
                 self.proto.received("block", block, None, 2)
             for trans in obj['transactions']:
@@ -67,13 +69,14 @@ class Persistence:
                 obj = self._store_data
                 self._store_data = None
 
-            with tempfile.NamedTemporaryFile(dir=os.path.dirname(self.path), delete=False, mode="w") as f:
+            with tempfile.NamedTemporaryFile(dir=os.path.dirname(self.path), mode="wb", delete=False) as tmpf:
                 try:
-                    json.dump(obj, f, indent=4)
-                    f.close()
-                    os.rename(f.name, self.path)
+                    with TextIOWrapper(gzip.open(tmpf, mode="w")) as f:
+                        json.dump(obj, f, indent=4)
+                    tmpf.close()
+                    os.rename(tmpf.name, self.path)
                 except Exception as e:
-                    os.unlink(f.name)
+                    os.unlink(tmpf.name)
                     raise e
 
 from .chainbuilder import ChainBuilder