Skip to content
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

feat: filestore #233

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Empty file.
4 changes: 4 additions & 0 deletions packages/blockstore-filestore/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This project is dual licensed under MIT and Apache-2.0.

MIT: https://www.opensource.org/licenses/mit
Apache-2.0: https://www.apache.org/licenses/license-2.0
5 changes: 5 additions & 0 deletions packages/blockstore-filestore/LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
19 changes: 19 additions & 0 deletions packages/blockstore-filestore/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
51 changes: 51 additions & 0 deletions packages/blockstore-filestore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# blockstore-fs <!-- omit in toc -->

[![ipfs.tech](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](https://ipfs.tech)
[![Discuss](https://img.shields.io/discourse/https/discuss.ipfs.tech/posts.svg?style=flat-square)](https://discuss.ipfs.tech)
[![codecov](https://img.shields.io/codecov/c/github/ipfs/js-stores.svg?style=flat-square)](https://codecov.io/gh/ipfs/js-stores)
[![CI](https://img.shields.io/github/actions/workflow/status/ipfs/js-stores/js-test-and-release.yml?branch=main\&style=flat-square)](https://github.com/ipfs/js-stores/actions/workflows/js-test-and-release.yml?query=branch%3Amain)

> Blockstore implementation with file system backend

## Table of contents <!-- omit in toc -->

- [Install](#install)
- [Usage](#usage)
- [API Docs](#api-docs)
- [License](#license)
- [Contribute](#contribute)

## Install

```console
$ npm i blockstore-filestore
```

## Usage

```js
import { Filestore } from 'blockstore-filestore'

const store = new Filestore(blockstore, datastore)
```

## API Docs

## License

Licensed under either of

- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

## Contribute

Contributions welcome! Please check out [the issues](https://github.com/ipfs/js-stores/issues).

Also see our [contributing document](https://github.com/ipfs/community/blob/master/CONTRIBUTING_JS.md) for more information on how we work, and about contributing in general.

Please be aware that all interactions related to this repo are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md)
177 changes: 177 additions & 0 deletions packages/blockstore-filestore/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
{
"name": "blockstore-filestore",
"version": "0.1.0",
"description": "Blockstore implementation with no-copy file system backend",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/ipfs/js-stores/tree/master/packages/blockstore-filestore#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/ipfs/js-stores.git"
},
"bugs": {
"url": "https://github.com/ipfs/js-stores/issues"
},
"keywords": [
"filestore",
"datastore",
"fs",
"interface",
"ipfs",
"key-value"
],
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"typesVersions": {
"*": {
"*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
],
"src/*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
]
}
},
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
}
},
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module",
"project": [
"tsconfig.json",
"benchmarks/encoding/tsconfig.json"
]
}
},
"release": {
"branches": [
"main"
],
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"preset": "conventionalcommits",
"releaseRules": [
{
"breaking": true,
"release": "major"
},
{
"revert": true,
"release": "patch"
},
{
"type": "feat",
"release": "minor"
},
{
"type": "fix",
"release": "patch"
},
{
"type": "docs",
"release": "patch"
},
{
"type": "test",
"release": "patch"
},
{
"type": "deps",
"release": "patch"
},
{
"scope": "no-release",
"release": false
}
]
}
],
[
"@semantic-release/release-notes-generator",
{
"preset": "conventionalcommits",
"presetConfig": {
"types": [
{
"type": "feat",
"section": "Features"
},
{
"type": "fix",
"section": "Bug Fixes"
},
{
"type": "chore",
"section": "Trivial Changes"
},
{
"type": "docs",
"section": "Documentation"
},
{
"type": "deps",
"section": "Dependencies"
},
{
"type": "test",
"section": "Tests"
}
]
}
}
],
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/github",
"@semantic-release/git"
]
},
"scripts": {
"clean": "aegir clean",
"lint": "aegir lint",
"build": "aegir build --bundle false",
"generate": "protons ./src/pb/dataobj.proto",
"release": "aegir release",
"test": "aegir test -t node -t electron-main",
"test:node": "aegir test -t node",
"test:electron-main": "aegir test -t electron-main",
"dep-check": "aegir dep-check",
"docs": "aegir docs"
},
"dependencies": {
"interface-datastore": "^8.2.3",
"multiformats": "^12.0.1",
"protons-runtime": "^5.0.0"
},
"devDependencies": {
"aegir": "^39.0.9",
"interface-blockstore": "^5.2.3",
"interface-store": "^5.1.2",
"protons": "^7.0.2"
},
"typedoc": {
"entryPoint": "./src/index.ts"
}
}
116 changes: 116 additions & 0 deletions packages/blockstore-filestore/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { CID } from 'multiformats/cid'
import * as raw from 'multiformats/codecs/raw'
import { sha256 } from 'multiformats/hashes/sha2'
import { DataObj } from './pb/dataobj.js'
import { readChunk, cidToKey, keyToCid } from './utils.js'
import type { Blockstore, Pair } from 'interface-blockstore'
import type { Datastore } from 'interface-datastore'
import type { AbortOptions, AwaitIterable, Await } from 'interface-store'

export class Filestore implements Blockstore {
private readonly blockstore: Blockstore
private readonly datastore: Datastore

constructor (blockstore: Blockstore, datastore: Datastore) {
this.blockstore = blockstore
this.datastore = datastore
}

async get (key: CID, options?: AbortOptions): Promise<Uint8Array> {
if (await this.blockstore.has(key, options)) {
const block = await this.blockstore.get(key, options)

return block
}

const dKey = cidToKey(key)
const index = await this.datastore.get(dKey, options)
const dataObj = DataObj.decode(index)
const chunk = await readChunk(dataObj.FilePath, dataObj.Offset, dataObj.Size)
const hash = await sha256.digest(chunk)
const cid = CID.create(key.version, raw.code, hash)

if (!cid.equals(key)) {
throw new Error('CID does not match.')
}

return chunk
}

async * getMany (source: AwaitIterable<CID>, options?: AbortOptions): AsyncGenerator<Pair, void, undefined> {
for await (const cid of source) {
const block = await this.get(cid, options)

yield { cid, block }
}
}

async * getAll (options?: AbortOptions): AsyncGenerator<Pair, void, undefined> {
yield * this.blockstore.getAll(options)

const keys = this.datastore.queryKeys({ filters: [() => true] }, options)

for await (const key of keys) {
let cid: CID

try {
cid = keyToCid(key)
} catch (error) {
continue
}

const block = await this.get(cid, options)

yield { block, cid }
}
}

async has (key: CID): Promise<boolean> {
if (await this.blockstore.has(key)) {
return true
}

const dKey = cidToKey(key)
const hasKey = await this.datastore.has(dKey)

return hasKey
}

put (key: CID, val: Uint8Array, options?: AbortOptions): Await<CID> {
return this.blockstore.put(key, val, options)
}

putMany (source: AwaitIterable<Pair>, options?: AbortOptions): AwaitIterable<CID> {
return this.blockstore.putMany(source, options)
}

async delete (key: CID, options?: AbortOptions): Promise<void> {
const dKey = cidToKey(key)

await Promise.all([
this.blockstore.delete(key, options),
this.datastore.delete(dKey, options)
])
}

async * deleteMany (source: AwaitIterable<CID>, options?: AbortOptions): AwaitIterable<CID> {
for await (const cid of source) {
await this.delete(cid, options)
yield cid
}
}

async putLink (key: CID, path: string, offset: bigint, size: bigint, options?: AbortOptions): Promise<CID> {
const data = DataObj.encode({
FilePath: path,
Offset: offset,
Size: size
})

const dKey = cidToKey(key)

await this.datastore.put(dKey, data, options)

return key
}
}
7 changes: 7 additions & 0 deletions packages/blockstore-filestore/src/pb/dataobj.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

message DataObj {
string FilePath = 1;
uint64 Offset = 2;
uint64 Size = 3;
}
Loading