forked from datastax/nodejs-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.js
286 lines (258 loc) · 7.71 KB
/
token.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const types = require('./types');
const util = require('util');
const _Murmur3TokenType = types.dataTypes.getByName('bigint');
const _RandomTokenType = types.dataTypes.getByName('varint');
const _OrderedTokenType = types.dataTypes.getByName('blob');
/**
* Represents a token on the Cassandra ring.
*/
class Token {
constructor(value) {
this._value = value;
}
/**
* @returns {{code: number, info: *|Object}} The type info for the
* type of the value of the token.
*/
getType() {
throw new Error('You must implement a getType function for this Token instance');
}
/**
* @returns {*} The raw value of the token.
*/
getValue() {
return this._value;
}
toString() {
return this._value.toString();
}
/**
* Returns 0 if the values are equal, 1 if greater than other, -1
* otherwise.
*
* @param {Token} other
* @returns {Number}
*/
compare(other) {
return this._value.compare(other._value);
}
equals(other) {
return this.compare(other) === 0;
}
inspect() {
return this.constructor.name + ' { ' + this.toString() + ' }';
}
}
/**
* Represents a token from a Cassandra ring where the partitioner
* is Murmur3Partitioner.
*
* The raw token type is a varint (represented by MutableLong).
*/
class Murmur3Token extends Token {
constructor(value) {
super(value);
}
getType() {
return _Murmur3TokenType;
}
}
/**
* Represents a token from a Cassandra ring where the partitioner
* is RandomPartitioner.
*
* The raw token type is a bigint (represented by Number).
*/
class RandomToken extends Token {
constructor(value) {
super(value);
}
getType() {
return _RandomTokenType;
}
}
/**
* Represents a token from a Cassandra ring where the partitioner
* is ByteOrderedPartitioner.
*
* The raw token type is a blob (represented by Buffer or Array).
*/
class ByteOrderedToken extends Token {
constructor(value) {
super(value);
}
getType() {
return _OrderedTokenType;
}
toString() {
return this._value.toString('hex').toUpperCase();
}
}
/**
* Represents a range of tokens on a Cassandra ring.
*
* A range is start-exclusive and end-inclusive. It is empty when
* start and end are the same token, except if that is the minimum
* token, in which case the range covers the whole ring (this is
* consistent with the behavior of CQL range queries).
*
* Note that CQL does not handle wrapping. To query all partitions
* in a range, see {@link unwrap}.
*/
class TokenRange {
constructor(start, end, tokenizer) {
this.start = start;
this.end = end;
Object.defineProperty(this, '_tokenizer', { value: tokenizer, enumerable: false});
}
/**
* Splits this range into a number of smaller ranges of equal "size"
* (referring to the number of tokens, not the actual amount of data).
*
* Splitting an empty range is not permitted. But not that, in edge
* cases, splitting a range might produce one or more empty ranges.
*
* @param {Number} numberOfSplits Number of splits to make.
* @returns {TokenRange[]} Split ranges.
* @throws {Error} If splitting an empty range.
*/
splitEvenly(numberOfSplits) {
if (numberOfSplits < 1) {
throw new Error(util.format("numberOfSplits (%d) must be greater than 0.", numberOfSplits));
}
if (this.isEmpty()) {
throw new Error("Can't split empty range " + this.toString());
}
const tokenRanges = [];
const splitPoints = this._tokenizer.split(this.start, this.end, numberOfSplits);
let splitStart = this.start;
let splitEnd;
for (let splitIndex = 0; splitIndex < splitPoints.length; splitIndex++) {
splitEnd = splitPoints[splitIndex];
tokenRanges.push(new TokenRange(splitStart, splitEnd, this._tokenizer));
splitStart = splitEnd;
}
tokenRanges.push(new TokenRange(splitStart, this.end, this._tokenizer));
return tokenRanges;
}
/**
* A range is empty when start and end are the same token, except if
* that is the minimum token, in which case the range covers the
* whole ring. This is consistent with the behavior of CQL range
* queries.
*
* @returns {boolean} Whether this range is empty.
*/
isEmpty() {
return this.start.equals(this.end) && !this.start.equals(this._tokenizer.minToken());
}
/**
* A range wraps around the end of the ring when the start token
* is greater than the end token and the end token is not the
* minimum token.
*
* @returns {boolean} Whether this range wraps around.
*/
isWrappedAround() {
return this.start.compare(this.end) > 0 && !this.end.equals(this._tokenizer.minToken());
}
/**
* Splits this range into a list of two non-wrapping ranges.
*
* This will return the range itself if it is non-wrapped, or two
* ranges otherwise.
*
* This is useful for CQL range queries, which do not handle
* wrapping.
*
* @returns {TokenRange[]} The list of non-wrapping ranges.
*/
unwrap() {
if (this.isWrappedAround()) {
return [
new TokenRange(this.start, this._tokenizer.minToken(), this._tokenizer),
new TokenRange(this._tokenizer.minToken(), this.end, this._tokenizer)
];
}
return [this];
}
/**
* Whether this range contains a given Token.
*
* @param {*} token Token to check for.
* @returns {boolean} Whether or not the Token is in this range.
*/
contains(token) {
if (this.isEmpty()) {
return false;
}
const minToken = this._tokenizer.minToken();
if (this.end.equals(minToken)) {
if (this.start.equals(minToken)) {
return true; // ]minToken, minToken] === full ring
} else if (token.equals(minToken)) {
return true;
}
return token.compare(this.start) > 0;
}
const isAfterStart = token.compare(this.start) > 0;
const isBeforeEnd = token.compare(this.end) <= 0;
// if wrapped around ring, token is in ring if its after start or before end.
// otherwise, token is in ring if its after start and before end.
return this.isWrappedAround()
? isAfterStart || isBeforeEnd
: isAfterStart && isBeforeEnd;
}
/**
* Determines if the input range is equivalent to this one.
*
* @param {TokenRange} other Range to compare with.
* @returns {boolean} Whether or not the ranges are equal.
*/
equals(other) {
if (other === this) {
return true;
} else if (other instanceof TokenRange) {
return this.compare(other) === 0;
}
return false;
}
/**
* Returns 0 if the values are equal, otherwise compares against
* start, if start is equal, compares against end.
*
* @param {TokenRange} other Range to compare with.
* @returns {Number}
*/
compare(other) {
const compareStart = this.start.compare(other.start);
return compareStart !== 0 ? compareStart : this.end.compare(other.end);
}
toString() {
return util.format(']%s, %s]',
this.start.toString(),
this.end.toString()
);
}
}
exports.Token = Token;
exports.TokenRange = TokenRange;
exports.ByteOrderedToken = ByteOrderedToken;
exports.Murmur3Token = Murmur3Token;
exports.RandomToken = RandomToken;