-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sharedcounter): persist counter to disk
make a shared counter util for persisting a unique deal id counter to disk
- Loading branch information
1 parent
5ee324c
commit b996444
Showing
6 changed files
with
173 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package storedcounter | ||
|
||
import ( | ||
"encoding/binary" | ||
|
||
"github.com/ipfs/go-datastore" | ||
) | ||
|
||
// StoredCounter is a counter that persists to a datastore as it increments | ||
type StoredCounter struct { | ||
ds datastore.Datastore | ||
name datastore.Key | ||
} | ||
|
||
// New returns a new StoredCounter for the given datastore and key | ||
func New(ds datastore.Datastore, name datastore.Key) *StoredCounter { | ||
return &StoredCounter{ds, name} | ||
} | ||
|
||
// Next returns the next counter value, updating it on disk in the process | ||
// if no counter is present, it creates one and returns a 0 value | ||
func (sc *StoredCounter) Next() (uint64, error) { | ||
has, err := sc.ds.Has(sc.name) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
var next uint64 = 0 | ||
if has { | ||
curBytes, err := sc.ds.Get(sc.name) | ||
if err != nil { | ||
return 0, err | ||
} | ||
cur, _ := binary.Uvarint(curBytes) | ||
next = cur + 1 | ||
} | ||
buf := make([]byte, binary.MaxVarintLen64) | ||
size := binary.PutUvarint(buf, next) | ||
|
||
return next, sc.ds.Put(sc.name, buf[:size]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package storedcounter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ipfs/go-datastore" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/filecoin-project/go-fil-markets/storedcounter" | ||
) | ||
|
||
|
||
func TestStoredCounter(t * testing.T) { | ||
ds := datastore.NewMapDatastore() | ||
|
||
t.Run("test two instances with same data store and key count together", func(t *testing.T) { | ||
key := datastore.NewKey("counter") | ||
sc1 := storedcounter.New(ds, key) | ||
next, err := sc1.Next() | ||
require.NoError(t, err) | ||
require.Equal(t, next, uint64(0)) | ||
|
||
sc2 := storedcounter.New(ds, key) | ||
next, err = sc2.Next() | ||
require.NoError(t, err) | ||
require.Equal(t, next, uint64(1)) | ||
|
||
next, err = sc1.Next() | ||
require.NoError(t, err) | ||
require.Equal(t, next, uint64(2)) | ||
|
||
next, err = sc2.Next() | ||
require.NoError(t, err) | ||
require.Equal(t, next, uint64(3)) | ||
}) | ||
|
||
|
||
t.Run("test two instances with same data store but different keys count seperate", func(t *testing.T) { | ||
|
||
key1 := datastore.NewKey("counter 1") | ||
key2 := datastore.NewKey("counter 2") | ||
|
||
sc1 := storedcounter.New(ds, key1) | ||
next, err := sc1.Next() | ||
require.NoError(t, err) | ||
require.Equal(t, next, uint64(0)) | ||
|
||
sc2 := storedcounter.New(ds, key2) | ||
next, err = sc2.Next() | ||
require.NoError(t, err) | ||
require.Equal(t, next, uint64(0)) | ||
|
||
next, err = sc1.Next() | ||
require.NoError(t, err) | ||
require.Equal(t, next, uint64(1)) | ||
|
||
next, err = sc2.Next() | ||
require.NoError(t, err) | ||
require.Equal(t, next, uint64(1)) | ||
}) | ||
} |