Class: Higgs::TransactionManager

Inherits:
Object
  • Object
show all
Includes:
Exceptions, InitOptions
Defined in:
lib/higgs/tman.rb

Defined Under Namespace

Modules: InitOptions Classes: Error, NotWritableError, PseudoSecondaryCache

Constant Summary collapse

CVS_ID =

for ident(1)

'$Id: tman.rb 841 2008-12-24 09:23:20Z toki $'

Instance Attribute Summary

Attributes included from InitOptions

#read_only

Instance Method Summary collapse

Methods included from InitOptions

#init_options

Constructor Details

#initialize(storage, options = {}) ⇒ TransactionManager

see Higgs::TransactionManager::InitOptions for options.



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/higgs/tman.rb', line 89

def initialize(storage, options={})
  @storage = storage
  init_options(options)
  if (@read_only == :standby && ! @jlog_apply_dir) then
    raise ArgumentError, "need for `:jlog_apply_dir' parameter in standby mode"
  end
  @cnum_func = @storage.method(:change_number)
  @mvcc_cache = MVCCCache.new
  @master_cache = SharedWorkCache.new(@master_cache) {|key|
    (id = @storage.unique_data_id(key) and @secondary_cache[id]) or
      (value = @storage.fetch(key) and @secondary_cache[@storage.unique_data_id(key)] = value.freeze)
  }
end

Instance Method Details

#apply_journal_log(not_delete = false) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/higgs/tman.rb', line 124

def apply_journal_log(not_delete=false)
  @lock_manager.exclusive{
    if (@read_only != :standby) then
      raise "not standby mode: #{@read_only}"
    end
    name = File.join(@jlog_apply_dir, File.basename(@storage.name))
    for jlog_path in Storage.rotated_entries("#{name}.jlog")
      @storage.apply_journal_log(jlog_path) {|key|
        @master_cache.delete(key)
      }
      File.unlink(jlog_path) unless not_delete
    end
  }
  nil
end

#switch_to_writeObject



140
141
142
143
144
145
146
147
148
149
# File 'lib/higgs/tman.rb', line 140

def switch_to_write
  @lock_manager.exclusive{
    if (@read_only != :standby) then
      raise "not standby mode: #{@read_only}"
    end
    @read_only = false
    @storage.switch_to_write
  }
  nil
end

#transaction(read_only = @read_only) ⇒ Object

tx of block argument is transaction context and see Higgs::TransactionContext for detail.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/higgs/tman.rb', line 105

def transaction(read_only=@read_only)
  r = nil
  @lock_manager.transaction(read_only) {|lock_handler|
    @mvcc_cache.transaction(@cnum_func) {|snapshot|
      if (read_only) then
        tx = ReadOnlyTransactionContext.new(lock_handler, @storage, snapshot, @master_cache, @secondary_cache, @decode, @encode)
      else
        if (@read_only) then
          raise NotWritableError, 'not writable'
        end
        tx = ReadWriteTransactionContext.new(lock_handler, @storage, snapshot, @master_cache, @secondary_cache, @decode, @encode)
      end
      r = yield(tx)
      tx.commit(false) unless read_only
    }
  }
  r
end