Added back in some of the parser methods. (These were previously named "Cast" and I think that's silly and wrong so I fixed it.) Functions are named overly-literally with their type (e.g. ParseCidString and ParseCidStruct rather than ParseCid or even just Parse) because for this research package I don't want to bother with many sub-packages. (Maybe I'll regret this, but at the moment it seems simpler to hold back on sub-packages.) Functions that produce Cids are literal with their return types, as well. Part of the purpose of this research package is going to be to concretely benchmark exactly how much performance overhead there is to using interfaces (which will likely cause a lot of boxing and unboxing in practice) -- since we want to explore where this boxing happens and how much it costs, it's important that none of our basic implementation functions do the boxing! The entire set of codec enums came along in this commit. Ah well; they would have eventually anyway, I guess. But it's interesting to note the only thing that dragged them along so far is the reference to 'DagProtobuf' when constructing v0 CIDs; otherwise, this enum is quite unused here.
49 lines
1.8 KiB
Go
49 lines
1.8 KiB
Go
package cid
|
|
|
|
import (
|
|
mh "github.com/multiformats/go-multihash"
|
|
)
|
|
|
|
// Cid represents a self-describing content adressed identifier.
|
|
//
|
|
// A CID is composed of:
|
|
//
|
|
// - a Version of the CID itself,
|
|
// - a Multicodec (indicates the encoding of the referenced content),
|
|
// - and a Multihash (which identifies the referenced content).
|
|
//
|
|
// (Note that the Multihash further contains its own version and hash type
|
|
// indicators.)
|
|
type Cid interface {
|
|
// n.b. 'yields' means "without copy", 'produces' means a malloc.
|
|
|
|
Version() uint64 // Yields the version prefix as a uint.
|
|
Multicodec() uint64 // Yields the multicodec as a uint.
|
|
Multihash() mh.Multihash // Yields the multihash segment.
|
|
|
|
String() string // Produces the CID formatted as b58 string.
|
|
Bytes() []byte // Produces the CID formatted as raw binary.
|
|
|
|
Prefix() Prefix // Produces a tuple of non-content metadata.
|
|
|
|
// some change notes:
|
|
// - `KeyString() CidString` is gone because we're natively a map key now, you're welcome.
|
|
// - `StringOfBase(mbase.Encoding) (string, error)` is skipped, maybe it can come back but maybe it should be a formatter's job.
|
|
// - `Equals(o Cid) bool` is gone because it's now `==`, you're welcome.
|
|
|
|
// TODO: make a multi-return method for {v,mc,mh} decomposition. CidStr will be able to implement this more efficiently than if one makes a series of the individual getter calls.
|
|
}
|
|
|
|
// Prefix represents all the metadata of a Cid,
|
|
// that is, the Version, the Codec, the Multihash type
|
|
// and the Multihash length. It does not contains
|
|
// any actual content information.
|
|
// NOTE: The use -1 in MhLength to mean default length is deprecated,
|
|
// use the V0Builder or V1Builder structures instead
|
|
type Prefix struct {
|
|
Version uint64
|
|
Codec uint64
|
|
MhType uint64
|
|
MhLength int
|
|
}
|