Class: Origen::Database::KeyValueStores

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ KeyValueStores

Returns a new instance of KeyValueStores.



7
8
9
10
11
12
13
# File 'lib/origen/database/key_value_stores.rb', line 7

def initialize(app, options = {})
  options = {
    persist: true
  }.merge(options)
  @app = app
  @persist = options[:persist]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Used to create new key value stores on the fly.

On first call of a missing method a method is generated to avoid the missing lookup next time, this should be faster for repeated lookups of the same method, e.g. reg



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/origen/database/key_value_stores.rb', line 68

def method_missing(method, *args, &block)
  if method.to_s =~ /(=|\(|\)|\.|\[|\]|{|}|\\|\/)/ || [:test, :_system].include?(method)
    fail "Invalid database name: #{method}"
  else
    define_singleton_method(method) do
      loaded[method] ||= KeyValueStore.new(self, method)
    end
  end

  send(method, *args, &block)
end

Instance Attribute Details

#appObject (readonly)

Returns the application that owns the database



5
6
7
# File 'lib/origen/database/key_value_stores.rb', line 5

def app
  @app
end

Instance Method Details

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/origen/database/key_value_stores.rb', line 89

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

#inspectObject



15
16
17
18
19
20
21
# File 'lib/origen/database/key_value_stores.rb', line 15

def inspect
  if persisted?
    app == Origen ? "Origen's Global Database" : "< #{app.class}'s Database >"
  else
    app == Origen ? "Origen's Global Session" : "< #{app.class}'s Session >"
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/origen/database/key_value_stores.rb', line 85

def persisted?
  @persist
end

#record_new_store(name) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/origen/database/key_value_stores.rb', line 55

def record_new_store(name)
  unless name == :_system || name == :_database
    _system.refresh
    s = stores
    s << name unless s.include?(name)
    _system[:stores] = s
  end
end

#record_refresh(name) ⇒ Object

Record that the given store was just refreshed



47
48
49
50
51
52
53
# File 'lib/origen/database/key_value_stores.rb', line 47

def record_refresh(name)
  if persisted?
    t = refresh_table
    t[name] = Time.now
    app.session._database[:refresh_table] = t
  end
end

#refreshObject

Refresh all stores



24
25
26
27
28
29
30
31
32
# File 'lib/origen/database/key_value_stores.rb', line 24

def refresh
  if persisted?
    _system.refresh
    files = stores.map { |name| send(name).send(:file) }
    dssc.check_out(files.join(' '), version: 'Trunk', force: true)
    stores.each { |name| send(name).record_refresh }
  end
  nil
end

#storesObject

Returns the names of all known stores



81
82
83
# File 'lib/origen/database/key_value_stores.rb', line 81

def stores
  _system[:stores] || []
end

#time_since_refresh(name) ⇒ Object

Returns the time in minutes since the given store was last refreshed



36
37
38
39
40
41
42
43
44
# File 'lib/origen/database/key_value_stores.rb', line 36

def time_since_refresh(name)
  if persisted?
    if refresh_table[name]
      ((Time.now - refresh_table[name]) / 60).floor
    end
  else
    Time.now
  end
end