From 3a98211431070f92e83a757751878edf703845d5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Wed, 30 Dec 2020 01:22:48 +0100
Subject: [PATCH] flush() before reenabling mmap

---
 README.md                 | 1 +
 setup.py                  | 2 +-
 tempsdb/chunks/base.pyx   | 1 +
 tempsdb/chunks/direct.pxd | 1 +
 tempsdb/chunks/direct.pyx | 6 ++++++
 5 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 14b92b3..e2ffc1e 100644
--- a/README.md
+++ b/README.md
@@ -56,6 +56,7 @@ Then copy your resulting wheel and install it via pip on the target system.
 
 * older TempsDB databases that do not support varlens will be updated upon opening
 * added metadata support for databases
+* a flush will be done before re-enabling mmap
 
 ## v0.5.3
 
diff --git a/setup.py b/setup.py
index df8396f..dc63bba 100644
--- a/setup.py
+++ b/setup.py
@@ -42,7 +42,7 @@ ext_modules = build([Multibuild('tempsdb', find_pyx('tempsdb'),
                      **cythonize_kwargs)
 
 setup(name='tempsdb',
-      version='0.5.4a3',
+      version='0.5.4a4',
       packages=find_packages(include=['tempsdb', 'tempsdb.*']),
       install_requires=['satella>=2.14.24', 'ujson'],
       ext_modules=ext_modules,
diff --git a/tempsdb/chunks/base.pyx b/tempsdb/chunks/base.pyx
index c856b1d..222f7b0 100644
--- a/tempsdb/chunks/base.pyx
+++ b/tempsdb/chunks/base.pyx
@@ -124,6 +124,7 @@ cdef class Chunk:
         :raises Corruption: unable to mmap file due to an unrecoverable error
         """
         if isinstance(self.mmap, AlternativeMMap):
+            self.mmap.flush()
             try:
                 self.mmap = mmap.mmap(self.file.fileno(), 0)
                 self.file_lock_object = None
diff --git a/tempsdb/chunks/direct.pxd b/tempsdb/chunks/direct.pxd
index ddee23a..38214db 100644
--- a/tempsdb/chunks/direct.pxd
+++ b/tempsdb/chunks/direct.pxd
@@ -8,3 +8,4 @@ cdef class DirectChunk(Chunk):
     cpdef object open_file(self, str path)
     cpdef int after_init(self) except -1
     cpdef int append(self, unsigned long long timestamp, bytes data) except -1
+    cpdef int switch_to_mmap_based_access(self) except -1
diff --git a/tempsdb/chunks/direct.pyx b/tempsdb/chunks/direct.pyx
index 4f23a97..af811cf 100644
--- a/tempsdb/chunks/direct.pyx
+++ b/tempsdb/chunks/direct.pyx
@@ -80,6 +80,12 @@ cdef class DirectChunk(Chunk):
         self.max_ts, = STRUCT_Q.unpack(b)
         return 0
 
+    cpdef int switch_to_mmap_based_access(self) except -1:
+        if self.gzip:
+            raise RuntimeError('Cannot switch to mmap because its gzipped!')
+        super().switch_to_mmap_based_access()
+        return 0
+
     cpdef int append(self, unsigned long long timestamp, bytes data) except -1:
         cdef bytes b
         if self.file_lock_object:
-- 
GitLab