Permalink
Browse files

Remove last of `operation.h` code in Iterator

  • Loading branch information...
mikepb committed Feb 25, 2012
1 parent 15a1d92 commit 8438dc8124d729311c693a43db6f262976acadeb
Showing with 189 additions and 273 deletions.
  1. +19 −23 src/coffee/leveldb/iterator.coffee
  2. +159 −82 src/cpp/iterator.cc
  3. +11 −39 src/cpp/iterator.h
  4. +0 −128 src/cpp/operation.h
  5. +0 −1 wscript
@@ -29,6 +29,7 @@ binding = require '../leveldb.node'
exports.Iterator = class Iterator
busy = false
+ validKey = false
lock = ->
throw new Error 'Concurrent operations not supported' if busy
@@ -38,6 +39,11 @@ exports.Iterator = class Iterator
throw new Error 'Not locked' unless busy
busy = false
+ afterSeek = (callback) -> (err, valid) ->
+ unlock()
+ validKey = valid
+ callback err if callback
+
###
Constructor.
@@ -115,11 +121,7 @@ exports.Iterator = class Iterator
###
- valid: ->
- lock()
- answer = @self.valid()
- unlock()
- answer
+ valid: -> validKey
###
@@ -135,9 +137,7 @@ exports.Iterator = class Iterator
seek: (key, callback) ->
key = new Buffer key unless Buffer.isBuffer key
lock()
- @self.seek key, (err) ->
- unlock()
- callback err if callback
+ @self.seek key, afterSeek callback
###
@@ -151,9 +151,7 @@ exports.Iterator = class Iterator
first: (callback) ->
lock()
- @self.first (err) ->
- unlock()
- callback err if callback
+ @self.first afterSeek callback
###
@@ -167,9 +165,7 @@ exports.Iterator = class Iterator
last: (callback) ->
lock()
- @self.last (err) ->
- unlock()
- callback err if callback
+ @self.last afterSeek callback
###
@@ -182,10 +178,9 @@ exports.Iterator = class Iterator
###
next: (callback) ->
+ throw new Error 'Illegal state' unless validKey
lock()
- @self.next (err) ->
- unlock()
- callback err if callback
+ @self.next afterSeek callback
###
@@ -198,10 +193,9 @@ exports.Iterator = class Iterator
###
prev: (callback) ->
+ throw new Error 'Illegal state' unless validKey
lock()
- @self.prev (err) ->
- unlock()
- callback err if callback
+ @self.prev afterSeek callback
###
@@ -218,6 +212,7 @@ exports.Iterator = class Iterator
###
key: (options = {}, callback) ->
+ throw new Error 'Illegal state' unless validKey
# optional options
if typeof options is 'function'
@@ -226,7 +221,8 @@ exports.Iterator = class Iterator
throw new Error 'Missing callback' unless callback
- # async
+ return callback() unless validKey
+
lock()
@self.key (err, key) ->
unlock()
@@ -272,15 +268,15 @@ exports.Iterator = class Iterator
###
current: (options = {}, callback) ->
-
# optional options
if typeof options is 'function'
callback = options
options = {}
throw new Error 'Missing callback' unless callback
- # async
+ return callback() unless validKey
+
lock()
@self.current (err, kv) ->
unlock()
Oops, something went wrong.

0 comments on commit 8438dc8

Please sign in to comment.