From b82ce54d60fb31af54b3e1bb77015f18167cff66 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Tue, 2 Mar 2010 00:51:15 +0100 Subject: [PATCH] $SAFE=4 and persistent storage! That is what i want. --- bin/box.rb | 70 ++++++++++++++++++++++++++++++++++++---------------- bin/loganinc | 1 + 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/bin/box.rb b/bin/box.rb index 0058f77..7515402 100755 --- a/bin/box.rb +++ b/bin/box.rb @@ -2,38 +2,66 @@ require 'thread' -class Queue - attr_reader :que, :waiting -end - class Box - attr_reader :_ + attr_reader :_, :emited + alias db emited + alias persistent emited attr_accessor :emited - def initialize _ - @_, @emited = _, [] + def initialize db, _ + @_, @emited = _, db end def emit k, v - @emited.push [k, v] + @emited[k] = v + end + + def do code + instance_eval code, self.class.to_s, 0 end end -Thread.abort_on_exception = true -q, o, r = Queue.new, Queue.new, nil -puts q.inspect -$stdout.print "(0)$ " +require 'sbdb' -STDIN.each_with_index do |l, i| - r = begin - Thread.new do +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 + end +end + +#Persistent.freeze + +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 l.untaint $SAFE = 4 - b = Box.new r - [b.instance_eval( l, 'BOX', 0), b.emited] + b = Box.new db, r + begin + b.do( l) + rescue Object + $! + end end.value - rescue Object - [$!.class, $!, $!.backtrace].inspect - end - $stdout.print "#{r.inspect}\n(#{i+1})$ " + $stdout.print "=> #{r.inspect}\n(#{i+1})$ " + end end diff --git a/bin/loganinc b/bin/loganinc index 2817055..3e330e0 100755 --- a/bin/loganinc +++ b/bin/loganinc @@ -44,6 +44,7 @@ end SBDB::Env.new 'conf' do |conf| SBDB::Env.new 'logs' do |logs| SBDB::Env.new 'cache' do |cache| + test = cache[ 'test'] while line = logs[ 'newids'].get( nil, "\0\0\0\0", nil, SBDB::CONSUME_WAIT) end