Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ReadResultSet iterable [API-1315] #1399

Merged
merged 5 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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++;
}
});
});