Skip to content

Commit

Permalink
chore: remove custom blockstore
Browse files Browse the repository at this point in the history
  • Loading branch information
clostao committed Oct 25, 2024
1 parent 58883c1 commit f5c963f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 58 deletions.
9 changes: 0 additions & 9 deletions packages/auto-drive/src/ipld/blockstore/base.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/auto-drive/src/ipld/blockstore/index.ts

This file was deleted.

33 changes: 0 additions & 33 deletions packages/auto-drive/src/ipld/blockstore/memory.ts

This file was deleted.

24 changes: 11 additions & 13 deletions packages/auto-drive/src/ipld/chunker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { AwaitIterable } from 'interface-store'
import { CID } from 'multiformats'
import { cidOfNode, cidToString } from '../cid/index.js'
import { decodeIPLDNodeData, IPLDNodeData, OffchainMetadata } from '../metadata/index.js'
import { IPLDBlockstore } from './blockstore/base.js'
import { Builders, fileBuilders, metadataBuilders } from './builders.js'
import { createFolderInlinkIpldNode, createFolderIpldNode } from './nodes.js'
import { chunkBuffer, encodeNode } from './utils.js'
Expand All @@ -13,22 +12,23 @@ export const DEFAULT_MAX_CHUNK_SIZE = 1024 * 64
export const DEFAULT_MAX_LINK_PER_NODE = DEFAULT_MAX_CHUNK_SIZE / 64

export const processFileToIPLDFormat = (
blockstore: IPLDBlockstore,
blockstore: BaseBlockstore,
file: AwaitIterable<Buffer>,
totalSize: number,
filename?: string,
{ maxChunkSize, maxLinkPerNode }: { maxChunkSize: number; maxLinkPerNode: number } = {
maxChunkSize: DEFAULT_MAX_CHUNK_SIZE,
maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
},
): Promise<CID> => {
return processBufferToIPLDFormat(blockstore, file, filename, fileBuilders, {
return processBufferToIPLDFormat(blockstore, file, filename, totalSize, fileBuilders, {
maxChunkSize,
maxLinkPerNode,
})
}

export const processMetadataToIPLDFormat = async (
blockstore: IPLDBlockstore,
blockstore: BaseBlockstore,
metadata: OffchainMetadata,
limits: { maxChunkSize: number; maxLinkPerNode: number } = {
maxChunkSize: DEFAULT_MAX_CHUNK_SIZE,
Expand All @@ -43,55 +43,53 @@ export const processMetadataToIPLDFormat = async (
yield buffer
})(),
name,
buffer.byteLength,
metadataBuilders,
limits,
)
}

const processBufferToIPLDFormat = async (
blockstore: IPLDBlockstore,
blockstore: BaseBlockstore,
buffer: AwaitIterable<Buffer>,
filename: string | undefined,
totalSize: number,
builders: Builders,
{ maxChunkSize, maxLinkPerNode }: { maxChunkSize: number; maxLinkPerNode: number } = {
maxChunkSize: DEFAULT_MAX_CHUNK_SIZE,
maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
},
): Promise<CID> => {
const bufferChunks = chunkBuffer(buffer, { maxChunkSize })
let totalSize = 0

let CIDs: CID[] = []
for await (const chunk of bufferChunks) {
const node = builders.chunk(chunk)
const cid = cidOfNode(node)
await blockstore.put(cid, encodeNode(node))
totalSize += chunk.byteLength
CIDs.push(cid)
}

return processBufferToIPLDFormatFromChunks(blockstore, CIDs, filename, builders, {
return processBufferToIPLDFormatFromChunks(blockstore, CIDs, filename, totalSize, builders, {
maxLinkPerNode,
maxChunkSize,
})
}

export const processBufferToIPLDFormatFromChunks = async (
blockstore: IPLDBlockstore,
blockstore: BaseBlockstore,
chunks: AwaitIterable<CID>,
filename: string | undefined,
totalSize: number,
builders: Builders,
{ maxLinkPerNode, maxChunkSize }: { maxLinkPerNode: number; maxChunkSize: number } = {
maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
maxChunkSize: DEFAULT_MAX_CHUNK_SIZE,
},
): Promise<CID> => {
let totalSize = 0
let chunkCount = 0
let CIDs: CID[] = []
for await (const chunk of chunks) {
totalSize += await blockstore.getSize(chunk)

CIDs.push(chunk)
chunkCount++
}
Expand Down Expand Up @@ -162,7 +160,7 @@ export const processFolderToIPLDFormat = async (
* @returns the last chunk if it's not full, otherwise an empty buffer
*/
export const processChunksToIPLDFormat = async (
blockstore: IPLDBlockstore,
blockstore: BaseBlockstore,
chunks: AwaitIterable<Buffer>,
builders: Builders,
{ maxChunkSize = DEFAULT_MAX_CHUNK_SIZE }: { maxChunkSize?: number },
Expand Down
1 change: 0 additions & 1 deletion packages/auto-drive/src/ipld/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './blockstore/index.js'
export * from './builders.js'
export * from './chunker.js'
export * from './nodes.js'
Expand Down

0 comments on commit f5c963f

Please sign in to comment.