Skip to content

Commit

Permalink
Make ReadResultSet iterable [API-1315] (hazelcast#1399)
Browse files Browse the repository at this point in the history
* Make ReadResultSet iterable [API-1315]
* Changes made according to comments on PR.[API-1315]
  • Loading branch information
harunalpak authored Oct 14, 2022
1 parent d95a69c commit 1f21026
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
7 changes: 7 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1917,6 +1917,13 @@ let value = await rb.readOne(sequence);
console.log('Value:', value);
value = await rb.readOne(sequence.add(1));
console.log('Next value:', value);
// Add some elements to the Ringbuffer
await rb.addAll([300, 400, 500]);
// We want to get 5 items from Ringbuffer and write to console.
const items = await rb.readMany(0, 1, 5);
for(const item of items){
console.log("Item:" item);
}
```

### 8.4.8. Using Reliable Topic
Expand Down
7 changes: 7 additions & 0 deletions src/core/ReadResultSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export interface ReadResultSet<T> {
*/
size(): number;

/**
* Returns an iterator for elements in the list.
*
* @returns the iterator for elements in the list.
*/
[Symbol.iterator](): Iterator<T>;

/**
* Returns the sequence of the item following the last read item. This
* sequence can then be used to read items following the ones returned by
Expand Down
35 changes: 35 additions & 0 deletions src/proxy/ringbuffer/LazyReadResultSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ import {SerializationService, SerializationServiceV1} from '../../serialization/
import {ReadResultSet} from '../../core';
import {HazelcastSerializationError, SchemaNotFoundError} from './../../core/HazelcastError';



/** @internal */
class LazyReadResultSetIterator<T> implements Iterator<T> {

private index = 0;
private list: LazyReadResultSet<T>;

constructor(list: LazyReadResultSet<T>) {
this.list = list;
}

/**
* Returns the next element in the iteration.
* @throws {@link HazelcastSerializationError} if the next item is a compact object whose schema is not known
*/
next(): IteratorResult<T> {
if (this.index < this.list.size()) {
return {done: false, value: this.list.get(this.index++)};
} else {
return {done: true, value: undefined};
}
}

}

/** @internal */
export class LazyReadResultSet<T> implements ReadResultSet<T> {

Expand Down Expand Up @@ -80,4 +106,13 @@ export class LazyReadResultSet<T> implements ReadResultSet<T> {
return this.nextSeq;
}

values(): Iterator<T> {
return new LazyReadResultSetIterator(this);
}

[Symbol.iterator](): Iterator<T> {
return this.values();
}


}
10 changes: 10 additions & 0 deletions test/unit/ringbuffer/LazyReadResultSetTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ describe('LazyReadResultSetTest', function () {
const set = new LazyReadResultSet(mockSerializationService, 4, [1, 2, 3, 4], [11, 12, 13, 14], 15);
expect(set.get(4)).to.be.undefined;
});

it('should be iterable', function () {
const set = new LazyReadResultSet(mockSerializationService, 4, [1, 2, 3, 4], [11, 12, 13, 14], 15);
let index = 0;
const values = [101, 102, 3, 4];
for (const item of set) {
expect(item).to.equal(values[index]);
index++;
}
});
});

0 comments on commit 1f21026

Please sign in to comment.