Class: Droonga::Catalog::Version1

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

Instance Attribute Summary

Attributes inherited from Base

#base_path, #path

Instance Method Summary collapse

Methods inherited from Base

#dataset, #have_dataset?

Constructor Details

#initialize(data, path) ⇒ Version1

Returns a new instance of Version1.

Raises:



22
23
24
25
26
27
28
29
30
# File 'lib/droonga/catalog/version1.rb', line 22

def initialize(data, path)
  super
  @errors = []

  validate
  raise MultiplexError.new(@errors) unless @errors.empty?

  prepare_data
end

Instance Method Details

#datasetsObject



32
33
34
# File 'lib/droonga/catalog/version1.rb', line 32

def datasets
  @datasets
end

#get_partition(dataset, key) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/droonga/catalog/version1.rb', line 85

def get_partition(dataset, key)
  continuum = dataset["continuum"]
  return dataset["ring"].keys[0] unless continuum
  hash = Zlib.crc32(key)
  min = 0
  max = continuum.size - 1
  while (min < max) do
    index = (min + max) / 2
    value, key = continuum[index]
    return key if value == hash
    if value > hash
      max = index
    else
      min = index + 1
    end
  end
  return continuum[max][1]
end

#get_partitions(name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/droonga/catalog/version1.rb', line 40

def get_partitions(name)
  device = @data["farms"][name]["device"]
  pattern = Regexp.new("^#{name}\.")
  results = {}
  @data["datasets"].each do |dataset_name, dataset_data|
    dataset = Dataset.new(dataset_name, dataset_data)
    workers = dataset["workers"]
    plugins = dataset["plugins"]
    dataset["ring"].each do |key, part|
      part["partitions"].each do |range, partitions|
        partitions.each do |partition|
          if partition =~ pattern
            path = File.join([device, $POSTMATCH, "db"])
            path = File.expand_path(path, base_path)
            options = {
              :dataset => dataset_name,
              :database => path,
              :n_workers => workers,
              :plugins => plugins
            }
            results[partition] = options
          end
        end
      end
    end
  end
  return results
end

#get_routes(name, args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/droonga/catalog/version1.rb', line 69

def get_routes(name, args)
  routes = []
  dataset = dataset(name)
  case args["type"]
  when "broadcast"
    dataset["ring"].each do |key, partition|
      select_range_and_replicas(partition, args, routes)
    end
  when "scatter"
    name = get_partition(dataset, args["record"]["_key"])
    partition = dataset["ring"][name]
    select_range_and_replicas(partition, args, routes)
  end
  return routes
end

#select_range_and_replicas(partition, args, routes) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/droonga/catalog/version1.rb', line 104

def select_range_and_replicas(partition, args, routes)
  date_range = args["date_range"] || 0..-1
  partition["partitions"].sort[date_range].each do |time, replicas|
    case args["replica"]
    when "top"
      routes << replicas[0]
    when "random"
      routes << replicas[rand(replicas.size)]
    when "all"
      routes.concat(replicas)
    end
  end
end

#slices(name) ⇒ Object



36
37
38
# File 'lib/droonga/catalog/version1.rb', line 36

def slices(name)
  get_partitions(name)
end