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.



539
540
541
542
543
544
545
# File 'lib/fdbimpl.rb', line 539

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.



530
531
532
# File 'lib/fdbimpl.rb', line 530

def options
  @options
end

Class Method Details

.finalize(ptr) ⇒ Object



532
533
534
535
536
537
# File 'lib/fdbimpl.rb', line 532

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

Instance Method Details

#clear(key) ⇒ Object



593
594
595
596
597
# File 'lib/fdbimpl.rb', line 593

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

#clear_and_watch(key) ⇒ Object



643
644
645
646
647
648
# File 'lib/fdbimpl.rb', line 643

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

#clear_range(bkey, ekey) ⇒ Object



599
600
601
602
603
# File 'lib/fdbimpl.rb', line 599

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

#clear_range_start_with(prefix) ⇒ Object



622
623
624
625
626
# File 'lib/fdbimpl.rb', line 622

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

#create_transactionObject



547
548
549
550
551
# File 'lib/fdbimpl.rb', line 547

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: []



575
576
577
578
579
# File 'lib/fdbimpl.rb', line 575

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

#get_and_watch(key) ⇒ Object



628
629
630
631
632
633
634
# File 'lib/fdbimpl.rb', line 628

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



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

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

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



582
583
584
585
586
587
588
589
590
591
# File 'lib/fdbimpl.rb', line 582

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



611
612
613
614
615
616
617
618
619
620
# File 'lib/fdbimpl.rb', line 611

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: []=



568
569
570
571
572
# File 'lib/fdbimpl.rb', line 568

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

#set_and_watch(key, value) ⇒ Object



636
637
638
639
640
641
# File 'lib/fdbimpl.rb', line 636

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

#transactObject



553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/fdbimpl.rb', line 553

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



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

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