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.

OffsetSizeHolds
0x008JBC__HS4
0x084frame size in bytes
0x0c4secondary record size: a record body of this length or more is stored out of its group
0x104number of groups (the modulo)
0x1441
0x188creation time, seconds since 1970
0x204hash method
0x244flags given at creation
0x288offset of group 0, equal to the frame size
0x308offset of the first free frame, or 0
0x384number of free frames
0x3c8end of the file: where the next new frame goes, or 0 if none was allocated yet
0x5464resize parameters for jrestore, a C string

CREATE-FILE name modulo,separation,secondary sets the fields as follows:

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.

OffsetSizeHolds
0x008offset of the next frame of the chain, or 0
0x084number of frames after this one in the chain
0x0c8offset of the previous frame, or 0
0x144number of frames before this one
0x188offset of the first frame of the chain
0x204sequence number, one more each time the chain is written
0x248time of the last write
0x2c4flags; bit 0 is set by every write after creation
0x304payload 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:

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

OffsetSizeHolds
0x004key length in the low 28 bits
0x044body length
0x088time of the last write
0x10key lengththe key
10xff

What follows depends on the body length:

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:

MethodStartStep for each byteResult
0, 1, 7 and above0h = h x 16 + clow 31 bits
20h = 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
30h = h x 10 + clow 32 bits
40h = (h x 10 + c) AND 0x7fffffffh
50x14a24a5h = (h XOR c) x 0x1000193low 31 bits
60x811c9dc5h = (h XOR c) x 0x1000193low 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.