1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | 1 1 1 1 1 1 1 1 4 1 1 1 1 2226 2226 1 4 4 4 1 3 4 4 4 4 4 4 4 4 2226 2226 2225 1 1 1 4 4 1 4 4 1 4 4 1 1 3634 3634 3634 3633 1 1 1 1 1 1 1 1 1 1 1 1 3 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 | 'use strict'; var fs = require('fs'); var path = require('path'); var isLocalId = require('./docs/isLocalId'); var merge = require('../merge'); var levelup = require('levelup'); var through = require('through2').obj; var stores = [ 'document-store', 'by-sequence', 'attach-store', 'attach-binary-store' ]; function formatSeq(n) { return ('0000000000000000' + n).slice(-16); } var UPDATE_SEQ_KEY = '_local_last_update_seq'; var DOC_COUNT_KEY = '_local_doc_count'; var UUID_KEY = '_local_uuid'; exports.toSublevel = function (name, db, callback) { // local require to prevent crashing if leveldown isn't installed. var leveldown = require("leveldown"); var base = path.resolve(name); function move(store, index, cb) { var storePath = path.join(base, store); var opts; if (index === 3) { opts = { valueEncoding: 'binary' }; } else { opts = { valueEncoding: 'json' }; } var sub = db.sublevel(store, opts); var orig = levelup(storePath, opts); var from = orig.createReadStream(); var to = sub.createWriteStream(); from.on('end', function () { orig.close(function (err) { cb(err, storePath); }); }); from.pipe(to); } fs.unlink(base + '.uuid', function (err) { if (err) { return callback(); } var todo = 4; var done = []; stores.forEach(function (store, i) { move(store, i, function (err, storePath) { /* istanbul ignore if */ Iif (err) { return callback(err); } done.push(storePath); if (!(--todo)) { done.forEach(function (item) { leveldown.destroy(item, function () { if (++todo === done.length) { fs.rmdir(base, callback); } }); }); } }); }); }); }; exports.localAndMetaStores = function (db, stores, callback) { var batches = []; stores.bySeqStore.get(UUID_KEY, function (err, value) { if (err) { // no uuid key, so don't need to migrate; return callback(); } batches.push({ key: UUID_KEY, value: value, prefix: stores.metaStore, type: 'put', valueEncoding: 'json' }); batches.push({ key: UUID_KEY, prefix: stores.bySeqStore, type: 'del' }); stores.bySeqStore.get(DOC_COUNT_KEY, function (err, value) { Eif (value) { // if no doc count key, // just skip // we can live with this batches.push({ key: DOC_COUNT_KEY, value: value, prefix: stores.metaStore, type: 'put', valueEncoding: 'json' }); batches.push({ key: DOC_COUNT_KEY, prefix: stores.bySeqStore, type: 'del' }); } stores.bySeqStore.get(UPDATE_SEQ_KEY, function (err, value) { Eif (value) { // if no UPDATE_SEQ_KEY // just skip // we've gone to far to stop. batches.push({ key: UPDATE_SEQ_KEY, value: value, prefix: stores.metaStore, type: 'put', valueEncoding: 'json' }); batches.push({ key: UPDATE_SEQ_KEY, prefix: stores.bySeqStore, type: 'del' }); } var deletedSeqs = {}; stores.docStore.createReadStream({ startKey: '_', endKey: '_\xFF' }).pipe(through(function (ch, _, next) { if (!isLocalId(ch.key)) { return next(); } batches.push({ key: ch.key, prefix: stores.docStore, type: 'del' }); var winner = merge.winningRev(ch.value); Object.keys(ch.value.rev_map).forEach(function (key) { Eif (key !== 'winner') { this.push(formatSeq(ch.value.rev_map[key])); } }, this); var winningSeq = ch.value.rev_map[winner]; stores.bySeqStore.get(formatSeq(winningSeq), function (err, value) { Eif (!err) { batches.push({ key: ch.key, value: value, prefix: stores.localStore, type: 'put', valueEncoding: 'json' }); } next(); }); })).pipe(through(function (seq, _, next) { Iif (deletedSeqs[seq]) { return next(); } deletedSeqs[seq] = true; stores.bySeqStore.get(seq, function (err, resp) { Iif (err || !isLocalId(resp._id)) { return next(); } batches.push({ key: seq, prefix: stores.bySeqStore, type: 'del' }); next(); }); }, function (next) { db.batch(batches, callback); })); }); }); }); }; |