X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/a5c83e92d7feea2e104b5515043a76c8c4c56c60..f5c601f1801aaa276f8c335fcb51c373edc0b50c:/records.go diff --git a/records.go b/records.go index abd88a8..52824cb 100644 --- a/records.go +++ b/records.go @@ -11,6 +11,16 @@ import ( // positionBytes is the size of an encoded position. const positionBytes = 16 +// Records is a file of fixed-size Records, numbered from 0. Record n starts at +// byte n*size. A record that was never written reads as zeros. An index is data +// that the day files can give again, so Records cuts a partial record at the +// end of the file. +type Records struct { + file *os.File + size int + count int +} + // openRecords opens or creates the record file at path with records of size // bytes. func openRecords(path string, size int) (*Records, error) { @@ -40,32 +50,6 @@ func openRecords(path string, size int) (*Records, error) { return r, nil } -// encodePosition writes place into the first positionBytes bytes of record. -func encodePosition(record []byte, place Position) { - binary.LittleEndian.PutUint32(record[0:], place.day) - binary.LittleEndian.PutUint32(record[4:], place.length) - binary.LittleEndian.PutUint64(record[8:], place.offset) -} - -// decodePosition reads a position from the first positionBytes bytes of record. -func decodePosition(record []byte) Position { - return Position{ - day: binary.LittleEndian.Uint32(record[0:]), - length: binary.LittleEndian.Uint32(record[4:]), - offset: binary.LittleEndian.Uint64(record[8:]), - } -} - -// Records is a file of fixed-size Records, numbered from 0. Record n starts at -// byte n*size. A record that was never written reads as zeros. An index is data -// that the day files can give again, so Records cuts a partial record at the -// end of the file. -type Records struct { - file *os.File - size int - count int -} - // Read returns record n, or false when the file does not reach it. func (r *Records) Read(n int) ([]byte, bool, error) { if n >= r.count { @@ -102,3 +86,19 @@ func (r *Records) Append(record []byte) error { func (r *Records) Close() error { return r.file.Close() } + +// encodePosition writes place into the first positionBytes bytes of record. +func encodePosition(record []byte, place Position) { + binary.LittleEndian.PutUint32(record[0:], place.day) + binary.LittleEndian.PutUint32(record[4:], place.length) + binary.LittleEndian.PutUint64(record[8:], place.offset) +} + +// decodePosition reads a position from the first positionBytes bytes of record. +func decodePosition(record []byte) Position { + return Position{ + day: binary.LittleEndian.Uint32(record[0:]), + length: binary.LittleEndian.Uint32(record[4:]), + offset: binary.LittleEndian.Uint64(record[8:]), + } +}