Skip to content
This repository was archived by the owner on Aug 13, 2019. It is now read-only.

Commit 896a642

Browse files
author
Fabian Reinartz
committed
Add Replace function
Signed-off-by: Fabian Reinartz <[email protected]>
1 parent b81e0fb commit 896a642

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

checkpoint.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type CheckpointStats struct {
3737
DroppedTombstones int
3838
TotalSeries int // Processed series including dropped ones.
3939
TotalSamples int // Processed samples inlcuding dropped ones.
40-
TotalTombstones int // Processed tombstones including droppes ones.
40+
TotalTombstones int // Processed tombstones including dropped ones.
4141
}
4242

4343
// LastCheckpoint returns the directory name of the most recent checkpoint.
@@ -260,8 +260,8 @@ func Checkpoint(logger log.Logger, w *wal.WAL, m, n int, keep func(id uint64) bo
260260
if err := cp.Close(); err != nil {
261261
return nil, errors.Wrap(err, "close checkpoint")
262262
}
263-
if err := fileutil.Rename(cpdirtmp, cpdir); err != nil {
264-
return nil, errors.Wrap(err, "rename checkpoint file")
263+
if err := fileutil.Replace(cpdirtmp, cpdir); err != nil {
264+
return nil, errors.Wrap(err, "rename checkpoint directory")
265265
}
266266
if err := w.Truncate(n + 1); err != nil {
267267
// If truncating fails, we'll just try again at the next checkpoint.

fileutil/fileutil.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,26 @@ func Rename(from, to string) error {
4343
}
4444
return pdir.Close()
4545
}
46+
47+
// Replace moves a file or directory to a new location and deletes any previous data.
48+
// It is not atomic.
49+
func Replace(from, to string) error {
50+
if err := os.RemoveAll(to); err != nil {
51+
return nil
52+
}
53+
if err := os.Rename(from, to); err != nil {
54+
return err
55+
}
56+
57+
// Directory was renamed; sync parent dir to persist rename.
58+
pdir, err := OpenDir(filepath.Dir(to))
59+
if err != nil {
60+
return err
61+
}
62+
63+
if err = Fsync(pdir); err != nil {
64+
pdir.Close()
65+
return err
66+
}
67+
return pdir.Close()
68+
}

wal/wal.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,11 @@ func (w *WAL) flushPage(clear bool) error {
393393
type recType uint8
394394

395395
const (
396-
recPageTerm recType = 0 // rest of page is empty
397-
recFull recType = 1 // full record
398-
recFirst recType = 2 // first fragment of a record
399-
recMiddle recType = 3 // middle fragments of a record
400-
recLast recType = 4 // final fragment of a record
396+
recPageTerm recType = 0 // Rest of page is empty.
397+
recFull recType = 1 // Full record.
398+
recFirst recType = 2 // First fragment of a record.
399+
recMiddle recType = 3 // Middle fragments of a record.
400+
recLast recType = 4 // Final fragment of a record.
401401
)
402402

403403
func (t recType) String() string {
@@ -442,8 +442,8 @@ func (w *WAL) log(rec []byte, final bool) error {
442442
// If the record is too big to fit within pages in the current
443443
// segment, terminate the active segment and advance to the next one.
444444
// This ensures that records do not cross segment boundaries.
445-
left := w.page.remaining() - recordHeaderSize // active page
446-
left += (pageSize - recordHeaderSize) * (w.pagesPerSegment() - w.donePages - 1) // free pages
445+
left := w.page.remaining() - recordHeaderSize // Active pages.
446+
left += (pageSize - recordHeaderSize) * (w.pagesPerSegment() - w.donePages - 1) // Free pages.
447447

448448
if len(rec) > left {
449449
if err := w.nextSegment(); err != nil {
@@ -716,7 +716,7 @@ func (r *Reader) next() (err error) {
716716
// It's not strictly necessary but may catch sketchy state early.
717717
k := pageSize - (r.total % pageSize)
718718
if k == pageSize {
719-
continue // initial 0 byte was last page byte
719+
continue // Initial 0 byte was last page byte.
720720
}
721721
n, err := io.ReadFull(r.rdr, buf[:k])
722722
if err != nil {

0 commit comments

Comments
 (0)