-
Notifications
You must be signed in to change notification settings - Fork 4
Implement support for BLOB and MEMO datatypes #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maxthoursie
wants to merge
1
commit into
linville:main
Choose a base branch
from
maxthoursie:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,14 +43,15 @@ Datatypes | |
| | -- | --------- | --- |----------------------- | | ||
| | 1 | String | Variable | Size defined in column definition at `0xA6` | | ||
| | 2 | Date | 4 | Days -1 since [AD 1, Jan 0](https://en.wikipedia.org/wiki/List_of_non-standard_dates#January_0) | | ||
| | 3 | BLOB | ? | Not yet supported. BLOBs are stored in a separate `.blb` file. The data in the `.dat` file is likely an address for the `.blb` file. | | ||
| | 3 | BLOB | 8 | Block index in the `.blb` file. The actual content is stored in blocks with 18-byte headers. | | ||
| | 4 | Boolean | 1 | Missing the trailing `\x01` marker | | ||
| | 5 | Short Int | 2 | | | ||
| | 6 | Int | 4 | | | ||
| | 7 | Double | 8 | IEEE-754 | | ||
| | 11 | Timestamp | 8 | IEEE-754, milliseconds since [AD 1, Jan 0](https://en.wikipedia.org/wiki/List_of_non-standard_dates#January_0) | | ||
| | 5383 | Currency | 8 | IEEE-754 | | ||
| | 7430 | Autoincrement | 4 | Int | | ||
| | 7431 | MEMO | 8 | Similar to BLOB, stores text content in the `.blb` file | | ||
|
|
||
|
|
||
| Row Definition | ||
|
|
@@ -66,3 +67,24 @@ A row in the actual data section of the database has a 26-byte row header. The m | |
| | `0x9` | 16 | Checksum (MD5?) | | ||
| | `0x19` | 2 | Trailing `\x01` marker | | ||
| | `0x20` | | Start of first field | | ||
|
|
||
| Blob Format | ||
| ----------- | ||
|
|
||
| Blobs are stored in a separate `.blb` file and use a block-based structure. Each block has a header followed by content. | ||
|
|
||
| Block Header (18 bytes): | ||
| | Offset | Size<br>(bytes) | Description | | ||
| | ------: | ---- | ------------------------------ | | ||
| | `0x0` | 4 | Previous block index | | ||
| | `0x4` | 4 | Next block index | | ||
| | `0x8` | 2 | Length of block content | | ||
| | `0xA` | 4 | Unknown index | | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0xA is the row index, from the main table file. This is how the engine can link the blob block to a specific row. One blb file contains the data of all blob and memo type columns. |
||
| | `0xE` | 4 | Total length (on first block) | | ||
|
|
||
| The blocks form a linked list structure where: | ||
| - Each block points to the next block using the next block index | ||
| - The chain ends when next block index is 0 | ||
| - The first block (index 0) is empty | ||
| - Content is stored after the header in each block | ||
| - The total length field in the first block indicates the complete blob size | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import struct | ||
| import sys | ||
| import hashlib | ||
| import os | ||
|
|
||
|
|
||
| class Blob: | ||
| """ | ||
| Blob is a class that decodes the blob data from a file. | ||
|
|
||
| `_data` is the memoryview of the blob data | ||
|
|
||
| `_block_size` is the size of the block | ||
|
|
||
| """ | ||
|
|
||
| _BLOCK_HEADER_SIZE = 18 | ||
| _BLOB_DIR = "blobs" | ||
|
|
||
| def __init__(self, content, block_size): | ||
| self._data = memoryview(content) | ||
| self._block_size = block_size | ||
|
|
||
| def get_blob(self, block_index): | ||
| # print("GET BLOB", block_index, file=sys.stderr) | ||
| content = bytearray() | ||
| if block_index == 0: | ||
| return content | ||
|
|
||
| offset = block_index * self._block_size | ||
| remaining_length = 0 | ||
| try: | ||
| while True: | ||
| (prev_block, next_block, length_of_block, blob_index, total_length) = struct.unpack_from("<IIHII", self._data, offset) | ||
| if total_length > 0: | ||
| remaining_length = total_length | ||
| content_offset = offset + self._BLOCK_HEADER_SIZE | ||
| block_content = bytearray(self._data[content_offset:content_offset+length_of_block]) | ||
| content.extend(block_content) | ||
| # print(" BLOCK", int(offset/self._block_size), (prev_block, next_block, length_of_block, blob_index, total_length), length_of_block, total_length, offset, len(self._data), file=sys.stderr) | ||
| # print(" CONTENT HEX", block_content.hex(), file=sys.stderr) | ||
| # print(" CONTENT", block_content, file=sys.stderr) | ||
|
|
||
| remaining_length -= length_of_block | ||
| #if next_block == 0: | ||
| if remaining_length <= 0: | ||
| break | ||
| offset = next_block * self._block_size | ||
|
|
||
| except Exception as e: | ||
| print("ERROR decoding block", e, "offset", offset, file=sys.stderr) | ||
| return bytearray() | ||
|
|
||
| # print(" HASH", self._hash(content), file=sys.stderr) | ||
| # print(" COMPLETE", remaining_length, content, file=sys.stderr) | ||
| return content | ||
|
|
||
| def write_blob_to_content_hash(self, content): | ||
| content_hash = self._hash(content) | ||
| self._write_blob_to_file(content, os.path.join(self._BLOB_DIR, content_hash)) | ||
| return content_hash | ||
|
|
||
| def _hash(self, content): | ||
| md5 = hashlib.md5() | ||
| md5.update(content) | ||
| return md5.hexdigest(); | ||
|
|
||
|
|
||
| def _write_blob_to_file(self, content, path): | ||
| os.makedirs(os.path.dirname(path), exist_ok=True) | ||
| with open(path, "wb") as file: | ||
| file.write(content) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In all file headers, this is the engine's checksum and not a file checksum. This needs to match the reading engine, otherwise the engine is unable to read the file. It doesn't matter for this project, it is only used by the engine.