J4 file format
A J4 file holds a header frame, a fixed number of group frames and, after them, frames allocated on demand. A record key is hashed to a group. See Hashed file types for what all types share. The data files explorer draws this structure and shows it in sample J4 files.
Offsets in the file are 8 bytes long.
HEADER
The header is 0x98 bytes at offset 0. The rest of the first frame is zero.
| Offset | Size | Holds |
|---|---|---|
| 0x00 | 8 | JBC__HS4 |
| 0x08 | 4 | frame size in bytes |
| 0x0c | 4 | secondary record size: a record body of this length or more is stored out of its group |
| 0x10 | 4 | number of groups (the modulo) |
| 0x14 | 4 | 1 |
| 0x18 | 8 | creation time, seconds since 1970 |
| 0x20 | 4 | hash method |
| 0x24 | 4 | flags given at creation |
| 0x28 | 8 | offset of group 0, equal to the frame size |
| 0x30 | 8 | offset of the first free frame, or 0 |
| 0x38 | 4 | number of free frames |
| 0x3c | 8 | end of the file: where the next new frame goes, or 0 if none was allocated yet |
| 0x54 | 64 | resize parameters for jrestore, a C string |
CREATE-FILE name modulo,separation,secondary sets the fields as follows:
- Frame size is the separation times 4096. Separation is 1 if not given.
- The number of groups is the modulo with its lowest bit set: an even modulo becomes the next odd number.
- Secondary record size is the frame size times "secondary", and at least 32. "secondary" is 2 if not given.
- The hash method is 5.
FRAMES AND CHAINS
Every frame other than the header starts with a frame header of 0x34 bytes. The rest of the frame is its payload.
| Offset | Size | Holds |
|---|---|---|
| 0x00 | 8 | offset of the next frame of the chain, or 0 |
| 0x08 | 4 | number of frames after this one in the chain |
| 0x0c | 8 | offset of the previous frame, or 0 |
| 0x14 | 4 | number of frames before this one |
| 0x18 | 8 | offset of the first frame of the chain |
| 0x20 | 4 | sequence number, one more each time the chain is written |
| 0x24 | 8 | time of the last write |
| 0x2c | 4 | flags; bit 0 is set by every write after creation |
| 0x30 | 4 | payload bytes used in this frame |
A chain is a first frame and the frames linked from it. It stores one string of bytes: the payloads in chain order. Every frame but the last is full. There are two kinds of chain:
- A group. The first frame of group n is at the offset of group 0 plus n times the frame size. Its other frames are allocated.
- A record body out of its group. All its frames are allocated.
In every frame after the first the sequence number equals the one in the first frame, and the previous frame field names the frame before it. A reader that finds anything else reports error 2007.
A write rewrites all frames of the chain with a new sequence number and time. A chain that grew takes new frames, and a chain that shrank gives its last frames to the free list.
RECORDS
| Offset | Size | Holds |
|---|---|---|
| 0x00 | 4 | key length in the low 28 bits |
| 0x04 | 4 | body length |
| 0x08 | 8 | time of the last write |
| 0x10 | key length | the key |
| 1 | 0xff |
What follows depends on the body length:
- Less than the secondary record size: the body, then one more 0xff. The record takes key length + body length + 18 bytes.
- Otherwise: the 8-byte offset of the chain that holds the body. The record takes key length + 25 bytes.
Reading a group stops when 16 bytes or fewer remain.
HASHING
The group of a key is the hash of the key modulo the number of groups. The hash is computed from the bytes c of the key, in a 64-bit number h, by the hash method in the header:
| Method | Start | Step for each byte | Result |
|---|---|---|---|
| 0, 1, 7 and above | 0 | h = h x 16 + c | low 31 bits |
| 2 | 0 | h = h x 16 + c; then t = h AND 0xf0000000 and, if t is not 0, h = h XOR t XOR (t shifted right by 24) | low 31 bits |
| 3 | 0 | h = h x 10 + c | low 32 bits |
| 4 | 0 | h = (h x 10 + c) AND 0x7fffffff | h |
| 5 | 0x14a24a5 | h = (h XOR c) x 0x1000193 | low 31 bits |
| 6 | 0x811c9dc5 | h = (h XOR c) x 0x1000193 | low 31 bits |
For an empty key methods 5 and 6 give 0x14a24a5 and 0x11c9dc5, and the others give 0.
FREE FRAMES
The header keeps a stack of free frames. A released frame gets the offset of the previous head in its first 8 bytes and becomes the head. A new frame is the head of the stack if there is one; otherwise it goes at the end of the file.
NOTES
A write removes the old record of the key, if any, and adds the new one at the end of the group, so a rewritten record moves to the end.
SELECT returns the records by group number, and within a group in the order they are stored.