Skip to content

Rough python library start of implementation. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# tidesdb-python

Official Python binding for TidesDB

In active development.. Check back later!


### Usage

```
# Open the database
db = TidesDB.open('/path/to/tidesdb')

# Create a column family
db.create_column_family("cf_name", 100, 3, 0.5, True, 1, True)

# Start a transaction
txn = Transaction.begin(db, "cf_name")

# Put a key-value pair in the transaction
txn.put(b"my_key", b"my_value", 3600)

# Commit the transaction
txn.commit()

# Get the value back from the database
value = db.get("cf_name", b"my_key")
print(value)

# Start a cursor to iterate over key-value pairs
cursor = Cursor.init(db, "cf_name")
cursor.next() # or cursor.prev()
key, value = cursor.get()
print(key, value)

# Clean up resources
cursor.free()
db.close()
```
Empty file added requirements.txt
Empty file.
18 changes: 18 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from setuptools import setup, find_packages

setup(
name="tidesdb",
version="0.1",
packages=find_packages(),
author="Alex Gaetano Padula",
author_email="[email protected]",
description="A Python wrapper-binding for TidesDB",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
classifiers=[
"Topic :: Database",
"Topic :: Database :: Front-End",
"Topic :: Software Development :: Libraries :: Python Modules"
],
python_requires='>=3.6',
)
168 changes: 168 additions & 0 deletions tidesdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# Copyright (C) TidesDB
#
# Original Author: Alex Gaetano Padula
#
# Licensed under the Mozilla Public License, v. 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.mozilla.org/en-US/MPL/2.0/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import ctypes
from ctypes import c_char_p, c_int, c_float, c_bool, c_size_t, c_uint8, c_time_t, POINTER, byref, create_string_buffer

# Load the tidesdb library
lib = ctypes.CDLL('PATH_TO_TIDESDB_LIB')

class TidesDB:
def __init__(self, tdb):
self.tdb = tdb

@staticmethod
def open(directory):
c_dir = create_string_buffer(directory.encode('utf-8'))
tdb = POINTER(ctypes.c_void_p)()
result = lib.tidesdb_open(c_dir, byref(tdb))
if result != 0:
raise Exception("Failed to open TidesDB")
return TidesDB(tdb)

def close(self):
result = lib.tidesdb_close(self.tdb)
if result != 0:
raise Exception("Failed to close TidesDB")

def create_column_family(self, name, flush_threshold, max_level, probability, compressed, compress_algo, bloom_filter):
c_name = create_string_buffer(name.encode('utf-8'))
result = lib.tidesdb_create_column_family(self.tdb, c_name, c_int(flush_threshold), c_int(max_level), c_float(probability), c_bool(compressed), c_int(compress_algo), c_bool(bloom_filter))
if result != 0:
raise Exception("Failed to create column family")

def drop_column_family(self, name):
c_name = create_string_buffer(name.encode('utf-8'))
result = lib.tidesdb_drop_column_family(self.tdb, c_name)
if result != 0:
raise Exception("Failed to drop column family")

def compact_sstables(self, column_family_name, max_threads):
c_name = create_string_buffer(column_family_name.encode('utf-8'))
result = lib.tidesdb_compact_sstables(self.tdb, c_name, c_int(max_threads))
if result != 0:
raise Exception("Failed to compact SSTables")

def put(self, column_family_name, key, value, ttl):
c_name = create_string_buffer(column_family_name.encode('utf-8'))
c_key = (c_uint8 * len(key)).from_buffer_copy(key)
c_value = (c_uint8 * len(value)).from_buffer_copy(value)
result = lib.tidesdb_put(self.tdb, c_name, c_key, c_size_t(len(key)), c_value, c_size_t(len(value)), c_time_t(ttl))
if result != 0:
raise Exception("Failed to put key-value pair")

def get(self, column_family_name, key):
c_name = create_string_buffer(column_family_name.encode('utf-8'))
c_key = (c_uint8 * len(key)).from_buffer_copy(key)
c_value = POINTER(c_uint8)()
c_value_size = c_size_t()
result = lib.tidesdb_get(self.tdb, c_name, c_key, c_size_t(len(key)), byref(c_value), byref(c_value_size))
if result != 0:
raise Exception("Failed to get value")
return bytes((c_uint8 * c_value_size.value).from_address(ctypes.addressof(c_value.contents)))

def delete(self, column_family_name, key):
c_name = create_string_buffer(column_family_name.encode('utf-8'))
c_key = (c_uint8 * len(key)).from_buffer_copy(key)
result = lib.tidesdb_delete(self.tdb, c_name, c_key, c_size_t(len(key)))
if result != 0:
raise Exception("Failed to delete key-value pair")

def list_column_families(self):
cf_list = lib.tidesdb_list_column_families(self.tdb)
if not cf_list:
raise Exception("Failed to list column families")
return ctypes.string_at(cf_list).decode('utf-8')

class Cursor:
def __init__(self, cursor):
self.cursor = cursor

@staticmethod
def init(db, column_family):
c_name = create_string_buffer(column_family.encode('utf-8'))
cursor = POINTER(ctypes.c_void_p)()
result = lib.tidesdb_cursor_init(db.tdb, c_name, byref(cursor))
if result != 0:
raise Exception("Failed to initialize cursor")
return Cursor(cursor)

def next(self):
result = lib.tidesdb_cursor_next(self.cursor)
if result != 0:
raise Exception("Failed to move cursor to next")

def prev(self):
result = lib.tidesdb_cursor_prev(self.cursor)
if result != 0:
raise Exception("Failed to move cursor to previous")

def get(self):
c_key = POINTER(c_uint8)()
c_key_size = c_size_t()
c_value = POINTER(c_uint8)()
c_value_size = c_size_t()
result = lib.tidesdb_cursor_get(self.cursor, byref(c_key), byref(c_key_size), byref(c_value), byref(c_value_size))
if result != 0:
raise Exception("Failed to get key-value pair from cursor")
key = bytes((c_uint8 * c_key_size.value).from_address(ctypes.addressof(c_key.contents)))
value = bytes((c_uint8 * c_value_size.value).from_address(ctypes.addressof(c_value.contents)))
return key, value

def free(self):
result = lib.tidesdb_cursor_free(self.cursor)
if result != 0:
raise Exception("Failed to free cursor")

class Transaction:
def __init__(self, txn):
self.txn = txn

@staticmethod
def begin(db, column_family):
c_name = create_string_buffer(column_family.encode('utf-8'))
txn = POINTER(ctypes.c_void_p)()
result = lib.tidesdb_txn_begin(db.tdb, byref(txn), c_name)
if result != 0:
raise Exception("Failed to begin transaction")
return Transaction(txn)

def put(self, key, value, ttl):
c_key = (c_uint8 * len(key)).from_buffer_copy(key)
c_value = (c_uint8 * len(value)).from_buffer_copy(value)
result = lib.tidesdb_txn_put(self.txn, c_key, c_size_t(len(key)), c_value, c_size_t(len(value)), c_time_t(ttl))
if result != 0:
raise Exception("Failed to put key-value pair in transaction")

def delete(self, key):
c_key = (c_uint8 * len(key)).from_buffer_copy(key)
result = lib.tidesdb_txn_delete(self.txn, c_key, c_size_t(len(key)))
if result != 0:
raise Exception("Failed to delete key-value pair from transaction")

def commit(self):
result = lib.tidesdb_txn_commit(self.txn)
if result != 0:
raise Exception("Failed to commit transaction")

def rollback(self):
result = lib.tidesdb_txn_rollback(self.txn)
if result != 0:
raise Exception("Failed to rollback transaction")

def free(self):
result = lib.tidesdb_txn_free(self.txn)
if result != 0:
raise Exception("Failed to free transaction")