Class: Haplocheirus::MockService

Inherits:
Object
  • Object
show all
Defined in:
lib/haplocheirus/mock_service.rb

Overview

:nodoc:

Defined Under Namespace

Classes: MockNode

Instance Method Summary collapse

Constructor Details

#initializeMockService

Returns a new instance of MockService.



28
29
30
# File 'lib/haplocheirus/mock_service.rb', line 28

def initialize
  @timelines = {}
end

Instance Method Details

#append(e, p, is) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/haplocheirus/mock_service.rb', line 32

def append(e, p, is)
  is.each do |i|
    key = p + i.to_s
    next unless @timelines.key?(key)
    # NOTE: This check occurs on read, server-side
    @timelines[key].unshift(e) unless @timelines[key].include?(e)
  end
end

#delete_timeline(i) ⇒ Object



131
132
133
# File 'lib/haplocheirus/mock_service.rb', line 131

def delete_timeline(i)
  @timelines.delete(i)
end

#filter(i, e, depth = -1)) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/haplocheirus/mock_service.rb', line 89

def filter(i, e, depth = -1)
  raise Haplocheirus::TimelineStoreException unless @timelines.key?(i)

  haystack = @timelines[i].map do |ea|
    node = MockNode.unpack(ea)
    if node.is_share?
      node.secondary_id
    else
      node.status_id
    end
  end.uniq

  # FIXME: Only send the first 8 bytes for the needles
  e.select do |packed|
    node = MockNode.unpack(packed)
    haystack.include?(node.status_id)
  end
end

#get(i, o, l, d = false) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/haplocheirus/mock_service.rb', line 50

def get(i, o, l, d = false)
  raise Haplocheirus::TimelineStoreException unless @timelines.key?(i)
  t = @timelines[i].to_a[o..(o+l)]
  t.sort! { |a, b| a[0,8].unpack("Q") <=> b[0,8].unpack("Q") }
  t = dedupe(t) if d
  Haplocheirus::TimelineSegment.new(:entries => t.reverse.map{ |tt| tt.dup },
                                    :size => t.length,
                                    :state => Haplocheirus::TimelineSegmentState::HIT)
end

#get_multi(qs) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/haplocheirus/mock_service.rb', line 60

def get_multi(qs)
  qs.map do |q|
    begin
      get q.timeline_id, q.offset, q.length, q.dedupe
    rescue Haplocheirus::TimelineStoreException
      Haplocheirus::TimelineSegment.new(:entries => [],
                                        :size => 0,
                                        :state => Haplocheirus::TimelineSegmentState::MISS)
    end
  end
end

#get_range(i, f, t = 0, d = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/haplocheirus/mock_service.rb', line 72

def get_range(i, f, t = 0, d = false)
  raise Haplocheirus::TimelineStoreException unless @timelines.key?(i)
  min = @timelines[i].index([f].pack("Q"))
  max = t > 0 ? @timelines[i].index([t].pack("Q")) : 0
  t = min ? @timelines[i][max..min-1] : @timelines[i]
  t.sort! { |a, b| a[0,8].unpack("Q") <=> b[0,8].unpack("Q") }
  t = dedupe(t) if d
  Haplocheirus::TimelineSegment.new(:entries => t.reverse,
                                    :size => @timelines[i].length,
                                    :state => Haplocheirus::TimelineSegmentState::HIT)
end

#merge(i, e) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/haplocheirus/mock_service.rb', line 108

def merge(i, e)
  return unless @timelines.key?(i)

  e.each do |el|
    o = 0
    o += 1 while @timelines[i][0] <= el
    @timelines[i].insert(o + 1, el)
  end
end

#merge_indirect(d, s) ⇒ Object



118
119
120
# File 'lib/haplocheirus/mock_service.rb', line 118

def merge_indirect(d, s)
  merge(d, @timelines[s]) if @timelines.key?(s)
end

#remove(e, p, is) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/haplocheirus/mock_service.rb', line 41

def remove(e, p, is)
  is.each do |i|
    key = p + i.to_s
    next unless @timelines.key?(key)
    @timelines[key].reject! { |i| i == e }
    @timelines.delete(key) if @timelines[key].empty?
  end
end

#reset!Object

This is not part of Haplo, but is useful for test harnesses



136
137
138
# File 'lib/haplocheirus/mock_service.rb', line 136

def reset!
  @timelines = {}
end

#store(i, e) ⇒ Object



84
85
86
87
# File 'lib/haplocheirus/mock_service.rb', line 84

def store(i, e)
  @timelines[i] = []
  e.reverse.each { |n| append n, '', [i] }
end

#unmerge(i, e) ⇒ Object



122
123
124
125
# File 'lib/haplocheirus/mock_service.rb', line 122

def unmerge(i, e)
  return unless @timelines.key?(i)
  @timelines[i].reject! { |o| e.find { |el| MockNode.unpack(el) == MockNode.unpack(o) } }
end

#unmerge_indirect(d, s) ⇒ Object



127
128
129
# File 'lib/haplocheirus/mock_service.rb', line 127

def unmerge_indirect(d, s)
  unmerge(d, @timelines[s]) if @timelines.key?(s)
end