Permalink
Browse files

Support providing a database to getCursor

  • Loading branch information...
1 parent fcc1ee8 commit dc0e80a041b747598c69fd915400a868910c894f @wezm committed Jun 30, 2011
Showing with 214 additions and 34 deletions.
  1. +4 −6 src/cursor.coffee
  2. +6 −3 src/db.coffee
  3. +172 −4 test/cursor_test.coffee
  4. +32 −21 test/db_test.coffee
View
10 src/cursor.coffee
@@ -13,21 +13,19 @@ class Cursor
this
_jumpUsing: (procedure, args) ->
+ options = {}
switch args.length
when 1 then callback = args[0]
when 2
- key = args[0]
- callback = args[1]
+ [key, callback] = args
when 3
- key = args[0]
- database = args[1]
- callback = args[2]
+ [key, options, callback] = args
else
throw new Error("Invalid number of arguments (#{args.length}) to #{procedure}");
rpc_args = {CUR: 1}
rpc_args.key = key if key?
- rpc_args.DB = database if database?
+ rpc_args.DB = options.database or @db.database
@client.call procedure, rpc_args, (error, status, output) ->
if error?
View
9 src/db.coffee
@@ -427,20 +427,23 @@ class DB
matchRegex: (regex, args...) ->
this._matchUsing 'match_regex', regex, args
- # [key], callback
+ # [key], [options], callback
getCursor: (args...) ->
switch args.length
when 1
options = {}
callback = args[0]
when 2
- key = args[0]
+ options = {}
+ key = args[0]
callback = args[1]
+ when 3
+ [key, options, callback] = args
else
throw new Error("Invalid number of arguments (#{args.length}) to getCursor");
cursor = new Cursor(this)
- cursor.jump key, (error) ->
+ cursor.jump key, options, (error) ->
callback error, cursor
module.exports = DB
View
176 test/cursor_test.coffee
@@ -3,7 +3,8 @@ util = require 'util'
testCase = require('nodeunit').testCase
fs = require 'fs'
-testDb = "test.kct"
+testDb = "test"
+alternateDb = "test2"
db = new kt.Db testDb
db.open('localhost', 1979)
@@ -16,7 +17,8 @@ module.exports =
setUp: (callback) ->
db.clear (error, output) =>
db.set 'cursor-test', "Cursor\tValue", (error) =>
- db.getCursor (error, cursor) =>
+ db.getCursor 'cursor-test', (error, cursor) =>
+ throw error if error?
@cursor = cursor
callback()
@@ -26,8 +28,9 @@ module.exports =
get:
'returns the key and value': (test) ->
- test.expect 2
+ test.expect 3
@cursor.get (error, output) ->
+ test.ifError error
test.equal output.key.toString('utf8'), "cursor-test"
test.equal output.value.toString('utf8'), "Cursor\tValue"
test.done()
@@ -89,7 +92,7 @@ module.exports =
'last': "At the end"
db.clear (error, output) =>
db.setBulk @records, (error) =>
- db.getCursor 'cursor-test', (error, cursor) =>
+ db.getCursor (error, cursor) =>
@cursor = cursor
callback()
@@ -237,3 +240,168 @@ module.exports =
db.set '1', 'One', (error, output) ->
db.set '2', 'Two', (error, output) ->
runTest()
+
+ 'Cursor for an alternate database': testCase
+ setUp: (callback) ->
+ @records =
+ 'first': "Cursor\tValue"
+ 'last': "At the end"
+
+ # Clear both databases
+ db.clear (error, output) =>
+ console.log "clear default"
+ throw error if error?
+ db.clear {database: alternateDb}, (error, output) =>
+ throw error if error?
+ db.setBulk @records, {database: alternateDb}, (error) =>
+ throw error if error?
+ db.getCursor null, {database: alternateDb}, (error, cursor) =>
+ throw error if error?
+ console.log "getCursor"
+ @cursor = cursor
+ callback()
+
+ tearDown: (callback) ->
+ @cursor.delete ->
+ callback()
+
+ jump:
+ 'allows traversal from the first record': (test) ->
+ test.expect 2
+ @cursor.jump (error, output) =>
+ test.ifError error
+ @cursor.getKey (error, output) ->
+ test.equal output.key.toString('utf8'), "first"
+ test.done()
+
+ 'step goes to the next record': (test) ->
+ test.expect 1
+ @cursor.jump (error, output) =>
+ @cursor.step (error, output) =>
+ @cursor.getKey (error, output) ->
+ test.equal output.key.toString('utf8'), "last"
+ test.done()
+
+ jumpBack:
+ 'allows traversal from the last record': (test) ->
+ test.expect 2
+ @cursor.jumpBack (error, output) =>
+ test.ifError error
+ @cursor.getKey (error, output) ->
+ test.equal output.key.toString('utf8'), "last"
+ test.done()
+
+ 'stepBack goes to the previous record': (test) ->
+ test.expect 1
+ @cursor.jumpBack (error, output) =>
+ @cursor.stepBack (error, output) =>
+ @cursor.get (error, output) ->
+ test.equal output.key.toString('utf8'), "first"
+ test.done()
+
+ setValue:
+ 'sets the value of the current record': (test) ->
+ test.expect 2
+ @cursor.setValue "New Value", (error, output) =>
+ test.ifError error
+ @cursor.getValue (error, output) ->
+ test.equal output.value.toString('utf8'), "New Value"
+ test.done()
+
+ 'allows stepping to the next record': (test) ->
+ test.expect 2
+ @cursor.setValue "New Value", true, (error, output) =>
+ test.ifError error
+ @cursor.getKey (error, output) ->
+ test.equal output.key.toString('utf8'), 'last'
+ test.done()
+
+ 'accept a Buffer as the value': (test) ->
+ test.expect 2
+ buffer = new Buffer("Some Value", 'ascii')
+ @cursor.setValue buffer, (error, output) =>
+ test.ifError error
+ @cursor.getValue (error, output) ->
+ test.equal output.value.toString('ascii'), "Some Value"
+ test.done()
+
+ remove:
+ 'removes the record': (test) ->
+ test.expect 2
+ @cursor.remove (error, output) ->
+ test.ifError error
+
+ db.get 'cursor-test', (error, value) ->
+ test.ok value == null
+ test.done()
+
+ getKey:
+ 'returns the key of the current record': (test) ->
+ test.expect 1
+ @cursor.getKey (error, output) ->
+ test.equal output.key.toString('utf8'), "first"
+ test.done()
+
+ 'allows stepping to the next record': (test) ->
+ test.expect 2
+ @cursor.getKey true, (error, output) =>
+ test.ifError error
+ @cursor.get (error, output) ->
+ test.equal output.key.toString('utf8'), 'last'
+ test.done()
+
+ getValue:
+ 'returns the value of the current record': (test) ->
+ test.expect 1
+ @cursor.getValue (error, output) ->
+ test.equal output.value.toString('utf8'), "Cursor\tValue"
+ test.done()
+
+ 'allows stepping to the next record': (test) ->
+ test.expect 2
+ @cursor.getValue true, (error, output) =>
+ test.ifError error
+ @cursor.get (error, output) ->
+ test.equal output.key.toString('utf8'), 'last'
+ test.done()
+
+ get:
+ 'returns the key and value of the current record': (test) ->
+ test.expect 2
+ @cursor.get (error, output) ->
+ test.equal output.key.toString('utf8'), "first"
+ test.equal output.value.toString('utf8'), "Cursor\tValue"
+ test.done()
+
+ 'allows stepping to the next record': (test) ->
+ test.expect 2
+ @cursor.get true, (error, output) =>
+ test.ifError error
+ @cursor.get (error, output) ->
+ test.equal output.key.toString('utf8'), 'last'
+ test.done()
+
+ each:
+ 'yields records on each iteration': (test) ->
+ test.expect 2
+
+ runTest = =>
+ results = []
+ @cursor.jump '1', {database: alternateDb}, (error) =>
+ @cursor.each (error, output) ->
+ if output.key?
+ results.push [output.key, output.value]
+ else
+ test.ifError error
+ test.deepEqual [
+ [ '1', 'One' ]
+ [ '2', 'Two' ]
+ [ 'first', 'Cursor\tValue' ]
+ [ 'last', 'At the end' ]
+ ],
+ results
+ test.done()
+
+ db.set '1', 'One', {database: alternateDb}, (error, output) ->
+ db.set '2', 'Two', {database: alternateDb}, (error, output) ->
+ runTest()
View
53 test/db_test.coffee
@@ -2,15 +2,26 @@ kt = require '../lib/index'
util = require 'util'
testCase = require('nodeunit').testCase
-testDb = "test.kct"
+###
+
+These tests need a running instance of kyoto tycoon running:
+
+ ktserver -port 1979 'test#type=kct' 'test2#type=kct'
+
+Tree type databases are used as the order of values retrieved is important
+for the cursor tests
+
+###
+
+testDb = "test"
db = new kt.Db testDb
db.open('localhost', 1979)
dbClear = (callback) ->
# Clear default db (tests that behaviour)
db.clear (error, output) ->
# Clear alternate db (tests specifying the db)
- db.clear {database: 'test2.kct'}, (error, output) ->
+ db.clear {database: 'test2'}, (error, output) ->
callback()
module.exports =
@@ -19,7 +30,7 @@ module.exports =
'allows the default database to be retrieved': (test) ->
test.expect 1
- test.equal db.defaultDatabase(), "test.kct"
+ test.equal db.defaultDatabase(), "test"
test.done()
# This would actually change the setting for the tests...
@@ -56,14 +67,14 @@ module.exports =
test.expect 2
db.status (error, output) ->
test.ok Object.keys(output).length > 0
- test.equal output.path, 'test.kct'
+ test.equal output.path, 'test'
test.done()
'allows the database to be specified': (test) ->
test.expect 2
- db.status {database: 'test2.kct'}, (error, output) ->
+ db.status {database: 'test2'}, (error, output) ->
test.ok Object.keys(output).length > 0
- test.equal output.path, 'test2.kct'
+ test.equal output.path, 'test2'
test.done()
# TODO: set should accept Numbers
@@ -98,7 +109,7 @@ module.exports =
'allows the database to be specified': (test) ->
test.expect 2
- db.set 'test', 'other db', {database: 'test2.kct'}, (error, output) ->
+ db.set 'test', 'other db', {database: 'test2'}, (error, output) ->
test.ifError error
# Check that the value wasn't set on the default db
@@ -145,7 +156,7 @@ module.exports =
'allows the database to be specified': (test) ->
test.expect 2
- db.add 'test', 'other db', {database: 'test2.kct'}, (error, output) ->
+ db.add 'test', 'other db', {database: 'test2'}, (error, output) ->
test.ifError error
# Check that the value wasn't set on the default db
@@ -172,7 +183,7 @@ module.exports =
'allows the database to be specified': (test) ->
test.expect 2
- options = {database: 'test2.kct'}
+ options = {database: 'test2'}
db.add 'test', 'other db', options, (error, output) ->
test.ifError error
@@ -205,7 +216,7 @@ module.exports =
'allows the database to be specified': (test) ->
test.expect 2
- db.append 'test', 'other db', {database: 'test2.kct'}, (error, output) ->
+ db.append 'test', 'other db', {database: 'test2'}, (error, output) ->
test.ifError error
# Check that the value wasn't set on the default db
@@ -241,7 +252,7 @@ module.exports =
'allows the database to be specified': (test) ->
test.expect 2
- db.increment 'test', 10, {database: 'test2.kct'}, (error, output) ->
+ db.increment 'test', 10, {database: 'test2'}, (error, output) ->
test.ifError error
# Check that the value wasn't set on the default db
@@ -277,7 +288,7 @@ module.exports =
'allows the database to be specified': (test) ->
test.expect 2
- db.incrementDouble 'test', 1.89, {database: 'test2.kct'}, (error, output) ->
+ db.incrementDouble 'test', 1.89, {database: 'test2'}, (error, output) ->
test.ifError error
# Check that the value wasn't set on the default db
@@ -326,7 +337,7 @@ module.exports =
'allows the database to be specified': (test) ->
test.expect 2
- options = {database: 'test2.kct'}
+ options = {database: 'test2'}
db.add 'test', 'old', options, ->
db.cas 'test', 'old', 'new', options, (error, output) ->
test.ifError error
@@ -357,7 +368,7 @@ module.exports =
'allows the database to be specified': (test) ->
test.expect 2
- options = {database: 'test2.kct'}
+ options = {database: 'test2'}
# Set in both dbs, remove from one
db.set 'test', 'old', ->
@@ -389,7 +400,7 @@ module.exports =
db.set 'test', 'other db', (error, output) ->
test.ifError error
- db.get 'test', {database: 'test2.kct'}, (error, value) ->
+ db.get 'test', {database: 'test2'}, (error, value) ->
test.ok value == null
test.done()
@@ -411,7 +422,7 @@ module.exports =
'allows the database to be specified': (test) ->
test.expect 2
- options = {database: 'test2.kct'}
+ options = {database: 'test2'}
db.set 'test', "Here", options, (error) ->
db.exists 'test', (error, exists) ->
test.equal exists, false
@@ -435,7 +446,7 @@ module.exports =
'allows the database to be specified': (test) ->
test.expect 3
- options = {database: 'test2.kct'}
+ options = {database: 'test2'}
db.setBulk @records, options, (error, output) ->
test.ifError error
@@ -464,7 +475,7 @@ module.exports =
'allows the database to be specified': (test) ->
test.expect 3
- options = {database: 'test2.kct'}
+ options = {database: 'test2'}
db.setBulk @records, (error, output) =>
db.setBulk @records, options, (error, output) =>
db.removeBulk Object.keys(@records), options, (error, output) ->
@@ -495,7 +506,7 @@ module.exports =
records =
bulk1: "Bulk\tValue"
bulk2: "Bulk Value 2"
- options = {database: 'test2.kct'}
+ options = {database: 'test2'}
db.setBulk records, options, (error, output) ->
test.ifError error
@@ -531,7 +542,7 @@ module.exports =
'allows the database to be specified': (test) ->
test.expect 5
- options = {database: 'test2.kct'}
+ options = {database: 'test2'}
db.setBulk @records, options, (error, output) ->
test.equal output.num, '3'
db.matchPrefix 'bulk', null, options, (error, output) ->
@@ -575,7 +586,7 @@ module.exports =
'allows the database to be specified': (test) ->
test.expect 5
- options = {database: 'test2.kct'}
+ options = {database: 'test2'}
db.setBulk @records, options, (error, output) ->
test.equal output.num, '4'
db.matchRegex 'bulk.*', null, options, (error, output) ->

0 comments on commit dc0e80a

Please sign in to comment.