-
Notifications
You must be signed in to change notification settings - Fork 5
/
version2.rb
104 lines (93 loc) · 3.09 KB
/
version2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Copyright (C) 2013-2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
require "pathname"
require "fileutils"
require "droonga/address"
require "droonga/catalog/base"
require "droonga/catalog/dataset"
require "droonga/catalog/version2_validator"
module Droonga
module Catalog
class Version2 < Base
def initialize(data, path)
super
validate
prepare_data
end
def datasets
@datasets
end
def slices(node)
device = "."
results = {}
@datasets.each do |dataset_name, dataset|
n_workers = dataset.n_workers
plugins = dataset.plugins
dataset.replicas.each do |volume|
volume.slices.each do |slice|
volume_address = slice.volume.address
if volume_address.node == node
name = volume_address.name
migrate_database_location(device, name)
path = File.join([device, Path.databases.basename.to_s, name, "db"])
path = Pathname(path).expand_path(base_path)
options = {
:dataset => dataset_name,
:database => path.to_s,
:n_workers => n_workers,
:plugins => plugins
}
results[volume_address.to_s] = options
end
end
end
end
results
end
def all_nodes
@all_nodes ||= collect_all_nodes
end
private
def validate
validator = Version2Validator.new(@data, @path)
validator.validate
end
def prepare_data
@datasets = {}
@data["datasets"].each do |name, dataset|
@datasets[name] = Dataset.new(name, dataset)
end
end
def collect_all_nodes
nodes = []
@datasets.each do |name, dataset|
nodes += dataset.all_nodes
end
nodes.sort.uniq
end
def migrate_database_location(device, name)
common_base_path = Pathname(base_path) + device
old_db_dir_path = common_base_path + name
old_db_path = old_db_dir_path + "db"
current_db_dir_path = common_base_path + Path.databases.basename + name
current_db_path = current_db_dir_path + "db"
if old_db_path.exist? and not current_db_path.exist?
FileUtils.mkdir_p(current_db_dir_path.parent)
FileUtils.move(old_db_dir_path, current_db_dir_path)
end
end
end
end
end