@nozbe/lokijs
Version:
Nozbe's fork of LokiJS, optimized for WatermelonDB
3 lines (2 loc) • 44.8 kB
JavaScript
!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():t.loki=e()}(this,function(){return function(){"use strict";function t(i){var r,n;if(Array.isArray(i)){for(n=0;n<i.length;n++)t(i[n]);e(i)}else if(null!==i&&"object"==typeof i){for(r in i)i.hasOwnProperty(r)&&t(i[r]);e(i)}}function e(t){Object.isFrozen(t)||Object.freeze(t)}function i(t){return Object.isFrozen(t)?d(t,"shallow"):t}function r(t,e){var i,r,n,s;if(t===e)return!0;if(!t||!e||!0===t||!0===e||t!==t||e!==e){switch(t){case void 0:case null:n=1;break;case!1:n=3;break;case!0:n=4;break;case"":n=5;break;default:n=t===t?9:0}switch(e){case void 0:case null:s=1;break;case!1:s=3;break;case!0:s=4;break;case"":s=5;break;default:s=e===e?9:0}if(9!==n||9!==s)return n===s}return i=Number(t),r=Number(e),i===i||r===r?i===r:(i=t.toString(),r=e.toString(),i==r)}function n(t,e,i){var r,n,s,a;if(!t||!e||!0===t||!0===e||t!==t||e!==e){switch(t){case void 0:case null:s=1;break;case!1:s=3;break;case!0:s=4;break;case"":s=5;break;default:s=t===t?9:0}switch(e){case void 0:case null:a=1;break;case!1:a=3;break;case!0:a=4;break;case"":a=5;break;default:a=e===e?9:0}if(9!==s||9!==a)return s===a?i:s<a}return r=Number(t),n=Number(e),r===r&&n===n?r<n||!(r>n)&&i:r===r&&n!==n||(n!==n||r===r)&&(t<e||!(t>e)&&(t==e?i:(r=t.toString(),n=e.toString(),r<n||r==n&&i)))}function s(t,e,i){var r,n,s,a;if(!t||!e||!0===t||!0===e||t!==t||e!==e){switch(t){case void 0:case null:s=1;break;case!1:s=3;break;case!0:s=4;break;case"":s=5;break;default:s=t===t?9:0}switch(e){case void 0:case null:a=1;break;case!1:a=3;break;case!0:a=4;break;case"":a=5;break;default:a=e===e?9:0}if(9!==s||9!==a)return s===a?i:s>a}return r=Number(t),n=Number(e),r===r&&n===n?r>n||!(r<n)&&i:(r!==r||n===n)&&(n===n&&r!==r||(t>e||!(t<e)&&(t==e?i:(r=t.toString(),n=e.toString(),r>n||r==n&&i))))}function a(t,e,i){return w.aeq(t,e)?0:w.lt(t,e,!1)?i?1:-1:w.gt(t,e,!1)?i?-1:1:0}function o(t,e,i){for(var r,n,s,o,l,h=0,c=0,d=t.length;c<d;c++)if(r=t[c],n=r[0],~n.indexOf(".")?(l=n.split("."),s=m.getIn(e,l,!0),o=m.getIn(i,l,!0)):(s=e[n],o=i[n]),0!==(h=a(s,o,r[1])))return h;return 0}function l(t,e,i,r,n,s){var a,o=s||0,h=e[o],c=!1;if("object"==typeof t&&h in t&&(a=t[h]),o+1>=e.length)c=i(a,r,n);else if(Array.isArray(a))for(var d=0,u=a.length;d<u&&!0!==(c=l(a[d],e,i,r,n,o+1));d+=1);else c=l(a,e,i,r,n,o+1);return c}function h(t){return"string"==typeof t||Array.isArray(t)?function(e){return-1!==t.indexOf(e)}:"object"==typeof t&&null!==t?function(e){return I.call(t,e)}:null}function c(t,e,i){for(var r in e)if(I.call(e,r))return O[r](t,e[r],i);return!1}function d(t,e){if(null===t||void 0===t)return null;var i,r=e||"parse-stringify";switch(r){case"parse-stringify":i=JSON.parse(JSON.stringify(t))}return i}function u(){}function f(t,e){this.filename=t||"loki.db",this.collections=[],this.databaseVersion=1.5,this.engineVersion=1.5,this.autosave=!1,this.autosaveInterval=5e3,this.autosaveHandle=null,this.throttledSaves=!0,this.options={},this.persistenceMethod=null,this.persistenceAdapter=null,this.throttledSavePending=!1,this.throttledCallbacks=[],this.verbose=!(!e||!e.hasOwnProperty("verbose"))&&e.verbose,this.events={init:[],loaded:[],flushChanges:[],close:[],changes:[],warning:[]},this.configureOptions(e,!0)}function p(t){this.hashStore={},this.options=t||{},this.options.hasOwnProperty("asyncResponses")||(this.options.asyncResponses=!1),this.options.hasOwnProperty("asyncTimeout")||(this.options.asyncTimeout=50)}function y(t,e){return e=e||{},this.collection=t,this.filteredrows=[],this.filterInitialized=!1,this}function v(t,e){if("$regex"===t)Array.isArray(e)?e=new RegExp(e[0],e[1]):e instanceof RegExp||(e=new RegExp(e));else if("object"==typeof e)for(var i in e)"$regex"!==i&&"object"!=typeof e[i]||(e[i]=v(i,e[i]));return e}function g(t,e){this.name=t,this.data=[],this.idIndex=null,this.binaryIndices={},this.constraints={unique:{},exact:{}},this.uniqueNames=[],this.transforms={},this.objType=t,this.dirty=!0,this.cachedIndex=null,this.cachedBinaryIndex=null,this.cachedData=null;var i=this;e=e||{},e.hasOwnProperty("unique")&&(Array.isArray(e.unique)||(e.unique=[e.unique]),e.unique.forEach(function(t){i.uniqueNames.push(t)})),this.adaptiveBinaryIndices=!e.hasOwnProperty("adaptiveBinaryIndices")||e.adaptiveBinaryIndices,this.transactional=!!e.hasOwnProperty("transactional")&&e.transactional,this.cloneObjects=!!e.hasOwnProperty("clone")&&e.clone,this.cloneMethod=e.hasOwnProperty("cloneMethod")?e.cloneMethod:"parse-stringify",this.asyncListeners=!!e.hasOwnProperty("asyncListeners")&&e.asyncListeners,this.disableMeta=!!e.hasOwnProperty("disableMeta")&&e.disableMeta,this.disableChangesApi=!e.hasOwnProperty("disableChangesApi")||e.disableChangesApi,this.disableDeltaChangesApi=!e.hasOwnProperty("disableDeltaChangesApi")||e.disableDeltaChangesApi,this.disableChangesApi&&(this.disableDeltaChangesApi=!0),this.autoupdate=!!e.hasOwnProperty("autoupdate")&&e.autoupdate,this.serializableIndices=!e.hasOwnProperty("serializableIndices")||e.serializableIndices,this.disableFreeze=!e.hasOwnProperty("disableFreeze")||e.disableFreeze,this.maxId=0,this.DynamicViews=[],this.events={insert:[],update:[],"pre-insert":[],"pre-update":[],close:[],flushbuffer:[],error:[],delete:[],warning:[]},this.changes=[],this.dirtyIds=[];var r=[];if(e&&e.indices)if("[object Array]"===Object.prototype.toString.call(e.indices))r=e.indices;else{if("string"!=typeof e.indices)throw new TypeError("Indices needs to be a string or an array of strings");r=[e.indices]}for(var n=0;n<r.length;n++)this.ensureIndex(r[n]);this.on("warning",function(t){i.lokiConsoleWrapper.warn(t)})}function b(t){this.field=t,this.keyMap=Object.create(null),this.lokiMap=Object.create(null)}var I=Object.prototype.hasOwnProperty,m={copyProperties:function(t,e){var i;for(i in t)e[i]=t[i]},getIn:function(t,e,i){if(null!=t){if(!i)return t[e];if("string"==typeof e&&(e=e.split(".")),!Array.isArray(e))throw new Error("path must be a string or array. Found "+typeof e);for(var r=0,n=e.length;null!=t&&r<n;)t=t[e[r++]];return r&&r==n?t:void 0}}},w={aeq:r,lt:n,gt:s},O={$eq:function(t,e){return t===e},$aeq:function(t,e){return t==e},$ne:function(t,e){return e!==e?t===t:t!==e},$dteq:function(t,e){return w.aeq(t,e)},$gt:function(t,e){return w.gt(t,e,!1)},$gte:function(t,e){return w.gt(t,e,!0)},$lt:function(t,e){return w.lt(t,e,!1)},$lte:function(t,e){return w.lt(t,e,!0)},$jgt:function(t,e){return t>e},$jgte:function(t,e){return t>=e},$jlt:function(t,e){return t<e},$jlte:function(t,e){return t<=e},$between:function(t,e){return void 0!==t&&null!==t&&(w.gt(t,e[0],!0)&&w.lt(t,e[1],!0))},$jbetween:function(t,e){return void 0!==t&&null!==t&&(t>=e[0]&&t<=e[1])},$in:function(t,e){return-1!==e.indexOf(t)},$inSet:function(t,e){return e.has(t)},$nin:function(t,e){return-1===e.indexOf(t)},$keyin:function(t,e){return t in e},$nkeyin:function(t,e){return!(t in e)},$definedin:function(t,e){return void 0!==e[t]},$undefinedin:function(t,e){return void 0===e[t]},$regex:function(t,e){return e.test(t)},$containsString:function(t,e){return"string"==typeof t&&-1!==t.indexOf(e)},$containsNone:function(t,e){return!O.$containsAny(t,e)},$containsAny:function(t,e){var i=h(t);return null!==i&&(Array.isArray(e)?e.some(i):i(e))},$contains:function(t,e){var i=h(t);return null!==i&&(Array.isArray(e)?e.every(i):i(e))},$elemMatch:function(t,e){return!!Array.isArray(t)&&t.some(function(t){return Object.keys(e).every(function(i){var r=e[i];return"object"==typeof r&&r||(r={$eq:r}),-1!==i.indexOf(".")?l(t,i.split("."),c,e[i],t):c(t[i],r,t)})})},$type:function(t,e,i){var r=typeof t;return"object"===r&&(Array.isArray(t)?r="array":t instanceof Date&&(r="date")),"object"!=typeof e?r===e:c(r,e,i)},$finite:function(t,e){return e===isFinite(t)},$size:function(t,e,i){return!!Array.isArray(t)&&("object"!=typeof e?t.length===e:c(t.length,e,i))},$len:function(t,e,i){return"string"==typeof t&&("object"!=typeof e?t.length===e:c(t.length,e,i))},$where:function(t,e){return!0===e(t)},$not:function(t,e,i){return!c(t,e,i)},$and:function(t,e,i){for(var r=0,n=e.length;r<n;r+=1)if(!c(t,e[r],i))return!1;return!0},$or:function(t,e,i){for(var r=0,n=e.length;r<n;r+=1)if(c(t,e[r],i))return!0;return!1},$exists:function(t,e){return e?void 0!==t:void 0===t}};["$eq","$aeq","$ne","$dteq","$gt","$gte","$lt","$lte","$jgt","$jgte","$jlt","$jlte","$type"].forEach(function(t){var e=O[t];O["$"+t]=function(t,i,r){if("string"==typeof i)return e(t,r[i]);if("function"==typeof i)return e(t,i(r));throw new Error("Invalid argument to $$ matcher")}});var $={$eq:O.$eq,$aeq:!0,$dteq:!0,$gt:!0,$gte:!0,$lt:!0,$lte:!0,$in:!0,$between:!0};return u.prototype.events={},u.prototype.asyncListeners=!1,u.prototype.on=function(t,e){var i,r=this;return Array.isArray(t)?(t.forEach(function(t){r.on(t,e)}),e):(i=this.events[t],i||(i=this.events[t]=[]),i.push(e),e)},u.prototype.emit=function(t){var e,i=this;if(!t||!this.events[t])throw new Error("No event "+t+" defined");this.events[t].length&&(e=Array.prototype.slice.call(arguments,1),this.events[t].forEach(function(t){i.asyncListeners?setTimeout(function(){t.apply(i,e)},1):t.apply(i,e)}))},u.prototype.addListener=u.prototype.on,u.prototype.removeListener=function(t,e){var i=this;if(Array.isArray(t))return void t.forEach(function(t){i.removeListener(t,e)});if(this.events[t]){var r=this.events[t];r.splice(r.indexOf(e),1)}},f.prototype=new u,f.prototype.constructor=f,f.prototype.configureOptions=function(t,e){var i={NODEJS:"fs",BROWSER:"localStorage",CORDOVA:"localStorage",MEMORY:"memory"},r={memory:p};if(this.options={},this.persistenceMethod=null,this.persistenceAdapter=null,void 0!==t){if(this.options=t,this.options.hasOwnProperty("persistenceMethod")&&"function"==typeof r[t.persistenceMethod]&&(this.persistenceMethod=t.persistenceMethod,this.persistenceAdapter=new r[t.persistenceMethod]),this.options.hasOwnProperty("adapter")&&(this.persistenceMethod="adapter",this.persistenceAdapter=t.adapter,this.options.adapter=null,this.isIncremental="incremental"===this.persistenceAdapter.mode),t.autoload&&e){var n=this;setTimeout(function(){n.loadDatabase(t,t.autoloadCallback)},1)}this.options.hasOwnProperty("autosaveInterval")&&(this.autosaveDisable(),this.autosaveInterval=parseInt(this.options.autosaveInterval,10)),this.options.hasOwnProperty("autosave")&&this.options.autosave&&(this.autosaveDisable(),this.autosave=!0,this.options.hasOwnProperty("autosaveCallback")?this.autosaveEnable(t,t.autosaveCallback):this.autosaveEnable()),this.options.hasOwnProperty("throttledSaves")&&(this.throttledSaves=this.options.throttledSaves)}this.options.hasOwnProperty("serializationMethod")||(this.options.serializationMethod="normal"),this.options.hasOwnProperty("destructureDelimiter")||(this.options.destructureDelimiter="$<\n"),null===this.persistenceAdapter&&(this.persistenceMethod=i[this.ENV],this.persistenceMethod&&(this.persistenceAdapter=new r[this.persistenceMethod]))},f.prototype.copy=function(t){var e,i,r=new f(this.filename,{env:"NA"});if(t=t||{},r.loadJSONObject(this,{retainDirtyFlags:!0}),t.hasOwnProperty("removeNonSerializable")&&!0===t.removeNonSerializable)for(r.autosaveHandle=null,r.persistenceAdapter=null,e=r.collections.length,i=0;i<e;i++)r.collections[i].constraints=null,r.collections[i].ttl=null;return r},f.prototype.addCollection=function(t,e){var i,r=this.collections.length;for(i=0;i<r;i+=1)if(this.collections[i].name===t)return this.collections[i];var n=new g(t,e);return n.isIncremental=this.isIncremental,this.collections.push(n),this.verbose&&(n.lokiConsoleWrapper=console),n},f.prototype.loadCollection=function(t){if(!t.name)throw new Error("Collection must have a name property to be loaded");this.collections.push(t)},f.prototype.getCollection=function(t){var e,i=this.collections.length;for(e=0;e<i;e+=1)if(this.collections[e].name===t)return this.collections[e];return this.emit("warning","collection "+t+" not found"),null},f.prototype.renameCollection=function(t,e){var i=this.getCollection(t);return i&&(i.name=e),i},f.prototype.removeCollection=function(t){var e,i=this.collections.length;for(e=0;e<i;e+=1)if(this.collections[e].name===t){var r=new g(t,{}),n=this.collections[e];for(var s in n)n.hasOwnProperty(s)&&r.hasOwnProperty(s)&&(n[s]=r[s]);return void this.collections.splice(e,1)}},f.prototype.getName=function(){return this.name},f.prototype.serializeReplacer=function(t,e){switch(t){case"autosaveHandle":case"persistenceAdapter":case"constraints":case"ttl":return null;case"throttledSavePending":case"throttledCallbacks":return;case"lokiConsoleWrapper":return null;default:return e}},f.prototype.serialize=function(t){switch(t=t||{},t.hasOwnProperty("serializationMethod")||(t.serializationMethod=this.options.serializationMethod),t.serializationMethod){case"normal":return JSON.stringify(this,this.serializeReplacer);case"pretty":return JSON.stringify(this,this.serializeReplacer,2);case"destructured":return this.serializeDestructured();default:return JSON.stringify(this,this.serializeReplacer)}},f.prototype.toJson=f.prototype.serialize,f.prototype.loadJSON=function(t,e){var i;if(0===t.length)i={};else switch(this.options.serializationMethod){case"normal":case"pretty":i=JSON.parse(t);break;case"destructured":i=this.deserializeDestructured(t);break;default:i=JSON.parse(t)}this.loadJSONObject(i,e)},f.prototype.loadJSONObject=function(e,i){var r,n,s,a,o,l,h=0,c=e.collections?e.collections.length:0;for(this.name=e.name,e.hasOwnProperty("throttledSaves")&&i&&!i.hasOwnProperty("throttledSaves")&&(this.throttledSaves=e.throttledSaves),this.collections=[],h;h<c;h+=1){if(r=e.collections[h],n=this.addCollection(r.name,{disableChangesApi:r.disableChangesApi,disableDeltaChangesApi:r.disableDeltaChangesApi,disableMeta:r.disableMeta,disableFreeze:!r.hasOwnProperty("disableFreeze")||r.disableFreeze}),n.adaptiveBinaryIndices=!!r.hasOwnProperty("adaptiveBinaryIndices")&&!0===r.adaptiveBinaryIndices,n.transactional=r.transactional,n.asyncListeners=r.asyncListeners,n.cloneObjects=r.cloneObjects,n.cloneMethod=r.cloneMethod||"parse-stringify",n.autoupdate=r.autoupdate,n.changes=r.changes,n.dirtyIds=r.dirtyIds||[],i&&!0===i.retainDirtyFlags?n.dirty=r.dirty:n.dirty=!1,r.getData){if(i&&i.hasOwnProperty(r.name)||!n.disableFreeze||n.autoupdate)throw new Error("this collection cannot be loaded lazily: "+r.name);n.getData=r.getData,Object.defineProperty(n,"data",{get:function(){var t=this.getData();return this.getData=null,Object.defineProperty(this,"data",{value:t,writable:!0}),t}})}else if(s=r.data.length,a=0,i&&i.hasOwnProperty(r.name))for(o=function(t){var e,r=i[t.name];return r.proto?(e=r.inflate||m.copyProperties,function(t){var i=new r.proto;return e(t,i),i}):r.inflate}(r),a;a<s;a++)l=o(r.data[a]),n.data[a]=l,n.addAutoUpdateObserver(l),n.disableFreeze||t(n.data[a]);else for(a;a<s;a++)n.data[a]=r.data[a],n.addAutoUpdateObserver(n.data[a]),n.disableFreeze||t(n.data[a]);n.maxId=void 0===r.maxId?0:r.maxId,void 0!==r.binaryIndices&&(n.binaryIndices=r.binaryIndices),void 0!==r.transforms&&(n.transforms=r.transforms),n.uniqueNames=[],r.hasOwnProperty("uniqueNames")&&(n.uniqueNames=r.uniqueNames),e.databaseVersion<1.5&&(n.ensureAllIndexes(!0),n.dirty=!0)}},f.prototype.close=function(t){this.autosave&&(this.autosaveDisable(),this.autosaveDirty()&&(this.saveDatabase(t),t=void 0)),t&&this.on("close",t),this.emit("close")},p.prototype.loadDatabase=function(t,e){var i=this;this.options.asyncResponses?setTimeout(function(){e(i.hashStore.hasOwnProperty(t)?i.hashStore[t].value:null)},this.options.asyncTimeout):e(this.hashStore.hasOwnProperty(t)?this.hashStore[t].value:null)},p.prototype.saveDatabase=function(t,e,i){var r,n=this;this.options.asyncResponses?setTimeout(function(){r=n.hashStore.hasOwnProperty(t)?n.hashStore[t].savecount:0,n.hashStore[t]={savecount:r+1,lastsave:new Date,value:e},i()},this.options.asyncTimeout):(r=this.hashStore.hasOwnProperty(t)?this.hashStore[t].savecount:0,this.hashStore[t]={savecount:r+1,lastsave:new Date,value:e},i())},p.prototype.deleteDatabase=function(t,e){this.hashStore.hasOwnProperty(t)&&delete this.hashStore[t],"function"==typeof e&&e()},f.prototype.throttledSaveDrain=function(t,e){var i=this,r=(new Date).getTime();if(this.throttledSaves||t(!0),e=e||{},e.hasOwnProperty("recursiveWait")||(e.recursiveWait=!0),e.hasOwnProperty("recursiveWaitLimit")||(e.recursiveWaitLimit=!1),e.hasOwnProperty("recursiveWaitLimitDuration")||(e.recursiveWaitLimitDuration=2e3),e.hasOwnProperty("started")||(e.started=(new Date).getTime()),this.throttledSaves&&this.throttledSavePending){if(!e.recursiveWait)return void this.throttledCallbacks.push(t);this.throttledCallbacks.push(function(){return i.throttledSavePending?e.recursiveWaitLimit&&r-e.started>e.recursiveWaitLimitDuration?void t(!1):void i.throttledSaveDrain(t,e):void t(!0)})}else t(!0)},f.prototype.loadDatabaseInternal=function(t,e){var i=e||function(t,e){if(t)throw t},r=this;null!==this.persistenceAdapter?this.persistenceAdapter.loadDatabase(this.filename,function(e){if("string"==typeof e){var n=!1;try{r.loadJSON(e,t||{}),n=!0}catch(t){i(t)}n&&(i(null),r.emit("loaded","database "+r.filename+" loaded"))}else{if(!e)return i(null),void r.emit("loaded","empty database "+r.filename+" loaded");if(e instanceof Error)return void i(e);if("object"==typeof e)return r.loadJSONObject(e,t||{}),i(null),void r.emit("loaded","database "+r.filename+" loaded");i("unexpected adapter response : "+e)}}):i(new Error("persistenceAdapter not configured"))},f.prototype.loadDatabase=function(t,e){var i=this;if(!this.throttledSaves)return void this.loadDatabaseInternal(t,e);this.throttledSaveDrain(function(r){if(r)return i.throttledSavePending=!0,void i.loadDatabaseInternal(t,function(t){0===i.throttledCallbacks.length?i.throttledSavePending=!1:i.saveDatabase(),"function"==typeof e&&e(t)});"function"==typeof e&&e(new Error("Unable to pause save throttling long enough to read database"))},t)},f.prototype.saveDatabaseInternal=function(t){var e=t||function(t){if(t)throw t},i=this;if(!this.persistenceAdapter)return void e(new Error("persistenceAdapter not configured"));if("incremental"===this.persistenceAdapter.mode){var r;this.ignoreAutosave=!0,this.persistenceAdapter.saveDatabase(this.filename,function(){if(i.ignoreAutosave=!1,r)return void e(new Error("adapter error - getLokiCopy called more than once"));var t=i.copy({removeNonSerializable:!0});return r=i.collections.map(function(t){return[t.dirty,t.dirtyIds]}),i.collections.forEach(function(t){t.dirty=!1,t.dirtyIds=[]}),t},function(t){i.ignoreAutosave=!1,t&&r&&i.collections.forEach(function(t,e){var i=r[e];t.dirty=t.dirty||i[0],t.dirtyIds=t.dirtyIds.concat(i[1])}),e(t)})}else"reference"===this.persistenceAdapter.mode&&"function"==typeof this.persistenceAdapter.exportDatabase?this.persistenceAdapter.exportDatabase(this.filename,this.copy({removeNonSerializable:!0}),function(t){i.autosaveClearFlags(),e(t)}):(this.autosaveClearFlags(),this.persistenceAdapter.saveDatabase(this.filename,this.serialize(),function(t){e(t)}))},f.prototype.saveDatabase=function(t){if(!this.throttledSaves)return void this.saveDatabaseInternal(t);if(this.throttledSavePending)return void this.throttledCallbacks.push(t);var e=this.throttledCallbacks;this.throttledCallbacks=[],e.unshift(t),this.throttledSavePending=!0;var i=this;this.saveDatabaseInternal(function(t){i.throttledSavePending=!1,e.forEach(function(e){"function"==typeof e&&setTimeout(function(){e(t)},1)}),i.throttledCallbacks.length>0&&i.saveDatabase()})},f.prototype.save=f.prototype.saveDatabase,f.prototype.deleteDatabase=function(t,e){var i=e||function(t,e){if(t)throw t};"function"!=typeof t||e||(i=t),null!==this.persistenceAdapter?this.persistenceAdapter.deleteDatabase(this.filename,function(t){i(t)}):i(new Error("persistenceAdapter not configured"))},f.prototype.autosaveDirty=function(){for(var t=0;t<this.collections.length;t++)if(this.collections[t].dirty)return!0;return!1},f.prototype.autosaveClearFlags=function(){for(var t=0;t<this.collections.length;t++)this.collections[t].dirty=!1},f.prototype.autosaveEnable=function(t,e){this.autosave=!0;var i=5e3,r=this;void 0!==this.autosaveInterval&&null!==this.autosaveInterval&&(i=this.autosaveInterval),this.autosaveHandle=setInterval(function(){r.autosaveDirty()&&!r.ignoreAutosave&&r.saveDatabase(e)},i)},f.prototype.autosaveDisable=function(){void 0!==this.autosaveHandle&&null!==this.autosaveHandle&&(clearInterval(this.autosaveHandle),this.autosaveHandle=null)},y.prototype.reset=function(){return this.filteredrows.length>0&&(this.filteredrows=[]),this.filterInitialized=!1,this},y.prototype.toJSON=function(){var t=this.copy();return t.collection=null,t},y.prototype.limit=function(t){this.filterInitialized||0!==this.filteredrows.length||(this.filteredrows=this.collection.prepareFullDocIndex());var e=new y(this.collection);return e.filteredrows=this.filteredrows.slice(0,t),e.filterInitialized=!0,e},y.prototype.offset=function(t){this.filterInitialized||0!==this.filteredrows.length||(this.filteredrows=this.collection.prepareFullDocIndex());var e=new y(this.collection);return e.filteredrows=this.filteredrows.slice(t),e.filterInitialized=!0,e},y.prototype.copy=function(){var t=new y(this.collection);return this.filteredrows.length>0&&(t.filteredrows=this.filteredrows.slice()),t.filterInitialized=this.filterInitialized,t},y.prototype.branch=y.prototype.copy,y.prototype.sort=function(t){this.filterInitialized||0!==this.filteredrows.length||(this.filteredrows=this.collection.prepareFullDocIndex());var e=function(t,e){return function(i,r){return t(e[i],e[r])}}(t,this.collection.data);return this.filteredrows.sort(e),this},y.prototype.simplesort=function(t,e){var i,r=10,n=this.collection.data.length,s=this.filteredrows.length,o=this.collection.binaryIndices.hasOwnProperty(t);if(void 0!==e&&!1!==e||(e={desc:!1}),!0===e&&(e={desc:!0}),0===s){if(this.filterInitialized)return this;if(this.collection.binaryIndices.hasOwnProperty(t))return this.collection.ensureIndex(t),this.filteredrows=this.collection.binaryIndices[t].values.slice(0),e.desc&&this.filteredrows.reverse(),this;this.filteredrows=this.collection.prepareFullDocIndex()}else if(!e.disableIndexIntersect&&o&&(i=n/s,e.useJavascriptSorting&&(r=6),i<=r||e.forceIndexIntersect)){var l,h=this.filteredrows,c={};for(l=0;l<s;l++)c[h[l]]=!0;var d=this.collection.binaryIndices[t].values;return this.filteredrows=d.filter(function(t){return c[t]}),e.desc&&this.filteredrows.reverse(),this}if(e.useJavascriptSorting)return this.sort(function(e,i){return e[t]===i[t]?0:e[t]>i[t]?1:e[t]<i[t]?-1:void 0});var u=function(t,e,i){var r,n,s;return function(o,l){return~t.indexOf(".")?(s=t.split("."),r=m.getIn(i[o],s,!0),n=m.getIn(i[l],s,!0)):(r=i[o][t],n=i[l][t]),a(r,n,e)}}(t,e.desc,this.collection.data);return this.filteredrows.sort(u),this},y.prototype.compoundsort=function(t){if(0===t.length)throw new Error("Invalid call to compoundsort, need at least one property");var e;if(1===t.length)return e=t[0],Array.isArray(e)?this.simplesort(e[0],e[1]):this.simplesort(e,!1);for(var i=0,r=t.length;i<r;i+=1)e=t[i],Array.isArray(e)||(t[i]=[e,!1]);this.filterInitialized||0!==this.filteredrows.length||(this.filteredrows=this.collection.prepareFullDocIndex());var n=function(t,e){return function(i,r){return o(t,e[i],e[r])}}(t,this.collection.data);return this.filteredrows.sort(n),this},y.prototype.findOr=function(t){for(var e=null,i=0,r=0,n=[],s=[],a=0,o=(this.count(),0),l=t.length;o<l;o++)for(e=this.branch().find(t[o]).filteredrows,r=e.length,i=0;i<r;i++)a=e[i],void 0===s[a]&&(s[a]=!0,n.push(a));return this.filteredrows=n,this.filterInitialized=!0,this},y.prototype.$or=y.prototype.findOr,y.prototype.findAnd=function(t){for(var e=0,i=t.length;e<i;e++){if(0===this.count())return this;this.find(t[e])}return this},y.prototype.$and=y.prototype.findAnd,y.prototype.find=function(t,e){if(0===this.collection.data.length)return this.filteredrows=[],this.filterInitialized=!0,this;var i,r,n,s,a,o,h,c=t||"getAll",d=!1,u=[],f=[],p=null;if(e=e||!1,"object"==typeof c){for(i in c)s={},s[i]=c[i],f.push(s),I.call(c,i)&&(r=i,n=c[i]);if(f.length>1)return this.find({$and:f},e)}if(!r||"getAll"===c)return e&&(this.filterInitialized?this.filteredrows=this.filteredrows.slice(0,1):(this.filteredrows=this.collection.data.length>0?[0]:[],this.filterInitialized=!0)),this;if("$and"===r||"$or"===r)return this[r](n),e&&this.filteredrows.length>1&&(this.filteredrows=this.filteredrows.slice(0,1)),this;if(null===n||"object"!=typeof n||n instanceof Date)a="$eq",o=n;else{if("object"!=typeof n)throw new Error("Do not know what you want to do.");for(h in n)if(I.call(n,h)){a=h,o=n[h];break}}"$regex"!==a&&"object"!=typeof o||(o=v(a,o));var y=-1!==r.indexOf(".");!this.filterInitialized&&this.collection.binaryIndices[r]&&$[a]&&(!0!==this.collection.adaptiveBinaryIndices&&this.collection.ensureIndex(r),d=!0,p=this.collection.binaryIndices[r]),!d&&"$in"===a&&Array.isArray(o)&&"undefined"!=typeof Set&&(o=new Set(o),a="$inSet");var g,b,w=O[a],k=this.collection.data,A=0,x=0,D=0;if(this.filterInitialized){if(g=this.filteredrows,x=g.length,y){for(r=r.split("."),A=0;A<x;A++)if(D=g[A],b=k[D],l(b,r,w,o,b)&&(u.push(D),e))return this.filteredrows=u,this}else for(A=0;A<x;A++)if(D=g[A],b=k[D],w(b[r],o,b)&&(u.push(D),e))return this.filteredrows=u,this}else if(d){var S=this.collection.calculateRange(a,r,o);if("$in"!==a){for(A=S[0];A<=S[1];A++)if(!0!==$[a]){if($[a](m.getIn(k[p.values[A]],r,y),o)&&(u.push(p.values[A]),e))return this.filteredrows=u,this.filterInitialized=!0,this}else if(u.push(p.values[A]),e)return this.filteredrows=u,this.filterInitialized=!0,this}else for(A=0,x=S.length;A<x;A++)if(u.push(p.values[S[A]]),e)return this.filteredrows=u,this.filterInitialized=!0,this}else if(x=k.length,y){for(r=r.split("."),A=0;A<x;A++)if(b=k[A],l(b,r,w,o,b)&&(u.push(A),e))return this.filteredrows=u,this.filterInitialized=!0,this}else for(A=0;A<x;A++)if(b=k[A],w(b[r],o,b)&&(u.push(A),e))return this.filteredrows=u,this.filterInitialized=!0,this;return this.filteredrows=u,this.filterInitialized=!0,this},y.prototype.where=function(t){var e,i=[];if("function"!=typeof t)throw new TypeError("Argument is not a stored view or a function");e=t;try{if(this.filterInitialized){for(var r=this.filteredrows.length;r--;)!0===e(this.collection.data[this.filteredrows[r]])&&i.push(this.filteredrows[r]);return this.filteredrows=i,this}for(var n=this.collection.data.length;n--;)!0===e(this.collection.data[n])&&i.push(n);return this.filteredrows=i,this.filterInitialized=!0,this}catch(t){throw t}},y.prototype.count=function(){return this.filterInitialized?this.filteredrows.length:this.collection.count()},y.prototype.data=function(t){var e,i,r,n,s=[],a=this.collection.data;if(t=t||{},t.removeMeta&&!t.forceClones&&(t.forceClones=!0,t.forceCloneMethod=t.forceCloneMethod||"shallow"),!this.collection.disableDeltaChangesApi&&this.collection.disableFreeze&&(t.forceClones=!0,t.forceCloneMethod="parse-stringify"),!this.filterInitialized){if(0===this.filteredrows.length){if(this.collection.cloneObjects||t.forceClones){for(i=a.length,n=t.forceCloneMethod||this.collection.cloneMethod,r=0;r<i;r++)e=d(a[r],n),t.removeMeta&&(delete e.$loki,delete e.meta),s.push(e);return s}return a.slice()}this.filterInitialized=!0}var o=this.filteredrows;if(i=o.length,this.collection.cloneObjects||t.forceClones)for(n=t.forceCloneMethod||this.collection.cloneMethod,r=0;r<i;r++)e=d(a[o[r]],n),t.removeMeta&&(delete e.$loki,delete e.meta),s.push(e);else for(r=0;r<i;r++)s.push(a[o[r]]);return s},y.prototype.update=function(t){if("function"!=typeof t)throw new TypeError("Argument is not a function");this.filterInitialized||0!==this.filteredrows.length||(this.filteredrows=this.collection.prepareFullDocIndex());for(var e,i=this.filteredrows.length,r=this.collection.data,n=0;n<i;n++)this.disableFreeze&&!this.collection.cloneObjects&&this.collection.disableDeltaChangesApi?(t(r[this.filteredrows[n]]),this.collection.update(r[this.filteredrows[n]])):(e=d(r[this.filteredrows[n]],this.collection.cloneMethod),t(e),this.collection.update(e));return this},y.prototype.remove=function(){return this.filterInitialized||0!==this.filteredrows.length||(this.filteredrows=this.collection.prepareFullDocIndex()),this.collection.removeBatchByPositions(this.filteredrows),this.filteredrows=[],this},y.prototype.map=function(t,e){var i=this.data(e).map(t);return this.collection=new g("mappedData"),this.collection.insert(i),this.filteredrows=[],this.filterInitialized=!1,this},g.prototype=new u,g.prototype.contructor=g,g.prototype.lokiConsoleWrapper={log:function(){},warn:function(){},error:function(){}},g.prototype.addAutoUpdateObserver=function(t){},g.prototype.removeAutoUpdateObserver=function(t){},g.prototype.prepareFullDocIndex=function(){for(var t=this.data.length,e=new Array(t),i=0;i<t;i+=1)e[i]=i;return e},g.prototype.configureOptions=function(t){t=t||{},t.hasOwnProperty("adaptiveBinaryIndices")&&(this.adaptiveBinaryIndices=t.adaptiveBinaryIndices,this.adaptiveBinaryIndices&&this.ensureAllIndexes())},g.prototype.ensureIndex=function(t,e){if(void 0===e&&(e=!1),null===t||void 0===t)throw new Error("Attempting to set index without an associated property");if((!this.binaryIndices[t]||e||this.binaryIndices[t].dirty)&&(!0!==this.adaptiveBinaryIndices||!this.binaryIndices.hasOwnProperty(t)||e)){var i={name:t,dirty:!0,values:this.prepareFullDocIndex()};this.binaryIndices[t]=i;var r=function(t,e){var i,r,n=!!~t.indexOf(".")&&t.split(".");return function(s,a){if(n?(i=m.getIn(e[s],n,!0),r=m.getIn(e[a],n,!0)):(i=e[s][t],r=e[a][t]),i!==r){if(w.lt(i,r,!1))return-1;if(w.gt(i,r,!1))return 1}return 0}}(t,this.data);i.values.sort(r),i.dirty=!1,this.dirty=!0}},g.prototype.checkAllIndexes=function(t){var e,i=this.binaryIndices,r=[];for(e in i)I.call(i,e)&&(this.checkIndex(e,t)||r.push(e));return r},g.prototype.checkIndex=function(t,e){e=e||{},e.randomSamplingFactor&&!1!==e.randomSampling&&(e.randomSampling=!0),e.randomSamplingFactor=e.randomSamplingFactor||.1,(e.randomSamplingFactor<0||e.randomSamplingFactor>1)&&(e.randomSamplingFactor=.1);var i,r,n,s,a,o=!0;if(!this.binaryIndices.hasOwnProperty(t))throw new Error("called checkIndex on property without an index: "+t);if(this.adaptiveBinaryIndices||this.ensureIndex(t),a=this.binaryIndices[t].values,(s=a.length)!==this.data.length)return e.repair&&this.ensureIndex(t,!0),!1;if(0===s)return!0;var l=-1!==t.indexOf(".");if(1===s)o=0===a[0];else if(e.randomSampling){if(O.$lte(m.getIn(this.data[a[0]],t,l),m.getIn(this.data[a[1]],t,l))||(o=!1),O.$lte(m.getIn(this.data[a[s-2]],t,l),m.getIn(this.data[a[s-1]],t,l))||(o=!1),o)for(r=Math.floor((s-1)*e.randomSamplingFactor),i=0;i<r-1;i++)if(n=Math.floor(Math.random()*(s-1)),!O.$lte(m.getIn(this.data[a[n]],t,l),m.getIn(this.data[a[n+1]],t,l))){o=!1;break}}else for(i=0;i<s-1;i++)if(!O.$lte(m.getIn(this.data[a[i]],t,l),m.getIn(this.data[a[i+1]],t,l))){o=!1;break}return!o&&e.repair&&this.ensureIndex(t,!0),o},g.prototype.getBinaryIndexValues=function(t){var e,i=this.binaryIndices[t].values,r=[];for(e=0;e<i.length;e++)r.push(m.getIn(this.data[i[e]],t,!0));return r},g.prototype.getUniqueIndex=function(t,e){var i=this.constraints.unique[t];return!i&&e?this.ensureUniqueIndex(t):i},g.prototype.ensureUniqueIndex=function(t){var e=this.constraints.unique[t];return e||-1==this.uniqueNames.indexOf(t)&&this.uniqueNames.push(t),this.constraints.unique[t]=e=new b(t),this.data.forEach(function(t){e.set(t)}),e},g.prototype.ensureAllIndexes=function(t){var e,i=this.binaryIndices;for(e in i)I.call(i,e)&&this.ensureIndex(e,t)},g.prototype.flagBinaryIndexesDirty=function(){var t,e=this.binaryIndices;for(t in e)I.call(e,t)&&(e[t].dirty=!0)},g.prototype.flagBinaryIndexDirty=function(t){this.binaryIndices[t]&&(this.binaryIndices[t].dirty=!0)},g.prototype.count=function(t){return t?this.chain().find(t).filteredrows.length:this.data.length},g.prototype.ensureId=function(){if(!this.idIndex){var t=this.data,e=0,i=t.length,r=new Array(i);for(e;e<i;e++)r[e]=t[e].$loki;this.idIndex=r}
},g.prototype.findAndUpdate=function(t,e){"function"==typeof t?this.updateWhere(t,e):this.chain().find(t).update(e)},g.prototype.findAndRemove=function(t){this.chain().find(t).remove()},g.prototype.insert=function(t,e){if(!Array.isArray(t))return this.insertOne(t);var i,r=[],n=e&&!this.cloneObjects&&this.adaptiveBinaryIndices&&Object.keys(this.binaryIndices).length>0;n&&(this.adaptiveBinaryIndices=!1);try{this.emit("pre-insert",t);for(var s=0,a=t.length;s<a;s++){if(!(i=this.insertOne(t[s],!0)))return;r.push(i)}}finally{n&&(this.ensureAllIndexes(),this.adaptiveBinaryIndices=!0)}return this.emit("insert",r),r=this.cloneObjects?d(r,this.cloneMethod):r,1===r.length?r[0]:r},g.prototype.insertOne=function(e,r){var n,s=null;if("object"!=typeof e?s=new TypeError("Document needs to be an object"):null===e&&(s=new TypeError("Object cannot be null")),null!==s)throw this.emit("error",s),s;var a=this.cloneObjects?d(e,this.cloneMethod):e;if(this.disableFreeze||(a=i(a)),this.disableMeta||(void 0===a.meta?a.meta={revision:0,created:0}:this.disableFreeze||(a.meta=i(a.meta))),r||this.emit("pre-insert",a),this.add(a))return this.disableFreeze||t(a),n=this.cloneObjects?d(a,this.cloneMethod):a,r||this.emit("insert",n),this.addAutoUpdateObserver(n),n},g.prototype.clear=function(t){var e=this;if(t=t||{},this.data=[],this.idIndex=null,this.cachedIndex=null,this.cachedBinaryIndex=null,this.cachedData=null,this.maxId=0,this.DynamicViews=[],this.dirty=!0,this.constraints={unique:{},exact:{}},!0===t.removeIndices)this.binaryIndices={},this.uniqueNames=[];else{Object.keys(this.binaryIndices).forEach(function(t){e.binaryIndices[t].dirty=!1,e.binaryIndices[t].values=[]})}},g.prototype.update=function(e){var i,r,n;if(Array.isArray(e)){n=e.length,(i=!this.cloneObjects&&this.adaptiveBinaryIndices&&Object.keys(this.binaryIndices).length>0)&&(this.adaptiveBinaryIndices=!1);try{for(r=0;r<n;r+=1)this.update(e[r])}finally{i&&(this.ensureAllIndexes(),this.adaptiveBinaryIndices=!0)}}else{if(!I.call(e,"$loki"))throw new Error("Trying to update unsynced document. Please save the document first by using insert() or addMany()");try{this.startTransaction();var s,a,o,l=this.get(e.$loki,!0),h=this;if(!l)throw new Error("Trying to update a document not in collection.");s=l[0],o=l[1],a=this.cloneObjects||!this.disableDeltaChangesApi&&this.disableFreeze?d(e,this.cloneMethod):e,this.emit("pre-update",e),this.uniqueNames.forEach(function(t){h.getUniqueIndex(t,!0).update(s,a)}),this.data[o]=a,a!==e&&this.addAutoUpdateObserver(e);for(var c=0;c<this.DynamicViews.length;c++)this.DynamicViews[c].evaluateDocument(o,!1);var u;if(this.adaptiveBinaryIndices){var f=this.binaryIndices;for(u in f)this.adaptiveBinaryIndexUpdate(o,u)}else this.flagBinaryIndexesDirty();this.idIndex[o]=a.$loki,this.isIncremental&&this.dirtyIds.push(a.$loki),this.commit(),this.dirty=!0,this.disableFreeze||t(a);var p;return p=this.cloneObjects?d(a,this.cloneMethod):a,this.emit("update",p,s),p}catch(t){throw this.rollback(),this.lokiConsoleWrapper.error(t.message),this.emit("error",t),t}}},g.prototype.add=function(t){if("object"!=typeof t)throw new TypeError("Object being added needs to be an object");if(void 0!==t.$loki)throw new Error("Document is already in collection, please use update()");try{this.startTransaction(),this.maxId++,isNaN(this.maxId)&&(this.maxId=this.data[this.data.length-1].$loki+1);var e=this.maxId;t.$loki=e,this.disableMeta||(t.meta.version=0);for(var i=0,r=this.uniqueNames.length;i<r;i++)this.getUniqueIndex(this.uniqueNames[i],!0).set(t);this.idIndex&&this.idIndex.push(e),this.isIncremental&&this.dirtyIds.push(e),this.data.push(t);var n=this.data.length-1,s=this.DynamicViews.length;for(i=0;i<s;i++)this.DynamicViews[i].evaluateDocument(n,!0);if(this.adaptiveBinaryIndices){var a=this.binaryIndices;for(var o in a)this.adaptiveBinaryIndexInsert(n,o)}else this.flagBinaryIndexesDirty();return this.commit(),this.dirty=!0,this.cloneObjects?d(t,this.cloneMethod):t}catch(t){throw this.rollback(),this.lokiConsoleWrapper.error(t.message),this.emit("error",t),t}},g.prototype.updateWhere=function(t,e){var i,r=this.where(t),n=0;try{for(n;n<r.length;n++)i=e(r[n]),this.update(i)}catch(t){this.rollback(),this.lokiConsoleWrapper.error(t.message)}},g.prototype.removeWhere=function(t){var e;"function"==typeof t?(e=this.data.filter(t),this.remove(e)):this.chain().find(t).remove()},g.prototype.removeDataOnly=function(){this.remove(this.data.slice())},g.prototype.removeBatchByPositions=function(t){var e,i,r,n,s=t.length,a={},o=Object.keys(this.binaryIndices).length,l=Object.keys(this.constraints.unique).length,h=this.adaptiveBinaryIndices&&Object.keys(this.binaryIndices).length>0,c=this;try{for(this.startTransaction(),this.ensureId(),r=0;r<s;r++)a[this.idIndex[t[r]]]=!0;if((e=this.DynamicViews.length)>0||o>0||l>0){if(e>0)for(i=0;i<e;i++)this.DynamicViews[i].removeDocument(t);if(this.adaptiveBinaryIndices&&!h){var d,u=this.binaryIndices;for(d in u)this.adaptiveBinaryIndexRemove(t,d)}else this.flagBinaryIndexesDirty();l&&this.uniqueNames.forEach(function(e){var i=c.getUniqueIndex(e);if(i)for(r=0;r<s;r++)n=c.data[t[r]],null!==n[e]&&void 0!==n[e]&&i.remove(n[e])})}if(!this.disableChangesApi||this.events.delete.length>1)for(r=0;r<s;r++)this.emit("delete",this.data[t[r]]);if(this.data=this.data.filter(function(t){return!a[t.$loki]}),this.isIncremental)for(r=0;r<s;r++)this.dirtyIds.push(this.idIndex[t[r]]);this.idIndex=this.idIndex.filter(function(t){return!a[t]}),this.adaptiveBinaryIndices&&h&&(this.adaptiveBinaryIndices=!1,this.ensureAllIndexes(!0),this.adaptiveBinaryIndices=!0),this.commit(),this.dirty=!0}catch(t){return this.rollback(),h&&(this.adaptiveBinaryIndices=!0),this.lokiConsoleWrapper.error(t.message),this.emit("error",t),null}},g.prototype.removeBatch=function(t){var e,i=t.length,r=this.data.length,n={},s=[];for(e=0;e<r;e++)n[this.data[e].$loki]=e;for(e=0;e<i;e++)"object"==typeof t[e]?s.push(n[t[e].$loki]):s.push(n[t[e]]);this.removeBatchByPositions(s)},g.prototype.remove=function(t){if("number"==typeof t&&(t=this.get(t)),"object"!=typeof t)throw new Error("Parameter is not an object");if(Array.isArray(t))return void this.removeBatch(t);if(!I.call(t,"$loki"))throw new Error("Object is not a document stored in the collection");try{this.startTransaction();var r=this.get(t.$loki,!0),n=r[1],s=this;this.uniqueNames.forEach(function(e){if(null!==t[e]&&void 0!==t[e]){var i=s.getUniqueIndex(e);i&&i.remove(t[e])}});for(var a=0;a<this.DynamicViews.length;a++)this.DynamicViews[a].removeDocument(n);if(this.adaptiveBinaryIndices){var o,l=this.binaryIndices;for(o in l)this.adaptiveBinaryIndexRemove(n,o)}else this.flagBinaryIndexesDirty();return this.data.splice(n,1),this.removeAutoUpdateObserver(t),this.idIndex.splice(n,1),this.isIncremental&&this.dirtyIds.push(t.$loki),this.commit(),this.dirty=!0,this.emit("delete",r[0]),this.disableFreeze||(t=i(t)),delete t.$loki,delete t.meta,this.disableFreeze||e(t),t}catch(t){return this.rollback(),this.lokiConsoleWrapper.error(t.message),this.emit("error",t),null}},g.prototype.get=function(t,e){this.idIndex||this.ensureId();var i=e||!1,r=this.idIndex,n=r.length-1,s=0,a=s+n>>1;if(t="number"==typeof t?t:parseInt(t,10),isNaN(t))throw new TypeError("Passed id is not an integer");for(;r[s]<r[n];)a=s+n>>1,r[a]<t?s=a+1:n=a;return n===s&&r[s]===t?i?[this.data[s],s]:this.data[s]:null},g.prototype.getBinaryIndexPosition=function(t,e){var i=m.getIn(this.data[t],e,!0),r=this.binaryIndices[e].values,n=this.calculateRange("$eq",e,i);if(0===n[0]&&-1===n[1])return null;for(var s=n[0],a=n[1],o=s;o<=a;o++)if(r[o]===t)return o;return null},g.prototype.adaptiveBinaryIndexInsert=function(t,e){var i=-1!==e.indexOf("."),r=this.binaryIndices[e].values,n=m.getIn(this.data[t],e,i);!0===this.serializableIndices&&n instanceof Date&&(this.data[t][e]=n.getTime(),n=m.getIn(this.data[t],e));var s=0===r.length?0:this.calculateRangeStart(e,n,!0,i);this.binaryIndices[e].values.splice(s,0,t)},g.prototype.adaptiveBinaryIndexUpdate=function(t,e){var i,r=this.binaryIndices[e].values,n=r.length;for(i=0;i<n&&r[i]!==t;i++);this.binaryIndices[e].values.splice(i,1),this.adaptiveBinaryIndexInsert(t,e)},g.prototype.adaptiveBinaryIndexRemove=function(t,e,i){var r,n,s,a,o,l,h,c=this.binaryIndices[e],d={};if(Array.isArray(t)){if(1!==(a=t.length)){for(s=0;s<a;s++)d[t[s]]=!0;if(c.values=c.values.filter(function(t){return!d[t]}),!0===i)return;var u=t.slice();for(u.sort(function(t,e){return t-e}),r=c.values.length,n=0;n<r;n++){for(o=c.values[n],l=0,s=0;s<a&&o>u[s];s++)l++;c.values[n]-=l}return}t=t[0]}if(null===(h=this.getBinaryIndexPosition(t,e)))return null;if(c.values.splice(h,1),!0!==i)for(r=c.values.length,n=0;n<r;n++)c.values[n]>t&&c.values[n]--},g.prototype.calculateRangeStart=function(t,e,i,r){var n=this.data,s=this.binaryIndices[t].values,a=0,o=s.length-1,l=0;if(0===s.length)return-1;for(m.getIn(n[s[a]],t,r),m.getIn(n[s[o]],t,r);a<o;)l=a+o>>1,w.lt(m.getIn(n[s[l]],t,r),e,!1)?a=l+1:o=l;var h=a;return w.aeq(e,m.getIn(n[s[h]],t,r))?h:w.lt(e,m.getIn(n[s[h]],t,r),!1)?i?h:h-1:i?h+1:h},g.prototype.calculateRangeEnd=function(t,e,i){var r=this.data,n=this.binaryIndices[t].values,s=0,a=n.length-1,o=0;if(0===n.length)return-1;for(m.getIn(r[n[s]],t,i),m.getIn(r[n[a]],t,i);s<a;)o=s+a>>1,w.lt(e,m.getIn(r[n[o]],t,i),!1)?a=o:s=o+1;var l=a;return w.aeq(e,m.getIn(r[n[l]],t,i))?l:w.gt(e,m.getIn(r[n[l]],t,i),!1)?l+1:w.aeq(e,m.getIn(r[n[l-1]],t,i))?l-1:l},g.prototype.calculateRange=function(t,e,i){var r,n,s,a=this.data,o=this.binaryIndices[e].values,l=o.length-1;if(0===a.length)return[0,-1];var h=-1!==e.indexOf("."),c=m.getIn(a[o[0]],e,h),d=m.getIn(a[o[l]],e,h);switch(t){case"$eq":case"$aeq":case"$dteq":if(w.lt(i,c,!1)||w.gt(i,d,!1))return[0,-1];break;case"$gt":if(w.gt(i,d,!0))return[0,-1];if(w.gt(c,i,!1))return[0,l];break;case"$gte":if(w.gt(i,d,!1))return[0,-1];if(w.gt(c,i,!0))return[0,l];break;case"$lt":if(w.lt(i,c,!0))return[0,-1];if(w.lt(d,i,!1))return[0,l];break;case"$lte":if(w.lt(i,c,!1))return[0,-1];if(w.lt(d,i,!0))return[0,l];break;case"$between":return w.gt(i[0],d,!1)?[0,-1]:w.lt(i[1],c,!1)?[0,-1]:(r=this.calculateRangeStart(e,i[0],!1,h),s=this.calculateRangeEnd(e,i[1],h),r<0&&r++,s>l&&s--,w.gt(m.getIn(a[o[r]],e,h),i[0],!0)||r++,w.lt(m.getIn(a[o[s]],e,h),i[1],!0)||s--,s<r?[0,-1]:[r,s]);case"$in":for(var u=[],f=[],p=0,y=i.length;p<y;p++)for(var v=this.calculateRange("$eq",e,i[p]),g=v[0];g<=v[1];g++)void 0===u[g]&&(u[g]=!0,f.push(g));return f}switch(t){case"$eq":case"$aeq":case"$dteq":case"$gte":case"$lt":r=this.calculateRangeStart(e,i,!1,h),n=m.getIn(a[o[r]],e,h)}switch(t){case"$eq":case"$aeq":case"$dteq":case"$lte":case"$gt":s=this.calculateRangeEnd(e,i,h),m.getIn(a[o[s]],e,h)}switch(t){case"$eq":case"$aeq":case"$dteq":return w.aeq(n,i)?[r,s]:[0,-1];case"$gt":return w.aeq(m.getIn(a[o[s]],e,h),i)?[s+1,l]:[s,l];case"$gte":return w.aeq(m.getIn(a[o[r]],e,h),i)?[r,l]:[r+1,l];case"$lt":return w.aeq(m.getIn(a[o[r]],e,h),i)?[0,r-1]:[0,r];case"$lte":return w.aeq(m.getIn(a[o[s]],e,h),i)?[0,s]:[0,s-1];default:return[0,a.length-1]}},g.prototype.by=function(t,e){var i;if(void 0===e)return i=this,function(e){return i.by(t,e)};var r=this.getUniqueIndex(t,!0).get(e);return this.cloneObjects?d(r,this.cloneMethod):r},g.prototype.findOne=function(t){t=t||{};var e=this.chain().find(t,!0).data();return Array.isArray(e)&&0===e.length?null:this.cloneObjects?d(e[0],this.cloneMethod):e[0]},g.prototype.chain=function(t,e){var i=new y(this);return void 0===t?i:i.transform(t,e)},g.prototype.find=function(t){return this.chain().find(t).data()},g.prototype.findOneUnindexed=function(t,e){for(var i=this.data.length;i--;)if(m.getIn(this.data[i],t,!0)===e)return this.data[i];return null},g.prototype.startTransaction=function(){},g.prototype.commit=function(){},g.prototype.rollback=function(){},g.prototype.where=function(t){return this.chain().where(t).data()},b.prototype.keyMap={},b.prototype.lokiMap={},b.prototype.set=function(t){var e=t[this.field];if(null!==e&&void 0!==e){if(this.keyMap[e])throw new Error("Duplicate key for property "+this.field+": "+e);this.keyMap[e]=t,this.lokiMap[t.$loki]=e}},b.prototype.get=function(t){return this.keyMap[t]},b.prototype.byId=function(t){return this.keyMap[this.lokiMap[t]]},b.prototype.update=function(t,e){if(this.lokiMap[t.$loki]!==e[this.field]){var i=this.lokiMap[t.$loki];this.set(e),this.keyMap[i]=void 0}else this.keyMap[t[this.field]]=e},b.prototype.remove=function(t){var e=this.keyMap[t];if(null===e||void 0===e)throw new Error("Key is not in unique index: "+this.field);this.keyMap[t]=void 0,this.lokiMap[e.$loki]=void 0},b.prototype.clear=function(){this.keyMap=Object.create(null),this.lokiMap=Object.create(null)},f.deepFreeze=t,f.freeze=e,f.unFreeze=i,f.LokiOps=O,f.Collection=g,f.Resultset=y,f.LokiMemoryAdapter=p,f.aeq=r,f.lt=n,f.gt=s,f.Comparators=w,f}()});