Skip to content

Fix iterate and greedyIterate methods #16

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
18 changes: 10 additions & 8 deletions src/clients/dataset-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,35 @@ export class ExtDatasetClient<T extends DatasetItem> extends DatasetClient<T> im
const { pageSize, ...listItemOptions } = options;
this.customLogger.info('Iterating Dataset', { pageSize }, { url: this.url });

let totalItems = 0;
const initialOffset = listItemOptions.offset ?? 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I feel weird about this being in the outer scope as it really doesn't matter when we don't have a pageSize - that would entail duplicating the log, but TBH I'm not against that as it's actually two implementations masquerading as one function..

let readItems = 0;

if (pageSize) {
let currentOffset = listItemOptions.offset ?? 0;
let currentPage = await this.superClient.listItems({
...listItemOptions,
offset: currentOffset,
limit: pageSize,
});
while (currentPage.items.length > 0) {
totalItems += currentPage.items.length;
readItems += currentPage.items.length;
for (const item of currentPage.items) {
yield item;
}

currentOffset += pageSize;
currentPage = await this.superClient.listItems({ offset: currentOffset, limit: pageSize });
currentPage = await this.superClient.listItems({
...listItemOptions,
offset: initialOffset + readItems,
limit: pageSize,
});
}
} else {
const itemList = await this.superClient.listItems(listItemOptions);
totalItems += itemList.items.length;
readItems += itemList.items.length;
for (const item of itemList.items) {
yield item;
}
}

this.customLogger.info('Finished reading dataset', { totalItems }, { url: this.url });
this.customLogger.info('Finished reading dataset', { initialOffset, readItems }, { url: this.url });
}

async* greedyIterate(options: GreedyIterateOptions = {}): AsyncGenerator<T, void, void> {
Expand Down
10 changes: 5 additions & 5 deletions test/clients/dataset-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('dataset-client', () => {
expect(listItemsSpy).toHaveBeenCalledWith({});
});

it('iterates the items from the dataset, using pagination', async () => {
it('iterates the items from the dataset, using pagination, passing the given options', async () => {
const listItemsSpy = vi.spyOn(DatasetClient.prototype, 'listItems')
.mockImplementationOnce(async () => ({
count: 2,
Expand All @@ -102,17 +102,17 @@ describe('dataset-client', () => {
limit: 2,
desc: true,
}));
const datasetIterator = datasetClient.iterate({ pageSize: 2 });
const datasetIterator = datasetClient.iterate({ pageSize: 2, fields: ['title'] });
let index = 0;
for await (const item of datasetIterator) {
expect(item).toEqual(testItems[index]);
index++;
}
expect(index).toBe(3);
expect(listItemsSpy).toHaveBeenCalledTimes(3);
expect(listItemsSpy).toHaveBeenNthCalledWith(1, { offset: 0, limit: 2 });
expect(listItemsSpy).toHaveBeenNthCalledWith(2, { offset: 2, limit: 2 });
expect(listItemsSpy).toHaveBeenNthCalledWith(3, { offset: 4, limit: 2 });
expect(listItemsSpy).toHaveBeenNthCalledWith(1, { offset: 0, limit: 2, fields: ['title'] });
expect(listItemsSpy).toHaveBeenNthCalledWith(2, { offset: 2, limit: 2, fields: ['title'] });
expect(listItemsSpy).toHaveBeenNthCalledWith(3, { offset: 4, limit: 2, fields: ['title'] });
});

it('iterates the items using pagination and starting from the desired offset', async () => {
Expand Down