@@ -5,6 +5,7 @@ const MountStore = core.MountDatastore
5
5
const ShardingStore = core . ShardingDatastore
6
6
7
7
const Key = require ( 'interface-datastore' ) . Key
8
+ const LevelStore = require ( 'datastore-level' )
8
9
const waterfall = require ( 'async/waterfall' )
9
10
const series = require ( 'async/series' )
10
11
const parallel = require ( 'async/parallel' )
@@ -17,12 +18,13 @@ const debug = require('debug')
17
18
const version = require ( './version' )
18
19
const config = require ( './config' )
19
20
const blockstore = require ( './blockstore' )
21
+ const defaultOptions = require ( './default-options' )
20
22
21
23
const log = debug ( 'repo' )
22
24
23
25
const apiFile = new Key ( 'api' )
24
- const blockStoreDirectory = 'blocks'
25
- const dataStoreDirectory = 'datastore'
26
+ const flatfsDirectory = 'blocks'
27
+ const levelDirectory = 'datastore'
26
28
const repoVersion = 5
27
29
28
30
/**
@@ -33,27 +35,27 @@ class IpfsRepo {
33
35
/**
34
36
* @param {string } repoPath - path where the repo is stored
35
37
* @param {object } options - Configuration
36
- * @param {datastore } options.blockStore
37
- * @param {datastore } options.dataStore
38
- * @param {object } [options.blockStoreOptions={}]
39
- * @param {object } [options.dataStoreOptions={}]
38
+ * @param {Datastore } options.fs
39
+ * @param {Leveldown } options.level
40
+ * @param {object } [options.fsOptions={}]
40
41
* @param {bool } [options.sharding=true] - Enable sharding (flatfs on disk), not needed in the browser.
41
42
* @param {string } [options.lock='fs'] - Either `fs` or `memory`.
42
43
*/
43
44
constructor ( repoPath , options ) {
44
45
assert . equal ( typeof repoPath , 'string' , 'missing repoPath' )
45
46
47
+ this . options = Object . assign ( { } , defaultOptions , options )
48
+
46
49
this . closed = true
47
50
this . path = repoPath
48
- this . options = Object . assign ( { lock : 'memory' , sharding : true } ,
49
- options , require ( './default-options' ) )
50
-
51
- const BlockStore = this . options . blockStore
52
- this . _blockStore = new BlockStore ( this . path ,
53
- Object . assign ( this . options . blockStoreOptions || { } , { extension : '' } ) )
51
+ const FsStore = this . options . fs
52
+ const fsOptions = Object . assign ( { } , this . options . fsOptions , {
53
+ extension : ''
54
+ } )
55
+ this . _fsStore = new FsStore ( this . path , fsOptions )
54
56
55
- this . version = version ( this . _blockStore )
56
- this . config = config ( this . _blockStore )
57
+ this . version = version ( this . _fsStore )
58
+ this . config = config ( this . _fsStore )
57
59
58
60
if ( this . options . lock === 'memory' ) {
59
61
this . _locker = require ( './lock-memory' )
@@ -75,7 +77,7 @@ class IpfsRepo {
75
77
log ( 'initializing at: %s' , this . path )
76
78
77
79
series ( [
78
- ( cb ) => this . _blockStore . open ( ( err ) => {
80
+ ( cb ) => this . _fsStore . open ( ( err ) => {
79
81
if ( err && err . message === 'Already open' ) {
80
82
return cb ( )
81
83
}
@@ -101,7 +103,7 @@ class IpfsRepo {
101
103
102
104
// check if the repo is already initialized
103
105
waterfall ( [
104
- ( cb ) => this . _blockStore . open ( ( err ) => {
106
+ ( cb ) => this . _fsStore . open ( ( err ) => {
105
107
if ( err && err . message === 'Already open' ) {
106
108
return cb ( )
107
109
}
@@ -114,8 +116,8 @@ class IpfsRepo {
114
116
this . lockfile = lck
115
117
116
118
log ( 'creating flatfs' )
117
- const BlockStore = this . options . blockStore
118
- const s = new BlockStore ( path . join ( this . path , blockStoreDirectory ) , this . options . blockStoreOptions )
119
+ const FsStore = this . options . fs
120
+ const s = new FsStore ( path . join ( this . path , flatfsDirectory ) , Object . assign ( { } , this . options . fsOptions ) )
119
121
120
122
if ( this . options . sharding ) {
121
123
const shard = new core . shard . NextToLast ( 2 )
@@ -124,21 +126,17 @@ class IpfsRepo {
124
126
cb ( null , s )
125
127
}
126
128
} ,
127
- ( blockStore , cb ) => {
129
+ ( flatfs , cb ) => {
128
130
log ( 'Flatfs store opened' )
129
- const DataStore = this . options . dataStore
130
- const dataStore = new DataStore ( path . join ( this . path , dataStoreDirectory ) , this . options . dataStoreOptions )
131
- log ( dataStore )
132
- this . store = new MountStore ( [
133
- {
134
- prefix : new Key ( blockStoreDirectory ) ,
135
- datastore : blockStore
136
- } ,
137
- {
138
- prefix : new Key ( '/' ) ,
139
- datastore : dataStore
140
- }
141
- ] )
131
+ this . store = new MountStore ( [ {
132
+ prefix : new Key ( flatfsDirectory ) ,
133
+ datastore : flatfs
134
+ } , {
135
+ prefix : new Key ( '/' ) ,
136
+ datastore : new LevelStore ( path . join ( this . path , levelDirectory ) , {
137
+ db : this . options . level
138
+ } )
139
+ } ] )
142
140
143
141
this . blockstore = blockstore ( this )
144
142
this . closed = false
@@ -194,14 +192,14 @@ class IpfsRepo {
194
192
195
193
log ( 'closing at: %s' , this . path )
196
194
series ( [
197
- ( cb ) => this . _blockStore . delete ( apiFile , ( err ) => {
195
+ ( cb ) => this . _fsStore . delete ( apiFile , ( err ) => {
198
196
if ( err && err . message . startsWith ( 'ENOENT' ) ) {
199
197
return cb ( )
200
198
}
201
199
cb ( err )
202
200
} ) ,
203
201
( cb ) => this . store . close ( cb ) ,
204
- ( cb ) => this . _blockStore . close ( cb ) ,
202
+ ( cb ) => this . _fsStore . close ( cb ) ,
205
203
( cb ) => {
206
204
log ( 'unlocking' )
207
205
this . closed = true
@@ -232,7 +230,7 @@ class IpfsRepo {
232
230
* @returns {void }
233
231
*/
234
232
setApiAddress ( addr , callback ) {
235
- this . _blockStore . put ( apiFile , Buffer . from ( addr . toString ( ) ) , callback )
233
+ this . _fsStore . put ( apiFile , Buffer . from ( addr . toString ( ) ) , callback )
236
234
}
237
235
238
236
/**
@@ -242,7 +240,7 @@ class IpfsRepo {
242
240
* @returns {void }
243
241
*/
244
242
apiAddress ( callback ) {
245
- this . _blockStore . get ( apiFile , ( err , rawAddr ) => {
243
+ this . _fsStore . get ( apiFile , ( err , rawAddr ) => {
246
244
if ( err ) {
247
245
return callback ( err )
248
246
}
0 commit comments