Class: DpStmMap::AtomicView

Inherits:
Object
  • Object
show all
Defined in:
lib/dp_stm_map/InMemoryStmMap.rb

Instance Method Summary collapse

Constructor Details

#initialize(global_state) ⇒ AtomicView

Returns a new instance of AtomicView.



21
22
23
24
25
# File 'lib/dp_stm_map/InMemoryStmMap.rb', line 21

def initialize global_state
  @global_state=global_state
  @local_state={}
  @to_delete=Set.new
end

Instance Method Details

#[](key) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/dp_stm_map/InMemoryStmMap.rb', line 35

def [] key
  if @local_state.has_key? key
    @local_state[key]
  else
    @local_state[key]=@global_state[key]
  end
end

#[]=(key, value) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/dp_stm_map/InMemoryStmMap.rb', line 27

def []= key, value
  if value == nil
    delete key
  else
    @local_state[key]=value
  end
end

#changesObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/dp_stm_map/InMemoryStmMap.rb', line 59

def changes
  changes={}
  @local_state.each do |k,v|
    changes[k]=[@global_state[k],v]
  end
  @to_delete.each do |k|
    changes[k]=[@global_state[k],nil]
  end
  changes
end

#commitObject



70
71
72
73
74
75
# File 'lib/dp_stm_map/InMemoryStmMap.rb', line 70

def commit
  @to_delete.each do |key|
    @global_state.delete key
  end
  @global_state.merge!(@local_state)
end

#delete(key) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/dp_stm_map/InMemoryStmMap.rb', line 51

def delete key
  if @local_state.has_key? key
    @local_state.delete key
  else
    @to_delete << key
  end
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
# File 'lib/dp_stm_map/InMemoryStmMap.rb', line 43

def has_key? key
  if @local_state.has_key?(key)
    @local_state[key] != @global_state[key]
  else        
    (not @to_delete.include?(key)) && @global_state.has_key?(key)
  end
end