class Origen::Database::KeyValueStore
Attributes
Returns the parent database (the application’s collection of key-value stores)
Public Class Methods
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
Source
# File lib/origen/database/key_value_store.rb, line 17 def [](key) refresh if stale? store[key] end
Read a value from the store
Source
# 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
Persist a new value to the store
Source
# File lib/origen/database/key_value_store.rb, line 76 def delete_key(key) store.delete(key) save_to_file load_from_file end
Deletes a key from the active store
Source
# File lib/origen/database/key_value_store.rb, line 64 def has_key?(key) store.include? key end
Check if the store has a key
Source
# File lib/origen/database/key_value_store.rb, line 83 def keys store.keys - [:refresh_interval_in_minutes] end
Return an array of store keys, excluding the ‘infrastructure’ key(s)
Source
# File lib/origen/database/key_value_store.rb, line 55 def persisted? database.persisted? end
Source
# File lib/origen/database/key_value_store.rb, line 38 def record_refresh database.record_refresh(name) @store = nil end
Source
# 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
Force a refresh of the database
Source
# File lib/origen/database/key_value_store.rb, line 71 def rm_session_file FileUtils.rm_f(file) end
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.
Source
# 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
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
Private Instance Methods
Source
# File lib/origen/database/key_value_store.rb, line 89 def dssc @dssc ||= Origen::Utility::DesignSync.new end
Source
# File lib/origen/database/key_value_store.rb, line 139 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
Source
# File lib/origen/database/key_value_store.rb, line 105 def load_from_file s = nil begin File.open(file.to_s) do |f| s = Marshal.load(f) end rescue Origen.log.error "Failed to load session file: #{file}" Origen.log.error 'This can happen if the file has been corrupted, please delete it and try again' end s end
Source
# File lib/origen/database/key_value_store.rb, line 118 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
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