Skip to content

Commit

Permalink
Merge pull request #82 from upstash/cursor-quickfix
Browse files Browse the repository at this point in the history
fix cursors not getting parsed correctly to numbers
  • Loading branch information
CahidArda authored Jun 12, 2024
2 parents 02e59e5 + 8dd0158 commit d5d5334
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-kangaroos-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@upstash/react-databrowser": patch
---

fix bug with cursor parsing
2 changes: 1 addition & 1 deletion packages/react-databrowser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@
"@radix-ui/react-toast": "^1.1.5",
"@radix-ui/react-tooltip": "^1.0.7",
"@tanstack/react-query": "^5.32.0",
"@upstash/redis": "^1.31.3"
"@upstash/redis": "^1.31.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class PaginatedRedis {
// console.log("************** RESET");
}

cache: Record<string, { cursor: number; keys: string[] }> = Object.fromEntries(
dataTypes.map((type) => [type, { cursor: 0, keys: [] }]),
// Cursor is 0 initially, then it is set to -1 when we reach the end
cache: Record<string, { cursor: string; keys: string[] }> = Object.fromEntries(
dataTypes.map((type) => [type, { cursor: "0", keys: [] }]),
);
targetCount = 0;

Expand All @@ -57,32 +58,26 @@ class PaginatedRedis {

while (true) {
const cursor = this.cache[type].cursor;
if (cursor === -1 || this.getLength() >= this.targetCount) {
if (cursor === "-1" || this.getLength() >= this.targetCount) {
break;
}

const [nextCursorStr, newKeys] = await this.redis.scan(cursor, {
const [nextCursor, newKeys] = await this.redis.scan(cursor, {
count: fetchCount,
match: this.searchTerm,
type: type,
});

const nextCursor = Number(nextCursorStr);

const stringifiedKeys = (newKeys as unknown[]).map((key) =>
typeof key === "string" ? key : JSON.stringify(key),
);

fetchCount = Math.min(fetchCount * 2, MAX_FETCH_COUNT);

// console.log("< scan", type, newKeys.length, nextCursor === 0 ? "END" : "MORE");

// Dedupe here because redis can and will return duplicates for example when
// a key is deleted because of ttl etc.
const dedupedSet = new Set([...this.cache[type].keys, ...stringifiedKeys]);
const dedupedSet = new Set([...this.cache[type].keys, ...newKeys]);

this.cache[type].keys = [...dedupedSet];
this.cache[type].cursor = nextCursor === 0 ? -1 : nextCursor;
this.cache[type].cursor = nextCursor === "0" ? "-1" : nextCursor;
}
};

Expand All @@ -94,7 +89,7 @@ class PaginatedRedis {
isFetching = false;

private isAllEnded() {
return (this.typeFilter ? [this.typeFilter] : dataTypes).every((type) => this.cache[type].cursor === -1);
return (this.typeFilter ? [this.typeFilter] : dataTypes).every((type) => this.cache[type].cursor === "-1");
}

async getPage(page: number) {
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d5d5334

Please sign in to comment.