Permalink
Browse files

Added Queue as a builtin

  • Loading branch information...
1 parent e6c06c0 commit 4dd434131e8714f73bf06caca21688a18f1ccdb2 @chriso committed May 15, 2011
Showing with 183 additions and 7 deletions.
  1. +5 −4 README.md
  2. +1 −1 lib/Redback.js
  3. +66 −0 lib/advanced_structures/Queue.js
  4. +1 −1 package.json
  5. +63 −1 site/api.html
  6. +47 −0 test/queue.test.js
View
@@ -18,6 +18,7 @@ It also comes with the following advanced data structures:
- **SocialGraph** - Similar to Twitter's (following vs. followers)
- **CappedList** - A list with a fixed length
- **FullText** - A full text index with support for stop words, stemming and basic boolean search
+- **Queue** - A simple FIFO or LIFO queue
*More coming soon!*
@@ -47,7 +48,7 @@ To create your own structure, use `addStructure(name, methods)`.
Let's create a queue that can be either FIFO or LIFO
```javascript
-redback.addStructure('Queue', {
+redback.addStructure('SimpleQueue', {
init: function (is_fifo) {
this.fifo = is_fifo;
},
@@ -61,11 +62,11 @@ redback.addStructure('Queue', {
});
```
-To use the queue, call `createQueue(key, is_fifo)`
+To use the queue, call `createSimpleQueue(key, is_fifo)`
```javascript
-var queue = redback.createQueue('my_queue', true);
-queue.add('awesome!', callback);
+var queue = redback.createSimpleQueue('my_queue', true);
+queue.add('awesome!');
```
Structures have access to a Redis key `this.key` and the Redis client
View
@@ -24,7 +24,7 @@ var base = ['Hash','List','Set','SortedSet','Bitfield'];
*/
var advanced = ['KeyPair','DensitySet','CappedList','SocialGraph',
- 'FullText'];
+ 'FullText', 'Queue'];
/**
* The Redback object wraps the Redis client and acts as a factory
@@ -0,0 +1,66 @@
+/*!
+ * Redback
+ * Copyright(c) 2011 Chris O'Hara <cohara87@gmail.com>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var Structure = require('../Structure'),
+ List = require('../base_structures/List').List;
+
+/**
+ * A simple FIFO/LIFO queue.
+ *
+ * Usage:
+ * `redback.createQueue(key [, is_fifo]);`
+ *
+ * Reference:
+ * http://redis.io/topics/data-types#lists
+ * http://en.wikipedia.org/wiki/Queue_(data_structure)
+ *
+ * Redis Structure:
+ * `(namespace:)key = list(values)`
+ */
+
+var Queue = exports.Queue = Structure.new();
+
+/**
+ * Setup the Queue to be either FIFO or LIFO.
+ *
+ * @param {bool} is_fifo
+ * @api private
+ */
+
+Queue.prototype.init = function (is_fifo) {
+ this.fifo = is_fifo;
+ this.list = new List(this.client, this.id, this.namespace);
+}
+
+/**
+ * Add one or more elements to the queue.
+ *
+ * @param {string|Array} value(s)
+ * @param {Function} callback (optional)
+ * @api public
+ */
+
+Queue.prototype.enqueue = Queue.prototype.add = function (values, callback) {
+ this.list.unshift(values, callback);
+ return this;
+}
+
+/**
+ * Remove the next element from the queue.
+ *
+ * @param {int} wait (optional) - block for this many seconds
+ * @param {Function} callback
+ * @api public
+ */
+
+Queue.prototype.dequeue = Queue.prototype.next = function (wait, callback) {
+ this.list[this.fifo ? 'pop' : 'shift'](wait, callback);
+ return this;
+}
View
@@ -1,6 +1,6 @@
{ "name" : "redback",
"description" : "A high-level Redis library",
- "version" : "0.1.6",
+ "version" : "0.1.7",
"homepage" : "https://github.com/chriso/redback",
"author" : "Chris O'Hara <cohara87@gmail.com>",
"main" : "index",
View
@@ -136,7 +136,7 @@
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">advanced</span> = [<span class="string">'KeyPair'</span>,<span class="string">'DensitySet'</span>,<span class="string">'CappedList'</span>,<span class="string">'SocialGraph'</span>,
- <span class="string">'FullText'</span>];</code></pre>
+ <span class="string">'FullText'</span>, <span class="string">'Queue'</span>];</code></pre>
</td>
</tr>
<tr class="code">
@@ -2575,6 +2575,68 @@ <h2></h2>
}
</code></pre>
</td>
+</tr><tr class="filename"><td><h2 id="lib/advanced_structures/Queue.js"><a href="#">Queue</a></h2></td><td>lib/advanced_structures/Queue.js</td></tr><tr class="code">
+<td class="docs">
+<p>Module dependencies.
+ </p>
+</td>
+<td class="code">
+<pre><code><span class="keyword">var</span> <span class="class">Structure</span> = <span class="variable">require</span>(<span class="string">'../Structure'</span>),
+ <span class="class">List</span> = <span class="variable">require</span>(<span class="string">'../base_structures/List'</span>).<span class="class">List</span>;</code></pre>
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<p>A simple FIFO/LIFO queue.</p>
+
+<h2>Usage</h2>
+
+<p> <code>redback.createQueue(key [, is_fifo]);</code></p>
+
+<h2>Reference</h2>
+
+<p> http://redis.io/topics/data-types#lists
+ http://en.wikipedia.org/wiki/Queue<em>(data</em>structure)</p>
+
+<h2>Redis Structure</h2>
+
+<p> <code>(namespace:)key = list(values)</code>
+ </p>
+</td>
+<td class="code">
+<pre><code><span class="keyword">var</span> <span class="class">Queue</span> = <span class="variable">exports</span>.<span class="class">Queue</span> = <span class="class">Structure</span>.<span class="keyword">new</span>();</code></pre>
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<p>Add one or more elements to the queue.</p>
+
+<h2></h2>
+
+<ul><li><p><strong>param</strong>: <em>string | Array</em> value(s)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+</td>
+<td class="code">
+<pre><code><span class="class">Queue</span>.<span class="variable">prototype</span>.<span class="variable">enqueue</span> = <span class="class">Queue</span>.<span class="variable">prototype</span>.<span class="variable">add</span> = <span class="keyword">function</span> (<span class="variable">values</span>, <span class="variable">callback</span>) {
+ <span class="this">this</span>.<span class="variable">list</span>.<span class="variable">unshift</span>(<span class="variable">values</span>, <span class="variable">callback</span>);
+ <span class="keyword">return</span> <span class="this">this</span>;
+}</code></pre>
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
+<p>Remove the next element from the queue.</p>
+
+<h2></h2>
+
+<ul><li><p><strong>param</strong>: <em>int</em> wait (optional) - block for this many seconds</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+</td>
+<td class="code">
+<pre><code><span class="class">Queue</span>.<span class="variable">prototype</span>.<span class="variable">dequeue</span> = <span class="class">Queue</span>.<span class="variable">prototype</span>.<span class="variable">next</span> = <span class="keyword">function</span> (<span class="variable">wait</span>, <span class="variable">callback</span>) {
+ <span class="this">this</span>.<span class="variable">list</span>[<span class="this">this</span>.<span class="variable">fifo</span> ? <span class="string">'pop'</span> : <span class="string">'shift'</span>](<span class="variable">wait</span>, <span class="variable">callback</span>);
+ <span class="keyword">return</span> <span class="this">this</span>;
+}
+</code></pre>
+</td>
</tr><tr class="filename"><td><h2 id="lib/advanced_structures/SocialGraph.js"><a href="#">SocialGraph</a></h2></td><td>lib/advanced_structures/SocialGraph.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
View
@@ -0,0 +1,47 @@
+var redback = require('redback').createClient(),
+ assert = require('assert');
+
+//Flush the DB and close the Redis connection after 500ms
+setTimeout(function () {
+ redback.client.flushdb(function (err) {
+ redback.client.quit();
+ });
+}, 500);
+
+module.exports = {
+
+ 'test social graph': function () {
+ var lifo = redback.createQueue('test_addstructure_lifo_queue');
+
+ lifo.add('foo', function (err) {
+ lifo.add('bar', function (err) {
+ lifo.next(function (err, value) {
+ assert.equal('bar', value);
+ lifo.next(function (err, value) {
+ assert.equal('foo', value);
+ lifo.next(function (err, value) {
+ assert.equal(null, value);
+ });
+ });
+ });
+ });
+ });
+
+ var fifo = redback.createQueue('test_queue_fifo', true);
+
+ fifo.enqueue('foo', function (err) {
+ fifo.enqueue('bar', function (err) {
+ fifo.dequeue(function (err, value) {
+ assert.equal('foo', value);
+ fifo.dequeue(function (err, value) {
+ assert.equal('bar', value);
+ fifo.dequeue(function (err, value) {
+ assert.equal(null, value);
+ });
+ });
+ });
+ });
+ });
+ },
+
+}

0 comments on commit 4dd4341

Please sign in to comment.