Skip to content

Commit

Permalink
Completing deleting & setting indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Jun 21, 2015
1 parent 521679d commit 15f36de
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 79 deletions.
69 changes: 41 additions & 28 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
* @link http://haro.rocks
* @version 1.1.0
*/
( function (global) {
const Promise = global.Promise || require("es6-promise").Promise;
const Map = global.Map || require("es6-map");
const Set = global.Set || require("es6-set");
const fetch = global.fetch || require("node-fetch");
const tuple = global.tuple || require("tiny-tuple");
(function (global) {
const Promise = global.Promise || require("es6-promise").Promise;
const Map = global.Map || require("es6-map");
const Set = global.Set || require("es6-set");
const fetch = global.fetch || require("node-fetch");
const tuple = global.tuple || require("tiny-tuple");

function clone (arg) {
return JSON.parse(JSON.stringify(arg));
Expand Down Expand Up @@ -43,16 +43,17 @@ function merge (a, b) {
let r = [8, 9, "a", "b"];

function s () {
return ( ( ( 1 + Math.random() ) * 0x10000 ) | 0 ).toString(16).substring(1);
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

function uuid () {
return ( s() + s() + "-" + s() + "-4" + s().substr(0, 3) + "-" + r[Math.floor(Math.random() * 4)] + s().substr(0, 3) + "-" + s() + s() + s() );
return (s() + s() + "-" + s() + "-4" + s().substr(0, 3) + "-" + r[Math.floor(Math.random() * 4)] + s().substr(0, 3) + "-" + s() + s() + s());
}

class Haro {
constructor (data, config={}, indexes=[]) {
constructor (data, config={}) {
this.data = new Map();
this.delimiter = "|";
this.config = {
method: "get",
credentials: false,
Expand All @@ -61,7 +62,7 @@ class Haro {
"content-type": "application/json"
}
};
this.index = clone(indexes);
this.index = [];
this.indexes = new Map();
this.registry = [];
this.key = "";
Expand All @@ -70,14 +71,14 @@ class Haro {
this.uri = "";
this.versions = new Map();

this.index.forEach(i => {
this.indexes.set(i, new Map());
});

Object.keys(config).forEach(i => {
this[i] = merge(this[i], config[i]);
});

this.index.forEach(i => {
this.indexes.set(i, new Map());
});

if (data) {
this.batch(data, "set");
}
Expand Down Expand Up @@ -124,7 +125,8 @@ class Haro {
let defer = deferred();

let next = () => {
let index = this.registry.indexOf(key);
let index = this.registry.indexOf(key),
data = this.data.get(key);

if (index > -1) {
if (index === 0) {
Expand All @@ -135,7 +137,10 @@ class Haro {
this.registry.splice(index, 1);
}

this.removeIndex(key);
this.index.forEach(i => {
this.delIndex(key, data, i);
});

this.data.delete(key);
this.versions.delete(key);
--this.total;
Expand All @@ -159,6 +164,15 @@ class Haro {
return defer.promise;
}

delIndex (key, data, index) {
let idx = this.indexes.get(index),
value = this.keyIndex(index, data);

if (idx.has(value)) {
idx.get(value).delete(key);
}
}

entries () {
return this.data.entries();
}
Expand Down Expand Up @@ -283,23 +297,22 @@ class Haro {
return defer.promise;
}

setIndex (key, data) {
let delimiter = "|";
keyIndex (key, data) {
return key.split(this.delimiter).map(function (i) {
return data[i].toString() || "";
}).join(this.delimiter);
}

setIndex (key, data) {
this.index.forEach(i => {
let keys = i.split(delimiter),
values = "",
index = this.indexes.get(i);

keys.forEach(function (k, kdx) {
values += (kdx > 0 ? delimiter : "") + data[k];
});
let index = this.indexes.get(i),
value = this.keyIndex(i, data);

if (!index.has(values)) {
index.set(values, new Set());
if (!index.has(value)) {
index.set(value, new Set());
}

index.get(values).add(key);
index.get(value).add(key);
});

return this;
Expand Down
56 changes: 35 additions & 21 deletions lib/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
var r = [8, 9, "a", "b"];

function s() {
return ((1 + Math.random()) * 65536 | 0).toString(16).substring(1);
return ((1 + Math.random()) * 0x10000 | 0).toString(16).substring(1);
}

function uuid() {
Expand All @@ -63,11 +63,11 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
var _this = this;

var config = arguments[1] === undefined ? {} : arguments[1];
var indexes = arguments[2] === undefined ? [] : arguments[2];

_classCallCheck(this, Haro);

this.data = new Map();
this.delimiter = "|";
this.config = {
method: "get",
credentials: false,
Expand All @@ -76,7 +76,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
"content-type": "application/json"
}
};
this.index = clone(indexes);
this.index = [];
this.indexes = new Map();
this.registry = [];
this.key = "";
Expand All @@ -85,14 +85,14 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
this.uri = "";
this.versions = new Map();

this.index.forEach(function (i) {
_this.indexes.set(i, new Map());
});

Object.keys(config).forEach(function (i) {
_this[i] = merge(_this[i], config[i]);
});

this.index.forEach(function (i) {
_this.indexes.set(i, new Map());
});

if (data) {
this.batch(data, "set");
}
Expand Down Expand Up @@ -151,7 +151,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
var defer = deferred();

var next = function next() {
var index = _this4.registry.indexOf(key);
var index = _this4.registry.indexOf(key),
data = _this4.data.get(key);

if (index > -1) {
if (index === 0) {
Expand All @@ -162,7 +163,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
_this4.registry.splice(index, 1);
}

_this4.removeIndex(key);
_this4.index.forEach(function (i) {
_this4.delIndex(key, data, i);
});

_this4.data["delete"](key);
_this4.versions["delete"](key);
--_this4.total;
Expand All @@ -185,6 +189,16 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

return defer.promise;
}
}, {
key: "delIndex",
value: function delIndex(key, data, index) {
var idx = this.indexes.get(index),
value = this.keyIndex(index, data);

if (idx.has(value)) {
idx.get(value)["delete"](key);
}
}
}, {
key: "entries",
value: function entries() {
Expand Down Expand Up @@ -328,27 +342,27 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

return defer.promise;
}
}, {
key: "keyIndex",
value: function keyIndex(key, data) {
return key.split(this.delimiter).map(function (i) {
return data[i].toString() || "";
}).join(this.delimiter);
}
}, {
key: "setIndex",
value: function setIndex(key, data) {
var _this6 = this;

var delimiter = "|";

this.index.forEach(function (i) {
var keys = i.split(delimiter),
values = "",
index = _this6.indexes.get(i);

keys.forEach(function (k, kdx) {
values += (kdx > 0 ? delimiter : "") + data[k];
});
var index = _this6.indexes.get(i),
value = _this6.keyIndex(i, data);

if (!index.has(values)) {
index.set(values, new Set());
if (!index.has(value)) {
index.set(value, new Set());
}

index.get(values).add(key);
index.get(value).add(key);
});

return this;
Expand Down
2 changes: 1 addition & 1 deletion lib/haro.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/haro.min.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 15f36de

Please sign in to comment.