Module: Xampl

Defined in:
lib/xamplr.rb,
lib/xamplr/mixins.rb,
lib/xamplr/to-xml.rb,
lib/xamplr/to-ruby.rb,
lib/xamplr/visitor.rb,
lib/xamplr/from-xml.rb,
lib/xamplr/iterator.rb,
lib/xamplr/visitors.rb,
lib/xamplr/xml-text.rb,
lib/xamplr/persister.rb,
lib/xamplr/exceptions.rb,
lib/xamplr/persistence.rb,
lib/xamplr/xampl-module.rb,
lib/xamplr/xampl-object.rb,
lib/xamplr/from-xml-orig.rb,
lib/xamplr/notifications.rb,
lib/xamplr/persist-to-xml.rb,
lib/xamplr/persisters/dumb.rb,
lib/xamplr/persisters/redis.rb,
lib/xamplr/persisters/caches.rb,
lib/xamplr/persisters/simple.rb,
lib/xamplr/persisters/caching.rb,
lib/xamplr/persisters/in-memory.rb,
lib/xamplr/persisters/filesystem.rb,
lib/xamplr/tests/redis/redis_spec.rb,
lib/xamplr/xampl-object-internals.rb,
lib/xamplr/xampl-persisted-object.rb,
lib/xamplr/persisters/tokyo-cabinet.rb

Defined Under Namespace

Modules: InvalidXampl, XamplExtensionsToRubyObjects, XamplObject, XamplPersistedObject, XamplWithDataContent, XamplWithMixedContent, XamplWithSimpleContent, XamplWithoutContent Classes: AbstractCachingPersister, AlreadyKnownToPersister, BlockedChange, CopyXML, CountingVisitor, DumbPersister, DuplicateXamplInPersister, FilesystemPersister, FromXML, FromXML_original, InMemoryPersister, IncompatiblePersisterConfiguration, IncompatiblePersisterRequest, Iterator, MarkChangedDeep, MixedPersisters, NoActivePersister, NoAnonymousPersisters, NoPersisterNamed, NotXamplPersistedObject, PersistXML, Persister, PrettyXML, RedisPersister, RemoveWhitespaceContentVisitor, ResetIsChanged, ReturnOrThrowInTransaction, RubyPrinter, SimplePersister, TableQuery, TokyoCabinetPersister, TransactionWork, UnexpectedExitFromTransaction, UnmanagedChange, Visitor, XMLPrinter, XMLText, XamplCache, XamplCacheLFU, XamplException, XamplIsInvalid, XamplLiteralRubyObject

Constant Summary collapse

DEFAULT_CAPACITY =

TODO – this is way too big! TODO – FIX THIS PROBLEM

increase the default cache size to 2000 (which is way too big, but there is
a bug... removing from the cache does not remove from memory, so if a
storybook exists that points (even indirectly) at something flushed, the
thing flushed is still held in memory. What is needed is for the thing to
be invalidated somehow withought DUPLICATES being created when the thing
is read again next time)
2000
@@logger =
Logger.new(STDOUT)
@@persister =
nil
@@known_persisters =
{}
@@persister_kinds =
{}
@@factory_default_persister_options =
{
        :kind => :simple,
        :format => :xml_format
}
@@default_persister_options =
{}.merge(@@factory_default_persister_options)
@@xampl_lock =
Sync.new
@@verbose_transactions =
true
@@abnormal_return_from_transactions_are_errors =
true
@@xampl_extends_symbols =
false

Class Method Summary collapse

Class Method Details

.abnormal_return_from_transactions_are_errorsObject



201
202
203
# File 'lib/xamplr/persistence.rb', line 201

def Xampl.abnormal_return_from_transactions_are_errors
  @@abnormal_return_from_transactions_are_errors
end

.abnormal_return_from_transactions_are_errors=(v) ⇒ Object



205
206
207
# File 'lib/xamplr/persistence.rb', line 205

def Xampl.abnormal_return_from_transactions_are_errors=(v)
  @@abnormal_return_from_transactions_are_errors = v
end

.add_lexical_indexs(indexes) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/xamplr/persistence.rb', line 154

def Xampl.add_lexical_indexs(indexes)
  case Xampl.default_persister_kind
    when :tokyo_cabinet then
      Xampl::TokyoCabinetPersister.add_lexical_indexs(indexes)
    when :redis then
      Xampl::RedisPersister.add_lexical_indexs(indexes)
    #when :mongo then
    #  Xampl::MongoPersister.add_lexical_indexs(indexes)
    else
      raise IncompatiblePersisterConfiguration.new(Xampl.default_persister_kind, "lexical_indexes")
  end
end

.add_numerical_indexs(indexes) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/xamplr/persistence.rb', line 167

def Xampl.add_numerical_indexs(indexes)
  case Xampl.default_persister_kind
    when :tokyo_cabinet then
      Xampl::TokyoCabinetPersister.add_numerical_indexs(indexes)
    when :redis then
      Xampl::RedisPersister.add_numerical_indexs(indexes)
    #when :mongo then
    #  Xampl::MongoPersister.add_numerical_indexs(indexes)
    else
      raise IncompatiblePersisterConfiguration.new(Xampl.default_persister_kind, "numerical_indexs")
  end
end

.auto_cache(xampl) ⇒ Object



601
602
603
604
605
606
607
608
# File 'lib/xamplr/persistence.rb', line 601

def Xampl.auto_cache(xampl)
  if (nil == xampl.persister) and @@persister and @@persister.automatic then
    xampl.persister = @@persister
  end
  if xampl.persister and xampl.persister.automatic then
    xampl.persister.cache(xampl)
  end
end

.auto_persistence(on = true) ⇒ Object



19
20
21
22
23
# File 'lib/xamplr/persistence.rb', line 19

def Xampl.auto_persistence(on=true)
  if (@@persister) then
    @@persister.automatic = on
  end
end

.auto_uncache(xampl) ⇒ Object



610
611
612
613
614
# File 'lib/xamplr/persistence.rb', line 610

def Xampl.auto_uncache(xampl)
  if xampl.persister and xampl.persister.automatic then
    xampl.persister.uncache(xampl)
  end
end

.block_future_changes(on = true) ⇒ Object



13
14
15
16
17
# File 'lib/xamplr/persistence.rb', line 13

def Xampl.block_future_changes(on=true)
  if (@@persister) then
    @@persister.block_changes = on
  end
end

.cache(xampl) ⇒ Object



673
674
675
# File 'lib/xamplr/persistence.rb', line 673

def Xampl.cache(xampl)
  @@persister.cache(xampl)
end

.clear_cacheObject

Raises:



616
617
618
619
# File 'lib/xamplr/persistence.rb', line 616

def Xampl.clear_cache
  raise NoActivePersister unless @@persister
  @@persister.clear_cache
end

.close_all_persistersObject



631
632
633
634
635
# File 'lib/xamplr/persistence.rb', line 631

def Xampl.close_all_persisters
  @@known_persisters.each do |name, persister|
    persister.close
  end
end

.count_changedObject



593
594
595
# File 'lib/xamplr/persistence.rb', line 593

def Xampl.count_changed
  @@persister.count_changed if @@persister
end

.create_named_persister(name, kind, arg = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/xamplr/persistence.rb', line 79

def Xampl.create_named_persister(name, kind, arg=nil)
  raise NoAnonymousPersisters.new unless name # there is no such thing as an anonymous persister, maybe later

  persister = @@known_persisters[name]
  return persister if persister

  persister_class = @@persister_kinds[kind]
  return nil unless persister_class

  persister = persister_class.new(name, :xml_format, arg)
  @@known_persisters[name] = persister

  return persister
end

.default_persister_formatObject



71
72
73
# File 'lib/xamplr/persistence.rb', line 71

def Xampl.default_persister_format
  @@default_persister_options[:format]
end

.default_persister_kindObject



63
64
65
# File 'lib/xamplr/persistence.rb', line 63

def Xampl.default_persister_kind
  @@default_persister_options[:kind]
end

.default_persister_optionsObject



47
48
49
# File 'lib/xamplr/persistence.rb', line 47

def Xampl.default_persister_options
  {}.merge(@@default_persister_options)
end

.disable_all_persistersObject



29
30
31
32
33
# File 'lib/xamplr/persistence.rb', line 29

def Xampl.disable_all_persisters
  @@persister = nil
  @@known_persisters.each { |persister| persister.close }
  @@known_persisters = {}
end

.disable_persisterObject



35
36
37
# File 'lib/xamplr/persistence.rb', line 35

def Xampl.disable_persister
  @@persister = nil
end

.drop_all_persisters(verbose = false) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/xamplr/persistence.rb', line 130

def Xampl.drop_all_persisters(verbose=false)
  puts "Drop All Persisters:: --------------------------" if verbose
  @@persister = nil
  @@known_persisters.each do |name, persister|
    puts " #{ name } #{ persister.class.name }" if verbose
    next if persister == @@persister
    persister.close
    persister.clear_cache
  end
  @@known_persisters = {}
  puts "---------------------------------------------" if verbose
  GC.start
  GC.start
  GC.start
end

.drop_persister(name) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/xamplr/persistence.rb', line 146

def Xampl.drop_persister(name)
#    Xampl.print_known_persisters
  p = @@known_persisters[name]
  p.close if p
  @@known_persisters.delete(name)
#    Xampl.print_known_persisters
end

.enable_named_persister(name) ⇒ Object

Raises:



98
99
100
101
102
103
# File 'lib/xamplr/persistence.rb', line 98

def Xampl.enable_named_persister(name)
  persister = @@known_persisters[name]
  raise NoPersisterNamed.new(name) unless persister

  @@persister = persister
end

.enable_persister(name, preferred_kind = nil) ⇒ Object

Raises:



105
106
107
108
109
110
111
112
113
114
# File 'lib/xamplr/persistence.rb', line 105

def Xampl.enable_persister(name, preferred_kind=nil)
  # you'd better know what you are doing if you call this

  raise NoPersisterNamed.new unless name

  preferred_kind = preferred_kind || Xampl.default_persister_kind

  @@persister = @@known_persisters[name] || Xampl.create_named_persister(name, preferred_kind, nil)
  @@persister
end

.find_known(klass, pid) ⇒ Object

Raises:



663
664
665
666
667
# File 'lib/xamplr/persistence.rb', line 663

def Xampl.find_known(klass, pid)
  raise NoActivePersister unless @@persister
  xampl, ignore = @@persister.find_known(klass, pid)
  return xampl
end

.find_mentions_of(xampl) ⇒ Object

Raises:



800
801
802
803
# File 'lib/xamplr/persistence.rb', line 800

def Xampl.find_mentions_of(xampl)
  raise NoActivePersister unless @@persister
  @@persister.find_mentions_of(xampl)
end

.find_meta(hint = false) ⇒ Object

Raises:



790
791
792
793
# File 'lib/xamplr/persistence.rb', line 790

def Xampl.find_meta(hint=false)
  raise NoActivePersister unless @@persister
  @@persister.find_meta(hint) { |q| yield q }
end

.find_named_persister(name) ⇒ Object



94
95
96
# File 'lib/xamplr/persistence.rb', line 94

def Xampl.find_named_persister(name)
  persister = @@known_persisters[name]
end

.find_pids(hint = false) ⇒ Object

Raises:



795
796
797
798
# File 'lib/xamplr/persistence.rb', line 795

def Xampl.find_pids(hint=false)
  raise NoActivePersister unless @@persister
  @@persister.find_pids(hint) { |q| yield q }
end

.find_things_to_delete(scheduled_before = Time.now.to_i) ⇒ Object



219
220
221
222
223
224
# File 'lib/xamplr/xampl-object.rb', line 219

def Xampl.find_things_to_delete(scheduled_before=Time.now.to_i)
  things = Xampl.query do | q |
    q.add_condition('scheduled-delete-at', :lte, scheduled_before)
  end
  things
end

.find_xampl(hint = false) ⇒ Object

Raises:



785
786
787
788
# File 'lib/xamplr/persistence.rb', line 785

def Xampl.find_xampl(hint=false)
  raise NoActivePersister unless @@persister
  @@persister.find_xampl(hint) { |q| yield q }
end

.flush_persister_cachesObject



123
124
125
126
127
128
# File 'lib/xamplr/persistence.rb', line 123

def Xampl.flush_persister_caches
  Xampl.print_known_persisters
  @@persister.close
  @@known_persisters.delete(@@persister.name)
  Xampl.print_known_persisters
end

.from_xml_file(file_name, tokenise = true) ⇒ Object



273
274
275
# File 'lib/xamplr/xampl-object.rb', line 273

def Xampl.from_xml_file(file_name, tokenise=true)
  return FromXML.new.parse(file_name, tokenise)
end

.from_xml_string(xml_string, tokenise = true) ⇒ Object



269
270
271
# File 'lib/xamplr/xampl-object.rb', line 269

def Xampl.from_xml_string(xml_string, tokenise=true)
  return FromXML.new.parse_string(xml_string, tokenise)
end

.introduce_to_persister(xampl) ⇒ Object

Raises:



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

def Xampl.introduce_to_persister(xampl)
  raise NoActivePersister unless @@persister

  @@persister.introduce(xampl)
end

.lazy_load(xampl) ⇒ Object

Raises:



646
647
648
649
650
651
652
653
654
655
656
# File 'lib/xamplr/persistence.rb', line 646

def Xampl.lazy_load(xampl)
  raise NoActivePersister.new unless @@persister

  pid = xampl.get_the_index
  if xampl and pid then
    @@persister.lazy_load(xampl, xampl.class, pid)
    xampl.load_needed = false
  else
    raise "XAMPL.LAZY_LOAD -- REFUSED"
  end
end

.logObject



13
14
15
# File 'lib/xamplr.rb', line 13

def Xampl.log
  return @@logger
end

.lookup(klass, pid) ⇒ Object

Raises:



658
659
660
661
# File 'lib/xamplr/persistence.rb', line 658

def Xampl.lookup(klass, pid)
  raise NoActivePersister unless @@persister
  @@persister.lookup(klass, pid)
end

.lookup_in_map(map, klass, pid) ⇒ Object



677
678
679
680
681
682
683
684
685
686
687
688
689
690
# File 'lib/xamplr/persistence.rb', line 677

def Xampl.lookup_in_map(map, klass, pid)
  return nil if nil == pid

  module_name = klass.module_name
  tag = klass.tag

  tag_map = map[module_name]
  return nil if nil == tag_map

  pid_map = tag_map[tag]
  return nil if nil == pid_map

  return pid_map[pid]
end

.make(klass, pid = nil) {|xampl| ... } ⇒ Object

Yields:

  • (xampl)


5
6
7
8
9
10
# File 'lib/xamplr/mixins.rb', line 5

def Xampl.make(klass, pid=nil)
  xampl = klass.new
  xampl.set_the_index(pid) if nil != pid
  yield(xampl) if block_given?
  return xampl
end

.optimise(opts = {}) ⇒ Object

Raises:



769
770
771
772
773
# File 'lib/xamplr/persistence.rb', line 769

def Xampl.optimise(opts={})
  raise NoActivePersister unless @@persister

  @@persister.optimise(opts)
end

.persisterObject



9
10
11
# File 'lib/xamplr/persistence.rb', line 9

def Xampl.persister
  @@persister
end


116
117
118
119
120
121
# File 'lib/xamplr/persistence.rb', line 116

def Xampl.print_known_persisters
  puts "Known Persisters:: --------------------------"
  @@known_persisters.each { |n, k| puts " #{n} #{k}" }
  puts "---------------------------------------------"
  puts caller(0)
end


597
598
599
# File 'lib/xamplr/persistence.rb', line 597

def Xampl.print_stats
  @@persister.print_stats if @@persister
end

.query(hint = false) ⇒ Object

Raises:



780
781
782
783
# File 'lib/xamplr/persistence.rb', line 780

def Xampl.query(hint=false)
  raise NoActivePersister unless @@persister
  @@persister.query(hint) { |q| yield q }
end

.query_implementedObject

Raises:



775
776
777
778
# File 'lib/xamplr/persistence.rb', line 775

def Xampl.query_implemented
  raise NoActivePersister unless @@persister
  @@persister.query_implemented
end

.raw_persister_optionsObject



51
52
53
# File 'lib/xamplr/persistence.rb', line 51

def Xampl.raw_persister_options
  @@default_persister_options
end

.read_only(target_persister) ⇒ Object



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
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
# File 'lib/xamplr/persistence.rb', line 529

def Xampl.read_only(target_persister)
  @@xampl_lock.synchronize(:EX) do
    name = target_persister.name

    if block_given? then
      initial_persister = @@persister
      Xampl.enable_persister(name, target_persister.kind)

      rollback = true
      original_automatic = @@persister.automatic
      original_changed = @changed
      @changed = {}
      begin
        Xampl.auto_persistence(false)
        #Xampl.block_future_changes(true)

        yield
        rollback = false
      ensure
        ####        Xampl.auto_persistence(original_automatic)
        ####        #Xampl.block_future_changes(false)
        ####
        ####        if 0 < @changed.size then
        ####          puts "CHANGED COUNT: #{@changed.size}"
        ####          raise BlockedChange.new(target_persister)
        ####        end
        ####
        ####        @changed = original_changed
        ####
        ####        puts "ROLLBACK(#{__LINE__})" if rollback
        ####			  Xampl.rollback if rollback
        ####	      @@persister = initial_persister

        Xampl.auto_persistence(original_automatic)
        #Xampl.block_future_changes(false)

        if 0 == @changed.size then
          @changed = original_changed

          #puts "ROLLBACK(#{__LINE__})" if rollback
          Xampl.rollback if rollback
          @@persister = initial_persister
        else
          #puts "CHANGED COUNT: #{@changed.size}"
          @changed = original_changed

          #puts "ROLLBACK(#{__LINE__})" if rollback
          Xampl.rollback

          @@persister = initial_persister

          raise BlockedChange.new(target_persister)
        end
      end
    end
  end
end

.read_only_transaction(thing, kind = nil, automatic = true, format = nil, &block) ⇒ Object



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/xamplr/persistence.rb', line 466

def Xampl.read_only_transaction(thing, kind=nil, automatic=true, format=nil, & block)
  if String === thing then
    name = thing
  elsif XamplObject === thing then
    name = thing.persister.name
  else
    raise XamplException.new("can't base a transaction on a #{thing.class.name} (#{thing})")
  end

  target_persister = nil
  if block_given? then
    @@xampl_lock.synchronize(:EX) do
      initial_persister = @@persister
      Xampl.enable_persister(name, kind)
      target_persister = @@persister

      rollback = true
      original_automatic = @@persister.automatic
      @changed ||= nil
      original_changed = @changed
      @changed = {}
      begin
        Xampl.auto_persistence(false)
        #Xampl.block_future_changes(true)

        yield
        rollback = false
      rescue => e
        exception = e
      ensure
        Xampl.auto_persistence(original_automatic)
        #Xampl.block_future_changes(false)

        if 0 == @changed.size then
          @changed = original_changed

          #if exception then
          #  puts "ROLLBACK(#{__LINE__}):: #{exception}"
          #  print exception.backtrace.join("\n") if exception
          #end

          #no change so don't bother with rollback
#            if rollback then
#              Xampl.rollback
#            end
          @@persister = initial_persister
        else
          #puts "CHANGED COUNT: #{@changed.size}"
          @changed = original_changed

          #puts "ROLLBACK(#{__LINE__}) #{exception}" if rollback
          #print exception.backtrace.join("\n")
          Xampl.rollback

          @@persister = initial_persister

          raise BlockedChange.new(target_persister)
        end
      end
    end
  end
end

.register_persister_kind(klass) ⇒ Object



25
26
27
# File 'lib/xamplr/persistence.rb', line 25

def Xampl.register_persister_kind(klass)
  @@persister_kinds[klass.kind] = klass
end

.remove_from_map(map, xampl) ⇒ Object



756
757
758
759
760
761
762
763
764
765
766
767
# File 'lib/xamplr/persistence.rb', line 756

def Xampl.remove_from_map(map, xampl)
  pid = xampl.get_the_index
  return nil unless pid

  tag_map = map[xampl.module_name]
  return nil unless tag_map

  pid_map = tag_map[xampl.tag]
  return nil unless pid_map

  return pid_map.delete(pid)
end

.rollback(persister = @@persister) ⇒ Object

Raises:



637
638
639
640
# File 'lib/xamplr/persistence.rb', line 637

def Xampl.rollback(persister=@@persister)
  raise NoActivePersister unless @@persister
  persister.rollback_cleanup
end

.rollback_allObject



642
643
644
# File 'lib/xamplr/persistence.rb', line 642

def Xampl.rollback_all
  @@known_persisters.values.each { |persister| persister.rollback }
end

.set_default_persister_format(format) ⇒ Object



75
76
77
# File 'lib/xamplr/persistence.rb', line 75

def Xampl.set_default_persister_format(format)
  @@default_persister_options[:format] = format
end

.set_default_persister_kind(kind) ⇒ Object



67
68
69
# File 'lib/xamplr/persistence.rb', line 67

def Xampl.set_default_persister_kind(kind)
  @@default_persister_options[:kind] = kind
end

.set_default_persister_properties(options) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/xamplr/persistence.rb', line 55

def Xampl.set_default_persister_properties(options)
  if options[:reset] then
    @@default_persister_options = @@factory_default_persister_options.merge(options)
  else
    @@default_persister_options = @@default_persister_options.merge(options)
  end
end

.store_in_cache(map, xampl, container) ⇒ Object



724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# File 'lib/xamplr/persistence.rb', line 724

def Xampl.store_in_cache(map, xampl, container)
  module_name = xampl.module_name
  tag = xampl.tag
  pid = xampl.get_the_index

  if nil == pid then
    return false
  end

  if block_given? then
    data = yield
  else
    data = xampl
  end

  tag_map = map[module_name]
  if nil == tag_map then
    tag_map = {}
    map[module_name] = tag_map
  end

  pid_map = tag_map[tag]
  if nil == pid_map then
    pid_map = container.fresh_cache
    tag_map[tag] = pid_map
  end

  pid_map[pid] = data

  return true
end

.store_in_map(map, xampl) ⇒ Object



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
717
718
719
720
721
722
# File 'lib/xamplr/persistence.rb', line 692

def Xampl.store_in_map(map, xampl)
  module_name = xampl.module_name
  tag = xampl.tag
  pid = xampl.get_the_index

  if nil == pid then
    return false
  end

  if block_given? then
    data = yield
  else
    data = xampl
  end

  tag_map = map[module_name]
  if nil == tag_map then
    tag_map = {}
    map[module_name] = tag_map
  end

  pid_map = tag_map[tag]
  if nil == pid_map then
    pid_map = {}
    tag_map[tag] = pid_map
  end

  pid_map[pid] = data

  return true
end

.syncObject

Raises:



621
622
623
624
625
# File 'lib/xamplr/persistence.rb', line 621

def Xampl.sync
  #raise XamplException.new(:live_across_rollback) if @@persister.rolled_back
  raise NoActivePersister unless @@persister
  @@persister.sync
end

.sync_allObject



627
628
629
# File 'lib/xamplr/persistence.rb', line 627

def Xampl.sync_all
  @@known_persisters.each { |name, persister| persister.sync }
end

.transaction(thing, kind = nil, automatic = true, format = nil, &block) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/xamplr/persistence.rb', line 292

def Xampl.transaction(thing, kind=nil, automatic=true, format=nil, & block)
  # this method cannot account for returns in transactions (proc vs lambda/method issues)
  if String === thing then
    name = thing
  elsif XamplObject === thing then
    name = thing.persister.name
  else
    raise XamplException.new("can't base a transaction on a #{thing.class.name} (#{thing})")
  end

  if block_given? then
    @@xampl_lock.synchronize(:EX) do
      rollback = true
      exception = nil
      begin
        initial_persister = @@persister
        Xampl.enable_persister(name, kind)

        original_automatic = @@persister.automatic

        begin
          #TODO -- impose some rules on nested transactions/enable_persisters??

          Xampl.auto_persistence(automatic)

          result = yield

          rollback = false
          Xampl.block_future_changes(true)
          Xampl.sync
          return result
        rescue => e
#            puts e.backtrace
          exception = e
        ensure
          Xampl.block_future_changes(false)
          Xampl.auto_persistence(original_automatic)

          if rollback then
            # we get here if the transaction block finishes early, for one of three reasons:
            #    1) exception
            #    2) throw
            #    3) explicit return in the block
            # it is arguable that throw and returns are 'okay', or normal exits from the block... I don't know???

            if exception then
              # the early finish was caused by an exception

              Xampl.rollback
            else
              # this is the throw/explicit-return

              #TODO -- is this a good idea??
              Xampl.block_future_changes(true)
              Xampl.sync


              #TODO -- uncomment this, it's handy
#                STDERR.puts "---------"
#                STDERR.puts "Either a return or a throw from a transaction. The DB is synced, but this is not a good thing to be doing."
#                STDERR.puts caller(0)
#                STDERR.puts "---------"
            end
          end
        end
      ensure
        @@persister = initial_persister

        if exception then
          raise RuntimeError, "ROLLBACK(#{__LINE__}):: #{exception}", exception.backtrace
        end
      end
    end
  end
end

.transaction_as_a_method(thing, kind = nil, automatic = true, format = nil, &block) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/xamplr/persistence.rb', line 209

def Xampl.transaction_as_a_method(thing, kind=nil, automatic=true, format=nil, & block)
  #TODO -- should this be called Xampl.transaction_safe???
  #TODO -- won't work in xampl-gen
  return nil unless block_given?

  if String === thing then
    name = thing
  elsif XamplObject === thing then
    name = thing.persister.name
  else
    raise XamplException.new("can't base a transaction on a #{thing.class.name} (#{thing})")
  end

  work = Proc.new # get the block into a proc object

  @@xampl_lock.synchronize(:EX) do
    begin
      initial_persister = @@persister
      Xampl.enable_persister(name, kind)

      original_automatic = @@persister.automatic

      okay = false
      rollback = true
      exception = nil
      abnormal_return = true
      result = nil

      begin
        #TODO -- impose some rules on nested transactions/enable_persisters??

        Xampl.auto_persistence(automatic)

        result = TransactionWork.setup_work(work)

        abnormal_return = false

        Xampl.block_future_changes(true)
        Xampl.sync
        rollback = false

        okay = true
        return result
      rescue => e
        exception = e
      ensure
        Xampl.block_future_changes(false)
        Xampl.auto_persistence(original_automatic)

        unless okay then
          if exception then
            Xampl.rollback
          elsif abnormal_return then
            if Xampl.abnormal_return_from_transactions_are_errors then
              Xampl.rollback
              exception = UnexpectedExitFromTransaction.new
            else
              begin
                Xampl.block_future_changes(true)
                Xampl.sync
              rescue => e
                exception = e
              ensure
                Xampl.block_future_changes(false)
                Xampl.auto_persistence(original_automatic)

                Xampl.rollback if exception
              end
            end
          elsif rollback then
            # don't know how this can happen, but roll it back anyway and treat it as an unexpected exit from transaction
            Xampl.rollback
            exception =  UnexpectedExitFromTransaction.new
          end
        end

        @@persister = initial_persister
        raise exception if exception
      end
    end
  end
end

.transaction_not_so_good(thing, kind = nil, automatic = true, format = nil, &block) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/xamplr/persistence.rb', line 368

def Xampl.transaction_not_so_good(thing, kind=nil, automatic=true, format=nil, & block)
  # this method cannot account for returns in transactions (proc vs lambda/method issues)
  if String === thing then
    name = thing
  elsif XamplObject === thing then
    name = thing.persister.name
  else
    raise XamplException.new("can't base a transaction on a #{thing.class.name} (#{thing})")
  end

  if block_given? then
    @@xampl_lock.synchronize(:EX) do
#      if true then
      begin
#          @@xampl_lock.sync_lock(:EX)
        initial_persister = @@persister
        Xampl.enable_persister(name, kind)

        rollback = true
        exception = nil
        original_automatic = @@persister.automatic

        begin
          #TODO -- impose some rules on nested transactions/enable_persisters??

          Xampl.auto_persistence(automatic)

          result = yield

          rollback = false
          Xampl.block_future_changes(true)
          Xampl.sync
          return result
        rescue => e
          puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] TEST ME"
          puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] rollback: #{ rollback }"
          exception = e
        ensure
          Xampl.block_future_changes(false)
          Xampl.auto_persistence(original_automatic)

          puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] rollback: #{ rollback }"
          if rollback then
            puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] TEST ME"
            puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] rollback: #{ rollback }"
            # we get here if the transaction block finishes early
            if exception then
              puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] rollback: #{ rollback }"
              # the early finish was caused by an exception
              raise RuntimeError, "ROLLBACK(#{__LINE__}):: #{exception}", exception.backtrace
            else
              puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] TEST ME"
              # How could we have arrived at this point???
              # Well, I don't know all the reasons, but the ones I do know are:
              #  - return was used in the block passed into the transaction
              #  - a throw was made
              # There's no way that I know of to distinguish, so, assume that the transaction worked
              #    20091202 -- well maybe not...

#                Xampl.rollback # this is bad
              rollback = true
#                raise  ReturnOrThrowInTransaction
              STDERR.puts "---------"
              STDERR.puts "Either a return or a throw from a transaction. The DB is possibly not synced."
              caller(0).each do |trace|
                STDERR.puts(trace)
              end
              STDERR.puts "---------"
            end
          else
            puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] TEST ME"
          end

          if rollback then
            puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] TEST ME"
            Xampl.rollback
            rollback = false
          else
            puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] TEST ME"
          end
          @@persister = initial_persister
        end
        puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] rollback: #{ rollback }"
        if exception
          puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] rollback: #{ rollback }"
          raise exception
        else
          puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] rollback: #{ rollback }"
        end
      ensure
        puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] TEST ME" if rollback
        puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] rollback: #{ rollback } ????????????" if rollback
#          @@xampl_lock.sync_unlock
      end
    end
  end
end

.verboseTransactions(v) ⇒ Object



184
185
186
# File 'lib/xamplr/persistence.rb', line 184

def Xampl.verboseTransactions(v)
  @@verbose_transactions = v
end

.write_to_cache(xampl) ⇒ Object



669
670
671
# File 'lib/xamplr/persistence.rb', line 669

def Xampl.write_to_cache(xampl)
  @@persister.write_to_cache(xampl)
end

.xampl_extends_symbolsObject



9
10
11
# File 'lib/xamplr/xampl-module.rb', line 9

def Xampl.xampl_extends_symbols
  @@xampl_extends_symbols
end

.xampl_extends_symbols=(v) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/xamplr/xampl-module.rb', line 13

def Xampl.xampl_extends_symbols=(v)
  @@xampl_extends_symbols = v

  if @@xampl_extends_symbols then
    Symbol.module_eval("include XamplExtensionsToRubyObjects")
  end
end