Class: GameMachine::DataStore
- Inherits:
-
Object
- Object
- GameMachine::DataStore
- Extended by:
- Forwardable
- Includes:
- Singleton
- Defined in:
- lib/game_machine/data_store.rb
Instance Attribute Summary collapse
-
#classmap ⇒ Object
readonly
Returns the value of attribute classmap.
-
#serialization ⇒ Object
readonly
Returns the value of attribute serialization.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
- #class_cache(classname) ⇒ Object
- #delete(key) ⇒ Object
- #delete_matching(scope, query_string) ⇒ Object
-
#get(key, classname = 'Entity') ⇒ Object
for get/set/delete, backend stores return a value or nil, and raise on any underlying error Propogating the error could mess up actor states so we don’t do that.
-
#initialize ⇒ DataStore
constructor
A new instance of DataStore.
- #query(scope, query_string, limit, classname) ⇒ Object
- #set(key, value) ⇒ Object
- #set_store(store_name) ⇒ Object
Constructor Details
#initialize ⇒ DataStore
Returns a new instance of DataStore.
17 18 19 20 21 22 |
# File 'lib/game_machine/data_store.rb', line 17 def initialize @store_name = Application.config.datastore.store @serialization = Application.config.datastore.serialization @classmap = {} connect end |
Instance Attribute Details
#classmap ⇒ Object (readonly)
Returns the value of attribute classmap.
16 17 18 |
# File 'lib/game_machine/data_store.rb', line 16 def classmap @classmap end |
#serialization ⇒ Object (readonly)
Returns the value of attribute serialization.
16 17 18 |
# File 'lib/game_machine/data_store.rb', line 16 def serialization @serialization end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
16 17 18 |
# File 'lib/game_machine/data_store.rb', line 16 def store @store end |
Instance Method Details
#class_cache(classname) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/game_machine/data_store.rb', line 24 def class_cache(classname) return MessageLib::Entity if classname.nil? if cached = classmap.fetch(classname,nil) return cached else classmap[classname] = "GameMachine::MessageLib::#{classname}".constantize classmap[classname] end end |
#delete(key) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/game_machine/data_store.rb', line 114 def delete(key) begin @store.delete(key) rescue Exception => e GameMachine.logger.error(e.+"\n"+e.backtrace.join("\n")) return false end end |
#delete_matching(scope, query_string) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/game_machine/data_store.rb', line 123 def delete_matching(scope,query_string) query_string = scope + "##" + query_string begin @store.delete_matching(query_string) rescue Exception => e GameMachine.logger.error(e.+"\n"+e.backtrace.join("\n")) return false end end |
#get(key, classname = 'Entity') ⇒ Object
for get/set/delete, backend stores return a value or nil, and raise on any underlying error Propogating the error could mess up actor states so we don’t do that.
Callers should treat nil/false as a failure. We log errors so they can be dealt with
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/game_machine/data_store.rb', line 47 def get(key,classname='Entity') value = nil begin value = @store.get(key) rescue Exception => e GameMachine.logger.error(e.+"\n"+e.backtrace.join("\n")) return nil end return nil if value.nil? klass = class_cache(classname) if serialization == 'json' klass.parse_from_json(value) else klass.parse_from(value) end end |
#query(scope, query_string, limit, classname) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/game_machine/data_store.rb', line 66 def query(scope,query_string,limit,classname) query_string = scope + "##" + query_string values = [] begin # Returns an array values = @store.query(query_string,limit) rescue Exception => e GameMachine.logger.error(e.+"\n"+e.backtrace.join("\n")) return values end return values if values.empty? klass = class_cache(classname) if serialization == 'json' = values.map do |r| = klass.parse_from_json(r) if .id.match(/##/) .setId(klass.unscopeId(.id)) end end else = values.map do |r| = klass.parse_from(r.to_byte_array) if .id.match(/##/) .setId(klass.unscopeId(.id)) end end end end |
#set(key, value) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/game_machine/data_store.rb', line 101 def set(key,value) begin if serialization == 'json' @store.set(key,value.to_json) else @store.set(key,value.to_byte_array) end rescue Exception => e GameMachine.logger.error(e.+"\n"+e.backtrace.join("\n")) return false end end |
#set_store(store_name) ⇒ Object
35 36 37 38 39 |
# File 'lib/game_machine/data_store.rb', line 35 def set_store(store_name) @store = nil @store_name = store_name connect end |