Class: SLDB::AbstractSLDB
- Inherits:
-
Object
- Object
- SLDB::AbstractSLDB
show all
- Includes:
- Util
- Defined in:
- lib/sldb.rb,
lib/sldb-0.2.0.rb
Instance Method Summary
collapse
Methods included from Util
append_features, #env_boolean, #env_float, #env_floatlist, #env_int, #env_intlist, #erreq, #escape, #escape!, export, #fork, #getopt, #hashify, #host, #hostname, #optfilter, #quote, #stamptime, #system, #timestamp, #tmpnam, #uncache
Constructor Details
Returns a new instance of AbstractSLDB.
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
# File 'lib/sldb.rb', line 552
def initialize(*args)
@args, @opts = Util::optfilter args
@path = @args.first || __getopt('path')
raise ArgumentError, 'no path' unless @path
@dirname = @path
@dbpath = File::join @dirname, 'db'
@schema = File::join @dirname, '.schema'
@waiting_w = File::join @dirname, ".#{ Util::hostname }.#{ $$ }.waiting.w"
@waiting_r = File::join @dirname, ".#{ Util::hostname }.#{ $$ }.waiting.r"
@lock_w = File::join @dirname, ".#{ Util::hostname }.#{ $$ }.lock.w"
@lock_r = File::join @dirname, ".#{ Util::hostname }.#{ $$ }.lock.r"
@lockfile = File::join @dirname, '.lock'
@lockf = Lockfile::new(File::join(@dirname, '.lockf'))
@in_transaction = false
@in_ro_transaction = false
@db = nil
@synchronizing = false
@lockd_recover = File::join(File::dirname(@dirname), ".#{ File::basename(@dirname) }.lockd_recover")
@lockd_recover_lockf = Lockfile::new "#{ @lockd_recover }.lock"
@lockd_recovered = false
@sync = Sync::new
@logger = __getopt 'logger', SLDB::default_logger
@sql_debug = __getopt 'sql_debug'
@busy_sc = __getopt 'busy_sc'
@transaction_retries = __getopt 'transaction_retries'
@aquire_lock_sc = __getopt 'aquire_lock_sc'
@transaction_retries_sc = __getopt 'transaction_retries_sc'
@attempt_lockd_recovery = __getopt 'attempt_lockd_recovery'
@lockd_recover_wait = __getopt 'lockd_recover_wait'
@aquire_lock_lockfile_stale_age = __getopt 'aquire_lock_lockfile_stale_age'
@aquire_lock_refresh_rate = __getopt 'aquire_lock_refresh_rate'
@refresher = __getopt 'refresher'
@administrator = __getopt 'administrator'
__bootstrap
end
|
Instance Method Details
#execute(sql, &block) ⇒ Object
650
651
652
653
654
655
656
657
658
659
660
661
662
|
# File 'lib/sldb.rb', line 650
def execute sql, &block
raise 'not in transaction' unless @in_transaction or @in_ro_transaction
raise 'not connected' unless @connected
__logger << "sql_debug:\n#{ sql }\n" if @sql_debug
begin
@db.execute sql, &block
rescue SQLite::SQLException => e
error{ "bad sql!\n#{ sql }" }
raise
end
end
|
#field_names(table_name = nil) ⇒ Object
Also known as:
fieldnames, fields, fields_for
744
745
746
747
748
749
750
751
752
753
754
755
756
|
# File 'lib/sldb.rb', line 744
def field_names table_name = nil
fields = Hash::new{|h,k| h[k] = []}
ro_transaction do
tnames = (table_name ? [table_name] : table_names)
tnames.each do |tname|
tuples = execute "pragma table_info('#{ tname }')"
tuples.each{|t| fields[tname] << t['name']}
end
end
table_name ? fields[table_name] : fields
end
|
#h2t(h, table_name = nil) ⇒ Object
770
771
772
773
774
775
776
777
778
779
|
# File 'lib/sldb.rb', line 770
def h2t h, table_name = nil
table_name = table_names.first
raise ArgumentError, "no table_name" unless table_name
fields = field_names table_name
t = tuple
fields.each{|f| t[f] = h[f]}
t
end
|
#integrity_check ⇒ Object
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
|
# File 'lib/sldb.rb', line 690
def integrity_check
debug{ "running integrity_check on <#{ @dbpath }>" }
ret = nil
__synchronizing do
if @in_transaction
ret = yield
else
__busy_catch do
__transaction do
__lockd_recover_wrap(opts) do
__transaction_wrap(opts) do
__aquire_lock(opts) do
__connect(opts) do
tuple = execute 'PRAGMA integrity_check;'
ret = (tuple and tuple.first and (tuple.first["integrity_check"] =~ /^\s*ok\s*$/io))
end
end
end
end
end
end
end
end
ret
end
|
#lock(opts = {}) ⇒ Object
Also known as:
write_lock
718
719
720
721
722
|
# File 'lib/sldb.rb', line 718
def lock opts = {}
__lockd_recover_wrap(opts){ __aquire_lock(opts){ yield }}
end
|
#read_lock(opts = {}, &block) ⇒ Object
Also known as:
rlock
726
727
728
729
730
731
|
# File 'lib/sldb.rb', line 726
def read_lock(opts = {}, &block)
opts['read_only'] = true
lock opts, &block
end
|
#ro_transaction(opts = {}, &block) ⇒ Object
Also known as:
read_only_transaction
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
|
# File 'lib/sldb.rb', line 594
def ro_transaction(opts = {}, &block)
ret = nil
opts['read_only'] = true
__synchronizing do
if @in_ro_transaction or @in_transaction
ret = yield
else
__busy_catch do
__ro_transaction do
__lockd_recover_wrap(opts) do
__transaction_wrap(opts) do
__aquire_lock(opts) do
__connect(opts) do
ret = yield
end
end
end
end
end
end
end
end
ret
end
|
#t2h(tuple) ⇒ Object
761
762
763
764
765
766
767
768
|
# File 'lib/sldb.rb', line 761
def t2h tuple
h = {}
fields = tuple.fields
fields.each{|f| h[f] = tuple[f]}
h
end
|
#table_names ⇒ Object
Also known as:
tablenames
734
735
736
737
738
739
740
741
|
# File 'lib/sldb.rb', line 734
def table_names
ro_transaction do
tuples = execute "select name from sqlite_master where type = 'table' and name notnull"
tuples.map{|t| t['name']}
end
end
|
#transaction(opts = {}) ⇒ Object
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
|
# File 'lib/sldb.rb', line 622
def transaction opts = {}
ret = nil
__synchronizing do
if @in_transaction
ret = yield
else
__busy_catch do
__transaction do
__lockd_recover_wrap(opts) do
__transaction_wrap(opts) do
__aquire_lock(opts) do
__connect(opts) do
execute 'begin'
ret = yield
execute 'commit'
end
end
end
end
end
end
end
end
ret
end
|
#tuple(table_name = nil) ⇒ Object
Also known as:
tuple_for
781
782
783
784
785
786
787
788
789
790
|
# File 'lib/sldb.rb', line 781
def tuple table_name = nil
table_name = table_names.first
raise ArgumentError, "no table_name" unless table_name
fields = field_names table_name
t = Array::new fields.size
t.fields = fields
t
end
|
#vacuum ⇒ Object
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
|
# File 'lib/sldb.rb', line 664
def vacuum
ret = false
__synchronizing do
if @in_transaction
ret = yield
else
__busy_catch do
__transaction do
__lockd_recover_wrap(opts) do
__transaction_wrap(opts) do
__aquire_lock(opts) do
__connect(opts) do
ret = execute 'vacuum'
end
end
end
end
end
end
end
end
ret
end
|