Skip to content

Commit

Permalink
Added basic query support. Only supports equivalence.
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgul committed Apr 14, 2013
1 parent 3ea3fd7 commit 6f575b7
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 4 deletions.
30 changes: 28 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ You can even remove all objects from a bin:
dustbin.removeAllKeys("testBin");
```

### Basic Query support

Currently, only basic query support has been added. You will need to construct an object containing `key` and `value`
where you want back a list of object that have `key` equal to `value`. Here's an example:

```javascript
var alex = {animal:"cat", name: "alex", age: 4};
var izzy = {animal:"cat", name: "izzy", age: 4};
var baal = {animal:"snake", name: "baal", age: 2};

dustbin.store("pets", alex);
dustbin.store("pets", izzy);
dustbin.store("pets", baal);

// `cats` will be equal to `[alex, izzy]`
var cats = dustbin.query("pets", {animal: "cat"});

// `pets` will be equal to `[alex, izzy, baal]`
var pets = dustbin.query("pets", {});
```

Right now the only thing supported is pure _equivalence_, **not** equality. Also, this is basically two nested `forEach`
calls, so performance isn't as good as it could be. I'll be looking at improving this in the future, but for now it
should meet most needs.

### Session store support

All operations can also be done on the session store. By default, the `dustbin` object's functions are simply wrappers
Expand All @@ -108,8 +133,9 @@ except the inherent difference of session storage (all objects only last for the

## Status

Currently, all unit tests pass, and the basic functionality is there. You can get, remove and store objects by key. As
near as I can tell, this code is simple enough it can be used in a production site.
Currently, all unit tests pass, and the basic functionality is there. You can get, remove and store objects by key.
There is also basic query support, which I estimate will work for 70% of most use cases. As near as I can tell, this
code is simple enough it can be used in a production site.

That being said, I would very much like to add support for [map/reduce](http://docs.basho.com/riak/latest/tutorials/querying/MapReduce/)
and a [django-like query api](https://docs.djangoproject.com/en/dev/ref/models/querysets/#id4). I will work on that as
Expand Down
36 changes: 34 additions & 2 deletions dustbin.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,36 @@
delete this.storage[bin];
}; // end removeAllKeys

BinStorage.prototype.query = function(query)
BinStorage.prototype.query = function(bin, query)
{
console.error("Not Implemented yet!")
query = query || {};
var binObj = this._get_bin(bin);
var matches = [];

Object.keys(binObj).forEach(function(key) //TODO: Apparently for loops are much faster than forEach.
{
var content = binObj[key];
var match = true;

// Check to see if object matches
Object.keys(query).forEach(function(objKey) //TODO: Apparently for loops are much faster than forEach.
{
if(content[objKey] != query[objKey])
{
match = false;
} // end if
}); // end for

if(match)
{
if(matches.indexOf(content) < 0)
{
matches.push(content);
} // end if
} // end if
}); // end forEach

return matches;
}; // end query

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -162,6 +189,11 @@
return this.local.store(bin, key, value);
}; // end store

DustBin.prototype.query = function(bin, query)
{
return this.local.query(bin, query);
}; // end query

DustBin.prototype.remove = function(bin, key)
{
return this.local.remove(bin, key);
Expand Down
32 changes: 32 additions & 0 deletions tests/dustbin.session.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

describe("DustBin Session Store", function()
{
afterEach(function()
{
Object.keys(sessionStorage).forEach(function(key)
{

delete sessionStorage[key];
});
});

it("allows storage of objects.", function()
{
var key;
Expand Down Expand Up @@ -106,6 +115,29 @@ describe("DustBin Session Store", function()
expect(testGet).not.toThrow();
expect(binObj).toEqual({});
});

it("allows basic query by values.", function()
{
var alex = {animal:"cat", name: "alex", age: 4};
var izzy = {animal:"cat", name: "izzy", age: 4};
var baal = {animal:"snake", name: "baal", age: 2};

dustbin.session.store("pets", alex);
dustbin.session.store("pets", izzy);
dustbin.session.store("pets", baal);

var cats;
var testQuery = function()
{
cats = dustbin.session.query("pets", {animal: "cat"});
};

expect(testQuery).not.toThrow();
expect(cats).toEqual([alex, izzy]);

var pets = dustbin.session.query("pets", {});
expect(pets).toEqual([alex, izzy, baal]);
});
});

//----------------------------------------------------------------------------------------------------------------------
32 changes: 32 additions & 0 deletions tests/dustbin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

describe("DustBin", function()
{
afterEach(function()
{
Object.keys(localStorage).forEach(function(key)
{

delete localStorage[key];
});
});

it("correctly initializes both local and session storage.", function()
{
expect(dustbin.local).toBeDefined();
Expand Down Expand Up @@ -112,6 +121,29 @@ describe("DustBin", function()
expect(testGet).not.toThrow();
expect(binObj).toEqual({});
});

it("allows basic query by values.", function()
{
var alex = {animal:"cat", name: "alex", age: 4};
var izzy = {animal:"cat", name: "izzy", age: 4};
var baal = {animal:"snake", name: "baal", age: 2};

dustbin.store("pets", alex);
dustbin.store("pets", izzy);
dustbin.store("pets", baal);

var cats;
var testQuery = function()
{
cats = dustbin.query("pets", {animal: "cat"});
};

expect(testQuery).not.toThrow();
expect(cats).toEqual([alex, izzy]);

var pets = dustbin.query("pets", {});
expect(pets).toEqual([alex, izzy, baal]);
});
});

//----------------------------------------------------------------------------------------------------------------------

0 comments on commit 6f575b7

Please sign in to comment.