From 9c7ddc1c61ae4f0114f66ca7c9a7e37b6b1b7dd6 Mon Sep 17 00:00:00 2001 From: Archivist Date: Sun, 12 Aug 2018 16:28:24 +0200 Subject: [PATCH] Refactoring the code, adding more tests --- spec/crystal-scatter_spec.cr | 2 + src/crystal-scatter/metaring.cr | 69 ++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/spec/crystal-scatter_spec.cr b/spec/crystal-scatter_spec.cr index 15ebe56..57b869e 100644 --- a/spec/crystal-scatter_spec.cr +++ b/spec/crystal-scatter_spec.cr @@ -20,10 +20,12 @@ describe Crystal::Scatter do (1..1000).each do |numb| slices = mr.get_slices_for(numb.to_s) + slices_history = mr.get_slices_for(numb.to_s,2) set = Set(String).new slices.each do |sl| set<=slice.s_begin && slice.s_end>=h end return value.not_nil! end + + private def unfold_ring(ring : Ring, targets : Array(UInt64)) + slices = Array(Slice).new + targets.each do |h| + slices << get_slice_from_hash(ring, h) + end + return slices + end def add(element : Daemon) @lock.synchronize do @@ -78,44 +110,25 @@ module Crystal::Scatter ring? = @rings.last end ring = ring?.not_nil! - h = hash_impl data - space=UInt64::MAX/@shards - targets = Array(UInt64).new - (1..@shards).each do - targets << h - h+=space - end - slices = Array(Slice).new - targets.each do |h| - slices << get_slice_from_hash(ring, h) - end + targets = hash_and_split(data) + slices = unfold_ring(ring,targets) return slices end - def get_slices_for(data : String, n_last = 1 : Int32) + def get_slices_for(data : String, n_last : UInt32) n_rings = Array(Ring).new + sllast = 0 @lock.synchronize do + sllast = @rings.size - 1 t = @rings.size-n_last - n_rings = @rings.skip( t > 0 ? t : 1 ) + n_rings = @rings.skip( t >= 0 ? t : 1 ) end - h = hash_impl data - space=UInt64::MAX/@shards - targets = Array(UInt64).new - (1..@shards).each do - targets << h - h+=space - end - + targets = hash_and_split(data) ret = Array(Array(Slice)).new n_rings.each do |ring| - slices = Array(Slice).new - targets.each do |h| - slices << get_slice_from_hash(ring, h) - end - ret << slices + ret << unfold_ring(ring,targets) end - return ret + return SliceInfo.new ret, sllast, @shards end end - end \ No newline at end of file