Skip to content

Instantly share code, notes, and snippets.

@uu59
Created July 12, 2011 23:35
Show Gist options
  • Save uu59/1079428 to your computer and use it in GitHub Desktop.
Save uu59/1079428 to your computer and use it in GitHub Desktop.
# -- coding: utf-8
require "kyotocabinet"
class Stash
include Enumerable
def initialize(&block)
@buf = []
@filter = block
end
def each(&block)
@buf.each{|v| block.call(v)}
end
def visit_full(key, value)
@buf << key if @filter.call(key, value)
KyotoCabinet::Visitor::NOP
end
end
KyotoCabinet::DB.process('bar.kct') {|db|
1.upto(100){|v|
db[v.to_s] = v.to_s + "-value"
}
visitor = Stash.new{|k,v|
k.match(/3/)
}
db.iterate(visitor)
p visitor.take(2).map{|k| db[k]}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment