Skip to content

Commit 5a58f45

Browse files
authored
Merge pull request #120 from powersync-ja/compact-specific-buckets
Option to compact specific buckets
2 parents dc04949 + 1e50d6b commit 5a58f45

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ const HEAP_LIMIT = v8.getHeapStatistics().heap_size_limit;
2121
const COMPACT_MEMORY_LIMIT_MB = Math.min(HEAP_LIMIT / 1024 / 1024 - 128, 1024);
2222

2323
export function registerCompactAction(program: Command) {
24-
const compactCommand = program.command(COMMAND_NAME);
24+
const compactCommand = program
25+
.command(COMMAND_NAME)
26+
.option(`-b, --buckets [buckets]`, 'Bucket or bucket definition name (optional, comma-separate multiple names)');
2527

2628
wrapConfigCommand(compactCommand);
2729

2830
return compactCommand.description('Compact storage').action(async (options) => {
29-
logger.info('Compacting storage...');
31+
const buckets = options.buckets?.split(',');
32+
if (buckets != null) {
33+
logger.info('Compacting storage for all buckets...');
34+
} else {
35+
logger.info(`Compacting storage for ${buckets.join(', ')}...`);
36+
}
3037
const runnerConfig = extractRunnerOptions(options);
3138
const configuration = await utils.loadConfig(runnerConfig);
3239
logger.info('Successfully loaded configuration...');
@@ -46,7 +53,7 @@ export function registerCompactAction(program: Command) {
4653
}
4754
using p = bucketStorage.getInstance(active);
4855
logger.info('Performing compaction...');
49-
await p.compact({ memoryLimitMB: COMPACT_MEMORY_LIMIT_MB });
56+
await p.compact({ memoryLimitMB: COMPACT_MEMORY_LIMIT_MB, compactBuckets: buckets });
5057
logger.info('Successfully compacted storage.');
5158
} catch (e) {
5259
logger.error(`Failed to compact: ${e.toString()}`);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@ export interface CompactOptions {
472472
* If specified, compact only the specific buckets.
473473
*
474474
* If not specified, compacts all buckets.
475+
*
476+
* These can be individual bucket names, or bucket definition names.
475477
*/
476478
compactBuckets?: string[];
477479
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,33 @@ export class MongoCompactor {
9494

9595
let currentState: CurrentBucketState | null = null;
9696

97+
let bucketLower: string | MinKey;
98+
let bucketUpper: string | MaxKey;
99+
100+
if (bucket == null) {
101+
bucketLower = new MinKey();
102+
bucketUpper = new MaxKey();
103+
} else if (bucket.includes('[')) {
104+
// Exact bucket name
105+
bucketLower = bucket;
106+
bucketUpper = bucket;
107+
} else {
108+
// Bucket definition name
109+
bucketLower = `${bucket}[`;
110+
bucketUpper = `${bucket}[\uFFFF`;
111+
}
112+
97113
// Constant lower bound
98114
const lowerBound: BucketDataKey = {
99115
g: this.group_id,
100-
b: bucket ?? (new MinKey() as any),
116+
b: bucketLower as string,
101117
o: new MinKey() as any
102118
};
103119

104120
// Upper bound is adjusted for each batch
105121
let upperBound: BucketDataKey = {
106122
g: this.group_id,
107-
b: bucket ?? (new MaxKey() as any),
123+
b: bucketUpper as string,
108124
o: new MaxKey() as any
109125
};
110126

0 commit comments

Comments
 (0)