From f580a54664278a56aab1db4d785ec163fb32f9f7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Sat, 28 Nov 2020 20:47:44 +0100
Subject: [PATCH] fix the docs

---
 docs/index.rst       |  1 -
 docs/time-series.rst |  4 ----
 docs/usage.rst       |  4 ++++
 tempsdb/database.pxd |  3 +++
 tempsdb/database.pyx | 43 +++++++++++++++++++++++++++++++++++++++----
 5 files changed, 46 insertions(+), 9 deletions(-)
 delete mode 100644 docs/time-series.rst

diff --git a/docs/index.rst b/docs/index.rst
index 38dc9a6..0f61f69 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -10,7 +10,6 @@ Welcome to tempsdb's documentation!
    :maxdepth: 2
    :caption: Contents:
 
-   time-series
    usage
    exceptions
 
diff --git a/docs/time-series.rst b/docs/time-series.rst
deleted file mode 100644
index 8ac5564..0000000
--- a/docs/time-series.rst
+++ /dev/null
@@ -1,4 +0,0 @@
-Time series
-===========
-
-The name of the series must be a valid name for a directory on your filesystem.
diff --git a/docs/usage.rst b/docs/usage.rst
index 9836297..3daca86 100644
--- a/docs/usage.rst
+++ b/docs/usage.rst
@@ -6,6 +6,10 @@ Start off by instantiating an object
 .. autoclass:: tempsdb.database.Database
     :members:
 
+You can create new databases via
+
+.. autofunction:: tempsdb.database.create_database
+
 Then you can create and retrieve particular series:
 
 .. autoclass:: tempsdb.series.TimeSeries
diff --git a/tempsdb/database.pxd b/tempsdb/database.pxd
index 0de2225..3050a8b 100644
--- a/tempsdb/database.pxd
+++ b/tempsdb/database.pxd
@@ -9,3 +9,6 @@ cdef class Database:
 
     cpdef void close(self)
     cpdef TimeSeries get_series(self, str name)
+
+cpdef Database create_database(str path)
+
diff --git a/tempsdb/database.pyx b/tempsdb/database.pyx
index 676689e..24705dc 100644
--- a/tempsdb/database.pyx
+++ b/tempsdb/database.pyx
@@ -1,13 +1,15 @@
 import os
 import threading
 
-from tempsdb.exceptions import DoesNotExist
+from tempsdb.exceptions import DoesNotExist, AlreadyExists
 from .series cimport TimeSeries
 
 
 cdef class Database:
     """
     A basic TempsDB object.
+
+    :param path: path to the directory with the database
     """
     def __init__(self, path: str):
         self.path = path
@@ -16,16 +18,33 @@ cdef class Database:
         self.lock = threading.Lock()
 
     cpdef TimeSeries get_series(self, name: str):
-        cdef TimeSeries result
+        """
+        Load and return an existing series
+        
+        :param name: name of the series
+        :type name: str
+        :return: a loaded time series
+        :rtype: TimeSeries
+        :raises DoesNotExist: series does not exist
+        """
+        cdef:
+            TimeSeries result
+            str path
         if name in self.open_series:
             result = self.open_series[name]
         else:
+            path = os.path.join(self.path, name)
             with self.lock:
-                if not os.path.isdir(os.path.join(self.path, name)):
+                # Check a second time due to the lock
+                if name in self.open_series:
+                    return self.open_series[name]
+                if not os.path.isdir(path):
                     raise DoesNotExist('series %s does not exist' % (name, ))
-                self.open_series[name] = result = TimeSeries(self, name)
+                self.open_series[name] = result = TimeSeries(path)
         return result
 
+    def __del__(self):
+        self.close()
 
     cpdef void close(self):
         """
@@ -37,3 +56,19 @@ cdef class Database:
         for series in self.open_series.values():
             series.close()
         self.closed = True
+
+
+cpdef Database create_database(str path):
+    """
+    Creates a new, empty database
+    
+    :param path: path where the DB directory will be put
+    :type path: str
+    :return: a Database object
+    :rtype: Database
+    :raises AlreadyExists: the directory exists
+    """
+    if os.path.exists(path):
+        raise AlreadyExists('directory already exists')
+    os.mkdir(path)
+    return Database(path)
-- 
GitLab