-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement new index type that also includes mutltihash code #217
Merged
+383
−18
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package index | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
"sort" | ||
|
||
"github.com/ipfs/go-cid" | ||
"github.com/multiformats/go-multicodec" | ||
"github.com/multiformats/go-multihash" | ||
) | ||
|
||
type ( | ||
// multihashIndexSorted maps multihash code (i.e. hashing algorithm) to multiWidthCodedIndex. | ||
// This index ignores any Record with multihash.IDENTITY. | ||
multihashIndexSorted map[uint64]*multiWidthCodedIndex | ||
// multiWidthCodedIndex stores multihash code for each multiWidthIndex. | ||
multiWidthCodedIndex struct { | ||
multiWidthIndex | ||
code uint64 | ||
} | ||
) | ||
|
||
func newMultiWidthCodedIndex() *multiWidthCodedIndex { | ||
return &multiWidthCodedIndex{ | ||
multiWidthIndex: make(multiWidthIndex), | ||
} | ||
} | ||
|
||
func (m *multiWidthCodedIndex) Marshal(w io.Writer) error { | ||
if err := binary.Write(w, binary.LittleEndian, m.code); err != nil { | ||
return err | ||
} | ||
return m.multiWidthIndex.Marshal(w) | ||
} | ||
|
||
func (m *multiWidthCodedIndex) Unmarshal(r io.Reader) error { | ||
if err := binary.Read(r, binary.LittleEndian, &m.code); err != nil { | ||
return err | ||
} | ||
return m.multiWidthIndex.Unmarshal(r) | ||
} | ||
|
||
func (m *multihashIndexSorted) Codec() multicodec.Code { | ||
return multicodec.CarMultihashIndexSorted | ||
} | ||
|
||
func (m *multihashIndexSorted) Marshal(w io.Writer) error { | ||
if err := binary.Write(w, binary.LittleEndian, int32(len(*m))); err != nil { | ||
return err | ||
} | ||
// The codes are unique, but ranging over a map isn't deterministic. | ||
// As per the CARv2 spec, we must order buckets by digest length. | ||
// TODO update CARv2 spec to reflect this for the new index type. | ||
codes := m.sortedMultihashCodes() | ||
|
||
for _, code := range codes { | ||
mwci := (*m)[code] | ||
if err := mwci.Marshal(w); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (m *multihashIndexSorted) sortedMultihashCodes() []uint64 { | ||
codes := make([]uint64, 0, len(*m)) | ||
for code := range *m { | ||
codes = append(codes, code) | ||
} | ||
sort.Slice(codes, func(i, j int) bool { | ||
return codes[i] < codes[j] | ||
}) | ||
return codes | ||
} | ||
|
||
func (m *multihashIndexSorted) Unmarshal(r io.Reader) error { | ||
var l int32 | ||
if err := binary.Read(r, binary.LittleEndian, &l); err != nil { | ||
return err | ||
} | ||
for i := 0; i < int(l); i++ { | ||
mwci := newMultiWidthCodedIndex() | ||
if err := mwci.Unmarshal(r); err != nil { | ||
return err | ||
} | ||
m.put(mwci) | ||
} | ||
return nil | ||
} | ||
|
||
func (m *multihashIndexSorted) put(mwci *multiWidthCodedIndex) { | ||
(*m)[mwci.code] = mwci | ||
} | ||
|
||
func (m *multihashIndexSorted) Load(records []Record) error { | ||
// TODO optimize load by avoiding multihash decoding twice. | ||
// This implementation decodes multihashes twice: once here to group by code, and once in the | ||
// internals of multiWidthIndex to group by digest length. The code can be optimized by | ||
// combining the grouping logic into one step where the multihash of a CID is only decoded once. | ||
// The optimization would need refactoring of the IndexSorted compaction logic. | ||
|
||
// Group records by multihash code | ||
byCode := make(map[uint64][]Record) | ||
for _, record := range records { | ||
dmh, err := multihash.Decode(record.Hash()) | ||
if err != nil { | ||
return err | ||
} | ||
code := dmh.Code | ||
// Ignore IDENTITY multihash in the index. | ||
if code == multihash.IDENTITY { | ||
continue | ||
} | ||
recsByCode, ok := byCode[code] | ||
if !ok { | ||
recsByCode = make([]Record, 0) | ||
byCode[code] = recsByCode | ||
} | ||
byCode[code] = append(recsByCode, record) | ||
} | ||
|
||
// Load each record group. | ||
for code, recsByCode := range byCode { | ||
mwci := newMultiWidthCodedIndex() | ||
mwci.code = code | ||
if err := mwci.Load(recsByCode); err != nil { | ||
return err | ||
} | ||
m.put(mwci) | ||
} | ||
return nil | ||
} | ||
|
||
func (m *multihashIndexSorted) GetAll(cid cid.Cid, f func(uint64) bool) error { | ||
hash := cid.Hash() | ||
dmh, err := multihash.Decode(hash) | ||
if err != nil { | ||
return err | ||
} | ||
mwci, err := m.get(dmh) | ||
if err != nil { | ||
return err | ||
} | ||
return mwci.GetAll(cid, f) | ||
} | ||
|
||
func (m *multihashIndexSorted) get(dmh *multihash.DecodedMultihash) (*multiWidthCodedIndex, error) { | ||
if codedIdx, ok := (*m)[dmh.Code]; ok { | ||
return codedIdx, nil | ||
} | ||
return nil, ErrNotFound | ||
} | ||
|
||
func newMultihashSorted() Index { | ||
index := make(multihashIndexSorted) | ||
return &index | ||
} |
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,107 @@ | ||
package index_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/multiformats/go-multicodec" | ||
|
||
"github.com/ipfs/go-cid" | ||
"github.com/ipld/go-car/v2/index" | ||
"github.com/multiformats/go-multihash" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMutilhashSortedIndex_Codec(t *testing.T) { | ||
subject, err := index.New(multicodec.CarMultihashIndexSorted) | ||
require.NoError(t, err) | ||
require.Equal(t, multicodec.CarMultihashIndexSorted, subject.Codec()) | ||
} | ||
|
||
func TestMultiWidthCodedIndex_LoadDoesNotLoadIdentityMultihash(t *testing.T) { | ||
rng := rand.New(rand.NewSource(1413)) | ||
identityRecords := generateIndexRecords(t, multihash.IDENTITY, rng) | ||
nonIdentityRecords := generateIndexRecords(t, multihash.SHA2_256, rng) | ||
records := append(identityRecords, nonIdentityRecords...) | ||
|
||
subject, err := index.New(multicodec.CarMultihashIndexSorted) | ||
require.NoError(t, err) | ||
err = subject.Load(records) | ||
require.NoError(t, err) | ||
|
||
// Assert index does not contain any records with IDENTITY multihash code. | ||
for _, r := range identityRecords { | ||
wantCid := r.Cid | ||
err = subject.GetAll(wantCid, func(o uint64) bool { | ||
require.Fail(t, "subject should not contain any records with IDENTITY multihash code") | ||
return false | ||
}) | ||
require.Equal(t, index.ErrNotFound, err) | ||
} | ||
|
||
// Assert however, index does contain the non IDENTITY records. | ||
requireContainsAll(t, subject, nonIdentityRecords) | ||
} | ||
|
||
func TestMultiWidthCodedIndex_MarshalUnmarshal(t *testing.T) { | ||
rng := rand.New(rand.NewSource(1413)) | ||
records := generateIndexRecords(t, multihash.SHA2_256, rng) | ||
|
||
// Create a new mh sorted index and load randomly generated records into it. | ||
subject, err := index.New(multicodec.CarMultihashIndexSorted) | ||
require.NoError(t, err) | ||
err = subject.Load(records) | ||
require.NoError(t, err) | ||
|
||
// Marshal the index. | ||
buf := new(bytes.Buffer) | ||
err = subject.Marshal(buf) | ||
require.NoError(t, err) | ||
|
||
// Unmarshal it back to another instance of mh sorted index. | ||
umSubject, err := index.New(multicodec.CarMultihashIndexSorted) | ||
require.NoError(t, err) | ||
err = umSubject.Unmarshal(buf) | ||
require.NoError(t, err) | ||
|
||
// Assert original records are present in both index instances with expected offset. | ||
requireContainsAll(t, subject, records) | ||
requireContainsAll(t, umSubject, records) | ||
} | ||
|
||
func generateIndexRecords(t *testing.T, hasherCode uint64, rng *rand.Rand) []index.Record { | ||
var records []index.Record | ||
recordCount := rng.Intn(99) + 1 // Up to 100 records | ||
for i := 0; i < recordCount; i++ { | ||
records = append(records, index.Record{ | ||
Cid: generateCidV1(t, hasherCode, rng), | ||
Offset: rng.Uint64(), | ||
}) | ||
} | ||
return records | ||
} | ||
|
||
func generateCidV1(t *testing.T, hasherCode uint64, rng *rand.Rand) cid.Cid { | ||
data := []byte(fmt.Sprintf("🌊d-%d", rng.Uint64())) | ||
mh, err := multihash.Sum(data, hasherCode, -1) | ||
require.NoError(t, err) | ||
return cid.NewCidV1(cid.Raw, mh) | ||
} | ||
|
||
func requireContainsAll(t *testing.T, subject index.Index, nonIdentityRecords []index.Record) { | ||
for _, r := range nonIdentityRecords { | ||
wantCid := r.Cid | ||
wantOffset := r.Offset | ||
|
||
var gotOffsets []uint64 | ||
err := subject.GetAll(wantCid, func(o uint64) bool { | ||
gotOffsets = append(gotOffsets, o) | ||
return false | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(gotOffsets)) | ||
require.Equal(t, wantOffset, gotOffsets[0]) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Postponing the optimisation here to reduce the size of this PR