entity-wharf
Advanced tools
Comparing version 0.0.0 to 0.1.0
145
index.js
@@ -10,2 +10,42 @@ (function(definition){ | ||
}(function(){ | ||
var escaper = "$:?:$"; | ||
var toValIndex = function(v){ | ||
if(typeof v === 'object'){ | ||
if(typeof v.length == 'number' && Object.prototype.toString.call(v) == "[object Array]"){ | ||
return escaper + 'array'; | ||
} | ||
return escaper + 'object'; | ||
}else if(typeof v === 'function'){ | ||
return escaper + 'function'; | ||
} | ||
return v; | ||
}; | ||
var toAttrValIndex = function(a, v){ | ||
return a + escaper + toValIndex(v); | ||
}; | ||
var isMap = function(o){ | ||
return !!o && typeof o === 'object' && o.constructor === Object; | ||
}; | ||
var keys = Object.keys; | ||
var has = function(o, attr){ | ||
return o.hasOwnProperty(attr); | ||
}; | ||
var appendToIndex = function(index, attr, id){ | ||
if(!has(index, attr)){ | ||
index[attr] = {}; | ||
} | ||
index[attr][id] = true; | ||
}; | ||
var removeFromIndex = function(index, attr, id){ | ||
if(has(index, attr)){ | ||
delete index[attr][id]; | ||
} | ||
}; | ||
return function(){ | ||
@@ -15,17 +55,29 @@ var nextID = (function(){ | ||
return function(){ | ||
return ++last; | ||
last++; | ||
return last + "";//id's are strings | ||
}; | ||
}()); | ||
var entities = {}; | ||
var e_index = {}; | ||
var a_index = {}; | ||
var v_index = {}; | ||
var av_index = {}; | ||
var attribute_index = {}; | ||
var value_index = {}; | ||
var has_e = function(id){ | ||
return has(e_index, id); | ||
}; | ||
var set = function(id, data){ | ||
if(!has_e(id) || !isMap(data)){ | ||
return false; | ||
} | ||
keys(data).forEach(function(attr){ | ||
var val = data[attr] | ||
e_index[id][attr] = val; | ||
var set= function(id, data){ | ||
//TODO check if it's a good id first | ||
//TODO extend entity[id] with the data | ||
//TODO set indexes | ||
appendToIndex(a_index, attr, id); | ||
appendToIndex(v_index, toValIndex(val), id); | ||
appendToIndex(av_index, toAttrValIndex(attr, val), id); | ||
}); | ||
return true; | ||
}; | ||
@@ -35,22 +87,69 @@ | ||
get: function(id){ | ||
return entities[id]; | ||
return has_e(id) ? e_index[id] : null; | ||
}, | ||
add: function(data){ | ||
var id = nextID(); | ||
entities[id] = {}; | ||
set(id, data); | ||
add: function(data){ | ||
var id = nextID(); | ||
e_index[id] = {}; | ||
if(set(id, data)){ | ||
return id; | ||
}, | ||
set: set, | ||
remove: function(id){ | ||
//TODO check if it's a good id first | ||
delete entities[id]; | ||
//TODO remove indexes | ||
}, | ||
} | ||
delete e_index[id]; | ||
return false; | ||
}, | ||
set: set, | ||
remove: function(id){ | ||
if(!has_e(id)){ | ||
return false; | ||
} | ||
keys(e_index[id]).forEach(function(attr){ | ||
removeFromIndex(a_index, attr, id); | ||
removeFromIndex(v_index, toValIndex(e_index[id][attr]), id); | ||
removeFromIndex(av_index, toAttrValIndex(attr, e_index[id][attr]), id); | ||
}); | ||
delete e_index[id]; | ||
return true; | ||
}, | ||
q: function(){ | ||
//TODO queries | ||
q: function(query){ | ||
if(!isMap(query)){ | ||
return keys(e_index); | ||
} | ||
var e = query.e || []; | ||
var a = query.a || []; | ||
var v = query.v || []; | ||
var av = query.av || []; | ||
var constraints = [e]; | ||
a.forEach(function(attr){ | ||
constraints.push(keys(a_index[attr] || {})); | ||
}); | ||
v.forEach(function(val){ | ||
constraints.push(keys(v_index[toValIndex(val)] || {})); | ||
}); | ||
av.forEach(function(av){ | ||
var a = av[0]; | ||
var v = av[1]; | ||
constraints.push(keys(av_index[toAttrValIndex(a, v)] || {})); | ||
}); | ||
var n_id_sets = 0; | ||
var result = {}; | ||
constraints.forEach(function(ids){ | ||
if(ids.length === 0){ | ||
return; | ||
} | ||
n_id_sets++; | ||
ids.forEach(function(id){ | ||
if(!has(result,id)){ | ||
result[id] = 0; | ||
} | ||
result[id]++; | ||
}); | ||
}); | ||
return keys(result).filter(function(id){ | ||
return result[id] === n_id_sets; | ||
}); | ||
} | ||
}; | ||
}; | ||
})); |
{ | ||
"name": "entity-wharf", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "Store data as entity attribute values in memory. Fast queries and manipulation of state.", | ||
@@ -15,3 +15,12 @@ "author": "smallhelm", | ||
"homepage": "https://github.com/smallhelm/entity-wharf-js", | ||
"keywords": [], | ||
"keywords": [ | ||
"entity attribute value", | ||
"model", | ||
"entity", | ||
"component entity", | ||
"ces", | ||
"ecs", | ||
"eav", | ||
"game" | ||
], | ||
"main": "index.js", | ||
@@ -18,0 +27,0 @@ "files": [ |
159
README.md
@@ -1,4 +0,157 @@ | ||
entity-wharf-js | ||
=============== | ||
# What it does | ||
Store data as entity attribute values in memory. Fast queries and manipulation of state. | ||
Stores your data in an elegant way that will give you great flexibility and leverage. This library is designed to be used as a state management solution for applications that need to be performant for quickly mutating state over many different types of entities with similar characteristics. Applications such as games and simulations. If performance isn't this crucial then check out [DataScript](https://github.com/tonsky/datascript) which provides immutability, versioning, undo/redo, and a powerful datalog query engine. | ||
All data stored in Wharf is organized uniformly as | ||
* **Entity** - just an id (i.e. _1_) | ||
* **Attribute** - an attribute of the entity (i.e. _name_ or _age_) | ||
* **Value** - the value of the attribute (i.e. _"bob"_ or _50_) | ||
For example let's describe some shapes. | ||
* entity 1 is a blue square | ||
* entity 2 is a red triangle | ||
* entity 3 is a red circle | ||
| Entity | Attribute | Value | | ||
| ------ | --------- | ----- | | ||
| 1 | color | blue | | ||
| 1 | width | 20 | | ||
| 1 | height | 20 | | ||
| 1 | n\_edges | 4 | | ||
| 2 | color | red | | ||
| 2 | base | 10 | | ||
| 2 | height | 40 | | ||
| 2 | n\_edges | 3 | | ||
| 3 | color | red | | ||
| 3 | radius | 20 | | ||
| 3 | n\_edges | 0 | | ||
Although there are 3 distinct types we can perform generic queries that cross types. | ||
All entities that have a `height` attribute returns `[ 1, 2 ]` | ||
| Entity | Attribute | Value | | ||
| ------ | --------- | ----- | | ||
| 1 | **height** | 20 | | ||
| 2 | **height** | 40 | | ||
All entities that have a `height` and a `width` attribute returns `[ 1 ]` | ||
| Entity | Attribute | Value | | ||
| ------ | --------- | ----- | | ||
| 1 | **width** | 20 | | ||
| 1 | **height** | 20 | | ||
All entities with a value `20` returns `[ 1, 3 ]` | ||
| Entity | Attribute | Value | | ||
| ------ | --------- | ----- | | ||
| 1 | width | **20** | | ||
| 1 | height | **20** | | ||
| 3 | radius | **20** | | ||
All entities where `color == red` returns `[ 2, 3 ]` | ||
| Entity | Attribute | Value | | ||
| ------ | --------- | ----- | | ||
| 2 | **color** | **red** | | ||
| 3 | **color** | **red** | | ||
# How to use it | ||
```js | ||
var db = Wharf();//make a new "db" to work with | ||
//create some entities | ||
var square_id = db.add({color: "blue", width: 20, height: 20, n_edges: 4});// (returns false on failure) | ||
var triangle_id = db.add({color: "red", base: 10, height: 40, n_edges: 3}); | ||
var circle_id = db.add({color: "blue", radius: 20}); | ||
//db.set will add/overwrite attributes and values on an entitiy | ||
db.set(circle_id, {color: "red", n_edges: 0});// -> true (returns false on failure) | ||
//db.get returns the entities data | ||
db.get(circle_id);// -> {color: "red", radius: 20, n_edges: 0} (returns null if it's not found) | ||
//At this point the db has 3 entities the same as in the example | ||
//All entities that have a `height` attribute | ||
db.q({a: ["height"]});// -> [ square_id, triangle_id ] | ||
//All entities that have a `height` and a `width` attribute | ||
db.q({a: ["height", "width"]});// -> [ square_id ] | ||
//All entities with a value `20` | ||
db.q({v: [20]});// -> [ square_id, circle_id ] | ||
//All entities where `color == red` | ||
db.q({av: [["color", "red"]]});// -> [ triangle_id, circle_id ] | ||
//entities are easy to remove | ||
db.remove(square_id);// -> true | ||
//if an ID doesn't exist it returns false | ||
db.remove(square_id);// -> false | ||
``` | ||
# Installing | ||
There are 3 ways to include this library | ||
### With browserify | ||
```sh | ||
$ npm install --save entity-wharf | ||
``` | ||
Then use it | ||
```js | ||
var Wharf = require('entity-wharf'); | ||
... | ||
``` | ||
### With a script tag | ||
Download [this](https://github.com/smallhelm/entity-wharf/blob/master/entity-wharf.min.js) script then include it in your html | ||
```html | ||
<script src="entity-wharf.min.js"></script> | ||
``` | ||
Then use it | ||
```js | ||
var Wharf = ENTITY_WHARF; | ||
... | ||
``` | ||
### With RequireJS | ||
```js | ||
require(['entity-wharf'], function(Wharf) { | ||
... | ||
}); | ||
``` | ||
# License | ||
The MIT License (MIT) | ||
Copyright (c) 2014 Small Helm LLC | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10292
137
158