]> git.giacomolli.uk - onechat/blobdiff - journal.go
Keep recovery advice out of log events
[onechat] / journal.go
index c12f7d089be403eb706f362843dd482961560737..745ae790089ec1f5a9bf98debe7e17d212537859 100644 (file)
@@ -71,16 +71,33 @@ func (j *Journal) Write(day string, line []byte) (Position, error) {
                offset: j.size,
                length: uint32(len(line)),
        }
                offset: j.size,
                length: uint32(len(line)),
        }
+
        j.size += uint64(len(line))
 
        return place, nil
 }
 
        j.size += uint64(len(line))
 
        return place, nil
 }
 
+func (j *Journal) closeWriter() error {
+       if j.file == nil {
+               return nil
+       }
+
+       err := j.file.Close()
+       j.file = nil
+       j.day = 0
+       if err != nil {
+               return fmt.Errorf("close day file: %w", err)
+       }
+
+       return nil
+}
+
 // Read returns the line at place without its newline.
 func (j *Journal) Read(place Position) ([]byte, error) {
        reader, open := j.readers[place.day]
        if !open {
                var err error
 // Read returns the line at place without its newline.
 func (j *Journal) Read(place Position) ([]byte, error) {
        reader, open := j.readers[place.day]
        if !open {
                var err error
+
                reader, err = os.Open(filepath.Join(j.dir, journalDayName(place.day)+".jsonl"))
                if err != nil {
                        return nil, fmt.Errorf("open day file: %w", err)
                reader, err = os.Open(filepath.Join(j.dir, journalDayName(place.day)+".jsonl"))
                if err != nil {
                        return nil, fmt.Errorf("open day file: %w", err)
@@ -111,6 +128,7 @@ func (j *Journal) Close() error {
        err := j.closeWriter()
        for day, reader := range j.readers {
                err = errors.Join(err, reader.Close())
        err := j.closeWriter()
        for day, reader := range j.readers {
                err = errors.Join(err, reader.Close())
+
                delete(j.readers, day)
        }
 
                delete(j.readers, day)
        }
 
@@ -128,6 +146,7 @@ func (j *Journal) Scan(ctx context.Context, place Position, add func([]byte, Pos
        }
 
        slices.Sort(paths)
        }
 
        slices.Sort(paths)
+
        for n, path := range paths {
                day, err := journalDayNumber(strings.TrimSuffix(filepath.Base(path), ".jsonl"))
                if err != nil {
        for n, path := range paths {
                day, err := journalDayNumber(strings.TrimSuffix(filepath.Base(path), ".jsonl"))
                if err != nil {
@@ -152,21 +171,6 @@ func (j *Journal) Scan(ctx context.Context, place Position, add func([]byte, Pos
        return nil
 }
 
        return nil
 }
 
-func (j *Journal) closeWriter() error {
-       if j.file == nil {
-               return nil
-       }
-
-       err := j.file.Close()
-       j.file = nil
-       j.day = 0
-       if err != nil {
-               return fmt.Errorf("close day file: %w", err)
-       }
-
-       return nil
-}
-
 // journalDayNumber returns the day YYYY-MM-DD as the number YYYYMMDD.
 func journalDayNumber(day string) (uint32, error) {
        number, err := strconv.ParseUint(strings.ReplaceAll(day, "-", ""), 10, 32)
 // journalDayNumber returns the day YYYY-MM-DD as the number YYYYMMDD.
 func journalDayNumber(day string) (uint32, error) {
        number, err := strconv.ParseUint(strings.ReplaceAll(day, "-", ""), 10, 32)