Class: Dopv::PersistentDisk::DB
- Inherits:
-
Object
- Object
- Dopv::PersistentDisk::DB
- Defined in:
- lib/dopv/persistent_disk.rb
Instance Method Summary collapse
- #<<(entry) ⇒ Object (also: #add)
- #append(entry) ⇒ Object
- #delete(entry) ⇒ Object
-
#initialize(state_store, node_name) ⇒ DB
constructor
A new instance of DB.
- #update(entry, attrs = {}) ⇒ Object
- #volumes ⇒ Object
Constructor Details
#initialize(state_store, node_name) ⇒ DB
Returns a new instance of DB.
56 57 58 59 60 61 62 63 |
# File 'lib/dopv/persistent_disk.rb', line 56 def initialize(state_store, node_name) @state_store = state_store @node_name = node_name @state_store.transaction do @state_store[:data_volumes] ||= {} @state_store[:data_volumes][@node_name] ||= [] end end |
Instance Method Details
#<<(entry) ⇒ Object Also known as: add
86 87 88 |
# File 'lib/dopv/persistent_disk.rb', line 86 def <<(entry) append(entry) end |
#append(entry) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/dopv/persistent_disk.rb', line 69 def append(entry) @state_store.transaction do entries.each do |disk| if disk == entry raise PersistentDiskError, "Disk #{disk.name} already exists for node #{disk.node}" end end if entry.is_a?(Entry) @entries << entry @state_store[:data_volumes][@node_name] << entry.to_hash else @entries << Entry.new(entry) @state_store[:data_volumes][@node_name] << entry end end end |
#delete(entry) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/dopv/persistent_disk.rb', line 103 def delete(entry) @state_store.transaction do index = entries.index {|stored_entry| stored_entry = entry} if index.nil? raise PersistentDiskError, "Entry update: Disk entry not found #{entry.to_s}" end @entries.delete_at(index) @state_store[:data_volumes][@node_name].delete_at(index) entry end end |
#update(entry, attrs = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/dopv/persistent_disk.rb', line 91 def update(entry, attrs={}) @state_store.transaction do index = entries.index {|stored_entry| stored_entry = entry} if index.nil? raise PersistentDiskError, "Entry update: Disk entry not found #{entry.to_s}" end @entries[index].update(attrs) @state_store[:data_volumes][@node_name][index] = @entries[index] @entries[index] end end |
#volumes ⇒ Object
65 66 67 |
# File 'lib/dopv/persistent_disk.rb', line 65 def volumes @state_store.transaction(true) { entries }.dup end |