| 526 | } |
| 527 | |
| 528 | split(start, end, numberOfSplits) { |
| 529 | const tokenOrder = start.compare(end); |
| 530 | |
| 531 | if (tokenOrder === 0 && start.equals(this.minToken())) { |
| 532 | throw new Error("Cannot split whole ring with ordered partitioner"); |
| 533 | } |
| 534 | |
| 535 | let startVal, endVal, range, ringLength, ringEnd; |
| 536 | const intNumberOfSplits = Integer.fromNumber(numberOfSplits); |
| 537 | // Since tokens are compared lexicographically, convert to numbers using the |
| 538 | // largest length (i.e. given 0x0A and 0x0BCD, switch to 0x0A00 and 0x0BCD) |
| 539 | let significantBytes = Math.max(start.getValue().length, end.getValue().length); |
| 540 | if (tokenOrder < 0) { |
| 541 | let addedBytes = 0; |
| 542 | while (true) { |
| 543 | startVal = this._toNumber(start.getValue(), significantBytes); |
| 544 | endVal = this._toNumber(end.getValue(), significantBytes); |
| 545 | range = endVal.subtract(startVal); |
| 546 | if (addedBytes === 4 || range.compare(intNumberOfSplits) >= 0) { |
| 547 | break; |
| 548 | } |
| 549 | significantBytes += 1; |
| 550 | addedBytes += 1; |
| 551 | } |
| 552 | } else { |
| 553 | let addedBytes = 0; |
| 554 | while (true) { |
| 555 | startVal = this._toNumber(start.getValue(), significantBytes); |
| 556 | endVal = this._toNumber(end.getValue(), significantBytes); |
| 557 | ringLength = Integer.fromNumber(Math.pow(2, significantBytes * 8)); |
| 558 | ringEnd = ringLength.subtract(Integer.ONE); |
| 559 | range = endVal.subtract(startVal).add(ringLength); |
| 560 | if (addedBytes === 4 || range.compare(intNumberOfSplits) >= 0) { |
| 561 | break; |
| 562 | } |
| 563 | significantBytes += 1; |
| 564 | addedBytes += 1; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | const values = this.splitBase(startVal, range, ringEnd, ringLength, numberOfSplits); |
| 569 | return values.map(v => new token.ByteOrderedToken(this._toBuffer(v, significantBytes))); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /** |