-
Notifications
You must be signed in to change notification settings - Fork 42
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
Unexport unecesserry index APIs and integrate with mulitcodec #151
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ import ( | |
"io" | ||
"os" | ||
|
||
"github.com/multiformats/go-multicodec" | ||
|
||
"github.com/multiformats/go-varint" | ||
|
||
internalio "github.com/ipld/go-car/v2/internal/io" | ||
|
@@ -16,22 +18,14 @@ import ( | |
|
||
// Codec table is a first var-int in CAR indexes | ||
const ( | ||
IndexSorted Codec = 0x0400 // as per https://github.com/multiformats/multicodec/pull/220 | ||
|
||
// TODO: unexport these before the final release, probably | ||
IndexHashed Codec = 0x300000 + iota | ||
IndexSingleSorted | ||
IndexGobHashed | ||
IndexInsertion | ||
indexHashed codec = 0x300000 + iota | ||
indexSingleSorted | ||
indexGobHashed | ||
) | ||
|
||
type ( | ||
// Codec is used as a multicodec identifier for CAR index files | ||
// TODO: use go-multicodec before the final release | ||
Codec int | ||
|
||
// Builder is a constructor for an index type | ||
Builder func() Index | ||
// codec is used as a multicodec identifier for CAR index files | ||
codec int | ||
|
||
// Record is a pre-processed record of a car item and location. | ||
Record struct { | ||
|
@@ -41,22 +35,22 @@ type ( | |
|
||
// Index provides an interface for looking up byte offset of a given CID. | ||
Index interface { | ||
Codec() Codec | ||
Codec() multicodec.Code | ||
Marshal(w io.Writer) error | ||
Unmarshal(r io.Reader) error | ||
Get(cid.Cid) (uint64, error) | ||
Load([]Record) error | ||
} | ||
) | ||
|
||
// BuildersByCodec holds known index formats | ||
// TODO: turn this into a func before the final release? | ||
var BuildersByCodec = map[Codec]Builder{ | ||
IndexHashed: mkHashed, | ||
IndexSorted: mkSorted, | ||
IndexSingleSorted: mkSingleSorted, | ||
IndexGobHashed: mkGobHashed, | ||
IndexInsertion: mkInsertion, | ||
// NewFromCodec constructs a new index corresponding to the given codec. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "given codec" is a bit too general IMO, as you only accept the CAR index codecs. How about "to the given CAR index codec"? |
||
func NewFromCodec(codec multicodec.Code) (Index, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if just And |
||
switch codec { | ||
case multicodec.CarIndexSorted: | ||
return newSorted(), nil | ||
default: | ||
return nil, fmt.Errorf("unknwon index codec: %v", codec) | ||
} | ||
} | ||
|
||
// Save writes a generated index into the given `path`. | ||
|
@@ -97,15 +91,15 @@ func WriteTo(idx Index, w io.Writer) error { | |
// Returns error if the encoding is not known. | ||
func ReadFrom(r io.Reader) (Index, error) { | ||
reader := bufio.NewReader(r) | ||
codec, err := varint.ReadUvarint(reader) | ||
code, err := varint.ReadUvarint(reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
builder, ok := BuildersByCodec[Codec(codec)] | ||
if !ok { | ||
return nil, fmt.Errorf("unknown codec: %d", codec) | ||
codec := multicodec.Code(code) | ||
idx, err := NewFromCodec(codec) | ||
if err != nil { | ||
return nil, err | ||
} | ||
idx := builder() | ||
if err := idx.Unmarshal(reader); err != nil { | ||
return nil, err | ||
} | ||
|
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.
surely this can be replaced by go-multicodec now?
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.
this is the insertion index codec. Do we have a codec for that? I thought we only had one for sorted index.
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.
Replaced this specific constant with a wrapped
multicodec.Code
. Please let me know if I missed anything. thanksThere 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.
Ah of course, I had misread the name. SGTM