class Cassandra::Cluster::Metadata

@private

Attributes

name[R]

Public Class Methods

new(cluster_registry, cluster_schema, schema_partitioners, replication_strategies, default_replication_strategy) click to toggle source
   # File lib/cassandra/cluster/metadata.rb
27 def initialize(cluster_registry,
28                cluster_schema,
29                schema_partitioners,
30                replication_strategies,
31                default_replication_strategy)
32   @registry         = cluster_registry
33   @schema           = cluster_schema
34   @partitioners     = schema_partitioners
35   @strategies       = replication_strategies
36   @default_strategy = default_replication_strategy
37   @token_replicas   = ::Hash.new
38   @token_ring       = ::Array.new
39 end

Public Instance Methods

find_replicas(keyspace, statement) click to toggle source
   # File lib/cassandra/cluster/metadata.rb
41 def find_replicas(keyspace, statement)
42   return EMPTY_LIST unless statement.respond_to?(:partition_key) && statement.respond_to?(:keyspace)
43 
44   keyspace      = String(statement.keyspace || keyspace)
45   partition_key = statement.partition_key
46   return EMPTY_LIST unless keyspace && partition_key
47 
48   partitioner = @partitioner
49   return EMPTY_LIST unless partitioner
50 
51   keyspace_hosts = @token_replicas[keyspace]
52   return EMPTY_LIST if keyspace_hosts.nil? || keyspace_hosts.empty?
53 
54   token = partitioner.create_token(partition_key)
55   index = insertion_point(@token_ring, token)
56   index = 0 if index >= @token_ring.size
57   hosts = keyspace_hosts[@token_ring[index]]
58   return EMPTY_LIST unless hosts
59 
60   hosts
61 end
rebuild_token_map() click to toggle source
    # File lib/cassandra/cluster/metadata.rb
 70 def rebuild_token_map
 71   partitioner = @partitioner
 72   return self unless partitioner
 73 
 74   tokens        = ::SortedSet.new
 75   token_to_host = ::Hash.new
 76 
 77   @registry.each_host do |host|
 78     host.tokens.each do |token|
 79       token = begin
 80                 partitioner.parse_token(token)
 81               rescue
 82                 next
 83               end
 84       tokens.add(token)
 85       token_to_host[token] = host
 86     end
 87   end
 88 
 89   token_ring     = tokens.to_a
 90   token_replicas = ::Hash.new
 91   token_maps     = ::Hash.new
 92 
 93   @schema.each_keyspace do |keyspace|
 94     replication = keyspace.replication
 95     key         = replication_key(replication.klass, replication.options)
 96 
 97     unless token_maps.include?(key)
 98       strategy        = @strategies[replication.klass] || @default_strategy
 99       token_maps[key] = strategy.replication_map(
100         token_to_host,
101         token_ring,
102         replication.options
103       )
104     end
105 
106     token_replicas[keyspace.name] = token_maps[key]
107   end
108 
109   @token_replicas = token_replicas
110   @token_ring     = token_ring
111 
112   self
113 end
update(data) click to toggle source
   # File lib/cassandra/cluster/metadata.rb
63 def update(data)
64   @name        = data['cluster_name']
65   @partitioner = @partitioners[data['partitioner']]
66 
67   self
68 end

Private Instance Methods

insertion_point(list, item) click to toggle source
    # File lib/cassandra/cluster/metadata.rb
121 def insertion_point(list, item)
122   min = 0
123   max = list.size - 1
124 
125   while min <= max
126     idx = (min + max) / 2
127     val = list[idx]
128 
129     if val < item
130       min = idx + 1
131     elsif val > item
132       max = idx - 1
133     else
134       return idx # item found
135     end
136   end
137 
138   min # item not found.
139 end
replication_key(klass, options) click to toggle source
    # File lib/cassandra/cluster/metadata.rb
117 def replication_key(klass, options)
118   (klass + ':' + options.keys.sort.map {|k| "#{k}=#{options[k]}"}.join(',')).hash
119 end