|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const CID = require('cids') |
| 4 | +const protobuf = require('protons') |
| 5 | +const fnv1a = require('fnv1a') |
| 6 | +const varint = require('varint') |
| 7 | +const dagpb = require('ipld-dag-pb') |
| 8 | +const { DAGNode, DAGLink } = dagpb |
| 9 | +const multicodec = require('multicodec') |
| 10 | +const pbSchema = require('./pin.proto') |
| 11 | +const { Buffer } = require('buffer') |
| 12 | +const { cidToKey, DEFAULT_FANOUT, MAX_ITEMS, EMPTY_KEY } = require('./utils') |
| 13 | + |
| 14 | +const pb = protobuf(pbSchema) |
| 15 | + |
| 16 | +function toB58String (hash) { |
| 17 | + return new CID(hash).toBaseEncodedString() |
| 18 | +} |
| 19 | + |
| 20 | +function readHeader (rootNode) { |
| 21 | + // rootNode.data should be a buffer of the format: |
| 22 | + // < varint(headerLength) | header | itemData... > |
| 23 | + const rootData = rootNode.Data |
| 24 | + const hdrLength = varint.decode(rootData) |
| 25 | + const vBytes = varint.decode.bytes |
| 26 | + |
| 27 | + if (vBytes <= 0) { |
| 28 | + throw new Error('Invalid Set header length') |
| 29 | + } |
| 30 | + |
| 31 | + if (vBytes + hdrLength > rootData.length) { |
| 32 | + throw new Error('Impossibly large set header length') |
| 33 | + } |
| 34 | + |
| 35 | + const hdrSlice = rootData.slice(vBytes, hdrLength + vBytes) |
| 36 | + const header = pb.Set.decode(hdrSlice) |
| 37 | + |
| 38 | + if (header.version !== 1) { |
| 39 | + throw new Error(`Unsupported Set version: ${header.version}`) |
| 40 | + } |
| 41 | + |
| 42 | + if (header.fanout > rootNode.Links.length) { |
| 43 | + throw new Error('Impossibly large fanout') |
| 44 | + } |
| 45 | + |
| 46 | + return { |
| 47 | + header: header, |
| 48 | + data: rootData.slice(hdrLength + vBytes) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function hash (seed, key) { |
| 53 | + const buf = Buffer.alloc(4) |
| 54 | + buf.writeUInt32LE(seed, 0) |
| 55 | + const data = Buffer.concat([ |
| 56 | + buf, Buffer.from(toB58String(key)) |
| 57 | + ]) |
| 58 | + return fnv1a(data.toString('binary')) |
| 59 | +} |
| 60 | + |
| 61 | +async function * walkItems (blockstore, node) { |
| 62 | + const pbh = readHeader(node) |
| 63 | + let idx = 0 |
| 64 | + |
| 65 | + for (const link of node.Links) { |
| 66 | + if (idx < pbh.header.fanout) { |
| 67 | + // the first pbh.header.fanout links are fanout bins |
| 68 | + // if a fanout bin is not 'empty', dig into and walk its DAGLinks |
| 69 | + const linkHash = link.Hash |
| 70 | + |
| 71 | + if (!EMPTY_KEY.equals(linkHash.buffer)) { |
| 72 | + // walk the links of this fanout bin |
| 73 | + const buf = await blockstore.get(cidToKey(linkHash)) |
| 74 | + const node = dagpb.util.deserialize(buf) |
| 75 | + |
| 76 | + yield * walkItems(blockstore, node) |
| 77 | + } |
| 78 | + } else { |
| 79 | + // otherwise, the link is a pin |
| 80 | + yield link.Hash |
| 81 | + } |
| 82 | + |
| 83 | + idx++ |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +async function * loadSet (blockstore, rootNode, name) { |
| 88 | + const link = rootNode.Links.find(l => l.Name === name) |
| 89 | + |
| 90 | + if (!link) { |
| 91 | + throw new Error('No link found with name ' + name) |
| 92 | + } |
| 93 | + |
| 94 | + const buf = await blockstore.get(cidToKey(link.Hash)) |
| 95 | + const node = dagpb.util.deserialize(buf) |
| 96 | + |
| 97 | + yield * walkItems(blockstore, node) |
| 98 | +} |
| 99 | + |
| 100 | +function storeItems (blockstore, items) { |
| 101 | + return storePins(items, 0) |
| 102 | + |
| 103 | + async function storePins (pins, depth) { |
| 104 | + const pbHeader = pb.Set.encode({ |
| 105 | + version: 1, |
| 106 | + fanout: DEFAULT_FANOUT, |
| 107 | + seed: depth |
| 108 | + }) |
| 109 | + const headerBuf = Buffer.concat([ |
| 110 | + Buffer.from(varint.encode(pbHeader.length)), pbHeader |
| 111 | + ]) |
| 112 | + const fanoutLinks = [] |
| 113 | + |
| 114 | + for (let i = 0; i < DEFAULT_FANOUT; i++) { |
| 115 | + fanoutLinks.push(new DAGLink('', 1, EMPTY_KEY)) |
| 116 | + } |
| 117 | + |
| 118 | + if (pins.length <= MAX_ITEMS) { |
| 119 | + const nodes = pins |
| 120 | + .map(item => { |
| 121 | + return ({ |
| 122 | + link: new DAGLink('', 1, item.key), |
| 123 | + data: item.data || Buffer.alloc(0) |
| 124 | + }) |
| 125 | + }) |
| 126 | + // sorting makes any ordering of `pins` produce the same DAGNode |
| 127 | + .sort((a, b) => Buffer.compare(a.link.Hash.buffer, b.link.Hash.buffer)) |
| 128 | + |
| 129 | + const rootLinks = fanoutLinks.concat(nodes.map(item => item.link)) |
| 130 | + const rootData = Buffer.concat( |
| 131 | + [headerBuf].concat(nodes.map(item => item.data)) |
| 132 | + ) |
| 133 | + |
| 134 | + return new DAGNode(rootData, rootLinks) |
| 135 | + } else { |
| 136 | + // If the array of pins is > MAX_ITEMS, we: |
| 137 | + // - distribute the pins among `DEFAULT_FANOUT` bins |
| 138 | + // - create a DAGNode for each bin |
| 139 | + // - add each pin as a DAGLink to that bin |
| 140 | + // - create a root DAGNode |
| 141 | + // - add each bin as a DAGLink |
| 142 | + // - send that root DAGNode via callback |
| 143 | + // (using go-ipfs' "wasteful but simple" approach for consistency) |
| 144 | + // https://github.com/ipfs/go-ipfs/blob/master/pin/set.go#L57 |
| 145 | + |
| 146 | + const bins = pins.reduce((bins, pin) => { |
| 147 | + const n = hash(depth, pin.key) % DEFAULT_FANOUT |
| 148 | + bins[n] = n in bins ? bins[n].concat([pin]) : [pin] |
| 149 | + return bins |
| 150 | + }, []) |
| 151 | + |
| 152 | + let idx = 0 |
| 153 | + for (const bin of bins) { |
| 154 | + const child = await storePins(bin, depth + 1) |
| 155 | + |
| 156 | + await storeChild(child, idx) |
| 157 | + |
| 158 | + idx++ |
| 159 | + } |
| 160 | + |
| 161 | + return new DAGNode(headerBuf, fanoutLinks) |
| 162 | + } |
| 163 | + |
| 164 | + async function storeChild (child, binIdx) { |
| 165 | + const opts = { |
| 166 | + version: 0, |
| 167 | + format: multicodec.DAG_PB, |
| 168 | + hashAlg: multicodec.SHA2_256, |
| 169 | + preload: false |
| 170 | + } |
| 171 | + |
| 172 | + const buf = dagpb.util.serialize(child) |
| 173 | + const cid = dagpb.util.cid(buf, { |
| 174 | + cidVersion: 0, |
| 175 | + hashAlg: multicodec.SHA2_256, |
| 176 | + }) |
| 177 | + await blockstore.put(cidToKey(cid), buf) |
| 178 | + |
| 179 | + fanoutLinks[binIdx] = new DAGLink('', child.size, cid) |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +async function storeSet (blockstore, type, cids) { |
| 185 | + const rootNode = await storeItems(blockstore, cids.map(cid => { |
| 186 | + return { |
| 187 | + key: cid, |
| 188 | + data: null |
| 189 | + } |
| 190 | + })) |
| 191 | + const buf = rootNode.serialize(rootNode) |
| 192 | + const cid = await dagpb.util.cid(buf, { |
| 193 | + cidVersion: 0, |
| 194 | + hashAlg: multicodec.SHA2_256 |
| 195 | + }) |
| 196 | + |
| 197 | + await blockstore.put(cidToKey(cid), buf) |
| 198 | + |
| 199 | + return new DAGLink(type, rootNode.size, cid) |
| 200 | +} |
| 201 | + |
| 202 | +module.exports = { |
| 203 | + loadSet, |
| 204 | + storeSet |
| 205 | +} |
0 commit comments