From 3f8702a60cc1125060f6dca7f77a73f918d5c1be Mon Sep 17 00:00:00 2001 From: Jackson Owens Date: Tue, 7 Sep 2021 11:37:39 -0400 Subject: [PATCH] vfs/atomicfs: add Marker NextIter method 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. --- vfs/atomicfs/marker.go | 9 ++++++++- vfs/atomicfs/marker_test.go | 10 +++++++++- vfs/atomicfs/testdata/marker | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/vfs/atomicfs/marker.go b/vfs/atomicfs/marker.go index 99bf60243c..22bbd554a0 100644 --- a/vfs/atomicfs/marker.go +++ b/vfs/atomicfs/marker.go @@ -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" @@ -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 { diff --git a/vfs/atomicfs/marker_test.go b/vfs/atomicfs/marker_test.go index 37d03f02bb..5668d25394 100644 --- a/vfs/atomicfs/marker_test.go +++ b/vfs/atomicfs/marker_test.go @@ -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" @@ -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) diff --git a/vfs/atomicfs/testdata/marker b/vfs/atomicfs/testdata/marker index ffb7a7a71a..2450bd24bf 100644 --- a/vfs/atomicfs/testdata/marker +++ b/vfs/atomicfs/testdata/marker @@ -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 ---- @@ -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