Currently, the Node
type is defined as :
type Node map[string]Node
This makes all IPLD users very dependent on the implementation of the Node object, and forces IPLD to parse everything on the JSON/CBOR data to put it in the map. The alternative is to provide only a handle on the data and decode it on demand, and let the Node consumer store just the things he needs.
Say an application just need some small keys on many Node objects which otherwise are also containing huge chunks of data. We can imagine a mode of operation where the huge data is left on the disk while the file is merely traversed to look for the small keys the application needs. The big data shouldn't need to be kept on memory if we don't use it.
To allow this, I'd propose to make Node
an interface equipped with a walk function (that would walk the JSON-like data structure, not local paths as the current walk function does) :
type Node interface {
func Walk(WalkFun walker) error;
}
And then:
const TokenInt = 1 << iota;
const TokenFloat;
const TokenString;
const TokenObjectKey;
const TokenArrayIndex;
const SkipObject error;
const SkipValue error;
type WalkFun fun(err error, path []interface{}, tokenType int, token interface{}) error;
An example of decoding the following JSON:
Would result in the walk function being called this way:
WalkFun(nil, []string{}, TokenObjectKey, "a");
WalkFun(nil, []string{"a"}, TokenInt, 1);
WalkFun(nil, []string{}, TokenObjectKey, "b");
WalkFun(nil, []string{"b"}, TokenArrayIndex, 0);
WalkFun(nil, []string{"b", 0}, TokenString, "c");
WalkFun(nil, []string{"b"}, TokenArrayIndex, 1);
WalkFun(nil, []string{"b", 1}, TokenString, "d");
This would be a very low level block that will be used to implement higher level functions, like path walking within an object, or decoding to application specific formats.
To provide compatibility with ipfs/merkledag, we could add to the interface a few functions:
type NodeMdagCompatible {
Node;
func Links() []Link;
func Data() []byte;
}
The Links
and Data
function would traverse the object using the walk function to provide a list of links and the data bytes required for ipfs/merkledag users.