Skip to content

Commit

Permalink
bugfixes & implemented support for dir property
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Aug 4, 2016
1 parent 546f877 commit 8d84756
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@
"standard": "^5.3.1"
},
"peerDependencies": {
"yjs": "^11.0.0 && >=11.2.0"
"yjs": "^11.0.0 && >=11.2.4"
},
"dependencies": {
"level": "^1.4.0",
"level-sublevel": "^6.5.4"
"level-sublevel": "^6.5.4",
"mkdirp": "^0.5.1"
}
}
36 changes: 23 additions & 13 deletions src/LevelDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
'use strict'
var levelup = require('levelup')
var sublevel = require('level-sublevel')
var path = require('path')
var mkdirp = require('mkdirp')

var idTemplate = '0000000000' // Length of 10 (Remember |2^32| == 10)
var levelOptions = {
valueEncoding: 'json',
keyEncoding: {
// id[1] will always have the length of 10 - making it sortable
encode: function (val) {
if (val.length === 1 || typeof val[1] === 'string') {
if (val.constructor !== Array) {
return val
} else if (val.length === 1 || typeof val[1] === 'string') {
return JSON.stringify(val)
} else if (val.length === 2 && typeof val[1] === 'number') {
var num = Number(val[1]).toString()
Expand Down Expand Up @@ -59,7 +63,6 @@ function extend (Y) {
if (err == null) {
resolve()
} else {
debugger
console.log('err', err)
}
})
Expand Down Expand Up @@ -214,25 +217,32 @@ function extend (Y) {
options.namespace = y.options.connector.room
}
}
options.dir = options.dir || '.'
var dbpath = path.join(options.dir, options.namespace)

var store = this

this.ready = null
if (options.cleanStart) {
this.ready = new Promise(function (resolve) {
leveldown.destroy(options.namespace, resolve)
debugger
this.ready = new Promise(function (resolve) {
mkdirp(dbpath, function (err) {
if (err) throw err
else resolve()
})
} else {
this.ready = Promise.resolve()
}
}).then(function () {
return new Promise(function (resolve) {
if (options.cleanStart) {
leveldown.destroy(dbpath, resolve)
} else {
resolve()
}
})
})
this.os = this.ds = this.ss = null
this.ready.then(() => {
this.db = sublevel(levelup(options.namespace))
this.db = sublevel(levelup(dbpath))
this.os = this.db.sublevel('os', levelOptions)
this.ds = this.db.sublevel('ds', levelOptions)
this.ss = this.db.sublevel('ss', levelOptions)
// this.os = levelup(options.namespace + '_os', levelOptions)
// this.ds = levelup(options.namespace + '_ds', levelOptions)
// this.ss = levelup(options.namespace + '_ss', levelOptions)
})
}
* operationAdded (transaction, op) {
Expand Down

0 comments on commit 8d84756

Please sign in to comment.