sliding_sqlite/test_slidingsqlite_simple.py
2025-03-16 10:30:47 +02:00

102 lines
3.9 KiB
Python

import time
import uuid
import hashlib
from datetime import datetime, timezone
from SlidingSqlite import SlidingSQLite
import logging
# Set up logging
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[logging.StreamHandler()],
)
# Initialize SlidingSQLite with 1-minute rotation and 5-minute retention
db = SlidingSQLite(
db_dir="./databases",
schema="CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY, timestamp TEXT, hash TEXT)",
rotation_interval=60, # Rotate every 1 minute (60 seconds)
retention_period=300, # Keep data for 5 minutes (300 seconds)
cleanup_interval=30, # Run cleanup every 30 seconds
auto_delete_old_dbs=True, # Enable automatic deletion of old databases
)
# Function to generate a random MD5 hash
def generate_md5():
return hashlib.md5(str(uuid.uuid4()).encode()).hexdigest()
try:
print("Starting test. Press Ctrl+C to stop.")
start_time = time.time()
while True:
# Generate timestamp and random MD5 hash
timestamp = datetime.now(timezone.utc).isoformat()
md5_hash = generate_md5()
# Insert data into the database
query_id = db.execute_write(
"INSERT INTO data (timestamp, hash) VALUES (?, ?)", (timestamp, md5_hash)
)
result = db.get_result(query_id)
if result.success:
print(f"Inserted: {timestamp} | {md5_hash} | Success: True")
else:
print(f"Failed to insert: {timestamp} | {md5_hash} | Error: {result.error}")
# Every 10 seconds, query the database
if int(time.time() - start_time) % 10 == 0:
try:
# Ensure at least one record exists before querying
if time.time() - start_time > 2: # Wait at least 2 seconds after start
read_id = db.execute_read("SELECT * FROM data", ())
# Add a longer delay to allow the worker thread to process
time.sleep(0.5)
read_result = db.get_read_result(
read_id, timeout=10.0
) # Increased timeout
if read_result.success:
print(
f"\nStored Entries ({len(read_result.data)}):",
read_result.data[:5],
)
if len(read_result.data) > 5:
print(f"...and {len(read_result.data) - 5} more entries\n")
else:
print(f"\nError retrieving entries: {read_result.error}\n")
except Exception as e:
print(f"\nException during database query: {e}\n")
# Test manual database management
if int(time.time() - start_time) % 60 == 0: # Every minute
try:
# Get information about all databases
print("\nDatabase Information:")
for db_info in db.get_databases_info():
start_time_str = datetime.fromtimestamp(db_info.start_time).strftime('%Y-%m-%d %H:%M:%S')
end_time_str = datetime.fromtimestamp(db_info.end_time).strftime('%Y-%m-%d %H:%M:%S')
print(f" - {db_info.db_file}: {start_time_str} to {end_time_str}")
# If auto_delete_old_dbs is False, demonstrate manual deletion
if not db.auto_delete_old_dbs:
# Delete databases older than 3 minutes
cutoff_time = time.time() - 180 # 3 minutes ago
deleted_count = db.delete_databases_before(cutoff_time)
print(f"\nManually deleted {deleted_count} databases older than 3 minutes\n")
except Exception as e:
print(f"\nException during database management: {e}\n")
# Wait a bit before continuing with inserts
time.sleep(0.5)
except KeyboardInterrupt:
print("\nShutting down...")
db.shutdown()