Skip to content

Commit e6f8663

Browse files
kriszypHarperfast
authored andcommitted
Merge pull request #496 from HarperFast/fix/rocksdb-entry-count
Use RocksDB's `estimate-num-keys` instead of non-existent `entryCount`
1 parent b50ec5a commit e6f8663

3 files changed

Lines changed: 10 additions & 5 deletions

File tree

resources/Table.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,8 @@ export function makeTable(options) {
957957
records: './', // an href to the records themselves
958958
name: tableName,
959959
database: databaseName,
960-
auditSize: auditStore?.getStats().entryCount,
960+
auditSize:
961+
auditStore instanceof RocksDatabase ? auditStore.getKeysCount() : auditStore?.getStats().entryCount,
961962
attributes,
962963
recordCount: undefined,
963964
estimatedRecordRange: undefined,
@@ -3173,13 +3174,13 @@ export function makeTable(options) {
31733174
}
31743175
static async getRecordCount(options?: any) {
31753176
// iterate through the metadata entries to exclude their count and exclude the deletion counts
3177+
const exactCount = options?.exactCount;
31763178
const entryCount = isRocksDB
3177-
? (primaryStore.getDBIntProperty('rocksdb.estimate-num-keys') ?? 0)
3179+
? primaryStore.getKeysCount({ start: exactCount ? null : undefined })
31783180
: primaryStore.getStats().entryCount;
31793181
const TIME_LIMIT = options?.timeLimit ?? 1000 / 2; // one second time limit, enforced by seeing if we are halfway through at 500ms
31803182
const start = performance.now();
31813183
const halfway = Math.floor(entryCount / 2);
3182-
const exactCount = options?.exactCount;
31833184
let recordCount = 0;
31843185
let entriesScanned = 0;
31853186
let limit: number;

resources/indexes/HierarchicalNavigableSmallWorld.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FLOAT32_OPTIONS } from 'msgpackr';
33
import { loggerWithTag } from '../../utility/logging/logger.ts';
44
import { ClientError } from '../../utility/errors/hdbError.js';
55
import type { Id } from '../../resources/ResourceInterface.ts';
6+
import { RocksDatabase } from '@harperfast/rocksdb-js';
67

78
const logger = loggerWithTag('HNSW');
89
/**
@@ -636,7 +637,9 @@ export class HierarchicalNavigableSmallWorld {
636637
* @returns
637638
*/
638639
estimateCountAsSort() {
639-
return Math.sqrt(this.indexStore.getStats().entryCount * this.efConstructionSearch);
640+
const count =
641+
this.indexStore instanceof RocksDatabase ? this.indexStore.getKeysCount() : this.indexStore.getStats().entryCount;
642+
return Math.sqrt(count * this.efConstructionSearch);
640643
}
641644

642645
/**

resources/search.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { DirectCondition, Id } from './ResourceInterface.ts';
77
import { RequestTarget } from './RequestTarget.ts';
88
import { lastMetadata } from './RecordEncoder.ts';
99
import { recordAction } from './analytics/write';
10+
import { RocksDatabase } from '@harperfast/rocksdb-js';
1011

1112
// these are ratios/percentages of overall table size
1213
const OPEN_RANGE_ESTIMATE = 0.3;
@@ -1294,7 +1295,7 @@ function estimatedEntryCount(store) {
12941295
const now = Date.now();
12951296
if ((store.estimatedEntryCountExpires || 0) < now) {
12961297
// use getStats for LMDB because it is fast path, otherwise RocksDB can handle fast path on its own
1297-
store.estimatedEntryCount = store.readerCheck ? store.getStats().entryCount : store.getKeysCount();
1298+
store.estimatedEntryCount = store instanceof RocksDatabase ? store.getKeysCount() : store.getStats().entryCount;
12981299
store.estimatedEntryCountExpires = now + 10000;
12991300
}
13001301
return store.estimatedEntryCount;

0 commit comments

Comments
 (0)