From 79971b9d74e7c3ff4383bf34ba64d7ad9fd38da7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Mon, 30 Nov 2020 20:06:06 +0100
Subject: [PATCH] added trimming option

---
 tempsdb/chunks.pxd |  2 +-
 tempsdb/chunks.pyx |  2 +-
 tempsdb/series.pxd |  1 +
 tempsdb/series.pyx | 12 ++++++++++++
 tests/test_db.py   |  6 +++---
 5 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/tempsdb/chunks.pxd b/tempsdb/chunks.pxd
index 4a05a41..34dece1 100644
--- a/tempsdb/chunks.pxd
+++ b/tempsdb/chunks.pxd
@@ -25,7 +25,7 @@ cdef class Chunk:
 
     cpdef object iterate_indices(self, unsigned long starting_entry, unsigned long stopping_entry)
     cpdef void close(self)
-    cpdef tuple get_piece_at(self, unsigned int index)
+    cdef tuple get_piece_at(self, unsigned int index)
     cpdef int append(self, unsigned long long timestamp, bytes data) except -1
     cpdef int sync(self) except -1
     cpdef unsigned int find_left(self, unsigned long long timestamp)
diff --git a/tempsdb/chunks.pyx b/tempsdb/chunks.pyx
index dbea93a..13b5541 100644
--- a/tempsdb/chunks.pyx
+++ b/tempsdb/chunks.pyx
@@ -225,7 +225,7 @@ cdef class Chunk:
     def __del__(self):
         self.close()
 
-    cpdef tuple get_piece_at(self, unsigned int index):
+    cdef tuple get_piece_at(self, unsigned int index):
         """
         Return a piece of data at a particular index, numbered from 0
         
diff --git a/tempsdb/series.pxd b/tempsdb/series.pxd
index 153a3f4..0725547 100644
--- a/tempsdb/series.pxd
+++ b/tempsdb/series.pxd
@@ -32,6 +32,7 @@ cdef class TimeSeries:
     cpdef int close_chunks(self) except -1
     cpdef Iterator iterate_range(self, unsigned long long start, unsigned long long stop)
     cpdef unsigned int get_index_of_chunk_for(self, unsigned long long timestamp)
+    cpdef int trim(self, unsigned long long timestamp) except -1
 
 cpdef TimeSeries create_series(str path, unsigned int block_size,
                                int max_entries_per_chunk, int page_size=*)
diff --git a/tempsdb/series.pyx b/tempsdb/series.pyx
index d1dbf1a..5a19b4f 100644
--- a/tempsdb/series.pyx
+++ b/tempsdb/series.pyx
@@ -111,6 +111,18 @@ cdef class TimeSeries:
             self.refs_chunks[name] += 1
         return self.open_chunks[name]
 
+    cpdef int trim(self, unsigned long long timestamp) except -1:
+        """
+        Delete all entries earlier than timestamp.
+        
+        Note that this will drop entire chunks, so it may be possible that some entries will linger
+        on. This will not delete opened chunks, but it will delete them on release.
+        
+        :param timestamp: timestamp to delete entries earlier than
+        :type timestamp: int
+        """
+        # todo: write it
+
     cpdef void close(self):
         """
         Close the series.
diff --git a/tests/test_db.py b/tests/test_db.py
index d070e0e..105fb27 100644
--- a/tests/test_db.py
+++ b/tests/test_db.py
@@ -35,9 +35,9 @@ class TestDB(unittest.TestCase):
         self.assertEqual(chunk.min_ts, 0)
         self.assertEqual(chunk.max_ts, 4)
         self.assertEqual(chunk.block_size, 4)
-        self.assertEqual(chunk.get_piece_at(0), (0, b'ala '))
-        self.assertEqual(chunk.get_piece_at(1), (1, b'ma  '))
-        self.assertEqual(chunk.get_piece_at(2), (4, b'kota'))
+        self.assertEqual(chunk[0], (0, b'ala '))
+        self.assertEqual(chunk[1], (1, b'ma  '))
+        self.assertEqual(chunk[2], (4, b'kota'))
         self.assertEqual(len(chunk), 3)
         self.assertEqual(list(iter(chunk)), data)
         chunk.append(5, b'test')
-- 
GitLab