Class: LevelDB::Iterator

Inherits:
Object
  • Object
show all
Defined in:
lib/leveldb/iterator.rb

Instance Method Summary collapse

Constructor Details

#initialize(db, read_opts, read_len, reverse = false) ⇒ Iterator

Returns a new instance of Iterator.


3
4
5
6
7
8
9
10
11
12
13
# File 'lib/leveldb/iterator.rb', line 3

def initialize(db, read_opts, read_len, reverse=false)
  @_db        = db
  @_read_opts = read_opts
  @_read_len  = read_len
  @_reverse   = reverse
  @_err       = C::Pointer.malloc(C::SIZEOF_VOIDP)
  @_err.free  = C[:free]
  @_iterator  = C.create_iterator(@_db, @_read_opts)
  # @_iterator.free = C[:iter_destroy]
  rewind
end

Instance Method Details

#each(&block) ⇒ Object


29
30
31
32
33
34
35
# File 'lib/leveldb/iterator.rb', line 29

def each(&block)
  return self unless block_given?
  if current = self.next
    block[*current]
  end while valid?
  @_range = nil
end

#inspectObject Also known as: to_s


77
78
79
# File 'lib/leveldb/iterator.rb', line 77

def inspect
  "#<LevelDB::Iterator:#{'0x%x' % object_id}>"
end

#nextObject


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/leveldb/iterator.rb', line 45

def next
  while valid? && !in_range?
    move_next
  end if range?

  key, val = current

  return unless key

  [key, val]
ensure
  move_next
end

#peekObject


59
60
61
62
63
# File 'lib/leveldb/iterator.rb', line 59

def peek
  self.next
ensure
  move_prev
end

#range(from, to, &block) ⇒ Object


37
38
39
40
41
42
43
# File 'lib/leveldb/iterator.rb', line 37

def range(from, to, &block)
  @_range = [from.to_s, to.to_s].sort
  @_range = @_range.reverse if reverse?
  C.iter_seek(@_iterator, @_range.first, @_range.first.bytesize) if !reverse?

  each(&block)
end

#range?Boolean

Returns:

  • (Boolean)

73
74
75
# File 'lib/leveldb/iterator.rb', line 73

def range?
  !!@_range
end

#reverse?Boolean

Returns:

  • (Boolean)

69
70
71
# File 'lib/leveldb/iterator.rb', line 69

def reverse?
  @_reverse
end

#reverse_each(&block) ⇒ Object


23
24
25
26
27
# File 'lib/leveldb/iterator.rb', line 23

def reverse_each(&block)
  @_reverse = !@_reverse
  rewind
  each(&block)
end

#rewindObject


15
16
17
18
19
20
21
# File 'lib/leveldb/iterator.rb', line 15

def rewind
  if reverse?
    C.iter_seek_to_last(@_iterator)
  else
    C.iter_seek_to_first(@_iterator)
  end
end

#valid?Boolean

Returns:

  • (Boolean)

65
66
67
# File 'lib/leveldb/iterator.rb', line 65

def valid?
  C.iter_valid(@_iterator) == 1
end