Skip to content

Commit

Permalink
vfs/atomicfs: add Marker NextIter method
Browse files Browse the repository at this point in the history
Add a NextIter method that allows clients to query an atomic marker for
its next iteration sequence number. In some usages, client code needs to
create a new file and then move the marker to the new file's path.
Creating the new file must not overrwrite the existing marked file.
Incorporating the marker's iteration number into the filename provides a
simple scheme client code may follow.
  • Loading branch information
jbowens committed Sep 7, 2021
1 parent 1f86284 commit 3f8702a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
9 changes: 8 additions & 1 deletion vfs/atomicfs/marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.

package vfs
package atomicfs

import (
"fmt"
Expand Down Expand Up @@ -195,6 +195,13 @@ func (a *Marker) Move(filename string) error {
return nil
}

// NextIter returns the next iteration number that the marker will use.
// Clients may use this number for formulating filenames that are
// unused.
func (a *Marker) NextIter() uint64 {
return a.iter + 1
}

// RemoveObsolete removes any obsolete files discovered while locating
// the marker or files unable to be removed during Move.
func (a *Marker) RemoveObsolete() error {
Expand Down
10 changes: 9 additions & 1 deletion vfs/atomicfs/marker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.

package vfs
package atomicfs

import (
"bytes"
Expand Down Expand Up @@ -117,6 +117,14 @@ func TestMarker(t *testing.T) {
}
return ""

case "next-iter":
var dir, marker string
td.ScanArgs(t, "dir", &dir)
td.ScanArgs(t, "marker", &marker)
m := markers[memFS.PathJoin(dir, marker)]
require.NotNil(t, m)
return fmt.Sprintf("%d", m.NextIter())

case "remove-obsolete":
var dir, marker string
td.ScanArgs(t, "dir", &dir)
Expand Down
16 changes: 16 additions & 0 deletions vfs/atomicfs/testdata/marker
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ mkdir-all data
locate dir=data marker=foo
----

next-iter dir=data marker=foo
----
1

next-iter dir=data marker=foo
----
1

# The directory should still be empty.
list data
----
Expand All @@ -23,11 +31,19 @@ list data
----
marker.foo.000001.MANIFEST-000010

next-iter dir=data marker=foo
----
2

# Moving the marker should move the existing marker file.
move dir=data marker=foo
MANIFEST-000016
----

next-iter dir=data marker=foo
----
3

list data
----
marker.foo.000002.MANIFEST-000016
Expand Down

0 comments on commit 3f8702a

Please sign in to comment.