Class: Phronomy::Persistence::InMemory::Contents

Inherits:
ContentStore::Base show all
Defined in:
lib/phronomy/persistence/in_memory.rb

Instance Method Summary collapse

Methods inherited from ContentStore::Base

#fetch_json, #fetch_many, #fetch_text, #put_json, #put_text

Constructor Details

#initialize(owner) ⇒ Contents

Returns a new instance of Contents.



10
11
12
# File 'lib/phronomy/persistence/in_memory.rb', line 10

def initialize(owner)
  @owner = owner
end

Instance Method Details

#exist?(content_id) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/phronomy/persistence/in_memory.rb', line 43

def exist?(content_id)
  @owner.synchronize { @owner.state[:contents].key?(content_id.to_s) }
end

#fetch(content_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/phronomy/persistence/in_memory.rb', line 30

def fetch(content_id)
  @owner.synchronize do
    record = @owner.state[:contents].fetch(content_id.to_s) do
      raise NotFoundError, "content not found: #{content_id}"
    end
    bytes = record[:bytes]
    unless content_id_for(bytes) == content_id.to_s
      raise Phronomy::ContentStore::IntegrityError, "content digest mismatch: #{content_id}"
    end
    bytes.dup
  end
end

#put(bytes, canonicalization_version:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/phronomy/persistence/in_memory.rb', line 14

def put(bytes, canonicalization_version:)
  value = String(bytes).b.freeze
  id = content_id_for(value)
  @owner.synchronize do
    current = @owner.state[:contents][id]
    if current && current[:bytes] != value
      raise Phronomy::ContentStore::IntegrityError, "digest collision for #{id}"
    end
    @owner.state[:contents][id] ||= {
      bytes: value,
      canonicalization_version: Integer(canonicalization_version)
    }
  end
  id
end