Class: Iriq::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/iriq/cluster.rb

Overview

A group of identifiers that share a host + shape key. Tracks examples and per-position segment statistics so callers can ask which positions are actually stable in practice (e.g. /users/ always literal, /integer_id always variable).

Constant Summary collapse

MAX_EXAMPLES =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, host:, scheme:, shape:) ⇒ Cluster

Returns a new instance of Cluster.



11
12
13
14
15
16
17
18
19
# File 'lib/iriq/cluster.rb', line 11

def initialize(key:, host:, scheme:, shape:)
  @key            = key
  @host           = host
  @scheme         = scheme
  @shape          = shape
  @examples       = []
  @count          = 0
  @segment_counts = []
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



7
8
9
# File 'lib/iriq/cluster.rb', line 7

def count
  @count
end

#examplesObject (readonly)

Returns the value of attribute examples.



7
8
9
# File 'lib/iriq/cluster.rb', line 7

def examples
  @examples
end

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/iriq/cluster.rb', line 7

def host
  @host
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/iriq/cluster.rb', line 7

def key
  @key
end

#schemeObject (readonly)

Returns the value of attribute scheme.



7
8
9
# File 'lib/iriq/cluster.rb', line 7

def scheme
  @scheme
end

#shapeObject (readonly)

Returns the value of attribute shape.



7
8
9
# File 'lib/iriq/cluster.rb', line 7

def shape
  @shape
end

Class Method Details

.from_dump(h) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/iriq/cluster.rb', line 73

def self.from_dump(h)
  cluster = new(key: h["key"], host: h["host"], scheme: h["scheme"], shape: h["shape"])
  cluster.instance_variable_set(:@count, h["count"])
  cluster.instance_variable_set(:@examples, h["examples"].map { |s| Parser.parse(s) })
  cluster.instance_variable_set(:@segment_counts, h["segment_counts"].map { |sub| Hash.new(0).merge(sub) })
  cluster
end

Instance Method Details

#add(identifier) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/iriq/cluster.rb', line 21

def add(identifier)
  @count += 1
  @examples << identifier if @examples.size < MAX_EXAMPLES

  identifier.path_segments.each_with_index do |seg, i|
    @segment_counts[i] ||= Hash.new(0)
    @segment_counts[i][seg] += 1
  end
end

#dumpObject

JSON-friendly dump for persistence (distinct from #to_h which is a display form). Examples are dumped as canonical strings and re-parsed on load.



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/iriq/cluster.rb', line 61

def dump
  {
    "key"            => key,
    "host"           => host,
    "scheme"         => scheme,
    "shape"          => shape,
    "count"          => count,
    "examples"       => examples.map(&:canonical),
    "segment_counts" => @segment_counts.map { |h| h || {} },
  }
end

#segment_statsObject

Per-position summary:

[
  { position: 0, stable: true,  values: { "users" => 3 } },
  { position: 1, stable: false, values: { "1" => 1, "2" => 1, "3" => 1 } },
]


36
37
38
39
40
41
42
43
44
# File 'lib/iriq/cluster.rb', line 36

def segment_stats
  @segment_counts.each_with_index.map do |counts, i|
    {
      position: i,
      stable:   counts.size == 1,
      values:   counts.dup,
    }
  end
end

#to_hObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/iriq/cluster.rb', line 46

def to_h
  {
    key:      key,
    host:     host,
    scheme:   scheme,
    shape:    shape,
    count:    count,
    examples: examples.map(&:canonical),
    segments: segment_stats,
  }
end