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.

OffsetSizeHolds
0x008JBC__HSR
0x084frame size (FRAMESIZE, 4096 if not given)
0x0c4out-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
0x104first internal modulo
0x144revision level, 2
0x184creation time, seconds since 1970
0x1c4hash method, 5 if not given
0x204flags, 0x240 if not given; bit 0x200 is secure updates (SECURE)
0x248frame size again
0x2c8offset of the first free frame, or 0
0x344number of free frames
0x388end of the file after the last frame added
0x404second internal modulo, or 0
0x842 x 32external modulos, a list ended by 0
0xc44third internal modulo, or 0
0xc84minimum split level (MINSPLIT, 0 if not given)
0xcc4deallocation mode (DEALLOCATE): 0, 1 for D, 3 for P
0xd88 x 485the 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:

LevelNumber of slotsFirst slot
0m00
1m0 x m1m0
2m0 x m1 x m2m0 + 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:

FRAMES

Every frame other than the header starts with a frame header of 0x18 bytes.

OffsetSizeHolds
0x002frame type
0x022data frame: bytes used. Pointer frame: number of children
0x042index of the frame in its parent
0x062level
0x088offset of the pointer that leads to this frame
0x108next frame, for body frames and free frames

The frame types are:

Frame typeFrame
0xdadadata frame: the records of one group, from offset 0x18
0xcafepointer frame: one 8-byte offset for each child, from offset 0x18
0xbeefbody frame: a part of a record body stored out of its group
0xdeadfree 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:

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

OffsetSizeHolds
0x004key length in the low 16 bits; bit 0x10000 is set when the body is out of the group
0x044body length
0x084time of the write, seconds since 1970
0x0ckey lengththe key
10xff

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.

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.