A comprehensive Python client for ShibuDb database
Project description
shibudb-client-python
ShibuDb client library for python
ShibuDb Python Client
A comprehensive Python client for ShibuDb database that supports authentication, key-value operations, vector similarity search, and space management.
Features
- 🔐 Authentication & User Management: Secure login with role-based access control
- 🔑 Key-Value Operations: Traditional key-value storage with PUT, GET, DELETE operations
- 🧮 Vector Similarity Search: Advanced vector operations with multiple index types
- 🗂️ Space Management: Create, delete, and manage different storage spaces
- 🛡️ Error Handling: Comprehensive error handling with custom exceptions
- 📊 Connection Management: Automatic connection handling with context managers
Installation
Prerequisites
-
ShibuDb Server: Ensure the ShibuDb server is running
# Start the server (requires sudo) sudo shibudb start 9090
-
Python Requirements: The client uses only standard library modules
- Python 3.7+
- No external dependencies required
Setup
-
Clone or download the client files:
# Copy the client files to your project cp shibudb_client.py your_project/
-
Import the client:
from shibudb_client import ShibuDbClient, User, connect
Quick Start
Basic Connection and Authentication
from shibudb_client import ShibuDbClient
# Create client and authenticate
client = ShibuDbClient("localhost", 9090)
client.authenticate("admin", "password")
# Use context manager for automatic cleanup
with ShibuDbClient("localhost", 9090) as client:
client.authenticate("admin", "password")
# Your operations here
Key-Value Operations
# Create and use a space
client.create_space("mytable", "key-value")
client.use_space("mytable")
# Basic operations
client.put("name", "John Doe")
response = client.get("name")
print(response["value"]) # "John Doe"
client.delete("name")
Vector Operations
# Create a vector space
client.create_space("vectors", "vector", dimension=128, index_type="Flat", metric="L2")
client.use_space("vectors")
# Insert vectors
client.insert_vector(1, [0.1, 0.2, 0.3, ...])
client.insert_vector(2, [0.4, 0.5, 0.6, ...])
# Search for similar vectors
results = client.search_topk([0.1, 0.2, 0.3, ...], k=5)
print(results["message"]) # Search results
# Range search
results = client.range_search([0.1, 0.2, 0.3, ...], radius=0.5)
API Reference
ShibuDbClient
Constructor
ShibuDbClient(host="localhost", port=9090, timeout=30)
Authentication
client.authenticate(username: str, password: str) -> Dict[str, Any]
Space Management
client.create_space(name: str, engine_type: str, dimension: Optional[int] = None,
index_type: str = "Flat", metric: str = "L2") -> Dict[str, Any]
client.delete_space(name: str) -> Dict[str, Any]
client.list_spaces() -> Dict[str, Any]
client.use_space(name: str) -> Dict[str, Any]
Key-Value Operations
client.put(key: str, value: str, space: Optional[str] = None) -> Dict[str, Any]
client.get(key: str, space: Optional[str] = None) -> Dict[str, Any]
client.delete(key: str, space: Optional[str] = None) -> Dict[str, Any]
Vector Operations
client.insert_vector(vector_id: int, vector: List[float], space: Optional[str] = None) -> Dict[str, Any]
client.search_topk(query_vector: List[float], k: int = 1, space: Optional[str] = None) -> Dict[str, Any]
client.range_search(query_vector: List[float], radius: float, space: Optional[str] = None) -> Dict[str, Any]
client.get_vector(vector_id: int, space: Optional[str] = None) -> Dict[str, Any]
User Management (Admin Only)
client.create_user(user: User) -> Dict[str, Any]
client.update_user_password(username: str, new_password: str) -> Dict[str, Any]
client.update_user_role(username: str, new_role: str) -> Dict[str, Any]
client.update_user_permissions(username: str, permissions: Dict[str, str]) -> Dict[str, Any]
client.delete_user(username: str) -> Dict[str, Any]
client.get_user(username: str) -> Dict[str, Any]
Data Models
User
@dataclass
class User:
username: str
password: str
role: str = "user"
permissions: Dict[str, str] = None
SpaceInfo
@dataclass
class SpaceInfo:
name: str
engine_type: str
dimension: Optional[int] = None
index_type: Optional[str] = None
metric: Optional[str] = None
Exceptions
ShibuDbError: Base exception for all client errorsAuthenticationError: Raised when authentication failsConnectionError: Raised when connection failsQueryError: Raised when query execution fails
Examples
Complete Example
from shibudb_client import ShibuDbClient, User
def main():
# Connect and authenticate
with ShibuDbClient("localhost", 9090) as client:
client.authenticate("admin", "password")
# Create spaces
client.create_space("users", "key-value")
client.create_space("embeddings", "vector", dimension=128)
# Store user data
client.use_space("users")
client.put("user1", "Alice Johnson")
client.put("user2", "Bob Smith")
# Store embeddings
client.use_space("embeddings")
client.insert_vector(1, [0.1, 0.2, 0.3, ...])
client.insert_vector(2, [0.4, 0.5, 0.6, ...])
# Search for similar embeddings
results = client.search_topk([0.1, 0.2, 0.3, ...], k=5)
print(f"Search results: {results}")
if __name__ == "__main__":
main()
Error Handling
from shibudb_client import ShibuDbClient, AuthenticationError, ConnectionError, QueryError
try:
client = ShibuDbClient("localhost", 9090)
client.authenticate("admin", "password")
# Your operations here
except AuthenticationError as e:
print(f"Authentication failed: {e}")
except ConnectionError as e:
print(f"Connection failed: {e}")
except QueryError as e:
print(f"Query failed: {e}")
finally:
client.close()
Advanced Usage
from shibudb_client import ShibuDbClient, User
# Create admin user
admin_user = User(
username="admin",
password="adminpass",
role="admin"
)
# Create regular user with permissions
user = User(
username="user1",
password="userpass",
role="user",
permissions={"mytable": "read", "vectortable": "write"}
)
with ShibuDbClient("localhost", 9090) as client:
client.authenticate("admin", "password")
# Create users
client.create_user(user)
# Create spaces for different purposes
client.create_space("users", "key-value")
client.create_space("products", "key-value")
client.create_space("embeddings", "vector", dimension=256)
client.create_space("recommendations", "vector", dimension=512)
# Store data in different spaces
client.use_space("users")
client.put("user1", "Alice Johnson")
client.use_space("embeddings")
client.insert_vector(1, [0.1, 0.2, 0.3, ...])
# Search for recommendations
query_vector = [0.1, 0.2, 0.3, ...]
results = client.search_topk(query_vector, k=10)
Running Examples
Simple Test
python simple_test.py
Comprehensive Examples
python example.py
Engine Types
Key-Value Engine
- Traditional key-value storage
- Supports PUT, GET, DELETE operations
- No dimension required
Vector Engine
- Vector similarity search
- Multiple index types:
- Flat: Exact search (default)
- HNSW: Hierarchical Navigable Small World
- IVF: Inverted File Index
- IVF with PQ: Product Quantization
- Distance metrics:
- L2: Euclidean distance (default)
- IP: Inner product
- COS: Cosine similarity
Security
- Authentication Required: All operations require valid credentials
- Role-Based Access: Admin and user roles with different permissions
- Space-Level Permissions: Read/write permissions per space
- Connection Security: TCP-based communication with timeout handling
Troubleshooting
Common Issues
-
Connection Failed
- Ensure ShibuDb server is running:
sudo shibudb start 9090 - Check server port and host settings
- Verify firewall settings
- Ensure ShibuDb server is running:
-
Authentication Failed
- Verify username and password
- Ensure user exists in the system
- Check user permissions
-
Space Not Found
- Use
list_spaces()to see available spaces - Create space before using:
create_space() - Use
use_space()to switch to a space
- Use
-
Vector Dimension Mismatch
- Ensure vector dimension matches space dimension
- Check space creation parameters
- Verify vector format (comma-separated floats)
Debug Mode
Enable debug logging:
import logging
logging.basicConfig(level=logging.DEBUG)
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Submit a pull request
License
This client is provided as-is for use with ShibuDb database.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file shibudb_client-1.0.0.tar.gz.
File metadata
- Download URL: shibudb_client-1.0.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
000c2858ff5d0eb5ef9d906577cbae465fb0911705ce36e0fc8035d3260c2fe2
|
|
| MD5 |
4c3e9f9f131a91714ddb8998518d1377
|
|
| BLAKE2b-256 |
f6b6f2fcdc729f16c002b8ca8d9cfe032cb8642e96af42050d49636fa0614738
|
File details
Details for the file shibudb_client-1.0.0-py3-none-any.whl.
File metadata
- Download URL: shibudb_client-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be479aa259236cfb8dabdb2f69525095a1669f2ac06ad676a6f03d5798f3d486
|
|
| MD5 |
0b6cec4ad0ec27d1ce8fcb141581af30
|
|
| BLAKE2b-256 |
1e522b9d070abcc3803688fdd739cc04de7d43099f8284e0193d08cd7552a9ff
|