class Origen::Database::KeyValueStore

Attributes

database[R]

Returns the parent database (the application’s collection of key-value stores)

name[R]
private[RW]

Public Class Methods

new(database, name) click to toggle source
# File lib/origen/database/key_value_store.rb, line 10
def initialize(database, name)
  @name = name
  @database = database
  @private = false
end

Public Instance Methods

[](key) click to toggle source

Read a value from the store

# File lib/origen/database/key_value_store.rb, line 17
def [](key)
  refresh if stale?
  store[key]
end
[]=(key, val) click to toggle source

Persist a new value to the store

# File lib/origen/database/key_value_store.rb, line 23
def []=(key, val)
  refresh if persisted?
  store[key] = val
  save_to_file
  val               # rubocop:disable Lint/Void
end
delete_key(key) click to toggle source

Deletes a key from the active store

# File lib/origen/database/key_value_store.rb, line 76
def delete_key(key)
  store.delete(key)
  save_to_file
  load_from_file
end
has_key?(key) click to toggle source

Check if the store has a key

# File lib/origen/database/key_value_store.rb, line 64
def has_key?(key)
  store.include? key
end
keys() click to toggle source

Return an array of store keys, excluding the ‘infrastructure’ key(s)

# File lib/origen/database/key_value_store.rb, line 83
def keys
  store.keys - [:refresh_interval_in_minutes]
end
persisted?() click to toggle source
# File lib/origen/database/key_value_store.rb, line 55
def persisted?
  database.persisted?
end
private?() click to toggle source
# File lib/origen/database/key_value_store.rb, line 59
def private?
  @private
end
record_refresh() click to toggle source
# File lib/origen/database/key_value_store.rb, line 38
def record_refresh
  database.record_refresh(name)
  @store = nil
end
refresh() click to toggle source

Force a refresh of the database

# File lib/origen/database/key_value_store.rb, line 31
def refresh
  unless @uncommitted || !persisted?
    dssc.check_out(file, version: 'Trunk', force: true)
    record_refresh
  end
end
rm_session_file() click to toggle source

Remove the session file in the case it gets corrupted This can happen when a complex object is not handled correctly by the Marshal method.

# File lib/origen/database/key_value_store.rb, line 71
def rm_session_file
  FileUtils.rm_f(file)
end
stale?() click to toggle source

Returns true if the database is due a time-based refresh, note that this has no bearing on whether or not someone else has committed to the store since the last refresh

# File lib/origen/database/key_value_store.rb, line 46
def stale?
  if persisted?
    t = database.time_since_refresh(name)
    !t || store[:refresh_interval_in_minutes] == 0 || t > store[:refresh_interval_in_minutes]
  else
    false
  end
end

Private Instance Methods

dssc() click to toggle source
# File lib/origen/database/key_value_store.rb, line 89
def dssc
  @dssc ||= Origen::Utility::DesignSync.new
end
file() click to toggle source
# File lib/origen/database/key_value_store.rb, line 134
def file
  file_path = database.app == Origen ? Origen.home : database.app.root
  if persisted?
    @file ||= Pathname.new("#{file_path}/.db/#{name.to_s.symbolize}")
  else
    @file ||= Pathname.new("#{file_path}/.session/#{name.to_s.symbolize}")
  end
end
load_from_file() click to toggle source
# File lib/origen/database/key_value_store.rb, line 105
def load_from_file
  s = nil
  File.open(file.to_s) do |f|
    s = Marshal.load(f)
  end
  s
end
save_to_file() click to toggle source
# File lib/origen/database/key_value_store.rb, line 113
def save_to_file
  unless file.dirname.exist?
    FileUtils.mkdir_p(file.dirname.to_s)
  end
  if @uncommitted
    database.record_new_store(name)
    @uncommitted = false
  end
  File.open(file.to_s, 'w') do |f|
    Marshal.dump(store, f)
  end
  if private?
    FileUtils.chmod(0o600, file)
  else
    FileUtils.chmod(0o664, file)
  end
  if persisted?
    dssc.check_in file, new: true, keep: true, branch: 'Trunk'
  end
end
store() click to toggle source
# File lib/origen/database/key_value_store.rb, line 93
def store
  @store ||= if file.exist?
               load_from_file
             elsif persisted? && dssc.managed_by_design_sync?(file)
               refresh
               load_from_file
             else
               @uncommitted = true
               { refresh_interval_in_minutes: 60 }
             end
end