Skip to content

Commit f193925

Browse files
committed
Simplify round robin array
Removed unused methods, simplified remove operation and added couple tests.
1 parent c9c4f1a commit f193925

File tree

3 files changed

+89
-61
lines changed

3 files changed

+89
-61
lines changed

src/v1/internal/round-robin-array.js

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,26 @@
2323
class RoundRobinArray {
2424
constructor(items) {
2525
this._items = items || [];
26-
this._index = 0;
26+
this._offset = 0;
2727
}
2828

2929
next() {
30-
let elem = this._items[this._index];
31-
if (this._items.length === 0) {
32-
this._index = 0;
33-
} else {
34-
this._index = (this._index + 1) % (this._items.length);
30+
if (this.isEmpty()) {
31+
return null;
3532
}
36-
return elem;
37-
}
38-
39-
push(elem) {
40-
this._items.push(elem);
33+
const index = this._offset % this.size();
34+
this._offset++;
35+
return this._items[index];
4136
}
4237

4338
pushAll(elems) {
4439
Array.prototype.push.apply(this._items, elems);
4540
}
4641

47-
empty() {
42+
isEmpty() {
4843
return this._items.length === 0;
4944
}
5045

51-
clear() {
52-
this._items = [];
53-
this._index = 0;
54-
}
55-
5646
size() {
5747
return this._items.length;
5848
}
@@ -62,20 +52,7 @@ class RoundRobinArray {
6252
}
6353

6454
remove(item) {
65-
let index = this._items.indexOf(item);
66-
while (index != -1) {
67-
this._items.splice(index, 1);
68-
if (index < this._index) {
69-
this._index -= 1;
70-
}
71-
//make sure we are in range
72-
if (this._items.length === 0) {
73-
this._index = 0;
74-
} else {
75-
this._index %= this._items.length;
76-
}
77-
index = this._items.indexOf(item, index);
78-
}
55+
this._items = this._items.filter(element => element !== item);
7956
}
8057
}
8158

src/v1/routing-driver.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ class ClusterView {
169169
needsUpdate() {
170170
return this._expires.lessThan(Date.now()) ||
171171
this.routers.size() <= 1 ||
172-
this.readers.empty() ||
173-
this.writers.empty();
172+
this.readers.isEmpty() ||
173+
this.writers.isEmpty();
174174
}
175175

176176
all() {
@@ -243,7 +243,7 @@ function newClusterView(session) {
243243
readers.pushAll(addresses);
244244
}
245245
}
246-
if (routers.empty() || writers.empty()) {
246+
if (routers.isEmpty() || writers.isEmpty()) {
247247
return Promise.reject(newError("Invalid routing response from server", SERVICE_UNAVAILABLE))
248248
}
249249
return new ClusterView(routers, readers, writers, expires);

test/internal/round-robin-array.test.js

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,69 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19+
import RoundRobinArray from "../../lib/v1/internal/round-robin-array";
1920

20-
var RoundRobinArray = require('../../lib/v1/internal/round-robin-array').default;
21+
describe('round-robin-array', () => {
2122

22-
describe('round-robin-array', function() {
23-
it('should step through array', function () {
24-
var array = new RoundRobinArray([1,2,3,4,5]);
23+
it('should behave correctly when empty', () => {
24+
const array = new RoundRobinArray();
25+
26+
expect(array.isEmpty()).toBeTruthy();
27+
expect(array.size()).toEqual(0);
28+
expect(array.next()).toBeNull();
29+
expect(array.toArray()).toEqual([]);
30+
31+
array.remove(1);
32+
33+
expect(array.isEmpty()).toBeTruthy();
34+
expect(array.size()).toEqual(0);
35+
expect(array.next()).toBeNull();
36+
expect(array.toArray()).toEqual([]);
37+
});
38+
39+
it('should behave correctly when contains single element', () => {
40+
const array = new RoundRobinArray([5]);
41+
42+
expect(array.isEmpty()).toBeFalsy();
43+
expect(array.size()).toEqual(1);
44+
expect(array.next()).toEqual(5);
45+
expect(array.toArray()).toEqual([5]);
46+
47+
array.remove(1);
48+
49+
expect(array.isEmpty()).toBeFalsy();
50+
expect(array.size()).toEqual(1);
51+
expect(array.next()).toEqual(5);
52+
expect(array.toArray()).toEqual([5]);
53+
54+
array.remove(5);
55+
56+
expect(array.isEmpty()).toBeTruthy();
57+
expect(array.size()).toEqual(0);
58+
expect(array.next()).toBeNull();
59+
expect(array.toArray()).toEqual([]);
60+
});
61+
62+
it('should push items', () => {
63+
const array1 = new RoundRobinArray();
64+
array1.pushAll([]);
65+
expect(array1.toArray()).toEqual([]);
66+
67+
const array2 = new RoundRobinArray();
68+
array2.pushAll([1]);
69+
expect(array2.toArray()).toEqual([1]);
70+
71+
const array3 = new RoundRobinArray([1, 2, 3]);
72+
array3.pushAll([4, 5]);
73+
expect(array3.toArray()).toEqual([1, 2, 3, 4, 5]);
74+
75+
const array4 = new RoundRobinArray([1, 2, 3]);
76+
array4.pushAll([]);
77+
expect(array4.toArray()).toEqual([1, 2, 3]);
78+
});
79+
80+
it('should step through array', () => {
81+
const array = new RoundRobinArray([1, 2, 3, 4, 5]);
2582

2683
expect(array.next()).toEqual(1);
2784
expect(array.next()).toEqual(2);
@@ -30,34 +87,32 @@ describe('round-robin-array', function() {
3087
expect(array.next()).toEqual(5);
3188
expect(array.next()).toEqual(1);
3289
expect(array.next()).toEqual(2);
33-
//....
3490
});
3591

36-
it('should step through single element array', function () {
37-
var array = new RoundRobinArray([5]);
92+
it('should step through single element array', () => {
93+
const array = new RoundRobinArray([5]);
3894

3995
expect(array.next()).toEqual(5);
4096
expect(array.next()).toEqual(5);
4197
expect(array.next()).toEqual(5);
42-
//....
4398
});
4499

45-
it('should handle deleting item before current ', function () {
46-
var array = new RoundRobinArray([1,2,3,4,5]);
100+
it('should handle deleting item before current ', () => {
101+
const array = new RoundRobinArray([1, 2, 3, 4, 5]);
47102

48103
expect(array.next()).toEqual(1);
49104
expect(array.next()).toEqual(2);
50105
array.remove(2);
51-
expect(array.next()).toEqual(3);
52106
expect(array.next()).toEqual(4);
53107
expect(array.next()).toEqual(5);
54108
expect(array.next()).toEqual(1);
55109
expect(array.next()).toEqual(3);
56-
//....
110+
expect(array.next()).toEqual(4);
111+
expect(array.next()).toEqual(5);
57112
});
58113

59-
it('should handle deleting item on current ', function () {
60-
var array = new RoundRobinArray([1,2,3,4,5]);
114+
it('should handle deleting item on current ', () => {
115+
const array = new RoundRobinArray([1, 2, 3, 4, 5]);
61116

62117
expect(array.next()).toEqual(1);
63118
expect(array.next()).toEqual(2);
@@ -67,11 +122,11 @@ describe('round-robin-array', function() {
67122
expect(array.next()).toEqual(1);
68123
expect(array.next()).toEqual(2);
69124
expect(array.next()).toEqual(4);
70-
//....
125+
expect(array.next()).toEqual(5);
71126
});
72127

73-
it('should handle deleting item after current ', function () {
74-
var array = new RoundRobinArray([1,2,3,4,5]);
128+
it('should handle deleting item after current ', () => {
129+
const array = new RoundRobinArray([1, 2, 3, 4, 5]);
75130

76131
expect(array.next()).toEqual(1);
77132
expect(array.next()).toEqual(2);
@@ -81,11 +136,10 @@ describe('round-robin-array', function() {
81136
expect(array.next()).toEqual(1);
82137
expect(array.next()).toEqual(2);
83138
expect(array.next()).toEqual(3);
84-
//....
85139
});
86140

87-
it('should handle deleting last item ', function () {
88-
var array = new RoundRobinArray([1,2,3,4,5]);
141+
it('should handle deleting last item ', () => {
142+
const array = new RoundRobinArray([1, 2, 3, 4, 5]);
89143

90144
expect(array.next()).toEqual(1);
91145
expect(array.next()).toEqual(2);
@@ -97,11 +151,10 @@ describe('round-robin-array', function() {
97151
expect(array.next()).toEqual(3);
98152
expect(array.next()).toEqual(4);
99153
expect(array.next()).toEqual(1);
100-
//....
101154
});
102155

103-
it('should handle deleting first item ', function () {
104-
var array = new RoundRobinArray([1,2,3,4,5]);
156+
it('should handle deleting first item ', () => {
157+
const array = new RoundRobinArray([1, 2, 3, 4, 5]);
105158
array.remove(1);
106159
expect(array.next()).toEqual(2);
107160
expect(array.next()).toEqual(3);
@@ -111,17 +164,15 @@ describe('round-robin-array', function() {
111164
expect(array.next()).toEqual(3);
112165
expect(array.next()).toEqual(4);
113166
expect(array.next()).toEqual(5);
114-
//....
115167
});
116168

117-
it('should handle deleting multiple items ', function () {
118-
var array = new RoundRobinArray([1,2,3,1,1]);
169+
it('should handle deleting multiple items ', () => {
170+
const array = new RoundRobinArray([1, 2, 3, 1, 1]);
119171
array.remove(1);
120172
expect(array.next()).toEqual(2);
121173
expect(array.next()).toEqual(3);
122174
expect(array.next()).toEqual(2);
123175
expect(array.next()).toEqual(3);
124-
//....
125176
});
126177

127178
});

0 commit comments

Comments
 (0)