Skip to content

ONQL/server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

39 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Go RDBMS with BadgerDB

A high-performance, embedded message-based RDBMS written in Go, powered by BadgerDB with a complete TCP server, query language (DSL), and protocol system for entity mapping.

πŸš€ Key Features

Core Database

  • 3-Layer Architecture: Clean separation between Engine, Store Manager, and Database layers
  • Hybrid Storage: RAM buffering with asynchronous disk persistence (500ms flush)
  • Full Indexing: Automatic reverse indexing for every column
  • Laravel-style Validation & Formatting: Schema-level rules (e.g., required|min:18, trim|upper)
  • Production Ready: Structured logging, graceful shutdown, configuration via environment

Message-Based System

  • TCP Server: Multi-client support with connection pooling
  • Message Router: Routes requests to database, DSL, or extensions
  • Protocol System: Maps entity aliases to actual DB/table names with relationships
  • DSL Query Engine: SQL-like query language with filters, projections, and aggregations

πŸ— Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    TCP Server (Port 8080)                β”‚
β”‚                  Message Protocol (\x04)                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
                     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   Message Router                         β”‚
β”‚  Routes: database | onql | protocol | schema            β”‚
β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚          β”‚          β”‚            β”‚
      β–Ό          β–Ό          β–Ό            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Database β”‚ β”‚ DSL  β”‚ β”‚ Protocol β”‚ β”‚  Schema  β”‚
β”‚ Handler  β”‚ β”‚Engineβ”‚ β”‚Extension β”‚ β”‚Extension β”‚
β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”¬β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
     β”‚          β”‚          β”‚            β”‚
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
                     β–Ό
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚   Database Layer     β”‚
          β”‚ (Validation/Format)  β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
                     β–Ό
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚   Store Manager      β”‚
          β”‚ (Buffer/Flush/Index) β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
                     β–Ό
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚   BadgerDB Engine    β”‚
          β”‚   (LSM Tree Store)   β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‹ Protocol System

The Protocol System allows you to define entity aliases that map to actual database tables with field mappings and relationships.

Protocol Structure

{
  "database": "mydb",
  "entities": {
    "users": {
      "table": "accounts",
      "fields": {
        "id": "account_id",
        "name": "full_name",
        "email": "email_address"
      },
      "relations": {
        "orders": {
          "type": "otm",
          "entity": "orders",
          "fk_field": "id:user_id"
        }
      },
      "context": {
        "user": "mydb.users[id=$1]",
        "admin": "mydb.users[role='admin']"
      }
    }
  }
}

Relationship Types

  • oto (One-to-One): Single related record
  • otm (One-to-Many): Multiple related records
  • mto (Many-to-One): Reverse of one-to-many
  • mtm (Many-to-Many): Through junction table

Protocol Commands

Store a Protocol:

{
  "target": "protocol",
  "payload": ["set", "mypassword", {...protocol_data...}]
}

Retrieve Protocol:

{
  "target": "protocol",
  "payload": ["desc", "mypassword"]
}

Delete Protocol:

{
  "target": "protocol",
  "payload": ["drop", "mypassword"]
}

πŸ” DSL Query Language

The DSL (Domain Specific Language) allows SQL-like queries with entity aliases:

mydb.users[age>18].name
mydb.orders[status='pending'].customer.name
mydb.products._count()

Features:

  • Entity-based queries (uses protocol mappings)
  • Filtering with conditions
  • Relationship traversal
  • Aggregations (count, sum, avg, etc.)
  • Projections and field selection

πŸ’» Message Protocol

All communication uses JSON messages with this structure:

{
  "id": "sender_id",
  "target": "database|onql|protocol|schema",
  "rid": "request_id",
  "type": "request|response",
  "payload": "json_string"
}

Messages are delimited by \x04 (EOT character).

Database Functions

{
  "target": "database",
  "payload": {
    "function": "GetDatabases|CreateDatabase|GetTables|...",
    "args": [...]
  }
}

DSL Queries

{
  "target": "onql",
  "payload": {
    "protopass": "mypassword",
    "query": "mydb.users[age>18]",
    "ctxkey": "user",
    "ctxvalues": ["123"]
  }
}

🏁 Getting Started

Prerequisites

  • Go 1.21+

Installation

git clone <repo-url>
cd rdbms
go mod tidy

Running the Server

go run examples/server_demo.go

The server starts on port 8080 by default.

Configuration

Environment variables:

  • DB_PATH: Data storage path (default: ./store)
  • FLUSH_INTERVAL: Buffer flush interval (default: 500ms)
  • LOG_LEVEL: DEBUG|INFO|WARN|ERROR (default: INFO)

πŸ“ Project Structure

rdbms/
β”œβ”€β”€ engine/              # BadgerDB wrapper
β”œβ”€β”€ storemanager/        # Buffer, indexing, schema
β”œβ”€β”€ database/            # Validation, formatting, protocols
β”œβ”€β”€ dsl/                 # Query language parser & evaluator
β”œβ”€β”€ router/              # Message routing
β”œβ”€β”€ server/              # TCP server
β”œβ”€β”€ extensions/          # Extension system
β”‚   β”œβ”€β”€ protocol/        # Protocol management extension
β”‚   └── schema/          # Schema management extension
β”œβ”€β”€ config/              # Configuration loader
β”œβ”€β”€ logger/              # Structured logging
└── examples/            # Demo applications

🎯 Use Cases

  • Multi-tenant Applications: Use protocols for tenant isolation
  • API Backends: Expose via TCP with custom client libraries
  • Embedded Systems: Lightweight, single-binary database
  • Real-time Applications: Fast RAM access with disk durability
  • Microservices: Message-based communication between services

πŸ“š Examples

See examples/ directory for:

  • protocol_demo.go - Protocol system demonstration
  • server_demo.go - TCP server with client
  • dsl_demo.go - DSL query examples

πŸ”§ Advanced Features

  • Context Queries: Parameterized queries with $1, $2 placeholders
  • Field Mapping: Alias fields to actual column names
  • Relationship Traversal: Navigate through entity relationships
  • Extension System: Add custom handlers via extensions
  • Graceful Shutdown: Ensures data integrity on exit

Built with ❀️ using Go and BadgerDB

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors