Class: MemDB::Index::PrefixTree::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/mem_db/index/prefix_tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket:) ⇒ Item

Returns a new instance of Item.



55
56
57
# File 'lib/mem_db/index/prefix_tree.rb', line 55

def initialize(bucket:)
  @bucket = bucket
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



53
54
55
# File 'lib/mem_db/index/prefix_tree.rb', line 53

def value
  @value
end

Instance Method Details

#add(prefix, i, obj, value) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/mem_db/index/prefix_tree.rb', line 71

def add(prefix, i, obj, value)
  if prefix.length == i
    set_value(obj, value)
  else
    item = fetch_children(prefix[i])
    item.add(prefix, i + 1, obj, value)
  end
end

#fetch_children(idx) ⇒ Object



85
86
87
88
# File 'lib/mem_db/index/prefix_tree.rb', line 85

def fetch_children(idx)
  @children ||= {}
  @children[idx] ||= Item.new(bucket: @bucket)
end

#select_values(content, i, query:, out:) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mem_db/index/prefix_tree.rb', line 59

def select_values(content, i, query:, out:)
  @value&.query(query, out: out)

  return if content.length == i

  return unless @children

  if (item = @children[content[i]])
    item.select_values(content, i + 1, query: query, out: out)
  end
end

#set_value(obj, value) ⇒ Object



80
81
82
83
# File 'lib/mem_db/index/prefix_tree.rb', line 80

def set_value(obj, value)
  @value ||= @bucket.new
  @value.add(obj, value)
end