Skip to content

Commit da69792

Browse files
authored
Merge pull request #47 from powersync-ja/compact-fixes
Compact fixes
2 parents 5cbec19 + 00da768 commit da69792

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

.changeset/purple-moles-shave.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@powersync/service-core': patch
3+
'@powersync/service-image': patch
4+
---
5+
6+
Fix compact command to use the correct database

packages/service-core/src/entry/commands/compact-action.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { Command } from 'commander';
22

33
import { logger } from '@powersync/lib-services-framework';
44
import * as v8 from 'v8';
5-
import { mongo } from '../../db/db-index.js';
6-
import { MongoBucketStorage, PowerSyncMongo } from '../../storage/storage-index.js';
5+
import { createPowerSyncMongo, MongoBucketStorage } from '../../storage/storage-index.js';
76
import { loadConfig } from '../../util/config.js';
87
import { extractRunnerOptions, wrapConfigCommand } from './config-command.js';
98

@@ -31,10 +30,10 @@ export function registerCompactAction(program: Command) {
3130

3231
const config = await loadConfig(runnerConfig);
3332
const { storage } = config;
34-
const client = mongo.createMongoClient(storage);
33+
const psdb = createPowerSyncMongo(storage);
34+
const client = psdb.client;
3535
await client.connect();
3636
try {
37-
const psdb = new PowerSyncMongo(client);
3837
const bucketStorage = new MongoBucketStorage(psdb, { slot_name_prefix: config.slot_name_prefix });
3938
const active = await bucketStorage.getActiveSyncRules();
4039
if (active == null) {

packages/service-core/src/storage/BucketStorage.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,11 @@ export interface CompactOptions {
426426
* not be compacted, to avoid invalidating checkpoints in use.
427427
*/
428428
maxOpId?: bigint;
429+
430+
/**
431+
* If specified, compact only the specific buckets.
432+
*
433+
* If not specified, compacts all buckets.
434+
*/
435+
compactBuckets?: string[];
429436
}

packages/service-core/src/storage/mongo/MongoCompactor.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ export class MongoCompactor {
5555
private moveBatchQueryLimit: number;
5656
private clearBatchLimit: number;
5757
private maxOpId: bigint | undefined;
58+
private buckets: string[] | undefined;
5859

5960
constructor(private db: PowerSyncMongo, private group_id: number, options?: MongoCompactOptions) {
6061
this.idLimitBytes = (options?.memoryLimitMB ?? DEFAULT_MEMORY_LIMIT_MB) * 1024 * 1024;
6162
this.moveBatchLimit = options?.moveBatchLimit ?? DEFAULT_MOVE_BATCH_LIMIT;
6263
this.moveBatchQueryLimit = options?.moveBatchQueryLimit ?? DEFAULT_MOVE_BATCH_QUERY_LIMIT;
6364
this.clearBatchLimit = options?.clearBatchLimit ?? DEFAULT_CLEAR_BATCH_LIMIT;
6465
this.maxOpId = options?.maxOpId;
66+
this.buckets = options?.compactBuckets;
6567
}
6668

6769
/**
@@ -70,21 +72,34 @@ export class MongoCompactor {
7072
* See /docs/compacting-operations.md for details.
7173
*/
7274
async compact() {
75+
if (this.buckets) {
76+
for (let bucket of this.buckets) {
77+
// We can make this more efficient later on by iterating
78+
// through the buckets in a single query.
79+
// That makes batching more tricky, so we leave for later.
80+
await this.compactInternal(bucket);
81+
}
82+
} else {
83+
await this.compactInternal(undefined);
84+
}
85+
}
86+
87+
async compactInternal(bucket: string | undefined) {
7388
const idLimitBytes = this.idLimitBytes;
7489

7590
let currentState: CurrentBucketState | null = null;
7691

7792
// Constant lower bound
7893
const lowerBound: BucketDataKey = {
7994
g: this.group_id,
80-
b: new MinKey() as any,
95+
b: bucket ?? (new MinKey() as any),
8196
o: new MinKey() as any
8297
};
8398

8499
// Upper bound is adjusted for each batch
85100
let upperBound: BucketDataKey = {
86101
g: this.group_id,
87-
b: new MaxKey() as any,
102+
b: bucket ?? (new MaxKey() as any),
88103
o: new MaxKey() as any
89104
};
90105

0 commit comments

Comments
 (0)