diff --git a/docs/index.rst b/docs/index.rst index 38dc9a6a2041dfe76ee744a9a61f46edffbc1ac6..0f61f694894872a5d950f48289261a7e5052b824 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 8ac5564a77219592adf5ff0aa280e77411c98df1..0000000000000000000000000000000000000000 --- 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 9836297df149c6d611a7c6227422c87358ad22c6..3daca865753310716f502c4dadbb2d75d9506bd9 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 0de222550691bd9ce779f98ef5288081d701cbe5..3050a8b76d6937f282d4489edd1cf5de87295e86 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 676689e89946158c4571b7b48179337cd7ada736..24705dc211e23bd6fb9d1ff4ab24a1e74924050e 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)