galileostudio/nyarudb2
**Lightweight, high-performance embedded database for Swift**
π Table of Contents
- Installation
- Quick Start Example
- Key Features
- Architecture
- Documentation
- Contributing
- License
- Acknowledgements
π¦ Installation
NyaruDB2 supports Swift Package Manager:
// swift-tools-version:5.9
let package = Package(
name: "YourApp",
dependencies: [
.package(url: "https://github.com/galileostudio/NyaruDB2.git", from: "0.1.0-alpha")
],
targets: [
.target(
name: "YourApp",
dependencies: ["NyaruDB2"]
)
]
)Requirements:
- Xcode 15+
- Swift 5.9+
- iOS 15+ / macOS 12+
π Quick Start Example
import NyaruDB2
// Define a model
struct User: Codable, Equatable {
let id: Int
let name: String
let createdAt: String
}
// 1. Initialize the database
let db = try NyaruDB2(
path: "NyaruDB_Demo",
compressionMethod: .lzfse,
fileProtectionType: .completeUntilFirstUserAuthentication
)
// 2. Create a partitioned collection
let users = try await db.createCollection(
name: "Users",
indexes: ["id"],
partitionKey: "createdAt"
)
// 3. Bulk insert documents
try await users.bulkInsert([
User(id: 1, name: "Alice", createdAt: "2024-01"),
User(id: 2, name: "Bob", createdAt: "2024-02")
])
// 4. Perform a query
var query = try await users.query() as Query<User>
query.where(\User.id, .greaterThan(1))
let results = try await query.execute()
print(results)β¨ Key Features
Performance
- Sharded Storage: automatic partitioning by configurable shard keys
- Multi-Algorithm Compression: GZIP, LZFSE, LZ4 via
Compression.framework - Actor-Safe B-Tree Indexing: configurable minimum degree for performance tuning
Advanced Queries
- Type-Safe Query Builder: supports 15+ predicates (equal, range, contains, startsWith, etc.)
- Lazy Loading:
AsyncThrowingStreamfor memory-efficient iterating - Cost-Based Query Planner: selects optimal indexes and prunes shards using statistics
Monitoring & Stats
- StatsEngine:
CollectionStats,GlobalStatswith shard count, document count, and sizes - IndexStats: tracks value distribution, selectivity, access counts
ποΈ Architecture
NyaruDB2/
βββ Sources/
β βββ NyaruDB2/
β β βββ Core/
β β β βββ Commons/ # FileProtection, DynamicDecoder
β β β βββ IndexManager/ # BTreeIndex, IndexManager
β β β βββ QueryEngine/ # Query, QueryPlanner, ExecutionPlan
β β β βββ StatsEngine/ # CollectionStats, GlobalStats
β β β βββ StorageEngine/ # ShardManager, Compression, StorageEngine
β β βββ CollectionEngine/ # DocumentCollection, CollectionCatalog
β β βββ NyaruDB2.swift # public API
β βββ Benchmark/ # performance test suite
βββ Tests/ # unit and integration testsπ Documentation
Full API reference: π https://nyarudb2.docs.example.com
π€ Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature/awesome - Commit your changes:
git commit -m "Add awesome feature" - Push to your branch:
git push origin feature/awesome - Open a Pull Request
Please see CONTRIBUTING.md for details.
π License
Apache 2.0 Β© 2025 galileostudio. See LICENSE.
π Acknowledgements
Inspired by the original NyaruDB by kelp404. Many thanks for the foundational ideas.
Package Metadata
Repository: galileostudio/nyarudb2
Default branch: main
README: README.md