MCPcopy Create free account
hub / github.com/apache/cassandra-nodejs-driver / buildTokens

Method buildTokens

lib/metadata/index.js:144–201  ·  view source on GitHub ↗

* Populates the information regarding primary replica per token, datacenters (+ racks) and sorted token ring. * @ignore * @param {HostMap} hosts

(hosts)

Source from the content-addressed store, hash-verified

142 * @param {HostMap} hosts
143 */
144 buildTokens(hosts) {
145 if (!this.tokenizer) {
146 return this.log('error', 'Tokenizer could not be determined');
147 }
148 //Get a sorted array of tokens
149 const allSorted = [];
150 //Get a map of <token, primaryHost>
151 const primaryReplicas = {};
152 //Depending on the amount of tokens, this could be an expensive operation
153 const hostArray = hosts.values();
154 const stringify = this.tokenizer.stringify;
155 const datacenters = {};
156 hostArray.forEach((h) => {
157 if (!h.tokens) {
158 return;
159 }
160 h.tokens.forEach((tokenString) => {
161 const token = this.tokenizer.parse(tokenString);
162 utils.insertSorted(allSorted, token, (t1, t2) => t1.compare(t2));
163 primaryReplicas[stringify(token)] = h;
164 });
165 let dc = datacenters[h.datacenter];
166 if (!dc) {
167 dc = datacenters[h.datacenter] = {
168 hostLength: 0,
169 racks: new utils.HashSet()
170 };
171 }
172 dc.hostLength++;
173 dc.racks.add(h.rack);
174 });
175 //Primary replica for given token
176 this.primaryReplicas = primaryReplicas;
177 //All the tokens in ring order
178 this.ring = allSorted;
179 // Build TokenRanges.
180 const tokenRanges = new Set();
181 if (this.ring.length === 1) {
182 // If there is only one token, return the range ]minToken, minToken]
183 const min = this.tokenizer.minToken();
184 tokenRanges.add(new TokenRange(min, min, this.tokenizer));
185 }
186 else {
187 for (let i = 0; i < this.ring.length; i++) {
188 const start = this.ring[i];
189 const end = this.ring[(i + 1) % this.ring.length];
190 tokenRanges.add(new TokenRange(start, end, this.tokenizer));
191 }
192 }
193 this.tokenRanges = tokenRanges;
194 //Compute string versions as it's potentially expensive and frequently reused later
195 this.ringTokensAsStrings = new Array(allSorted.length);
196 for (let i = 0; i < allSorted.length; i++) {
197 this.ringTokensAsStrings[i] = stringify(allSorted[i]);
198 }
199 //Datacenter metadata (host length and racks)
200 this.datacenters = datacenters;
201 }

Callers 2

_refreshHostsMethod · 0.80
metadata-tests.jsFile · 0.80

Calls 6

valuesMethod · 0.65
forEachMethod · 0.65
compareMethod · 0.65
addMethod · 0.65
parseMethod · 0.45
minTokenMethod · 0.45

Tested by

no test coverage detected