Class: JsonStore
- Inherits:
-
Object
- Object
- JsonStore
- Defined in:
- lib/json_store.rb
Overview
JsonStore is a simple key/value database in which key/values are stored in json format The basic concept is to pull data into an in memory map and then set or get data and push the data to file. You can also merge in 2 different ways to decide how you want to use the data coming from file or the map file locking is handled by the lock_method gem to prevent concurrent access when reading and writing
Instance Method Summary collapse
-
#all ⇒ Object
Returns the whole memory map.
-
#all_as_json ⇒ Object
Returns the whole memory map as a json string.
-
#clear ⇒ Object
�Clears the memory map.
-
#db_path ⇒ Object
Get the path to the datastore json file.
-
#get(key) ⇒ Object
Gets data from the memory map by key e.g.
-
#get_as_json(key) ⇒ Object
Gets data from the memory map by key but as a json string.
-
#get_json_opts ⇒ Object
Lists the Oj json parser options that have been set.
-
#initialize(db) ⇒ JsonStore
constructor
Creates a new store at the path/filename provided to the constructor e.g.
-
#merge(direction = :into_remote) ⇒ Object
Merges the data in 2 ways 1.
-
#pull ⇒ Object
Pulls data from file into the memory map - replacing the memory map with the data from file.
-
#push ⇒ Object
Pushes the data in the memory map to file.
-
#remove(key) ⇒ Object
Removes the data from memory map by key e.g.
-
#search(selector, kind = :matches) ⇒ Object
Uses the ruby gem for JSONSelect to search basic json.
-
#set(key, value) ⇒ Object
Sets data into the memory map by key/value e.g db.set(‘name’,‘Kingsley’).
-
#set_json(json) ⇒ Object
Replaces the memory map with some json directly.
-
#set_json_opts(options) ⇒ Object
Sets the Oj json parser options by overriding defaults.
-
#write ⇒ Object
Writes the in memory data to file by replacing file.
Constructor Details
#initialize(db) ⇒ JsonStore
Creates a new store at the path/filename provided to the constructor e.g. db = JsonStore.new(‘path/to/some/file.json’)
14 15 16 17 18 |
# File 'lib/json_store.rb', line 14 def initialize(db) @db = db @map = {} @json_opts = Oj. end |
Instance Method Details
#all ⇒ Object
Returns the whole memory map
44 45 46 |
# File 'lib/json_store.rb', line 44 def all @map end |
#all_as_json ⇒ Object
Returns the whole memory map as a json string
69 70 71 |
# File 'lib/json_store.rb', line 69 def all_as_json Oj.dump(@map,@json_opts) end |
#clear ⇒ Object
�Clears the memory map
64 65 66 |
# File 'lib/json_store.rb', line 64 def clear @map = {} end |
#db_path ⇒ Object
Get the path to the datastore json file
119 120 121 |
# File 'lib/json_store.rb', line 119 def db_path @db end |
#get(key) ⇒ Object
Gets data from the memory map by key e.g. db.get(‘name’)
28 29 30 |
# File 'lib/json_store.rb', line 28 def get(key) @map[key] end |
#get_as_json(key) ⇒ Object
Gets data from the memory map by key but as a json string
80 81 82 |
# File 'lib/json_store.rb', line 80 def get_as_json(key) Oj.dump(@map[key],@json_opts) end |
#get_json_opts ⇒ Object
Lists the Oj json parser options that have been set
59 60 61 |
# File 'lib/json_store.rb', line 59 def get_json_opts @json_opts end |
#merge(direction = :into_remote) ⇒ Object
Merges the data in 2 ways
-
db.merge(:into_remote) is the default and will merge data from the memory map into data from file and assign the result to the memory map
-
db.merge(:into_local) will merge data from file into the memory map and assign the result to the memory map.
into_remote will essentially load the memory map data into the file data thus overwriting the file data with the memory map data where the keys match into_local will essentially merge the file data into the memory map thus overwriting the memory map data with the file data where the keys match
109 110 111 112 113 114 115 116 |
# File 'lib/json_store.rb', line 109 def merge(direction=:into_remote) begin @map = direction == :into_local ? @map.merge(read_data) : read_data.merge(@map) rescue LockMethod::Locked sleep 0.5 merge(direction) end end |
#pull ⇒ Object
Pulls data from file into the memory map - replacing the memory map with the data from file.
85 86 87 88 89 90 91 92 |
# File 'lib/json_store.rb', line 85 def pull begin @map = read_data rescue LockMethod::Locked sleep 0.5 pull end end |
#push ⇒ Object
Pushes the data in the memory map to file
95 96 97 98 99 100 101 102 |
# File 'lib/json_store.rb', line 95 def push begin write_data rescue LockMethod::Locked sleep 0.5 push end end |
#remove(key) ⇒ Object
Removes the data from memory map by key e.g. db.remove(‘name’)
34 35 36 |
# File 'lib/json_store.rb', line 34 def remove(key) @map.delete(key) end |
#search(selector, kind = :matches) ⇒ Object
Uses the ruby gem for JSONSelect to search basic json. (doesn’t work on ruby objects stored as json by Oj :object mode) See the JSONSelect website / json_select ruby gem docs for more details
75 76 77 |
# File 'lib/json_store.rb', line 75 def search(selector, kind=:matches) JSONSelect(selector).send(kind, @map) end |
#set(key, value) ⇒ Object
Sets data into the memory map by key/value e.g db.set(‘name’,‘Kingsley’)
22 23 24 |
# File 'lib/json_store.rb', line 22 def set(key, value) @map[key] = value end |
#set_json(json) ⇒ Object
Replaces the memory map with some json directly
49 50 51 |
# File 'lib/json_store.rb', line 49 def set_json(json) @map = Oj.load(json) end |
#set_json_opts(options) ⇒ Object
Sets the Oj json parser options by overriding defaults. See Oj josn parser docs for more details.
54 55 56 |
# File 'lib/json_store.rb', line 54 def set_json_opts() @json_opts.merge!() end |
#write ⇒ Object
Writes the in memory data to file by replacing file
39 40 41 |
# File 'lib/json_store.rb', line 39 def write write_data end |