Class: Mistri::Workspace::Memory
- Inherits:
-
Object
- Object
- Mistri::Workspace::Memory
- Defined in:
- lib/mistri/workspace/memory.rb
Overview
A mutex-protected ephemeral document map with atomic conditional writes.
Instance Method Summary collapse
- #atomic_writes? ⇒ Boolean
- #compare_and_write(path, content, expected_revision:) ⇒ Object
- #delete(path) ⇒ Object
-
#initialize ⇒ Memory
constructor
A new instance of Memory.
- #list(prefix = nil) ⇒ Object
- #read(path) ⇒ Object
- #snapshot(path) ⇒ Object
- #write(path, content) ⇒ Object
Constructor Details
#initialize ⇒ Memory
Returns a new instance of Memory.
9 10 11 12 |
# File 'lib/mistri/workspace/memory.rb', line 9 def initialize @documents = {} @mutex = Mutex.new end |
Instance Method Details
#atomic_writes? ⇒ Boolean
23 |
# File 'lib/mistri/workspace/memory.rb', line 23 def atomic_writes? = true |
#compare_and_write(path, content, expected_revision:) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mistri/workspace/memory.rb', line 32 def compare_and_write(path, content, expected_revision:) @mutex.synchronize do key = path.to_s current = @documents[key] actual = current && Snapshot.for(current).revision unless actual == expected_revision raise WorkspaceConflictError.new( key, expected_revision:, actual_revision: actual ) end @documents[key] = own(content) Snapshot.for(@documents[key]) end end |
#delete(path) ⇒ Object
48 49 50 51 |
# File 'lib/mistri/workspace/memory.rb', line 48 def delete(path) @mutex.synchronize { @documents.delete(path.to_s) } nil end |
#list(prefix = nil) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/mistri/workspace/memory.rb', line 53 def list(prefix = nil) @mutex.synchronize do keys = @documents.keys.sort prefix ? keys.select { |key| key.start_with?(prefix.to_s) } : keys end end |
#read(path) ⇒ Object
14 15 16 |
# File 'lib/mistri/workspace/memory.rb', line 14 def read(path) @mutex.synchronize { @documents[path.to_s]&.dup } end |
#snapshot(path) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/mistri/workspace/memory.rb', line 25 def snapshot(path) @mutex.synchronize do content = @documents[path.to_s] content && Snapshot.for(content) end end |
#write(path, content) ⇒ Object
18 19 20 21 |
# File 'lib/mistri/workspace/memory.rb', line 18 def write(path, content) @mutex.synchronize { @documents[path.to_s] = own(content) } nil end |