Class: JsonStore

Inherits:
Object
  • Object
show all
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

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.default_options
end

Instance Method Details

#allObject

Returns the whole memory map



44
45
46
# File 'lib/json_store.rb', line 44

def all
  @map
end

#all_as_jsonObject

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

#clearObject

�Clears the memory map



64
65
66
# File 'lib/json_store.rb', line 64

def clear
  @map = {}
end

#db_pathObject

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_optsObject

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

  1. 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

  2. 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

#pullObject

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

#pushObject

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(options)
 @json_opts.merge!(options)
end

#writeObject

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