all files / lib/adapters/leveldb/ transaction.js

100% Statements 53/53
88.89% Branches 16/18
100% Functions 9/9
100% Lines 52/52
2 statements, 1 function, 1 branch Ignored     
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                85666× 85666× 85666× 85666× 16725× 16725×   85666×           7321× 7321×     29552× 29552× 29552× 1493× 1493×   28059×         28059× 28059×   16657× 16657×   16657×   11402× 11402×       32151× 56114×   56114×   56114× 54941×   1173×     32151×       6889× 6889×     6889× 56114× 56114× 56114× 3034×   53080× 53080×     6889×    
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _pouchdbCollections = require('pouchdb-collections');
 
var _pouchdbCollections2 = _interopRequireDefault(_pouchdbCollections);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function getCacheFor(transaction, store) {
  var prefix = store.prefix()[0];
  var cache = transaction._cache;
  var subCache = cache.get(prefix);
  if (!subCache) {
    subCache = new _pouchdbCollections2.default.Map();
    cache.set(prefix, subCache);
  }
  return subCache;
} // similar to an idb or websql transaction object
// designed to be passed around. basically just caches
// things in-memory and then does a big batch() operation
// when you're done
 
function LevelTransaction() {
  this._batch = [];
  this._cache = new _pouchdbCollections2.default.Map();
}
 
LevelTransaction.prototype.get = function (store, key, callback) {
  var cache = getCacheFor(this, store);
  var exists = cache.get(key);
  if (exists) {
    return process.nextTick(function () {
      callback(null, exists);
    });
  } else Iif (exists === null) {
    // deleted marker
    /* istanbul ignore next */
    return process.nextTick(function () {
      callback({ name: 'NotFoundError' });
    });
  }
  store.get(key, function (err, res) {
    if (err) {
      /* istanbul ignore else */
      Eif (err.name === 'NotFoundError') {
        cache.set(key, null);
      }
      return callback(err);
    }
    cache.set(key, res);
    callback(null, res);
  });
};
 
LevelTransaction.prototype.batch = function (batch) {
  for (var i = 0, len = batch.length; i < len; i++) {
    var operation = batch[i];
 
    var cache = getCacheFor(this, operation.prefix);
 
    if (operation.type === 'put') {
      cache.set(operation.key, operation.value);
    } else {
      cache.set(operation.key, null);
    }
  }
  this._batch = this._batch.concat(batch);
};
 
LevelTransaction.prototype.execute = function (db, callback) {
 
  var keys = new _pouchdbCollections2.default.Set();
  var uniqBatches = [];
 
  // remove duplicates; last one wins
  for (var i = this._batch.length - 1; i >= 0; i--) {
    var operation = this._batch[i];
    var lookupKey = operation.prefix.prefix()[0] + '\xff' + operation.key;
    if (keys.has(lookupKey)) {
      continue;
    }
    keys.add(lookupKey);
    uniqBatches.push(operation);
  }
 
  db.batch(uniqBatches, callback);
};
 
exports.default = LevelTransaction;
module.exports = exports['default'];