Skip to content

Commit 0208d60

Browse files
pgtedaviddias
authored andcommitted
API revamp and cleansing (#140)
* feat: API cleansing - closes #139
1 parent da45a57 commit 0208d60

File tree

53 files changed

+1225
-294
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1225
-294
lines changed

README.md

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,154 @@ This now has created the following structure, either on disk or as an in memory
137137

138138
## API
139139

140-
See https://ipfs.github.io/js-ipfs-repo
140+
### Setup
141+
142+
#### `new Repo(path[, options])`
143+
144+
Creates an IPFS Repo.
145+
146+
Arguments:
147+
148+
* `path` (string, mandatory): the path for this repo
149+
* `options` (object, optional): may contain the following values
150+
* `lock` (string, defaults to `"fs"` in Node.js, `"memory"` in the browser): what type of lock to use. Lock has to be acquired when opening.
151+
* `storageBackends` (object, optional): may contain the following values, which should each be a class implementing the [datastore interface](https://github.com/ipfs/interface-datastore#readme):
152+
* `root` (defaults to [`datastore-fs`](https://github.com/ipfs/js-datastore-fs#readme) in Node.js and [`datastore-level`](https://github.com/ipfs/js-datastore-level#readme) in the browser). Defines the back-end type used for gets and puts of values at the root (`repo.set()`, `repo.get()`)
153+
* `blocks` (defaults to [`datastore-fs`](https://github.com/ipfs/js-datastore-fs#readme) in Node.js and [`datastore-level`](https://github.com/ipfs/js-datastore-level#readme) in the browser). Defines the back-end type used for gets and puts of values at `repo.blocks`.
154+
* `datastore` (defaults to [`datastore-level`](https://github.com/ipfs/js-datastore-level#readme)). Defines the back-end type used as the key-valye store used for gets and puts of values at `repo.datastore`.
155+
156+
```js
157+
const repo = new Repo('path/to/repo')
158+
```
159+
160+
#### `repo.init (callback)`
161+
162+
Creates the necessary folder structure inside the repo.
163+
164+
#### `repo.open (callback)`
165+
166+
Locks the repo.
167+
168+
#### `repo.close (callback)`
169+
170+
Unlocks the repo.
171+
172+
#### `repo.exists (callback)`
173+
174+
Tells whether this repo exists or not. Calls back with `(err, bool)`.
175+
176+
### Repos
177+
178+
Root repo:
179+
180+
#### `repo.put (key, value:Buffer, callback)`
181+
182+
Put a value at the root of the repo.
183+
184+
* `key` can be a buffer, a string or a [Key](https://github.com/ipfs/interface-datastore#keys).
185+
186+
#### `repo.get (key, callback)`
187+
188+
Get a value at the root of the repo.
189+
190+
* `key` can be a buffer, a string or a [Key](https://github.com/ipfs/interface-datastore#keys).
191+
* `callback` is a callback function `function (err, result:Buffer)`
192+
193+
[Blocks](https://github.com/ipfs/js-ipfs-block#readme):
194+
195+
#### `repo.blocks.put (block:Block, callback)`
196+
197+
* `block` should be of type [Block](https://github.com/ipfs/js-ipfs-block#readme).
198+
199+
#### `repo.blocks.putMany (blocks, callback)`
200+
201+
Put many blocks.
202+
203+
* `block` should be an array of type [Block](https://github.com/ipfs/js-ipfs-block#readme).
204+
205+
#### `repo.blocks.get (cid, callback)`
206+
207+
Get block.
208+
209+
* `cid` is the content id of [type CID](https://github.com/ipld/js-cid#readme).
210+
* `callback` is a callback function `function (err, result:Buffer)`
211+
212+
Datastore:
213+
214+
#### `repo.datastore`
215+
216+
This is contains a full implementation of [the `interface-datastore` API](https://github.com/ipfs/interface-datastore#api).
217+
218+
219+
### Utils
220+
221+
#### `repo.config`
222+
223+
Instead of using `repo.set('config')` this exposes an API that allows you to set and get a decoded config object, as well as, in a safe manner, change any of the config values individually.
224+
225+
##### `repo.config.set(key:string, value, callback)`
226+
227+
Set a config value. `value` can be any object that is serializable to JSON.
228+
229+
* `key` is a string specifying the object path. Example:
230+
231+
```js
232+
repo.config.set('a.b.c', 'c value', (err) => {
233+
if (err) { throw err }
234+
repo.config.get((err, config) => {
235+
if (err) { throw err }
236+
assert.equal(config.a.b.c, 'c value')
237+
})
238+
})
239+
```
240+
241+
##### `repo.config.get(value, callback)`
242+
243+
Set the whole config value. `value` can be any object that is serializable to JSON.
244+
245+
##### `repo.config.get(key:string, callback)`
246+
247+
Get a config value. `callback` is a function with the signature: `function (err, value)`, wehre the `
248+
value` is of the same type that was set before.
249+
250+
* `key` is a string specifying the object path. Example:
251+
252+
```js
253+
repo.config.get('a.b.c', (err, value) => {
254+
if (err) { throw err }
255+
console.log('config.a.b.c = ', value)
256+
})
257+
```
258+
259+
##### `repo.config.get(callback)`
260+
261+
Get the entire config value. `callback` is a function with the signature: `function (err, configValue:Object)`.
262+
263+
#### `repo.config.exists(callback)`
264+
265+
Whether the config sub-repo exists. Calls back with `(err, bool)`.
266+
267+
#### `repo.version`
268+
269+
##### `repo.version.get (callback)`
270+
271+
Gets the repo version.
272+
273+
##### `repo.version.set (version:number, callback)`
274+
275+
Sets the repo version
276+
277+
#### `repo.apiAddr`
278+
279+
#### `repo.apiAddr.get (callback)`
280+
281+
Gets the API address.
282+
283+
#### `repo.apiAddr.set (value, callback)`
284+
285+
Sets the API address.
286+
287+
* `value` should be a [Multiaddr](https://github.com/multiformats/js-multiaddr) or a String representing a valid one.
141288

142289
## Notes
143290

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
"level-js": "timkuijsten/level.js#idbunwrapper",
6666
"leveldown": "^1.7.2",
6767
"lock-me": "^1.0.2",
68+
"lodash.get": "^4.4.2",
69+
"lodash.has": "^4.5.2",
70+
"lodash.set": "^4.3.2",
6871
"multiaddr": "^2.3.0",
6972
"safe-buffer": "^5.1.1"
7073
},
@@ -85,4 +88,4 @@
8588
"nginnever <[email protected]>",
8689
"npmcdn-to-unpkg-bot <[email protected]>"
8790
]
88-
}
91+
}

src/api-addr.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict'
2+
3+
const Key = require('interface-datastore').Key
4+
const Buffer = require('safe-buffer').Buffer
5+
6+
const apiFile = new Key('api')
7+
8+
module.exports = (store) => {
9+
return {
10+
/**
11+
* Get the current configuration from the repo.
12+
*
13+
* @param {function(Error, Object)} callback
14+
* @returns {void}
15+
*/
16+
get (callback) {
17+
store.get(apiFile, (err, value) => callback(err, value && value.toString()))
18+
},
19+
/**
20+
* Set the current configuration for this repo.
21+
*
22+
* @param {Object} value - the api address to be written
23+
* @param {function(Error)} callback
24+
* @returns {void}
25+
*/
26+
set (value, callback) {
27+
store.put(apiFile, Buffer.from(value.toString()), callback)
28+
},
29+
/**
30+
* Deletes api file
31+
*
32+
* @param {function(Error, bool)} callback
33+
* @returns {void}
34+
*/
35+
delete (callback) {
36+
store.delete(apiFile, callback)
37+
}
38+
}
39+
}

src/backends.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
exports.create = function createBackend (name, path, options) {
4+
const Ctor = options.storageBackends[name]
5+
const backendOptions = Object.assign({}, options.storageBackendOptions[name] || {})
6+
return new Ctor(path, backendOptions)
7+
}

src/blockstore.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
'use strict'
22

3-
const NamespaceStore = require('datastore-core').NamespaceDatastore
3+
const core = require('datastore-core')
4+
const ShardingStore = core.ShardingDatastore
45
const Key = require('interface-datastore').Key
56
const base32 = require('base32.js')
67
const Block = require('ipfs-block')
78
const setImmediate = require('async/setImmediate')
89
const reject = require('async/reject')
910
const CID = require('cids')
1011

11-
const blockPrefix = new Key('blocks')
12-
1312
/**
1413
* Transform a raw buffer to a base32 encoded key.
1514
*
@@ -31,8 +30,24 @@ const cidToDsKey = (cid) => {
3130
return keyFromBuffer(cid.buffer)
3231
}
3332

34-
module.exports = (repo) => {
35-
const store = new NamespaceStore(repo.store, blockPrefix)
33+
module.exports = (filestore, options, callback) => {
34+
maybeWithSharding(filestore, options, (err, store) => {
35+
if (err) { return callback(err) }
36+
37+
callback(null, createBaseStore(store))
38+
})
39+
}
40+
41+
function maybeWithSharding (filestore, options, callback) {
42+
if (options.sharding) {
43+
const shard = new core.shard.NextToLast(2)
44+
ShardingStore.createOrOpen(filestore, shard, callback)
45+
} else {
46+
setImmediate(() => callback(null, filestore))
47+
}
48+
}
49+
50+
function createBaseStore (store) {
3651
return {
3752
/**
3853
* Get a single block by CID.
@@ -50,9 +65,7 @@ module.exports = (repo) => {
5065

5166
const k = cidToDsKey(cid)
5267
store.get(k, (err, blockData) => {
53-
if (err) {
54-
return callback(err)
55-
}
68+
if (err) { return callback(err) }
5669

5770
callback(null, new Block(blockData, cid))
5871
})
@@ -67,12 +80,8 @@ module.exports = (repo) => {
6780
const k = cidToDsKey(block.cid)
6881

6982
store.has(k, (err, exists) => {
70-
if (err) {
71-
return callback(err)
72-
}
73-
if (exists) {
74-
return callback()
75-
}
83+
if (err) { return callback(err) }
84+
if (exists) { return callback() }
7685

7786
store.put(k, block.data, callback)
7887
})
@@ -134,6 +143,10 @@ module.exports = (repo) => {
134143
}
135144

136145
store.delete(cidToDsKey(cid), callback)
146+
},
147+
148+
close (callback) {
149+
store.close(callback)
137150
}
138151
}
139152
}

0 commit comments

Comments
 (0)