2010-02-26 01:57:26 +01:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
|
|
|
|
require 'thread'
|
|
|
|
|
2010-03-01 01:28:13 +01:00
|
|
|
class Box
|
2010-03-02 00:51:15 +01:00
|
|
|
attr_reader :_, :emited
|
|
|
|
alias db emited
|
|
|
|
alias persistent emited
|
2010-03-01 01:28:13 +01:00
|
|
|
attr_accessor :emited
|
2010-02-26 01:57:26 +01:00
|
|
|
|
2010-03-02 00:51:15 +01:00
|
|
|
def initialize db, _
|
|
|
|
@_, @emited = _, db
|
2010-03-01 01:28:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def emit k, v
|
2010-03-02 00:51:15 +01:00
|
|
|
@emited[k] = v
|
|
|
|
end
|
|
|
|
|
|
|
|
def do code
|
|
|
|
instance_eval code, self.class.to_s, 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
require 'sbdb'
|
|
|
|
|
|
|
|
class Persistent
|
|
|
|
include Enumerable
|
|
|
|
def initialize( db) @db, @cursor = db, db.cursor end
|
|
|
|
def emit( k, v) @db[k] = v end
|
|
|
|
alias push emit
|
|
|
|
alias put emit
|
|
|
|
alias []= emit
|
|
|
|
def get( k) @db[k] end
|
|
|
|
alias [] get
|
|
|
|
alias fetch get
|
|
|
|
def inspect() "#<%s:0x%016x>" % [ self.class, self.object_id ] end
|
|
|
|
def each &e
|
|
|
|
e ? @cursor.each( &e) : Enumerator.new( self, :each)
|
|
|
|
end
|
|
|
|
def to_hash
|
|
|
|
h = {}
|
|
|
|
each {|k, v| h[ k] = v }
|
|
|
|
h
|
2010-02-26 01:57:26 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-02 00:51:15 +01:00
|
|
|
#Persistent.freeze
|
2010-02-26 01:57:26 +01:00
|
|
|
|
2010-03-02 00:51:15 +01:00
|
|
|
r = nil
|
|
|
|
Dir.mkdir 'logs' rescue Errno::EEXIST
|
|
|
|
SBDB::Env.new 'logs', SBDB::CREATE | SBDB::Env::INIT_TRANSACTION do |logs|
|
|
|
|
db = Persistent.new logs['test', :type => SBDB::Btree, :flags => SBDB::CREATE]
|
|
|
|
$stdout.print "(0)$ "
|
|
|
|
STDIN.each_with_index do |l, i|
|
|
|
|
r = Thread.new do
|
2010-03-01 01:28:13 +01:00
|
|
|
l.untaint
|
|
|
|
$SAFE = 4
|
2010-03-02 00:51:15 +01:00
|
|
|
b = Box.new db, r
|
|
|
|
begin
|
|
|
|
b.do( l)
|
|
|
|
rescue Object
|
|
|
|
$!
|
|
|
|
end
|
2010-03-01 01:28:13 +01:00
|
|
|
end.value
|
2010-03-02 00:51:15 +01:00
|
|
|
$stdout.print "=> #{r.inspect}\n(#{i+1})$ "
|
|
|
|
end
|
2010-02-26 01:57:26 +01:00
|
|
|
end
|