From 0b0517bda148c34f9ccb1a4679d481b26483b4da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Thu, 10 Dec 2020 17:10:25 +0100
Subject: [PATCH] 0.4.4

---
 README.md          |  1 +
 setup.py           |  4 ++--
 tempsdb/chunks.pxd |  1 +
 tempsdb/chunks.pyx | 22 ++++++++++++++++++++++
 tempsdb/series.pxd |  1 +
 5 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index dd6ceaa..3d4fea6 100644
--- a/README.md
+++ b/README.md
@@ -31,6 +31,7 @@ You will need to have both snakehouse and satella installed.
 * more error conditions during mmap will be supported as well
 * ENOMEM will be correctly handled during resize operation
 * added `TimeSeries.descriptor_based_access`
+* added `Chunk.switch_to_mmap_based_access`
 
 ## v0.4.3
 
diff --git a/setup.py b/setup.py
index f1b34b3..3eedbb3 100644
--- a/setup.py
+++ b/setup.py
@@ -28,9 +28,9 @@ if 'CI' in os.environ:
 
 
 setup(name='tempsdb',
-      version='0.4.4a4',
+      version='0.4.4',
       packages=['tempsdb'],
-      install_requires=['satella>=2.14.23', 'ujson'],
+      install_requires=['satella>=2.14.24', 'ujson'],
       ext_modules=build([Multibuild('tempsdb', find_pyx('tempsdb')), ],
                         compiler_directives=directives),
       # ext_modules=cythonize(extensions,
diff --git a/tempsdb/chunks.pxd b/tempsdb/chunks.pxd
index 10d2ba8..6db8a10 100644
--- a/tempsdb/chunks.pxd
+++ b/tempsdb/chunks.pxd
@@ -32,6 +32,7 @@ cdef class Chunk:
     cdef int extend(self) except -1
     cpdef int delete(self) except -1
     cpdef int switch_to_descriptor_based_access(self) except -1
+    cpdef int switch_to_mmap_based_access(self) except -1
     cpdef unsigned long get_mmap_size(self)
 
     cdef inline unsigned long long name(self):
diff --git a/tempsdb/chunks.pyx b/tempsdb/chunks.pyx
index d03d898..360da4f 100644
--- a/tempsdb/chunks.pyx
+++ b/tempsdb/chunks.pyx
@@ -86,6 +86,28 @@ cdef class Chunk:
         else:
             return self.file_size
 
+    cpdef int switch_to_mmap_based_access(self) except -1:
+        """
+        Switch self to mmap-based access instead of descriptor-based.
+        
+        No-op if already in mmap mode.
+        """
+        if not isinstance(self.mmap, AlternativeMMap):
+            return 0
+        try:
+            self.mmap = mmap.mmap(self.file.fileno(), 0)
+            self.file_lock_object = None
+        except OSError as e:
+            if e.errno in (11,      # EAGAIN - memory is too low
+                           12,      # ENOMEM - no memory space available
+                           19,      # ENODEV - fs does not support mmapping
+                           75):     # EOVERFLOW - too many pages would have been used
+                pass
+            else:
+                self.file.close()
+                self.closed = True
+                raise Corruption(f'Failed to mmap chunk file: {e}')
+
     cpdef int switch_to_descriptor_based_access(self) except -1:
         """
         Switch self to descriptor-based access instead of mmap.
diff --git a/tempsdb/series.pxd b/tempsdb/series.pxd
index d6f32b3..15335fd 100644
--- a/tempsdb/series.pxd
+++ b/tempsdb/series.pxd
@@ -39,6 +39,7 @@ cdef class TimeSeries:
     cpdef unsigned long open_chunks_mmap_size(self)
     cpdef tuple get_current_value(self)
     cpdef int disable_mmap(self) except -1
+    cpdef int enable_mmap(self) except -1
     cdef inline int get_references_for(self, unsigned long long timestamp):
         return self.refs_chunks.get(timestamp, 0)
 
-- 
GitLab