From 21074bc355524ceb3675dc93579e219746c1b567 Mon Sep 17 00:00:00 2001 From: Will Scott Date: Fri, 10 Sep 2021 06:38:47 -0700 Subject: [PATCH 1/3] add interface describing iteration --- v2/index/index.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/v2/index/index.go b/v2/index/index.go index 071e8e6e..320751cc 100644 --- a/v2/index/index.go +++ b/v2/index/index.go @@ -8,6 +8,7 @@ import ( internalio "github.com/ipld/go-car/v2/internal/io" "github.com/multiformats/go-multicodec" + "github.com/multiformats/go-multihash" "github.com/multiformats/go-varint" @@ -58,6 +59,20 @@ type ( // ErrNotFound is returned. GetAll(cid.Cid, func(uint64) bool) error } + + // IterableIndex extends Index in cases where the Index is able to + // provide an iterator for getting the list of all entries in the + // index. + IterableIndex interface { + // ForEach takes a callback function that will be called + // on each entry in the index. The arguments to the callback are + // the multihash of the element, and the offset in the car file + // where the element appears. + // + // If the callback returns a non-nil error, the iteration is aborted, + // and the ForEach function returns the error to the user. + ForEach(func(multihash.Multihash, uint64) error) error + } ) // GetFirst is a wrapper over Index.GetAll, returning the offset for the first From 1f3b28b81666db275f70129b4b22597cca16afe6 Mon Sep 17 00:00:00 2001 From: Will Scott Date: Fri, 10 Sep 2021 06:43:25 -0700 Subject: [PATCH 2/3] InterableIndex is superset of Index --- v2/index/index.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/v2/index/index.go b/v2/index/index.go index 320751cc..8abd1d3a 100644 --- a/v2/index/index.go +++ b/v2/index/index.go @@ -64,6 +64,8 @@ type ( // provide an iterator for getting the list of all entries in the // index. IterableIndex interface { + Index + // ForEach takes a callback function that will be called // on each entry in the index. The arguments to the callback are // the multihash of the element, and the offset in the car file From 2d2568297f2630e0954909fd8b5de5300934efc4 Mon Sep 17 00:00:00 2001 From: Will Scott Date: Fri, 10 Sep 2021 06:47:12 -0700 Subject: [PATCH 3/3] assertion of new interface --- v2/index/mhindexsorted.go | 1 + 1 file changed, 1 insertion(+) diff --git a/v2/index/mhindexsorted.go b/v2/index/mhindexsorted.go index 75d30971..15a731c8 100644 --- a/v2/index/mhindexsorted.go +++ b/v2/index/mhindexsorted.go @@ -11,6 +11,7 @@ import ( ) var _ Index = (*MultihashIndexSorted)(nil) +var _ IterableIndex = (*MultihashIndexSorted)(nil) type ( // MultihashIndexSorted maps multihash code (i.e. hashing algorithm) to multiWidthCodedIndex.