Skip to content

Instantly share code, notes, and snippets.

@soldair
Created October 26, 2017 21:45
Show Gist options
  • Save soldair/189b3b90fc1275219db06dd14b623f18 to your computer and use it in GitHub Desktop.
Save soldair/189b3b90fc1275219db06dd14b623f18 to your computer and use it in GitHub Desktop.
query/parse out all /mydb/_local keys in your couchdb data file
var fs = require('fs')
var rs = fs.createReadStream(PATH TO YOUR DATABASE FILE.couch)
var match = new Buffer('_local/')
var matchi = 0
rs.on('data',function(b){
for(var i=0;i<b.length;++i) bite(b,i)
})
var state = 'out'
var id =[]
var equals = '='.charCodeAt(0)
var processed = 0
function bite(buf,i){
processed++
if(state === 'out') {
//console.log(matchi)
if(buf[i] === match[matchi]){
matchi++
} else if(buf[i] === match[0]){
matchi = 1
} else {
matchi = 0
}
if(matchi === match.length){
state = 'in'
matchi = 0
}
} else {
if(id.length === 100 || buf[i] === 0){
// remove
id = new Buffer(id)
id = id.slice(0,id.lastIndexOf('h'.charCodeAt(0)))
if(id.lastIndexOf(2) > -1){
id = id.slice(0,id.lastIndexOf(2)-1)
}
state = 'out'
process.stdout.write(JSON.stringify('_local/'+id)+'\n')
id = []
} else {
id.push(buf[i])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment