Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
Rename LoadPinner to New
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero committed Nov 24, 2020
1 parent a6d812c commit 9771671
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 52 deletions.
69 changes: 35 additions & 34 deletions dspinner/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,41 @@ type syncDAGService interface {
Sync() error
}

// New creates a new pinner and loads its keysets from the given datastore. If
// there is no data present in the datastore, then an empty pinner is returned.
func New(ctx context.Context, dstore ds.Datastore, dserv ipld.DAGService) (ipfspinner.Pinner, error) {
p := &pinner{
cidDIndex: dsindex.New(dstore, pinCidDIndexPath),
cidRIndex: dsindex.New(dstore, pinCidRIndexPath),
nameIndex: dsindex.New(dstore, pinNameIndexPath),
dserv: dserv,
dstore: dstore,
}

data, err := dstore.Get(dirtyKey)
if err != nil {
if err == ds.ErrNotFound {
return p, nil
}
return nil, fmt.Errorf("cannot load dirty flag: %v", err)
}
if data[0] == 1 {
p.dirty = 1

pins, err := p.loadAllPins(ctx)
if err != nil {
return nil, fmt.Errorf("cannot load pins: %v", err)
}

err = p.rebuildIndexes(ctx, pins)
if err != nil {
return nil, fmt.Errorf("cannot rebuild indexes: %v", err)
}
}

return p, nil
}

// Pin the given node, optionally recursive
func (p *pinner) Pin(ctx context.Context, node ipld.Node, recurse bool) error {
err := p.dserv.Add(ctx, node)
Expand Down Expand Up @@ -612,40 +647,6 @@ func (p *pinner) loadAllPins(ctx context.Context) ([]*pin, error) {
return pins, nil
}

// LoadPinner loads a pinner and its keysets from the given datastore
func LoadPinner(ctx context.Context, dstore ds.Datastore, dserv ipld.DAGService) (ipfspinner.Pinner, error) {
p := &pinner{
cidDIndex: dsindex.New(dstore, pinCidDIndexPath),
cidRIndex: dsindex.New(dstore, pinCidRIndexPath),
nameIndex: dsindex.New(dstore, pinNameIndexPath),
dserv: dserv,
dstore: dstore,
}

data, err := dstore.Get(dirtyKey)
if err != nil {
if err == ds.ErrNotFound {
return p, nil
}
return nil, fmt.Errorf("cannot load dirty flag: %v", err)
}
if data[0] == 1 {
p.dirty = 1

pins, err := p.loadAllPins(ctx)
if err != nil {
return nil, fmt.Errorf("cannot load pins: %v", err)
}

err = p.rebuildIndexes(ctx, pins)
if err != nil {
return nil, fmt.Errorf("cannot rebuild indexes: %v", err)
}
}

return p, nil
}

// rebuildIndexes uses the stored pins to rebuild secondary indexes. This
// resolves any discrepancy between secondary indexes and pins that could
// result from a program termination between saving the two.
Expand Down
36 changes: 18 additions & 18 deletions dspinner/pin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestPinnerBasic(t *testing.T) {

dserv := mdag.NewDAGService(bserv)

p, err := LoadPinner(ctx, dstore, dserv)
p, err := New(ctx, dstore, dserv)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -270,7 +270,7 @@ func TestPinnerBasic(t *testing.T) {
t.Fatal(err)
}

p, err = LoadPinner(ctx, dstore, dserv)
p, err = New(ctx, dstore, dserv)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -326,7 +326,7 @@ func TestRemovePinWithMode(t *testing.T) {

dserv := mdag.NewDAGService(bserv)

p, err := LoadPinner(ctx, dstore, dserv)
p, err := New(ctx, dstore, dserv)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -371,9 +371,9 @@ func TestIsPinnedLookup(t *testing.T) {

dserv := mdag.NewDAGService(bserv)

// Create new pinner. LoadPinner will not load anything since there are
// Create new pinner. New will not load anything since there are
// no pins saved in the datastore yet.
p, err := LoadPinner(ctx, dstore, dserv)
p, err := New(ctx, dstore, dserv)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -414,7 +414,7 @@ func TestDuplicateSemantics(t *testing.T) {

dserv := mdag.NewDAGService(bserv)

p, err := LoadPinner(ctx, dstore, dserv)
p, err := New(ctx, dstore, dserv)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -452,7 +452,7 @@ func TestFlush(t *testing.T) {
bserv := bs.New(bstore, offline.Exchange(bstore))

dserv := mdag.NewDAGService(bserv)
p, err := LoadPinner(ctx, dstore, dserv)
p, err := New(ctx, dstore, dserv)
if err != nil {
t.Fatal(err)
}
Expand All @@ -473,7 +473,7 @@ func TestPinRecursiveFail(t *testing.T) {
bserv := bs.New(bstore, offline.Exchange(bstore))
dserv := mdag.NewDAGService(bserv)

p, err := LoadPinner(ctx, dstore, dserv)
p, err := New(ctx, dstore, dserv)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -522,7 +522,7 @@ func TestPinUpdate(t *testing.T) {
bserv := bs.New(bstore, offline.Exchange(bstore))

dserv := mdag.NewDAGService(bserv)
p, err := LoadPinner(ctx, dstore, dserv)
p, err := New(ctx, dstore, dserv)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -590,7 +590,7 @@ func TestLoadDirty(t *testing.T) {
bserv := bs.New(bstore, offline.Exchange(bstore))
dserv := mdag.NewDAGService(bserv)

p, err := LoadPinner(ctx, dstore, dserv)
p, err := New(ctx, dstore, dserv)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -634,7 +634,7 @@ func TestLoadDirty(t *testing.T) {
}

// Create new pinner on same datastore that was never flushed.
p, err = LoadPinner(ctx, dstore, dserv)
p, err = New(ctx, dstore, dserv)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -862,7 +862,7 @@ func BenchmarkLoadRebuild(b *testing.B) {
defer cancel()

dstore, dserv := makeStore()
pinner, err := LoadPinner(ctx, dstore, dserv)
pinner, err := New(ctx, dstore, dserv)
if err != nil {
panic(err.Error())
}
Expand All @@ -874,7 +874,7 @@ func BenchmarkLoadRebuild(b *testing.B) {
for i := 0; i < b.N; i++ {
dstore.Put(dirtyKey, []byte{1})

_, err = LoadPinner(ctx, dstore, dserv)
_, err = New(ctx, dstore, dserv)
if err != nil {
panic(err.Error())
}
Expand All @@ -885,7 +885,7 @@ func BenchmarkLoadRebuild(b *testing.B) {
for i := 0; i < b.N; i++ {
dstore.Put(dirtyKey, []byte{0})

_, err = LoadPinner(ctx, dstore, dserv)
_, err = New(ctx, dstore, dserv)
if err != nil {
panic(err.Error())
}
Expand All @@ -898,7 +898,7 @@ func BenchmarkLoadRebuild(b *testing.B) {
// creating a pin in a larger number of existing pins.
func BenchmarkNthPin(b *testing.B) {
dstore, dserv := makeStore()
pinner, err := LoadPinner(context.Background(), dstore, dserv)
pinner, err := New(context.Background(), dstore, dserv)
if err != nil {
panic(err.Error())
}
Expand Down Expand Up @@ -953,7 +953,7 @@ func BenchmarkNPins(b *testing.B) {
for count := 128; count < 16386; count <<= 1 {
b.Run(fmt.Sprint("PinDS-", count), func(b *testing.B) {
dstore, dserv := makeStore()
pinner, err := LoadPinner(context.Background(), dstore, dserv)
pinner, err := New(context.Background(), dstore, dserv)
if err != nil {
panic(err.Error())
}
Expand Down Expand Up @@ -1000,7 +1000,7 @@ func BenchmarkNUnpins(b *testing.B) {
for count := 128; count < 16386; count <<= 1 {
b.Run(fmt.Sprint("UnpinDS-", count), func(b *testing.B) {
dstore, dserv := makeStore()
pinner, err := LoadPinner(context.Background(), dstore, dserv)
pinner, err := New(context.Background(), dstore, dserv)
if err != nil {
panic(err.Error())
}
Expand Down Expand Up @@ -1047,7 +1047,7 @@ func BenchmarkPinAll(b *testing.B) {
for count := 128; count < 16386; count <<= 1 {
b.Run(fmt.Sprint("PinAllDS-", count), func(b *testing.B) {
dstore, dserv := makeStore()
pinner, err := LoadPinner(context.Background(), dstore, dserv)
pinner, err := New(context.Background(), dstore, dserv)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 9771671

Please sign in to comment.