J3 and JP file formats
J3 and JP are the types that came before J4. They have one structure: a header frame, a fixed number of group frames and, after them, frames allocated on demand. See Hashed file types for what all types share. The data files explorer draws this structure and shows it in sample J3 and JP files.
JP differs from J3 in three ways. A file offset is 8 bytes long, not 4, so a file can be larger than 4 GB. The block is 4096 bytes, not 1024. The first frame of a group carries a size hint.
Below, P is the size of a file offset: 4 in J3 and 8 in JP.
HEADER
The header is 0x78 bytes in J3 and 0x84 bytes in JP, at offset 0. The rest of the first frame is zero.
| J3 offset | JP offset | Size | Holds |
|---|---|---|---|
| 0x00 | 0x00 | 8 | JBC__HS3 in a J3 file; in a JP file the last character is P |
| 0x08 | 0x08 | 4 | frame size in bytes |
| 0x0c | 0x0c | 4 | secondary record size: a record body of this length or more is stored out of its group |
| 0x10 | 0x10 | 4 | number of groups (the modulo) |
| 0x14 | 0x14 | 4 | 1 in J3, 3 in JP |
| 0x18 | 0x18 | 4 | creation time, seconds since 1970 |
| 0x1c | 0x1c | 4 | hash method |
| 0x20 | 0x20 | 4 | flags given at creation: 0x20 in a J3 file and 0x40 in a JP file made by CREATE-FILE |
| 0x24 | 0x24 | P | offset of group 0, equal to the frame size |
| 0x28 | 0x2c | P | offset of the first free frame, or 0 |
| 0x2c | 0x34 | 4 | number of free frames |
| 0x30 | 0x38 | P | end of the file: where the next new frame goes |
CREATE-FILE name modulo,separation,secondary sets the fields as follows:
- Frame size is the separation times the block size: 1024 in J3 and 4096 in JP. Separation is 1 if not given. JP accepts a separation up to 8.
- 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
Every frame other than the header starts with the offsets of the next and the previous frame of its chain and a frame type. The rest of the frame header depends on the type.
| Offset | Size | Frame types | Holds |
|---|---|---|---|
| 0 | P | all | offset of the next frame of the chain, or 0 |
| P | P | all | offset of the previous frame, or 0 |
| 2P | 4 | all | frame type |
| 2P + 4 | 4 | 1, 2 | bytes used in this frame, the frame header included |
| 2P + 8 | 4 | 1, 2 | sequence number of the group |
| 2P + 12 | 4 | 1 | a time; the creation time in a new file |
| 2P + 16 | 4 | 1 | number of records in the group whose body is out of the group |
The frame types are:
| Frame type | Frame | Header size | In J3 | In JP |
|---|---|---|---|---|
| 1 | first frame of a group | 2P + 20 | 0x1c | 0x24 |
| 2 | overflow frame of a group | 2P + 12 | 0x14 | 0x1c |
| 3 | frame of a record body out of its group | 2P + 4 | 0x0c | 0x14 |
| 4 | free frame | 2P + 4 | 0x0c | 0x14 |
In the first frame of a JP group the "bytes used" field holds two numbers. The low 16 bits are the bytes used. The high 16 bits are a hint for the reader: how many frames of memory to set aside for the group. It is 0 for a group of one frame, and a reader can ignore it.
GROUPS
The first frame of group n is at the offset of group 0 plus n times the frame size. It has type 1 and no previous frame. When the group does not fit in one frame, the first frame is full and points to a chain of frames of type 2. Each of them points back to the frame before it and repeats the sequence number of the first frame. A reader that finds anything else reports error 2007.
The data of a group is the used bytes of its frames after their headers, in chain order.
A new group has sequence number 0. Every write, delete and clear of the group adds 1.
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 |
When the body length is less than the secondary record size, the body follows, and then one more 0xff. Such a record takes key length + body length + 14 bytes.
Otherwise an offset of P bytes follows: the first frame of the chain that holds the body. Such a record takes key length + 13 + P bytes. A key is at most 65535 bytes long.
The body is stored in a chain of frames of type 3. Each frame gives the frame size less 2P + 4 bytes of the body. The first frame has no previous frame and the last has no next frame. The frames of a chain need not be in file order.
NOTES
The group of a key is the hash of the key modulo the number of groups, with the hash methods of J4 file format.
Free frames form a list. The header holds the offset of the first one and their number. A free frame has type 4 and the offset of the next free frame in its first P bytes.
SELECT returns the records by group number, and within a group in the order they are stored.
How a write lays out a group and in which order it takes frames from the free list is not described.