Skip to content

Commit

Permalink
Fix collection total_member count offset incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
Polleps committed Mar 26, 2024
1 parent 10d53c7 commit 616412a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
2 changes: 1 addition & 1 deletion browser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This changelog covers all three packages, as they are (for now) updated as a who
- Added `resource.matchClass()` method.
- Added `resource.setVersion()` method.
- Added `collection.getMembersOnPage()` method.
- Added `collection.numberOfPages`.
- Added `collection.totalPages`.
- BREAKING CHANGE: Renamed `resource.getCommitsCollection` to `resource.getCommitsCollectionSubject`.
- BREAKING CHANGE: `resource.getChildrenCollection()` now returns a `Promise<Collection>` instead of a subject.
- BREAKING CHANGE: Resource now keeps a reference to store internally, therefore all methods that required you to pass a store have been changed to not require a store.
Expand Down
17 changes: 7 additions & 10 deletions browser/lib/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { isNumber } from './datatypes.js';
import { Collections, collections } from './ontologies/collections.js';
import { Resource } from './resource.js';
import { Store } from './store.js';
import { urls } from './urls.js';

export interface QueryFilter {
property?: string;
Expand Down Expand Up @@ -36,6 +35,8 @@ export class Collection {

private _totalMembers = 0;

private _totalPages = 0;

private _waitForReady: Promise<void>;

public constructor(
Expand Down Expand Up @@ -79,7 +80,7 @@ export class Collection {
return this._totalMembers;
}

public get numberOfPages(): number {
public get totalPages(): number {
return Math.ceil(this.totalMembers / this.pageSize);
}

Expand Down Expand Up @@ -134,18 +135,12 @@ export class Collection {
}

public async getAllMembers(): Promise<string[]> {
const prevPageSize = this.params.page_size;
// Set page size to a high number for less request overhead.
this.params.page_size = '1000';

const members: string[] = [];

for await (const member of this) {
members.push(member);
}

this.params.page_size = prevPageSize;

return members;
}

Expand Down Expand Up @@ -173,7 +168,8 @@ export class Collection {

private async fetchPage(page: number): Promise<void> {
const subject = this.buildSubject(page);
const resource = await this.store.fetchResourceFromServer(subject);
const resource =
await this.store.fetchResourceFromServer<Collections.Collection>(subject);

if (!resource) {
throw new Error('Invalid collection: resource does not exist');
Expand All @@ -187,13 +183,14 @@ export class Collection {

this.pages.set(page, resource);

const totalMembers = resource.get(urls.properties.collection.totalMembers);
const totalMembers = resource.props.totalMembers;

if (!isNumber(totalMembers)) {
throw new Error('Invalid collection: total-members is not a number');
}

this._totalMembers = totalMembers;
this._totalPages = resource.props.totalPages ?? 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl Db {

let mut subjects: Vec<String> = vec![];
let mut resources: Vec<Resource> = vec![];
let mut total_count = 0;
let mut total_count = q.offset;

let atoms = self.get_index_iterator_for_query(q);

Expand Down

0 comments on commit 616412a

Please sign in to comment.