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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
module Groonga
module Sharding
class LogicalEnumerator
include Enumerable
attr_reader :target_range
attr_reader :logical_table
attr_reader :shard_key_name
def initialize(command_name, input, options={})
@command_name = command_name
@input = input
@options = options
@shards = []
initialize_parameters
end
def each(&block)
each_internal(:ascending, &block)
end
def reverse_each(&block)
each_internal(:descending, &block)
end
def unref
@shards.each(&:unref)
@shards.clear
end
private
def each_internal(order)
return enum_for(__method__, order) unless block_given?
context = Context.instance
each_shard_with_around(order) do |prev_shard, current_shard, next_shard|
shard_range_data = current_shard.range_data
shard_range = nil
if shard_range_data.day.nil?
if order == :ascending
if next_shard
next_shard_range_data = next_shard.range_data
else
next_shard_range_data = nil
end
else
if prev_shard
next_shard_range_data = prev_shard.range_data
else
next_shard_range_data = nil
end
end
max_day = compute_month_shard_max_day(shard_range_data.year,
shard_range_data.month,
next_shard_range_data)
shard_range = MonthShardRange.new(shard_range_data.year,
shard_range_data.month,
max_day)
else
shard_range = DayShardRange.new(shard_range_data.year,
shard_range_data.month,
shard_range_data.day)
end
yield(current_shard, shard_range)
end
end
def each_shard_with_around(order)
context = Context.instance
prefix = "#{@logical_table}_"
unref_immediately = @options.fetch(:unref_immediately, false)
shards = [nil]
begin
context.database.each_name(:prefix => prefix,
:order_by => :key,
:order => order) do |name|
shard_range_raw = name[prefix.size..-1]
case shard_range_raw
when /\A(\d{4})(\d{2})\z/
shard_range_data = ShardRangeData.new($1.to_i, $2.to_i, nil)
when /\A(\d{4})(\d{2})(\d{2})\z/
shard_range_data = ShardRangeData.new($1.to_i, $2.to_i, $3.to_i)
else
next
end
shard = Shard.new(name, @shard_key_name, shard_range_data)
previous_shard = shards.last
if previous_shard
shard.previous_shard = previous_shard
previous_shard.next_shard = shard
end
shards << shard
@shards << shard
next if shards.size < 3
yield(*shards)
shifted_shard = shards.shift
next unless unref_immediately
next if shifted_shard.nil?
shifted_shard.unref
@shards.shift
end
if shards.size == 2
yield(shards[0], shards[1], nil)
end
ensure
unref if unref_immediately
end
end
def initialize_parameters
@logical_table = @input[:logical_table]
if @logical_table.nil?
raise InvalidArgument, "[#{@command_name}] logical_table is missing"
end
@shard_key_name = @input[:shard_key]
if @shard_key_name.nil?
require_shard_key = @options[:require_shard_key]
require_shard_key = true if require_shard_key.nil?
if require_shard_key
raise InvalidArgument, "[#{@command_name}] shard_key is missing"
end
end
@target_range = TargetRange.new(@command_name, @input)
end
def compute_month_shard_max_day(year, month, next_shard_range)
return nil if next_shard_range.nil?
return nil if month != next_shard_range.month
next_shard_range.day
end
class Shard
attr_reader :table_name, :key_name, :range_data
attr_accessor :previous_shard
attr_accessor :next_shard
def initialize(table_name, key_name, range_data)
@table_name = table_name
@key_name = key_name
@range_data = range_data
@table = nil
@key = nil
@previous_shard = nil
@next_shard = nil
end
def table
@table ||= Context.instance[@table_name]
end
def full_key_name
"#{@table_name}.#{@key_name}"
end
def key
@key ||= Context.instance[full_key_name]
end
def first?
@previous_shard.nil?
end
def last?
@next_shard.nil?
end
def unref
@table.unref if @table
@key.unref if @key
end
end
class ShardRangeData
attr_reader :year, :month, :day
def initialize(year, month, day)
@year = year
@month = month
@day = day
end
def to_suffix
if @day.nil?
"_%04d%02d" % [@year, @month]
else
"_%04d%02d%02d" % [@year, @month, @day]
end
end
end
class DayShardRange
attr_reader :year, :month, :day
def initialize(year, month, day)
@year = year
@month = month
@day = day
end
def least_over_time
next_day = Time.local(@year, @month, @day) + (60 * 60 * 24)
while next_day.day == @day # For leap second
next_day += 1
end
next_day
end
def min_time
Time.local(@year, @month, @day)
end
def include?(time)
@year == time.year and
@month == time.month and
@day == time.day
end
end
class MonthShardRange
attr_reader :year, :month, :max_day
def initialize(year, month, max_day)
@year = year
@month = month
@max_day = max_day
end
def least_over_time
if @max_day.nil?
if @month == 12
Time.local(@year + 1, 1, 1)
else
Time.local(@year, @month + 1, 1)
end
else
Time.local(@year, @month, @max_day)
end
end
def min_time
Time.local(@year, @month, 1)
end
def include?(time)
return false unless @year == time.year
return false unless @month == time.month
if @max_day.nil?
true
else
time.day <= @max_day
end
end
end
class TargetRange
attr_reader :min, :min_border
attr_reader :max, :max_border
def initialize(command_name, input)
@command_name = command_name
@input = input
@min = parse_value(:min)
@min_border = parse_border(:min_border)
@max = parse_value(:max)
@max_border = parse_border(:max_border)
end
def cover_type(shard_range)
return :all if @min.nil? and @max.nil?
if @min and @max
return :none unless in_min?(shard_range)
return :none unless in_max?(shard_range)
min_partial_p = in_min_partial?(shard_range)
max_partial_p = in_max_partial?(shard_range)
if min_partial_p and max_partial_p
:partial_min_and_max
elsif min_partial_p
:partial_min
elsif max_partial_p
:partial_max
else
:all
end
elsif @min
return :none unless in_min?(shard_range)
if in_min_partial?(shard_range)
:partial_min
else
:all
end
else
return :none unless in_max?(shard_range)
if in_max_partial?(shard_range)
:partial_max
else
:all
end
end
end
private
def parse_value(name)
value = @input[name]
return nil if value.nil?
Converter.convert(value, Time)
end
def parse_border(name)
border = @input[name]
return :include if border.nil?
case border
when "include"
:include
when "exclude"
:exclude
else
message =
"[#{@command_name}] #{name} must be \"include\" or \"exclude\": " +
"<#{border}>"
raise InvalidArgument, message
end
end
def in_min?(shard_range)
@min < shard_range.least_over_time
end
def in_min_partial?(shard_range)
return false unless shard_range.include?(@min)
return true if @min_border == :exclude
shard_range.min_time != @min
end
def in_max?(shard_range)
max_base_time = shard_range.min_time
if @max_border == :include
@max >= max_base_time
else
@max > max_base_time
end
end
def in_max_partial?(shard_range)
shard_range.include?(@max)
end
end
end
end
end
|