level-q
Priority queuing for leveldb/levelup
Installation
This module is installed via npm:
$ npm install level-q
Example Usage
Basic Usage
By default adding items to a queue will be ordered first in, first out.
var queue =level =bytewise = ;// instantiate queue (adds a 'queue' property on the db)var q = ;// data to put in queuevar data =id: 1name: 'Eugene'value: 42;// add to queueqqueue;// read a single item from the queue, then stop listeningqqueue;// keep listening to the queueqqueue;
Custom Priority Queue
By providing an order by
function you can return a key that can be used
to sort the queue. This example uses a quality of service (QOS) field
so that the higher the QOS, the earlier it will get serviced.
var queue =level =bytewise = ;// order the queue by the QOS fieldvar db =;{return dataQOS;}// data to put in queuevar data =// should be serviced lastid: 1QOS: 'C'name: 'Eugene'value: 42// should be serviced firstid: 2QOS: 'A'name: 'Eugene'value: 42// should be serviced secondid: 3QOS: 'B'name: 'Eugene'value: 42;// add to queuevar count = datalength;data;{dbqueue;}
Control when an item is able to be released from the queue
You can use the release
option when instantiating your queue to control
when an item is able to be returned to consumers of the queue.
For example, you may wish to create a job queue, and delay jobs until to a scheduled time in the future (eg. like a cron job);
var bytewise =level =range =queue = ;// create a priority queue based on time to next servicevar db =;// sort queue by deadline{return datadeadline;}// only allow an item to be read from queue if it's time is up{return Date >= datadeadline;}// add some work to the queue that will be scheduled 5 seconds in the futuredbqueue;// The job will ONLY be available for processing once the 5 seconds has passeddbqueue;