How tomial/go-db works
Main Components
• Command Interface: The "cmd/godb.go" file indicates that the database is started as a standalone executable. The REPL (found under internal/repl) lets users interact with the database.
• Query Processor: Although not explicitly separated as “query evaluator,” the REPL and related buffer/stm components handle interpreting simple SQL commands.
• Storage Engine:
• B+Tree: The “internal/btree” folder contains the core logic for implementing the B+Tree. Include components like leaf, node, and overall tree management.
• Pager: Located in internal/pager, this component abstracts file/page I/O for reading and writing data pages to disk.
• Storage/Table Layer: The “internal/storage/table.go” file manages table-level logic (storing and retrieving rows).
• Data Representation: Modules like “internal/row” (which holds row, serialization, and cursor management) and “internal/datatype” handle the formatting and size/type details of stored records.
• Utilities and Constants: The “internal/constants” and “internal/util” directories provide support services like constant definitions and utility functions that all system components could use.
Relationships and Data Flow
• The entrypoint (cmd/godb.go) starts the REPL, which serves as the frontend for users entering SQL-like commands.
• The REPL interprets commands and triggers operations on the storage engine.
• For a write operation (e.g., insert):
• The command is processed by the REPL and passed to the table layer in “internal/storage.”
• The table layer interacts with the B+Tree (internal/btree) to manage indexing.
• The Pager (internal/pager) is used to persist the pages (representing tree nodes and table data) to disk.
• The row and serialization layers (internal/row) format the data for storage.
• For a read operation (e.g., select):
• The flow is similar, with the REPL sending a query to the storage layer,
• Which then uses the B+Tree for fast lookup, with the Pager handling data access.
• Utility functions and constant definitions support these interactions.
Architectural Patterns and Design Principles
• Modularity: Each functional area (REPL, Pager, Tree, Storage, Row operations) is isolated in its own subdirectory, promoting separation of concerns.
• Layered Architecture: You have a clear separation between user interaction (REPL) and backend processing (data storage and indexing).
• Testability: Several components have accompanying test files, which indicates that each module is designed to be unit-tested independently.