Skip to main content

No-SQL Documented Collection Database

Project description

AucoDB

AucoDB is a modern, lightweight NoSQL database designed for flexibility, fault tolerance, and seamless integration with agent-based systems. It provides a MongoDB-like document and collection structure, supports JSON-based data storage, and offers both HTTP-based and file-based CRUD operations. With thread-safe I/O and fault-tolerant design, AucoDB ensures data safety and reliability, making it an excellent choice for agent memory and other dynamic applications.

Features

  • MongoDB-like Document Storage: Organize data in collections and documents, similar to MongoDB.
  • Flexible JSON Support: Store any valid JSON class effortlessly.
  • HTTP-based CRUD Operations: Use AucoClient for remote CRUD operations via HTTP.
  • File-based CRUD Operations: Use AucoDB for direct file I/O-based data manipulation.
  • Thread-safe I/O: Ensure data integrity with thread-safe file operations.
  • Fault Tolerance: Robust design to handle failures gracefully.
  • Agent Memory Compatibility: Optimized for use in agent-based systems requiring persistent memory.

Installation

To get started with AucoDB, clone the repository and install the required dependencies.

Steps

There are two ways to install AucoDB:

  1. Clone the repository:
    git clone https://github.com/datascienceworld-kan/aucodb.git
    cd aucodb
    pip install -r requirements.txt
    poetry install
    
  2. Install by pip:
    pip install aucodb==0.1.4
    

Running the AucoDB Server

AucoDB provides a built-in server for HTTP-based operations. The server can be started with a simple Python script.

Example

from aucodb.server import auco_server

# Run the server on localhost:8000, using a JSON file for storage
auco_server.run(host="127.0.0.1", port=8000, data_path="cache/aucodb.json", data_name="aucodb")

This starts the AucoDB server, listening on http://127.0.0.1:8000 and storing data in cache/aucodb.json.

Using the AucoClient (HTTP-based Operations)

The AucoClient allows you to perform CRUD operations on AucoDB collections via HTTP. Below is an example demonstrating how to connect to the server, create collections, and manage records.

Example

from aucodb.client import AucoClient

# Initialize and connect client
client = AucoClient(base_url='http://localhost:8000')
client.connect()

# Create a collection
message = client.create_collection(collection_name="users")
print(message)

# Add records
user1 = {"name": "Alice", "age": 30, "email": "alice@example.com"}
user2 = {"name": "Bob", "age": 25, "email": "bob@example.com"}
user3 = {"name": "Charlie", "age": 35, "email": "Charlie@example.com"}

user1 = client.add(collection_name="users", fields=user1)
user2 = client.add(collection_name="users", fields=user2)
user3 = client.add(collection_name="users", fields=user3)

# Get a record by ID
record_id = user1["record_id"]
record = client.get(collection_name="users", record_id=record_id)
print(record)

# Find records with condition (age >= 30)
records = client.find(collection_name="users", query="age>=30")
print(records)

# Sort records by age (descending)
sorted_records = client.sort(collection_name="users", field="age", reverse=True)
print(sorted_records)

# Update a record
client.update(collection_name="users", record_id=record_id, fields={"age": 31})
record = client.get(collection_name="users", record_id=record_id)
print(record)

# Delete a record
message = client.delete(collection_name="users", record_id=record_id)
print(message)

# Get all records
records = client.get(collection_name="users")
print(records)

# Close the client
client.close()

Explanation

  • Initialization: The AucoClient connects to the AucoDB server at the specified base_url.
  • Collection Management: Create and manage collections using methods like create_collection.
  • CRUD Operations: Perform create (add), read (get, find), update (update), and delete (delete) operations.
  • Querying: Use conditions like age>=30 for filtering and sorting records.
  • Connection Management: Always close the client when done to free resources.

Using AucoDB (File-based Operations)

For direct file-based operations, the AucoDB class allows you to manipulate collections and records stored in a JSON file. This is ideal for local applications or when HTTP is not required.

Example

from aucodb.database import AucoDB, Collection, Record
from datetime import datetime
import logging

# Initialize AucoDB
db = AucoDB(data_name="aucodb", data_path="cache/aucodb.json")

# Create a new collection
users_collection = Collection(name="users")
db.add_collection(collection=users_collection)
logging.info("Created 'users' collection")

# Add records
user1 = {"id": 1, "name": "Alice", "age": 30, "email": "alice@example.com"}
user2 = {"id": 2, "name": "Bob", "age": 25, "email": "bob@example.com"}
user3 = {"id": 3, "name": "Charlie", "age": 35, "email": "Charlie@example.com"}

db.collections["users"].add(record=user1)
db.collections["users"].add(record=user2)
db.collections["users"].add(record=user3)
db.save() # or db.save_async() for faster

# Print all records
print("All users:")
for record in db.collections["users"].records:
    print(record)

# Find records where age >= 30
print("Users with age >= 30:")
found_records = db.collections["users"].find(query="age>=30")
for record in found_records:
    print(record)

# Update a record
db.collections["users"].update(record_id=user1.get("id"), updated_dict={"age": 31, "email": "alice.updated@example.com"})
print("After updating Alice's record:")
updated_record = db.collections["users"].get(record_id=user1.get("id"))
print(updated_record)

# Sort records by age (descending)
print("Users sorted by age (descending):")
sorted_records = db.collections["users"].sort(field="age", reverse=True)
for record in sorted_records:
    print(record)

# Delete a record
db.collections["users"].delete(record_id=user2.get("id"))
print("After deleting Bob's record:")
for record in db.collections["users"].records:
    print(record)

# Demonstrate loading from JSON file
new_db = AucoDB(data_path="cache/aucodb.json")
print("\nLoaded database from JSON:")
for record in new_db.collections["users"].records:
    print(record)

Explanation

  • Initialization: The AucoDB class is initialized with a database name and file path for JSON storage.
  • Collection Management: Create collections using the Collection class and add them to the database.
  • CRUD Operations: Add, retrieve, update, and delete records directly in the JSON file.
  • Persistence: Use db.save() to persist changes to the JSON file.
  • Querying: Filter and sort records using methods like find and sort.
  • File Loading: Load an existing database from a JSON file for continued operations.

Fault Tolerance and Thread Safety

AucoDB is designed with fault tolerance and thread-safe I/O operations to ensure data integrity. The database handles failures gracefully and prevents data corruption during concurrent operations, making it reliable for multi-threaded applications.

Use Cases

  • Agent Memory: Store and retrieve agent state and memory efficiently.
  • Prototyping: Rapidly develop applications with flexible JSON-based storage.
  • Local Applications: Use file-based operations for standalone applications.
  • Distributed Systems: Leverage HTTP-based operations for remote data access.

Contributing

Contributions to AucoDB are welcome! Please submit issues and pull requests via the GitHub repository. Ensure your code follows the project's coding standards and includes appropriate tests.

License

AucoDB is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aucodb-0.1.5.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aucodb-0.1.5-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file aucodb-0.1.5.tar.gz.

File metadata

  • Download URL: aucodb-0.1.5.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.3 Darwin/21.6.0

File hashes

Hashes for aucodb-0.1.5.tar.gz
Algorithm Hash digest
SHA256 f8160af54cecc7b0ddb433f9fe087c3981aeec6428a3d93a4349a5d3bca9102a
MD5 6d0d96997b18780648fc626c5963ee09
BLAKE2b-256 b2632c5a30a7edaf6f4e84c93d2cbf53a0a1c60e5d328306ec796ed9226cfc08

See more details on using hashes here.

File details

Details for the file aucodb-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: aucodb-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.3 Darwin/21.6.0

File hashes

Hashes for aucodb-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8cac4dbfcb023f959343bf89ddf1b240e1da22eb9109d1febfebc4d2e0861f7d
MD5 0892d84b4d7c04d6b9b9e25a4be99630
BLAKE2b-256 13806d06e665a2c7c8e77e93409068b3513488311cd5d841961dd8c9a48448ee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page