diff --git a/fluffy/database/content_db.nim b/fluffy/database/content_db.nim index 7c49d6137..f8cc7a25e 100644 --- a/fluffy/database/content_db.nim +++ b/fluffy/database/content_db.nim @@ -359,6 +359,13 @@ proc deleteContentOutOfRadius*(db: ContentDB, localId: UInt256, radius: UInt256) "SQL query OK" ) +proc reclaimAndTruncate*(db: ContentDB) = + notice "Reclaiming unused pages" + db.reclaimSpace() + if db.manualCheckpoint: + notice "Truncating WAL file" + db.backend.checkpoint(SqStoreCheckpointKind.truncate) + proc forcePrune*(db: ContentDB, localId: UInt256, radius: UInt256) = ## Force prune the database to a statically set radius. This will also run ## the reclaimSpace (vacuum) to free unused pages. As side effect this will @@ -369,11 +376,7 @@ proc forcePrune*(db: ContentDB, localId: UInt256, radius: UInt256) = ## skipped. notice "Starting the pruning of content" db.deleteContentOutOfRadius(localId, radius) - notice "Reclaiming unused pages" - db.reclaimSpace() - if db.manualCheckpoint: - notice "Truncating WAL file" - db.backend.checkpoint(SqStoreCheckpointKind.truncate) + db.reclaimAndTruncate() notice "Finished database pruning" proc put*( diff --git a/fluffy/fluffy.nim b/fluffy/fluffy.nim index 7439ee1f0..1d03f6922 100644 --- a/fluffy/fluffy.nim +++ b/fluffy/fluffy.nim @@ -167,7 +167,9 @@ proc run(config: PortalConf) {.raises: [CatchableError].} = # TODO I: Perhaps we want to add an offset to counter the latter. # TODO II: Perhaps for dynamical radius, we want to also apply the vacuum # without the forcePrune flag and purely by checking the amount of free - # space versus the pruning fraction. + # space versus the pruning fraction. The problem with this is that the + # vacuum will temporarily double the space usage (WAL + DB) and thus to do + # this automatically without user requesting it could be dangerous. # TODO III: Adding Radius metadata to the db could be yet another way to # decide whether or not to force prune, instead of this flag. db.forcePrune(d.localNode.id, radius) diff --git a/fluffy/tools/fcli_db.nim b/fluffy/tools/fcli_db.nim index c0cac3348..0d3f3a0e9 100644 --- a/fluffy/tools/fcli_db.nim +++ b/fluffy/tools/fcli_db.nim @@ -52,7 +52,11 @@ type name: "content-amount" .}: uint64 of DbCmd.prune: - discard + reclaimOnly* {. + desc: "Only reclaim space from the database, don't actually prune it", + defaultValue: true, + name: "reclaim-only" + .}: bool of DbCmd.validate: discard @@ -117,6 +121,19 @@ proc cmdBench(conf: DbConf) = printTimers(timers) +proc cmdPrune(conf: DbConf) = + if conf.reclaimOnly: + let db = ContentDB.new( + conf.databaseDir.string, + storageCapacity = 1_000_000, # Doesn't matter if only space reclaiming is done + manualCheckpoint = true, + ) + + db.reclaimAndTruncate() + else: + notice "Functionality not yet implemented" + quit QuitSuccess + proc controlCHook() {.noconv.} = notice "Shutting down after having received SIGINT." quit QuitSuccess @@ -138,6 +155,6 @@ when isMainModule: of DbCmd.generate: cmdGenerate(conf) of DbCmd.prune: - notice "Functionality not yet implemented" + cmdPrune(conf) of DbCmd.validate: notice "Functionality not yet implemented"