-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: check total blocks sent when theres a restart
- Loading branch information
Showing
7 changed files
with
303 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package cidsets | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/ipfs/go-datastore" | ||
"github.com/ipfs/go-datastore/namespace" | ||
"github.com/ipfs/go-datastore/query" | ||
|
||
"github.com/ipfs/go-cid" | ||
) | ||
|
||
type SetID string | ||
|
||
type CIDSetManager struct { | ||
ds datastore.Datastore | ||
} | ||
|
||
func NewCIDSetManager(ds datastore.Datastore) *CIDSetManager { | ||
return &CIDSetManager{ds: ds} | ||
} | ||
|
||
func (mgr *CIDSetManager) InsertSetCID(sid SetID, c cid.Cid) (bool, error) { | ||
return mgr.getSet(sid).Insert(c) | ||
} | ||
|
||
func (mgr *CIDSetManager) DeleteSet(sid SetID) error { | ||
return mgr.getSet(sid).Truncate() | ||
} | ||
|
||
func (mgr *CIDSetManager) getSet(sid SetID) *cidSet { | ||
// TODO: cache the wrapped DS instead of creating a new one every time | ||
return NewCIDSet(mgr.getSetDS(sid)) | ||
} | ||
|
||
func (mgr *CIDSetManager) getSetDS(sid SetID) datastore.Batching { | ||
return namespace.Wrap(mgr.ds, datastore.NewKey("/"+string(sid)+"/cids/")) | ||
} | ||
|
||
type cidSet struct { | ||
lk sync.Mutex | ||
ds datastore.Batching | ||
} | ||
|
||
func NewCIDSet(ds datastore.Batching) *cidSet { | ||
return &cidSet{ds: ds} | ||
} | ||
|
||
func (s *cidSet) Insert(c cid.Cid) (bool, error) { | ||
s.lk.Lock() | ||
defer s.lk.Unlock() | ||
|
||
k := datastore.NewKey(c.String()) | ||
has, err := s.ds.Has(k) | ||
if err != nil { | ||
return false, err | ||
} | ||
if has { | ||
return true, nil | ||
} | ||
return false, s.ds.Put(k, nil) | ||
} | ||
|
||
func (s *cidSet) Truncate() error { | ||
s.lk.Lock() | ||
defer s.lk.Unlock() | ||
|
||
res, err := s.ds.Query(query.Query{KeysOnly: true}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
entries, err := res.Rest() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
batched, err := s.ds.Batch() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, entry := range entries { | ||
err := batched.Delete(datastore.NewKey(entry.Key)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return batched.Commit() | ||
} |
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,47 @@ | ||
package cidsets | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/filecoin-project/go-data-transfer/testutil" | ||
ds "github.com/ipfs/go-datastore" | ||
ds_sync "github.com/ipfs/go-datastore/sync" | ||
) | ||
|
||
func TestCIDSetManager(t *testing.T) { | ||
cid1 := testutil.GenerateCids(1)[0] | ||
|
||
dstore := ds_sync.MutexWrap(ds.NewMapDatastore()) | ||
mgr := NewCIDSetManager(dstore) | ||
setID1 := SetID("set1") | ||
setID2 := SetID("set2") | ||
|
||
exists, err := mgr.InsertSetCID(setID1, cid1) | ||
require.NoError(t, err) | ||
require.False(t, exists) | ||
|
||
exists, err = mgr.InsertSetCID(setID1, cid1) | ||
require.NoError(t, err) | ||
require.True(t, exists) | ||
|
||
exists, err = mgr.InsertSetCID(setID2, cid1) | ||
require.NoError(t, err) | ||
require.False(t, exists) | ||
|
||
exists, err = mgr.InsertSetCID(setID2, cid1) | ||
require.NoError(t, err) | ||
require.True(t, exists) | ||
|
||
err = mgr.DeleteSet(setID1) | ||
require.NoError(t, err) | ||
|
||
exists, err = mgr.InsertSetCID(setID1, cid1) | ||
require.NoError(t, err) | ||
require.False(t, exists) | ||
|
||
exists, err = mgr.InsertSetCID(setID2, cid1) | ||
require.NoError(t, err) | ||
require.True(t, exists) | ||
} |
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
Oops, something went wrong.