Class: Droonga::Catalog::SlicesVolume

Inherits:
Object
  • Object
show all
Defined in:
lib/droonga/catalog/slices_volume.rb

Instance Method Summary collapse

Constructor Details

#initialize(dataset, raw) ⇒ SlicesVolume

Returns a new instance of SlicesVolume.



24
25
26
27
28
# File 'lib/droonga/catalog/slices_volume.rb', line 24

def initialize(dataset, raw)
  @dataset = dataset
  @raw = raw
  compute_continuum if ratio_scaled_slicer?
end

Instance Method Details

#all_nodesObject



78
79
80
# File 'lib/droonga/catalog/slices_volume.rb', line 78

def all_nodes
  @all_nodes ||= collect_all_nodes
end

#choose_slice(record) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/droonga/catalog/slices_volume.rb', line 54

def choose_slice(record)
  return slices.first unless ratio_scaled_slicer?

  key = stringify_key(record[dimension])
  hash = Zlib.crc32(key)
  min = 0
  max = @continuum.size - 1
  while (min < max)
    index = (min + max) / 2
    value, key = @continuum[index]
    return key if value == hash
    if value > hash
      max = index
    else
      min = index + 1
    end
  end
  @continuum[max][1]
end

#compute_routes(message, active_nodes) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/droonga/catalog/slices_volume.rb', line 82

def compute_routes(message, active_nodes)
  routes = []
  slices = []
  case message["type"]
  when "broadcast"
    slices = select_slices(message["slice"].to_sym)
  when "scatter"
    record = message["record"]
    if record
      slices = [choose_slice(record)]
    else
      slices = select_slices(message["slice"].to_sym)
    end
  end
  slices.each do |slice|
    routes.concat(slice.compute_routes(message, active_nodes))
  end
  routes
end

#dimensionObject



30
31
32
# File 'lib/droonga/catalog/slices_volume.rb', line 30

def dimension
  @raw["dimension"] || "_key"
end

#ratio_scaled_slicer?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/droonga/catalog/slices_volume.rb', line 74

def ratio_scaled_slicer?
  slicer == "hash"
end

#select_slices(how = :all, range = 0..-1)) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/droonga/catalog/slices_volume.rb', line 42

def select_slices(how=:all, range=0..-1)
  selected = slices.sort_by(&:label)[range]
  case how
  when :random
    [selected.sample]
  when :all
    selected
  else
    selected
  end
end

#sliced?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/droonga/catalog/slices_volume.rb', line 102

def sliced?
  slices.size > 1
end

#slicerObject



34
35
36
# File 'lib/droonga/catalog/slices_volume.rb', line 34

def slicer
  @raw["slicer"] || "hash"
end

#slicesObject



38
39
40
# File 'lib/droonga/catalog/slices_volume.rb', line 38

def slices
  @slices ||= create_slices
end