-
Notifications
You must be signed in to change notification settings - Fork 3
/
grafine.min.js
12 lines (12 loc) · 10.6 KB
/
grafine.min.js
1
2
3
4
5
6
7
8
9
10
11
12
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";module.exports=function(grafine){var Graph=function(hash,capacity){hash||(hash=255),capacity||(capacity=4*hash),this._hash=hash,this._capacity=capacity,this._nextId=0,this._shards=[],this._indexes=[]};return Graph.prototype.size=function(){for(var size=0,i=0;i<this._shards.length;i++)this._shards[i]&&(size+=this._shards[i]._size);return size},Graph.prototype.uuid=function(){return++this._nextId},Graph.prototype.shards=function(){return this._shards},Graph.prototype.indexes=function(){return this._indexes},Graph.prototype.shard=function(uuid){
var id=(uuid-uuid%this._capacity)/this._capacity%this._hash;return this._shards[id]||(this._shards[id]=this.createShard(id)),this._shards[id]},Graph.prototype.createShard=function(id){return new grafine.shard(this,id)},Graph.prototype.getIndex=function(key){var id=0;if("number"==typeof key)id=key%this._hash;else{var size=key.length;size>this._hash&&(size=this._hash);for(var i=0;i<size;i++)id=(id+key.charCodeAt(i))%this._hash}return this._indexes[id]||(this._indexes[id]=this.createIndex(id)),this._indexes[id]},Graph.prototype.readIndex=function(key,cb){for(var i=0;i<this._hash;i++)this._indexes[i]||(this._indexes[i]=this.createIndex(i)),this._indexes[i].each(key,cb);return this},Graph.prototype.index=function(key,value,point){var result=this.getIndex(value);return result.add(key,value,point),this},Graph.prototype.get=function(uuid){return this.shard(uuid).get(uuid)},Graph.prototype.resolve=function(data){if(Array.isArray(data)){for(var result=[],i=0;i<data.length;i++){var item=this.get(data[i]);item&&result.push(item);
}return result}return"number"==typeof data?this.get(data):null},Graph.prototype.removeIndex=function(key,value,point){var result=this.getIndex(value);return result.remove(key,value,point),this},Graph.prototype.createIndex=function(id){return new grafine.index(this,id)},Graph.prototype.export=function(){for(var shards=[],indexes=[],i=0;i<this._shards.length;i++)shards.push(this._shards[i]?this._shards[i].export():null);for(var i=0;i<this._indexes.length;i++)indexes.push(this._indexes[i]?this._indexes[i].export():null);return{hash:this._hash,capacity:this._capacity,uuid:this._nextId,shards:shards,indexes:indexes}},Graph.prototype.import=function(data){if(this._hash=data.hash,this._capacity=data.capacity,this._nextId=data.uuid,this._shards=[],this._indexes=[],data.shards&&data.shards.length>0)for(var i=0,size=data.shards.length;i<size;i++)data.shards[i]&&(this._shards[i]=new grafine.shard(this,i),this._shards[i].import(data.shards[i]));if(data.indexes&&data.indexes.length>0)for(var i=0,size=data.indexes.length;i<size;i++)data.indexes[i]&&(this._indexes[i]=new grafine.index(this,i),
this._indexes[i].import(data.indexes[i]));return this},Graph.prototype.create=function(result){var uuid=this.uuid(),shard=this.shard(uuid);return result?result.uuid=uuid:result=shard.factory(uuid),shard.attach(result),result},Graph.prototype.search=function(criteria){var results=[];for(var k in criteria){var check=criteria[k];results.push(this.getIndex(check).search(k,check))}if(1===results.length)return results[0];for(var data=[],s=0;s<results.length;s++)for(var set=results[s],i=0,size=set.length;i<size;i++){var miss=!1,item=set[i];if(item){for(var j=0;j<results.length;j++)if(j!==s&&results[j].indexOf(item)===-1){miss=!0;break}miss||data.push(item)}}return data},Graph}},{}],2:[function(require,module,exports){"use strict";module.exports=function(grafine){var Index=function(db,id){this._db=db,this._id=id,this._index=new Map,this._size=0,this._changed=!1};return Index.prototype.id=function(){return this._id},Index.prototype.isChanged=function(){return this._changed},Index.prototype.getSize=function(){return this._size;
},Index.prototype.export=function(){this._changed=!1;for(var entry,inner,it,v,toJson=[],entries=this._index.entries();(entry=entries.next())&&!entry.done;)if(entry.value[1]){for(inner=[],it=entry.value[1].entries();(v=it.next())&&!v.done;)v.value[1]&&v.value[1].length>0&&inner.push(v.value);toJson.push([entry.value[0],inner])}return[this._size,toJson]},Index.prototype.import=function(data){this._size=data[0];for(var indexMap=[],i=0;i<data[1].length;i++)indexMap.push([data[1][i][0],new Map(data[1][i][1])]);return this._index=new Map(indexMap),this._changed=!1,this},Index.prototype.add=function(key,value,point){var values=this._index.get(key);values||(values=new Map,this._index.set(key,values));var indexes=values.get(value);return indexes||(indexes=[],values.set(value,indexes)),point.uuid&&(point=point.uuid),indexes.indexOf(point)===-1&&(indexes.push(point),this._changed=!0,this._size++),this},Index.prototype.remove=function(key,value,point){var values=this._index.get(key);if(values){var indexes=values.get(value);
if(indexes){point.uuid&&(point=point.uuid);var index=indexes.indexOf(point);index!==-1&&(1===indexes.length?values.set(value,null):indexes.splice(index,1),this._changed=!0,this._size--)}}return this},Index.prototype.search=function(key,value){var values=this._index.get(key);if(values){var indexes=values.get(value);if(indexes)return indexes}return[]},Index.prototype.each=function(key,cb){var values=this._index.get(key);if(values)for(var entry,it=values.entries();(entry=it.next())&&!entry.done;)entry.value[1]&&cb(entry.value[0],entry.value[1]);return this},Index}},{}],3:[function(require,module,exports){"use strict";module.exports=function(grafine){var Point=function(db){this._db=db,this._properties={},this._related={},this._indexes=[],this.uuid=-1};return Point.prototype.export=function(){return{_p:this._properties,_i:this._indexes}},Point.prototype.import=function(data){return this._properties=data._p,this._indexes=data._i,this},Point.prototype.delete=function(){for(var property in this._properties){var relations=this._properties[property];
relations instanceof Array||(relations=[relations]);for(var i=0,size=relations.length;i<size;i++){var record=this._db.get(relations[i]),related=record._related[property],id=related.indexOf(this.uuid);id!==-1&&related.splice(id,1)}}for(var property in this._related)for(var relations=this._related[property],i=0,size=relations.length;i<size;i++){var record=this._db.get(relations[i]),related=record._properties[property];if(related===this.uuid)delete record._properties[property];else{var id=related.indexOf(this.uuid);id!==-1&&related.splice(id,1)}}for(var i=0;i<this._indexes.length;i++){var idx=this._indexes[i];this._db.removeIndex(idx[0],idx[1],this)}return this._db.shard(this.uuid).remove(this),this},Point.prototype.removeAttribute=function(property){if(property in this._properties){var relations=this._properties[property];delete this._properties[property],relations instanceof Array||(relations=[relations]);for(var i=0,size=relations.length;i<size;i++){var related=this._db.get(relations[i]);if(related){related=related._related[property];
var id=related.indexOf(this.uuid);id!==-1&&related.splice(id,1)}}}return this},Point.prototype.removeIndex=function(name){for(var i=0;i<this._indexes.length;i++){var index=this._indexes[i];if(index[0]===name){this._db.removeIndex(index[0],index[1],this),this._indexes.splice(i,1);break}}return this},Point.prototype.getIndex=function(name){for(var i=0;i<this._indexes.length;i++){var index=this._indexes[i];if(index[0]===name)return index[1]}return null},Point.prototype.index=function(name,value){value||(value="*"),this._db.index(name,value,this),this._indexes.push([name,value])},Point.prototype.set=function(property,object){if(property in object._related||(object._related[property]=[]),object._related[property].push(this.uuid),property in this._properties){var relations=this._properties[property];relations instanceof Array||(relations=[relations]);for(var i=0,size=relations.length;i<size;i++){var related=this._db.get(relations[i]);if(related){related=related._related[property];var id=related.indexOf(this.uuid);id!==-1&&related.splice(id,1);
}}}return this._properties[property]=object.uuid,this},Point.prototype.add=function(property,object){return property in object._related||(object._related[property]=[]),object._related[property].push(this.uuid),property in this._properties?this._properties[property]instanceof Array||(this._properties[property]=[this._properties[property]]):this._properties[property]=[],this._properties[property].push(object.uuid),this},Point.prototype.get=function(property){if(property in this._properties){var result=this._properties[property];return result instanceof Array||(result=[result]),result}return[]},Point.prototype.first=function(property){if(property in this._properties){var result=this._properties[property];return result instanceof Array&&(result=result[0]),result}return null},Point}},{}],4:[function(require,module,exports){"use strict";module.exports=function(grafine){var Shard=function(db,id){this._db=db,this._id=id,this._points={},this._size=0,this._changed=!1};return Shard.prototype.id=function(){return this._id;
},Shard.prototype.isChanged=function(){return this._changed},Shard.prototype.getSize=function(){return this._size},Shard.prototype.get=function(uuid){return this._points[uuid]},Shard.prototype.export=function(){var nodes={};for(var uuid in this._points)nodes[uuid]=this._points[uuid].export();return this._changed=!1,nodes},Shard.prototype.import=function(points){this._size=0,this._points={};for(var uuid in points)this._points[uuid]=this.factory(uuid,points[uuid]),this._size++;return this._changed=!1,this},Shard.prototype.attach=function(object){return this._points[object.uuid]=object,this._size++,this._changed=!0,this},Shard.prototype.remove=function(point){return point.uuid&&(point=point.uuid),delete this._points[point],this._size--,this._changed=!0,this},Shard.prototype.factory=function(uuid,object){var result=new grafine.point(this._db);return result.uuid=uuid,object&&result.import(object),result},Shard}},{}],grafine:[function(require,module,exports){"use strict";module.exports={graph:null,index:null,point:null,
shard:null},module.exports.graph=require("./src/graph")(module.exports),module.exports.index=require("./src/index")(module.exports),module.exports.point=require("./src/point")(module.exports),module.exports.shard=require("./src/shard")(module.exports)},{"./src/graph":1,"./src/index":2,"./src/point":3,"./src/shard":4}]},{},["grafine"]);
//# sourceMappingURL=grafine.min.js.map