lib/picky/backends/file/json.rb in picky-3.1.0 vs lib/picky/backends/file/json.rb in picky-3.1.1

- old
+ new

@@ -1,40 +1,81 @@ module Picky module Backends - module File + class File - # Index files dumped in the JSON format. + # File-based index files dumped in the JSON format. # class JSON < Basic - # Uses the extension "json". + # The in-memory mapping hash, mapping + # a Symbol key to [length, offset] of + # the JSON data in the file. # - def extension - :json + attr_accessor :mapping + + # See lib/picky/backends/file.rb for what this should return. + # + # 1. Gets the length and offset for the key. + # 2. Extracts and decodes the object from the file. + # + def [] key + length, offset = mapping[key] + return unless length + result = Yajl::Parser.parse IO.read(cache_path, length, offset) + result end - # Loads the index hash from json format. + + # Clears the currently loaded index. # - def load - Yajl::Parser.parse ::File.open(cache_path, 'r'), symbolize_keys: true + # Note: This only clears the in-memory mapping, + # but this is enough for the index to not exist + # anymore, at least to the application. + # + def clear + self.mapping.clear + end - # Note: Circumvents the yajl symbolize utf-8 characters problem. - # - # Yajl::Parser.parse(::File.open(cache_path, 'r')).inject({}) do |hash, (k, v)| - # hash[k.to_sym] = v - # hash - # end + # Loads the mapping hash from json format. + # + def load + self.mapping = mapping_file.load + self end + # Dumps the index hash in json format. # + # 1. Dump actual data. + # 2. Dumps mapping key => [length, offset]. + # def dump hash - hash.dump_json cache_path + offset = 0 + mapping = {} + + ::File.open(cache_path, 'w:utf-8') do |out_file| + hash.each do |(key, object)| + encoded = Yajl::Encoder.encode object + length = encoded.size + mapping[key] = [length, offset] + offset += length + out_file.write encoded + end + end + + mapping_file.dump mapping end + # A json file does not provide retrieve functionality. # def retrieve raise "Can't retrieve from JSON file. Use text file." + end + + # Uses the extension "json". + # + def extension + :json end end end \ No newline at end of file