BadDB is a lightweight binary storage engine for Java. It keeps the code small and explainable, while now supporting enough basic CRUD behavior to be usable for a classroom project or an early proof of concept.
- Binary on-disk storage using
RandomAccessFile - In-memory B-Tree index for primary-key lookups
- Insert, read, update, delete, and upsert operations
- Equality-based filtering with
select(columnName, value) - Active-record scanning with
getAllRecords() - Convenience helpers like
exists,countRecords, andcompact - Automatic index rebuild when reopening a database file
- Automatic upgrade of legacy v1 files to the newer record-marker format
BadDB now stores a small active/deleted marker in each record. That makes a few practical features possible without turning the project into a full database engine:
updateRecord(...)appends the new version of a row and marks the old one as deleteddeleteRecord(...)hides a row from future reads while keeping the file format simplecompact()rewrites the file with only active rows, which is useful after many updates or deletes
This keeps the implementation understandable while making the API much more usable.
Schema schema = new Schema();
schema.addColumn("id", DataType.INT);
schema.addColumn("name", DataType.STRING);
schema.addColumn("grade", DataType.FLOAT);
schema.addColumn("is_active", DataType.BOOLEAN);
Table students = new Table("Students", schema, "students.bad");
students.initialize();Record row = new Record(schema.getColumnCount());
row.setValue(0, 101);
row.setValue(1, "Alice Smith");
row.setValue(2, 88.5f);
row.setValue(3, true);
students.insertRecord(row);Record byId = students.searchByPrimaryKey(101);
List<Record> byName = students.select("name", "Alice Smith");
List<Record> all = students.getAllRecords();
boolean exists = students.exists(101);
int count = students.countRecords();Record updated = new Record(schema.getColumnCount());
updated.setValue(0, 101);
updated.setValue(1, "Alice Smith");
updated.setValue(2, 91.0f);
updated.setValue(3, true);
students.updateRecord(101, updated);students.deleteRecord(101);students.upsertRecord(updated);students.compact();| File | Description |
|---|---|
src/main/java/org/baddb/model/DataType.java |
Enum defining supported types (INT, FLOAT, STRING, BOOLEAN). |
src/main/java/org/baddb/model/Column.java |
Metadata for one schema column. |
src/main/java/org/baddb/model/Schema.java |
Collection of columns defining a table layout. |
src/main/java/org/baddb/model/Record.java |
Holds one row of data. |
src/main/java/org/baddb/index/BTreeNode.java |
B-Tree node implementation. |
src/main/java/org/baddb/index/BTree.java |
In-memory key-to-offset index. |
src/main/java/org/baddb/storage/DatabaseFileManager.java |
Low-level binary file read/write layer. |
src/main/java/org/baddb/storage/Table.java |
High-level CRUD API. |
src/main/java/org/baddb/Main.java |
Runnable demo showing the current features. |
- JDK 8 or higher
javac -d out (Get-ChildItem -Recurse -Filter *.java src/main/java | ForEach-Object { $_.FullName })java -cp out org.baddb.Mainsdk_guide.md: quick developer-facing usage guidecode_explanation.md: beginner-friendly implementation walkthroughwalkthrough.md: project walkthrough notes