Method: Reedb::DataFile#insertv2
- Defined in:
- lib/reedb/datafile.rb
#insertv2(data, mode = :hard) ⇒ Object
New and improved data insetion function! Data needs to have a certain format { ‘header’: { … }, ‘body’ { … } }
Malformed data will be ignored and reported in the vault logging.
Writing into body is farely straight forward. A value needs a field and is assigned a version automatically depending on the time in the cache cycle of the file
Writing to header is much more interesting. There is a header_set defined in the parent vault that specifies what fields are valid and what types they allow. For example, by default, there is a ‘urls’ field that is a list of urls.
WRITING THE SAME VALUE INTO A FIELD AS EXISTS BEFORE WILL RESET THAT VALUE! That’s how you can delete a field with this same function in the same step as adding new information (I know…hacky. But clever)
For further documentation on this function, please check with the Reedb wiki
Params: data => specifically formatted JSON data set (see docs) mode => overwrite default file/ vault caching mode.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/reedb/datafile.rb', line 85 def insertv2(data, mode = :hard) # Add option for :fast sync mode here at some point sync if mode == :hard # First split up the data and check what's actually there to_head = if data['header'] then data['header'] else false end to_body = if data['body'] then data['body'] else false end # Returns an error code for malformed data return FILE_MALFORMED_DATA_ERROR unless to_body || to_head # Gets the time difference between edits. # If it's not big enough (5 seconds) will not increment version number current_time = DateTime.now.strftime('%Q').to_i old_time = @version..to_i version_needs_changing = (current_time - old_time >= FILE_CACHE_TIME) && to_body # Actually updates the version if neccessary @version.update if version_needs_changing # Now make sure the version branch actually exists @dataset['body']["#{@version}"] = {} unless @dataset['body']["#{@version}"] # Update the latest version pointer in the dataset @dataset['header']['latest'] = "#{@version}" if version_needs_changing if to_head to_head.each do |key, value| @dataset['header']['name'] if key == 'name' # This means that the value can be stored if @vault.header_set.include?(key) # If inserting into a header single field if @vault.header_set[key] == 'single' if @dataset['header'][key] @dataset['header'][key] = nil else @dataset['header'][key] = value end # If inserting into a header list elsif @vault.header_set[key] == 'list' if value.instance_of? String if @dataset['header'][key].include?(value) @dataset['header'][key].delete(value) else @dataset['header'][key] << value end elsif value.instance_of? Array value.each do |sub_value| if @dataset['header'][key].include?(sub_value) @dataset['header'][key].delete(sub_value) else @dataset['header'][key] << sub_value end end end # If inserting into a header tree elsif @vault.header_set[key] == 'tree' if @dataset['header'][key].include?(value) @dataset['header'][key].delete(value) else @dataset['header'][key][value[0]] = value[1] end end else # Report the incident VaultLogger.write("Tried to write invalid data to #{@name}", 'warn') end end end # BOOORING! :C if to_body to_body.each do |field, value| @dataset['body']["#{@version}"]["#{field}"] = value end end return self end |