JR file format
A JR file starts as a header frame and grows one frame at a time. The header holds a table of pointers to groups. A group is one frame. A group that outgrows its frame splits: first into more slots of the table, then into pointer frames outside the header. See Hashed file types for what all types share. The data files explorer draws this structure and shows it in sample JR files.
Offsets in the file are 8 bytes long. The frame size was 4096 in every file studied.
HEADER
The header is the first frame.
| Offset | Size | Holds |
|---|---|---|
| 0x00 | 8 | JBC__HSR |
| 0x08 | 4 | frame size (FRAMESIZE, 4096 if not given) |
| 0x0c | 4 | out-of-group limit (SECSIZE, half a frame if not given, at least 32): a record body of this length or more is stored out of its group |
| 0x10 | 4 | first internal modulo |
| 0x14 | 4 | revision level, 2 |
| 0x18 | 4 | creation time, seconds since 1970 |
| 0x1c | 4 | hash method, 5 if not given |
| 0x20 | 4 | flags, 0x240 if not given; bit 0x200 is secure updates (SECURE) |
| 0x24 | 8 | frame size again |
| 0x2c | 8 | offset of the first free frame, or 0 |
| 0x34 | 4 | number of free frames |
| 0x38 | 8 | end of the file after the last frame added |
| 0x40 | 4 | second internal modulo, or 0 |
| 0x84 | 2 x 32 | external modulos, a list ended by 0 |
| 0xc4 | 4 | third internal modulo, or 0 |
| 0xc8 | 4 | minimum split level (MINSPLIT, 0 if not given) |
| 0xcc | 4 | deallocation mode (DEALLOCATE): 0, 1 for D, 3 for P |
| 0xd8 | 8 x 485 | the pointer table |
The internal modulos come from the INTMODS qualifier of CREATE-FILE (3,7,19 if not given) and the external ones from EXTMODS (31 if not given). jstat shows them as "Internal Modulo = 3/7/19" and "External Modulo = 31".
A new file is the header alone, 4096 bytes, with an empty table.
THE POINTER TABLE
With internal modulos m0, m1 and m2 the table has three levels:
| Level | Number of slots | First slot |
|---|---|---|
| 0 | m0 | 0 |
| 1 | m0 x m1 | m0 |
| 2 | m0 x m1 x m2 | m0 + m0 x m1 |
The children of slot s of level 0 are the m1 slots that start at slot m0 + m1 x s. The children of slot s of level 1 are the m2 slots that start at slot m0 + m0 x m1 + m2 x (s - m0). A file with fewer internal modulos has fewer levels.
A slot holds one of:
- 0: there is no group yet.
- An offset of 0x1000 or more: the frame of the group.
- An offset below 0x1000: the group was split, and this is the offset, in the header, of the slot of its first child.
FRAMES
Every frame other than the header starts with a frame header of 0x18 bytes.
| Offset | Size | Holds |
|---|---|---|
| 0x00 | 2 | frame type |
| 0x02 | 2 | data frame: bytes used. Pointer frame: number of children |
| 0x04 | 2 | index of the frame in its parent |
| 0x06 | 2 | level |
| 0x08 | 8 | offset of the pointer that leads to this frame |
| 0x10 | 8 | next frame, for body frames and free frames |
The frame types are:
| Frame type | Frame |
|---|---|
| 0xdada | data frame: the records of one group, from offset 0x18 |
| 0xcafe | pointer frame: one 8-byte offset for each child, from offset 0x18 |
| 0xbeef | body frame: a part of a record body stored out of its group |
| 0xdead | free frame |
A group never continues in another frame: a group of more than 4072 bytes is split.
For a data frame and a pointer frame the index, the level and the parent offset are checked on every read, and a mismatch is error 2007:
- A frame under the header has the index of its slot among the slots of its level, and the offset of the slot as its parent. Its level is the table level, except that a frame of the last table level has level 0.
- A frame under a pointer frame has its index in that frame, a level one higher than that frame, and the offset of its pointer as its parent.
HASHING
A key has two hashes: h1 by the hash method in the header and h2 by method 6. The methods are described in J4 file format. Even levels use h1 and odd levels use h2.
In the table, the index at a level is the hash modulo the internal modulo of that level.
In a pointer frame of level L the index is
((h XOR rotate_left(0xf0cc55aa, L)) AND 0x7fffffff) MOD m
where the rotation is on 32 bits and m is the external modulo of level L. When there are fewer external modulos than levels, the last one is used for the levels that have none.
To find the group of a key, start at level 0 and follow table slots while they hold an offset below 0x1000. Then follow pointer frames until a data frame or an offset of 0 is reached.
RECORDS
| Offset | Size | Holds |
|---|---|---|
| 0x00 | 4 | key length in the low 16 bits; bit 0x10000 is set when the body is out of the group |
| 0x04 | 4 | body length |
| 0x08 | 4 | time of the write, seconds since 1970 |
| 0x0c | key length | the key |
| 1 | 0xff |
Then comes the body followed by one more 0xff (the record takes key length + body length + 14 bytes), or the 8-byte offset of the first body frame (key length + 21 bytes). A key is at most 65535 bytes long. Reading a group stops when 12 bytes or fewer remain.
A body out of the group is stored in a chain of body frames, 4072 bytes in each. In a body frame the field at 0x04 is the number of frames after it, the field at 0x06 is its number in the chain from 0, the field at 0x08 is the previous frame and the field at 0x10 is the next frame. The field at 0x02 means nothing in a body frame.
NOTES
A write removes the old record of the key, if any, and adds the new one at the end of the group. A group that no longer fits in a frame is split: its records are dealt out to children by the hash of the next level, in the order they had.
- If the group's pointer is a table slot that has child slots, the children go there, the slot gets the offset of its first child slot, and the old frame is released.
- Otherwise the group's frame becomes a pointer frame that holds the offsets of the children.
A group under the table that loses all its records keeps its frame, with 0 bytes used. What happens to an emptied group under a pointer frame depends on the deallocation mode: with mode 0 it keeps its frame too; with mode 1 or 3 the frame is released and its pointer is set to 0; with mode 3 a pointer frame left with no children is released as well.
A released frame is written as zeroes with type 0xdead and the offset of the previous head of the free list at 0x10.
SELECT returns the groups in table order, depth first: the children of a split slot come in place of the slot, and the children of a pointer frame come in index order. Within a group the records come in the order they are stored.
Not described: secure updates (flag 0x200), preallocation with MINSPLIT, frame sizes other than 4096 and revision 1 files, which use one hash for all levels.