Class: FDB::Database

Inherits:
FormerFuture show all
Defined in:
lib/fdbimpl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FormerFuture

#block_until_ready, #on_ready, #ready?

Constructor Details

#initialize(dpointer) ⇒ Database

Returns a new instance of Database.



551
552
553
554
555
556
557
# File 'lib/fdbimpl.rb', line 551

def initialize(dpointer)
  @dpointer = dpointer
  @options = DatabaseOptions.new lambda { |code, param|
    FDBC.check_error FDBC.fdb_database_set_option(dpointer, code, param, param.nil? ? 0 : param.bytesize)
  }
  ObjectSpace.define_finalizer(self, self.class.finalize(@dpointer))
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



542
543
544
# File 'lib/fdbimpl.rb', line 542

def options
  @options
end

Class Method Details

.finalize(ptr) ⇒ Object



544
545
546
547
548
549
# File 'lib/fdbimpl.rb', line 544

def self.finalize(ptr)
  proc do
    # puts "Destroying database #{ptr}"
    FDBC.fdb_database_destroy(ptr)
  end
end

Instance Method Details

#clear(key) ⇒ Object



605
606
607
608
609
# File 'lib/fdbimpl.rb', line 605

def clear(key)
  transact do |tr|
    tr.clear(key)
  end
end

#clear_and_watch(key) ⇒ Object



655
656
657
658
659
660
# File 'lib/fdbimpl.rb', line 655

def clear_and_watch(key)
  transact do |tr|
    tr.clear(key)
    tr.watch(key)
  end
end

#clear_range(bkey, ekey) ⇒ Object



611
612
613
614
615
# File 'lib/fdbimpl.rb', line 611

def clear_range(bkey, ekey)
  transact do |tr|
    tr.clear_range(bkey, ekey)
  end
end

#clear_range_start_with(prefix) ⇒ Object



634
635
636
637
638
# File 'lib/fdbimpl.rb', line 634

def clear_range_start_with(prefix)
  transact do |tr|
    tr.clear_range_start_with(prefix)
  end
end

#create_transactionObject



559
560
561
562
563
# File 'lib/fdbimpl.rb', line 559

def create_transaction
  tr = FFI::MemoryPointer.new :pointer
  FDBC.check_error FDBC.fdb_database_create_transaction(@dpointer, tr)
  Transaction.new(tr.read_pointer, self)
end

#get(key) ⇒ Object Also known as: []



587
588
589
590
591
# File 'lib/fdbimpl.rb', line 587

def get(key)
  transact do |tr|
    tr[key].value
  end
end

#get_and_watch(key) ⇒ Object



640
641
642
643
644
645
646
# File 'lib/fdbimpl.rb', line 640

def get_and_watch(key)
  transact do |tr|
    value = tr.get(key)
    watch = tr.watch(key)
    [value.value, watch]
  end
end

#get_key(keysel) ⇒ Object



617
618
619
620
621
# File 'lib/fdbimpl.rb', line 617

def get_key(keysel)
  transact do |tr|
    tr.get_key(keysel).value
  end
end

#get_range(bkeysel, ekeysel, options = {}, &block) ⇒ Object



594
595
596
597
598
599
600
601
602
603
# File 'lib/fdbimpl.rb', line 594

def get_range(bkeysel, ekeysel, options={}, &block)
  transact do |tr|
    a = tr.get_range(bkeysel, ekeysel, options).to_a
    if block_given?
      a.each &block
    else
      a
    end
  end
end

#get_range_start_with(prefix, options = {}, &block) ⇒ Object



623
624
625
626
627
628
629
630
631
632
# File 'lib/fdbimpl.rb', line 623

def get_range_start_with(prefix, options={}, &block)
  transact do |tr|
    a = tr.get_range_start_with(prefix, options).to_a
    if block_given?
      a.each &block
    else
      a
    end
  end
end

#set(key, value) ⇒ Object Also known as: []=



580
581
582
583
584
# File 'lib/fdbimpl.rb', line 580

def set(key, value)
  transact do |tr|
    tr[key] = value
  end
end

#set_and_watch(key, value) ⇒ Object



648
649
650
651
652
653
# File 'lib/fdbimpl.rb', line 648

def set_and_watch(key, value)
  transact do |tr|
    tr.set(key, value)
    tr.watch(key)
  end
end

#transactObject



565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/fdbimpl.rb', line 565

def transact
  tr = create_transaction
  committed = false
  begin
    ret = yield tr
    # puts ret
    tr.commit.wait
    committed = true
  rescue Error => e
    # puts "Rescued #{e}"
    tr.on_error(e).wait
  end until committed
  ret
end

#watch(key) ⇒ Object



662
663
664
665
666
# File 'lib/fdbimpl.rb', line 662

def watch(key)
  transact do |tr|
    tr.watch(key)
  end
end