/**
 * global ldb tracker
 * @module ldb
 * @license
 * Copyright (c) 2014-2015, Fedor Indutny (MIT License)
 * Copyright (c) 2014-2016, Christopher Jeffrey (MIT License).
 * https://github.com/bcoin-org/bcoin
 */

'use strict';

var LowlevelUp = require('./lowlevelup');
var utils = require('../utils/utils');
var assert = utils.assert;

/**
 * @param {Object} options
 * @param {Boolean} options.compression
 * @param {Number} options.cacheSize
 * @param {Number} options.writeBufferSize
 * @param {Number} options.maxOpenFiles
 * @param {Boolean} options.sync
 * @param {Number} options.mapSize
 * @param {Boolean} options.writeMap
 * @param {String} options.db - Database backend (`"leveldb"` by default).
 * @param {String} options.name - Database name.
 * @param {String} options.location - Database location (overrides `name`).
 * @returns {LowlevelUp}
 */

function ldb(options) {
  var target = ldb.getTarget(options);

  if (target.backend !== 'rbt')
    utils.mkdir(target.location, true);

  return new LowlevelUp(target.location, {
    // LevelDB and others
    createIfMissing: options.createIfMissing !== false,
    errorIfExists: options.errorIfExists === true,
    compression: options.compression !== false,
    cacheSize: options.cacheSize || (8 << 20),
    writeBufferSize: options.writeBufferSize || (4 << 20),
    maxOpenFiles: options.maxOpenFiles || 64,
    filterBits: 0,
    paranoidChecks: false,
    memory: false,

    // For LMDB if we decide to use it:
    sync: options.sync || false,
    mapSize: options.mapSize || 300 * (1024 << 20),
    writeMap: options.writeMap || false,

    // For browser buffer keys
    bufferKeys: options.bufferKeys,

    db: target.db
  });
}

/**
 * Get database name and extension based on options.
 * @param {String} db
 * @returns {Object}
 */

ldb.getBackend = function getBackend(db) {
  var name, ext;

  if (!db)
    db = 'memory';

  switch (db) {
    case 'leveldb':
    case 'leveldown':
      name = 'leveldown';
      ext = 'ldb';
      break;
    case 'rocksdb':
    case 'rocksdown':
      name = 'rocksdown';
      ext = 'rdb';
      break;
    case 'lmdb':
      name = 'lmdb';
      ext = 'lmdb';
      break;
    case 'memory':
    case 'rbt':
      name = 'rbt';
      ext = 'mem';
      break;
    default:
      name = db;
      ext = 'db';
      break;
  }

  return { name: name, ext: ext };
};

/**
 * Get target backend and location.
 * @param {Object} options
 * @returns {Object}
 */

ldb.getTarget = function getTarget(options) {
  var backend = ldb.getBackend(options.db);
  var location = options.location;
  var db;

  if (backend.name === 'rbt')
    db = require('./rbt');
  else if (utils.isBrowser)
    db = require('./level');
  else
    db = require(backend.name);

  if (typeof location !== 'string') {
    assert(backend.name === 'rbt', 'Location required.');
    location = 'rbt';
  }

  return {
    db: db,
    backend: backend.name,
    location: location + '.' + backend.ext
  };
};

/*
 * Expose
 */

module.exports = ldb;

@@ -1375,27 +1375,8 @@
 64,%0A
-    filterBits: 0,%0A
     
@@ -1527,11 +1527,9 @@
  %7C%7C 
-300
+2
  * (
@@ -1577,24 +1577,66 @@
 ap %7C%7C false,
+%0A    noSubdir: options.noSubdir !== false,
 %0A%0A    // For
@@ -2180,25 +2180,24 @@
      ext = '
-l
 mdb';%0A      

change some lmdb options.