Class: Origen::Database::KeyValueStore

Inherits:
Object
  • Object
show all
Defined in:
lib/origen/database/key_value_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, name) ⇒ KeyValueStore

Returns a new instance of KeyValueStore.



10
11
12
13
14
# File 'lib/origen/database/key_value_store.rb', line 10

def initialize(database, name)
  @name = name
  @database = database
  @private = false
end

Instance Attribute Details

#databaseObject (readonly)

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



7
8
9
# File 'lib/origen/database/key_value_store.rb', line 7

def database
  @database
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/origen/database/key_value_store.rb', line 4

def name
  @name
end

#privateObject

Returns the value of attribute private.



8
9
10
# File 'lib/origen/database/key_value_store.rb', line 8

def private
  @private
end

Instance Method Details

#[](key) ⇒ Object

Read a value from the store



17
18
19
20
# File 'lib/origen/database/key_value_store.rb', line 17

def [](key)
  refresh if stale?
  store[key]
end

#[]=(key, val) ⇒ Object

Persist a new value to the store



23
24
25
26
27
28
# 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) ⇒ Object

Deletes a key from the active store



76
77
78
79
80
# 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) ⇒ Boolean

Check if the store has a key

Returns:

  • (Boolean)


64
65
66
# File 'lib/origen/database/key_value_store.rb', line 64

def has_key?(key)
  store.include? key
end

#keysObject

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



83
84
85
# File 'lib/origen/database/key_value_store.rb', line 83

def keys
  store.keys - [:refresh_interval_in_minutes]
end

#persisted?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/origen/database/key_value_store.rb', line 55

def persisted?
  database.persisted?
end

#private?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/origen/database/key_value_store.rb', line 59

def private?
  @private
end

#record_refreshObject



38
39
40
41
# File 'lib/origen/database/key_value_store.rb', line 38

def record_refresh
  database.record_refresh(name)
  @store = nil
end

#refreshObject

Force a refresh of the database



31
32
33
34
35
36
# 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_fileObject

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.



71
72
73
# File 'lib/origen/database/key_value_store.rb', line 71

def rm_session_file
  FileUtils.rm_f(file)
end

#stale?Boolean

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

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
# 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