11 // positionBytes is the size of an encoded position.
12 const positionBytes = 16
14 // openRecords opens or creates the record file at path with records of size
16 func openRecords(path string, size int) (*Records, error) {
17 file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
19 return nil, fmt.Errorf("open index: %w", err)
22 info, err := file.Stat()
24 return nil, errors.Join(fmt.Errorf("read index size: %w", err), file.Close())
30 count: int(info.Size()) / size,
33 if info.Size()%int64(size) != 0 {
34 err = file.Truncate(int64(r.count * size))
36 return nil, errors.Join(fmt.Errorf("cut partial index record: %w", err), file.Close())
43 // encodePosition writes place into the first positionBytes bytes of record.
44 func encodePosition(record []byte, place Position) {
45 binary.LittleEndian.PutUint32(record[0:], place.day)
46 binary.LittleEndian.PutUint32(record[4:], place.length)
47 binary.LittleEndian.PutUint64(record[8:], place.offset)
50 // decodePosition reads a position from the first positionBytes bytes of record.
51 func decodePosition(record []byte) Position {
53 day: binary.LittleEndian.Uint32(record[0:]),
54 length: binary.LittleEndian.Uint32(record[4:]),
55 offset: binary.LittleEndian.Uint64(record[8:]),
59 // Records is a file of fixed-size Records, numbered from 0. Record n starts at
60 // byte n*size. A record that was never written reads as zeros. An index is data
61 // that the day files can give again, so Records cuts a partial record at the
69 // Read returns record n, or false when the file does not reach it.
70 func (r *Records) Read(n int) ([]byte, bool, error) {
72 return nil, false, nil
75 record := make([]byte, r.size)
76 _, err := r.file.ReadAt(record, int64(n*r.size))
77 if err != nil && !errors.Is(err, io.EOF) {
78 return nil, false, fmt.Errorf("read index record %d: %w", n, err)
81 return record, true, nil
84 // Write writes record n.
85 func (r *Records) Write(n int, record []byte) error {
86 _, err := r.file.WriteAt(record, int64(n*r.size))
88 return fmt.Errorf("write index record %d: %w", n, err)
91 r.count = max(r.count, n+1)
96 // Append writes the record after the last one.
97 func (r *Records) Append(record []byte) error {
98 return r.Write(r.count, record)
101 // Close closes the file.
102 func (r *Records) Close() error {
103 return r.file.Close()