@wxn0brp/db-storage-bin 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +6 -3
- package/dist/bin/data.js +9 -0
- package/docs/data-structure.md +44 -0
- package/package.json +1 -1
- package/src/bin/data.ts +8 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [0.0.3](https://github.com/wxn0brp/ValtheraDB-storage-bin/compare/v0.0.2...v0.0.3) (2025-08-24)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* crc ([2b872a1](https://github.com/wxn0brp/ValtheraDB-storage-bin/commit/2b872a11fc42c32bbbeed80c76d62524426392d5))
|
|
11
|
+
|
|
5
12
|
### 0.0.2 (2025-08-24)
|
|
6
13
|
|
|
7
14
|
|
package/README.md
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
# ValtheraDB Bin Plugin
|
|
2
2
|
|
|
3
|
-
This is a proof-of-concept for an addon/plugin for the `@wxn0brp/db` (ValtheraDB) library.
|
|
4
|
-
|
|
5
3
|
The purpose of this experiment is to create a storage layer that allows ValtheraDB, which normally operates on a directory/file structure, to instead use a single binary file for data storage.
|
|
6
4
|
|
|
7
5
|
## Installation
|
|
8
6
|
|
|
9
7
|
```bash
|
|
10
|
-
yarn add
|
|
8
|
+
yarn add @wxn0brp/db-storage-bin
|
|
11
9
|
```
|
|
12
10
|
|
|
13
11
|
## Usage
|
|
@@ -55,6 +53,11 @@ Returns an object containing:
|
|
|
55
53
|
- `options`:
|
|
56
54
|
- `preferredSize`: The preferred block size for the database (default: `256`).
|
|
57
55
|
|
|
56
|
+
## Documentation
|
|
57
|
+
|
|
58
|
+
- [Data Structure](https://github.com/wxn0brP/ValtheraDB-storage-bin/blob/master/docs/data-structure.md)
|
|
59
|
+
- [ValtheraDB-Core](https://github.com/wxn0brP/ValtheraDB-core)
|
|
60
|
+
|
|
58
61
|
## License
|
|
59
62
|
|
|
60
63
|
This project is licensed under the MIT License.
|
package/dist/bin/data.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getFileCrc } from "../crc32.js";
|
|
1
2
|
import { _log } from "../log.js";
|
|
2
3
|
import { saveHeaderAndPayload } from "./head.js";
|
|
3
4
|
import { detectCollisions, pushToFreeList, readData, roundUpCapacity, writeData } from "./utils.js";
|
|
@@ -69,6 +70,14 @@ export async function writeLogic(cmp, collection, data) {
|
|
|
69
70
|
await saveHeaderAndPayload(cmp);
|
|
70
71
|
await _log(2, "Capacity exceeded");
|
|
71
72
|
}
|
|
73
|
+
else {
|
|
74
|
+
if (cmp.options.crc) {
|
|
75
|
+
const { computedCrc } = await getFileCrc(fd);
|
|
76
|
+
const crcBuf = Buffer.alloc(16);
|
|
77
|
+
crcBuf.writeUInt32LE(computedCrc);
|
|
78
|
+
await writeData(fd, 16, crcBuf, 16);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
72
81
|
}
|
|
73
82
|
export async function readLogic(cmp, collection) {
|
|
74
83
|
const collectionMeta = findCollection(cmp, collection);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Data Structure
|
|
2
|
+
|
|
3
|
+
This document describes the data structure used by the binary database format.
|
|
4
|
+
|
|
5
|
+
## File Layout
|
|
6
|
+
|
|
7
|
+
The file is structured as follows:
|
|
8
|
+
|
|
9
|
+
1. **Header (64 bytes)**: Contains metadata about the file.
|
|
10
|
+
2. **Payload**: Contains the serialized collections and free list data.
|
|
11
|
+
3. **Data Blocks**: Contains the actual data for each collection.
|
|
12
|
+
|
|
13
|
+
### Header
|
|
14
|
+
|
|
15
|
+
The header is a fixed-size block of 64 bytes, structured as follows:
|
|
16
|
+
|
|
17
|
+
| Offset | Size | Name | Description |
|
|
18
|
+
|--------|------|--------------|-------------------------------------------------------|
|
|
19
|
+
| 0 | 4 | Version | File format version (currently 1) |
|
|
20
|
+
| 4 | 4 | Payload Len | Length of the payload data |
|
|
21
|
+
| 8 | 4 | Payload Off | Offset of the payload data from the header start |
|
|
22
|
+
| 12 | 4 | Block Size | Preferred block size for allocations |
|
|
23
|
+
| 16 | 4 | CRC32 | CRC32 checksum of the file (excluding this field) |
|
|
24
|
+
| 20 | 44 | Reserved | Reserved for future use |
|
|
25
|
+
|
|
26
|
+
### Payload
|
|
27
|
+
|
|
28
|
+
The payload contains the serialized list of collections and free blocks. It is a msgpack-encoded object with the following structure:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
{
|
|
32
|
+
c: [string, number, number][]; // Collections: [name, offset, capacity]
|
|
33
|
+
f: [number, number][]; // Free blocks: [offset, capacity]
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Data Blocks
|
|
38
|
+
|
|
39
|
+
Each collection's data is stored in a data block. A data block consists of:
|
|
40
|
+
|
|
41
|
+
1. **Length (4 bytes)**: A 32-bit unsigned integer representing the length of the data (Uint32).
|
|
42
|
+
2. **Data (variable)**: The actual data, padded to the nearest block size.
|
|
43
|
+
|
|
44
|
+
The data is serialized using msgpack.
|
package/package.json
CHANGED
package/src/bin/data.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BinManager, CollectionMeta } from ".";
|
|
2
|
+
import { getFileCrc } from "../crc32";
|
|
2
3
|
import { _log } from "../log";
|
|
3
4
|
import { FileMeta, saveHeaderAndPayload } from "./head";
|
|
4
5
|
import { detectCollisions, pushToFreeList, readData, roundUpCapacity, writeData } from "./utils";
|
|
@@ -78,6 +79,13 @@ export async function writeLogic(cmp: BinManager, collection: string, data: obje
|
|
|
78
79
|
});
|
|
79
80
|
await saveHeaderAndPayload(cmp);
|
|
80
81
|
await _log(2, "Capacity exceeded");
|
|
82
|
+
} else {
|
|
83
|
+
if (cmp.options.crc) {
|
|
84
|
+
const { computedCrc } = await getFileCrc(fd);
|
|
85
|
+
const crcBuf = Buffer.alloc(16);
|
|
86
|
+
crcBuf.writeUInt32LE(computedCrc);
|
|
87
|
+
await writeData(fd, 16, crcBuf, 16);
|
|
88
|
+
}
|
|
81
89
|
}
|
|
82
90
|
}
|
|
83
91
|
|