@@ -5,6 +5,7 @@ const MountStore = core.MountDatastore
55const ShardingStore = core . ShardingDatastore
66
77const Key = require ( 'interface-datastore' ) . Key
8+ const LevelStore = require ( 'datastore-level' )
89const waterfall = require ( 'async/waterfall' )
910const series = require ( 'async/series' )
1011const parallel = require ( 'async/parallel' )
@@ -17,12 +18,13 @@ const debug = require('debug')
1718const version = require ( './version' )
1819const config = require ( './config' )
1920const blockstore = require ( './blockstore' )
21+ const defaultOptions = require ( './default-options' )
2022
2123const log = debug ( 'repo' )
2224
2325const apiFile = new Key ( 'api' )
24- const blockStoreDirectory = 'blocks'
25- const dataStoreDirectory = 'datastore'
26+ const flatfsDirectory = 'blocks'
27+ const levelDirectory = 'datastore'
2628const repoVersion = 5
2729
2830/**
@@ -33,27 +35,27 @@ class IpfsRepo {
3335 /**
3436 * @param {string } repoPath - path where the repo is stored
3537 * @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={}]
4041 * @param {bool } [options.sharding=true] - Enable sharding (flatfs on disk), not needed in the browser.
4142 * @param {string } [options.lock='fs'] - Either `fs` or `memory`.
4243 */
4344 constructor ( repoPath , options ) {
4445 assert . equal ( typeof repoPath , 'string' , 'missing repoPath' )
4546
47+ this . options = Object . assign ( { } , defaultOptions , options )
48+
4649 this . closed = true
4750 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 )
5456
55- this . version = version ( this . _blockStore )
56- this . config = config ( this . _blockStore )
57+ this . version = version ( this . _fsStore )
58+ this . config = config ( this . _fsStore )
5759
5860 if ( this . options . lock === 'memory' ) {
5961 this . _locker = require ( './lock-memory' )
@@ -75,7 +77,7 @@ class IpfsRepo {
7577 log ( 'initializing at: %s' , this . path )
7678
7779 series ( [
78- ( cb ) => this . _blockStore . open ( ( err ) => {
80+ ( cb ) => this . _fsStore . open ( ( err ) => {
7981 if ( err && err . message === 'Already open' ) {
8082 return cb ( )
8183 }
@@ -101,7 +103,7 @@ class IpfsRepo {
101103
102104 // check if the repo is already initialized
103105 waterfall ( [
104- ( cb ) => this . _blockStore . open ( ( err ) => {
106+ ( cb ) => this . _fsStore . open ( ( err ) => {
105107 if ( err && err . message === 'Already open' ) {
106108 return cb ( )
107109 }
@@ -114,8 +116,8 @@ class IpfsRepo {
114116 this . lockfile = lck
115117
116118 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 ) )
119121
120122 if ( this . options . sharding ) {
121123 const shard = new core . shard . NextToLast ( 2 )
@@ -124,21 +126,17 @@ class IpfsRepo {
124126 cb ( null , s )
125127 }
126128 } ,
127- ( blockStore , cb ) => {
129+ ( flatfs , cb ) => {
128130 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+ } ] )
142140
143141 this . blockstore = blockstore ( this )
144142 this . closed = false
@@ -194,14 +192,14 @@ class IpfsRepo {
194192
195193 log ( 'closing at: %s' , this . path )
196194 series ( [
197- ( cb ) => this . _blockStore . delete ( apiFile , ( err ) => {
195+ ( cb ) => this . _fsStore . delete ( apiFile , ( err ) => {
198196 if ( err && err . message . startsWith ( 'ENOENT' ) ) {
199197 return cb ( )
200198 }
201199 cb ( err )
202200 } ) ,
203201 ( cb ) => this . store . close ( cb ) ,
204- ( cb ) => this . _blockStore . close ( cb ) ,
202+ ( cb ) => this . _fsStore . close ( cb ) ,
205203 ( cb ) => {
206204 log ( 'unlocking' )
207205 this . closed = true
@@ -232,7 +230,7 @@ class IpfsRepo {
232230 * @returns {void }
233231 */
234232 setApiAddress ( addr , callback ) {
235- this . _blockStore . put ( apiFile , Buffer . from ( addr . toString ( ) ) , callback )
233+ this . _fsStore . put ( apiFile , Buffer . from ( addr . toString ( ) ) , callback )
236234 }
237235
238236 /**
@@ -242,7 +240,7 @@ class IpfsRepo {
242240 * @returns {void }
243241 */
244242 apiAddress ( callback ) {
245- this . _blockStore . get ( apiFile , ( err , rawAddr ) => {
243+ this . _fsStore . get ( apiFile , ( err , rawAddr ) => {
246244 if ( err ) {
247245 return callback ( err )
248246 }
0 commit comments