Class: Kasefet::IndexedFlatKV
- Inherits:
-
Object
- Object
- Kasefet::IndexedFlatKV
- Defined in:
- lib/kasefet/indexed_flat_kv.rb
Instance Attribute Summary collapse
-
#index ⇒ Object
Returns the value of attribute index.
-
#index_file ⇒ Object
Returns the value of attribute index_file.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #conflicted? ⇒ Boolean
- #each_keys ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(flat_kv:, index_dir: "index", index_ext: "") ⇒ IndexedFlatKV
constructor
A new instance of IndexedFlatKV.
- #load_index ⇒ Object
- #mark_key(key, digest) ⇒ Object
- #rebuild_index ⇒ Object
- #save_index ⇒ Object
Constructor Details
#initialize(flat_kv:, index_dir: "index", index_ext: "") ⇒ IndexedFlatKV
Returns a new instance of IndexedFlatKV.
8 9 10 11 12 13 14 |
# File 'lib/kasefet/indexed_flat_kv.rb', line 8 def initialize(flat_kv:, index_dir: "index", index_ext: "") @flat_kv = flat_kv @root = flat_kv.root @index_dir = @root + "#{index_dir}" @index_file = @index_dir + "index#{index_ext}" @index = Kasefet::Config.new(@index_file) end |
Instance Attribute Details
#index ⇒ Object
Returns the value of attribute index.
16 17 18 |
# File 'lib/kasefet/indexed_flat_kv.rb', line 16 def index @index end |
#index_file ⇒ Object
Returns the value of attribute index_file.
16 17 18 |
# File 'lib/kasefet/indexed_flat_kv.rb', line 16 def index_file @index_file end |
Instance Method Details
#[](key) ⇒ Object
18 19 20 |
# File 'lib/kasefet/indexed_flat_kv.rb', line 18 def [](key) return @flat_kv[key] end |
#[]=(key, value) ⇒ Object
22 23 24 25 |
# File 'lib/kasefet/indexed_flat_kv.rb', line 22 def []=(key, value) mark_key(key, @flat_kv.key_to_digest(key)) return @flat_kv[key] = value end |
#conflicted? ⇒ Boolean
32 33 34 |
# File 'lib/kasefet/indexed_flat_kv.rb', line 32 def conflicted? Dir[@index_dir + "*"].size != 1 end |
#each_keys ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/kasefet/indexed_flat_kv.rb', line 67 def each_keys return enum_for(__method__) unless block_given? @index.each_keys do |key| next unless key.start_with?("key:") yield key[4..-1] end end |
#has_key?(key) ⇒ Boolean
63 64 65 |
# File 'lib/kasefet/indexed_flat_kv.rb', line 63 def has_key?(key) return !! @index["key:" + key] end |
#load_index ⇒ Object
27 28 29 30 |
# File 'lib/kasefet/indexed_flat_kv.rb', line 27 def load_index index_contents = @flat_kv.read_file(@index_file) @index.parse(index_contents) end |
#mark_key(key, digest) ⇒ Object
52 53 54 55 |
# File 'lib/kasefet/indexed_flat_kv.rb', line 52 def mark_key(key, digest) @index["dig:" + digest] = key @index["key:" + key] = digest end |
#rebuild_index ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/kasefet/indexed_flat_kv.rb', line 36 def rebuild_index Dir.foreach(@root) do |prefix| next unless prefix =~ /\h\h/ Dir.foreach(@root + prefix) do |key_dir| next unless key_dir =~ /\h{62}/ # SHA256 digest is 64 bytes, first two are the prefix Dir.foreach(@root + prefix + key_dir) do |value_file| next if [".", ".."].include?(value_file) value_path = @root + prefix + key_dir + value_file contents = @flat_kv.read_file(value_path) key_name, value = @flat_kv.read_value(contents) mark_key(key_name, prefix + key_dir) end end end end |
#save_index ⇒ Object
57 58 59 60 61 |
# File 'lib/kasefet/indexed_flat_kv.rb', line 57 def save_index index_contents = @index.format FileUtils.mkdir_p(@index_dir) @flat_kv.write_file(@index.file, index_contents) end |