database

package
v0.0.0-...-8c9a138 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package database provides database connection utilities for SQLite and LevelDB. It handles database creation, connection management, and path building for multi-shard databases.

Package database provides fragmentation management for distributed data across shards. It uses LevelDB to maintain a global schema that maps row IDs to their respective shards.

Index

Constants

View Source
const (
	// GlobalSchemaPath is the default path for the fragmentation schema database
	GlobalSchemaPath = "GlobalSchema"
	// DefaultBaseDir is the default base directory for database files
	DefaultBaseDir = "Database/"
)

Variables

View Source
var (
	ErrInvalidPath     = errors.New("invalid path: at least 2 path components required")
	ErrDatabaseOpen    = errors.New("failed to open database")
	ErrDirectoryCreate = errors.New("failed to create directory")
)

Common errors for database operations

View Source
var (
	ErrShardNotFound      = errors.New("shard not found for the given key")
	ErrFragmentationWrite = errors.New("failed to write fragmentation entry")
	ErrFragmentationRead  = errors.New("failed to read fragmentation entry")
	ErrShardConversion    = errors.New("failed to convert shard to int64")
	ErrEmptyKey           = errors.New("key cannot be empty")
)

Fragmentation errors

Functions

func CleanPath

func CleanPath(p string) string

CleanPath removes leading/trailing slashes and normalizes the path.

func CreateLevelDBDatabase

func CreateLevelDBDatabase(params ...string) (*leveldb.DB, error)

CreateLevelDBDatabase creates and returns a LevelDB database connection. Params should include at least the base directory and shard name. Optional third parameter specifies the database filename or subfolder.

Example usage:

db, err := CreateLevelDBDatabase("Database/", "Common", "Shard_0.sqlite")

Returns a *leveldb.DB connection and any error encountered.

func CreateSQLiteDatabase

func CreateSQLiteDatabase(params ...string) (*sql.DB, error)

CreateSQLiteDatabase creates and returns a SQLite database connection. Params should include at least the base directory and shard name. Optional third parameter specifies the database filename.

Example usage:

db, err := CreateSQLiteDatabase("Database/", "UserData", "users.db")

Returns a *sql.DB connection and any error encountered.

func FragmentationAdd

func FragmentationAdd(shard int64, keys ...string) error

FragmentationAdd adds one or more fragment entries to the global schema. Each key is mapped to the specified shard number.

Parameters:

  • shard: The shard number to associate with the keys
  • keys: One or more keys to add to the fragmentation schema

Returns an error if any database operation fails.

func FragmentationExists

func FragmentationExists(key string) (bool, error)

FragmentationExists checks if a key exists in the fragmentation schema.

Parameters:

  • key: The key to check

Returns true if the key exists, false otherwise.

func FragmentationGet

func FragmentationGet(rowID string) (int64, error)

FragmentationGet retrieves the shard number for a given row ID.

Parameters:

  • rowID: The row ID to look up in the fragmentation schema

Returns the shard number and any error encountered.

func FragmentationRemove

func FragmentationRemove(key string) error

FragmentationRemove removes a fragment entry from the global schema.

Parameters:

  • key: The key to remove from the fragmentation schema

Returns an error if the database operation fails.

func FragmentationUpdate

func FragmentationUpdate(key string, newShard int64) error

FragmentationUpdate updates the shard assignment for an existing key.

Parameters:

  • key: The key to update
  • newShard: The new shard number to assign

Returns an error if the key doesn't exist or if the update fails.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL