Skip to content

Commit

Permalink
chore: review integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
ABresting committed Oct 9, 2023
1 parent c026cc9 commit 5022d3e
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions waku/waku_archive/retention_policy/retention_policy_size.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,27 @@ method execute*(p: SizeRetentionPolicy,
driver: ArchiveDriver):
Future[RetentionPolicyResult[void]] {.async.} =
## when db size overshoots the database limit, shread 20% of outdated messages

# to get the size of the database, pageCount and PageSize is required
# get page count in "messages" database
var pageCount = (await driver.getPagesCount()).valueOr:
return err("failed to get Pages count: " & $error)

# get page size of database
var pageSizeRes = await driver.getPagesSize()
var pageSize: int64 = int64(pageSizeRes.valueOr(0) div 1024)
let pageSizeRes = await driver.getPagesSize()
let pageSize: int64 = int64(pageSizeRes.valueOr(0) div 1024)

if pageSize == 0:
return err("failed to get Page size: " & pageSizeRes.error)

# database size in megabytes (Mb)
var totalSizeOfDB: float = float(pageSize * pageCount)/1024.0
# keep deleting until the current db size falls within size limit
while true:
# to get the size of the database, pageCount and PageSize is required
# get page count in "messages" database
let pageCount = (await driver.getPagesCount()).valueOr:
return err("failed to get Pages count: " & $error)

# check if current databse size crosses the db size limit
if totalSizeOfDB < p.sizeLimit:
return ok()
# database size in megabytes (Mb)
let totalSizeOfDB: float = float(pageSize * pageCount)/1024.0

if totalSizeOfDB < p.sizeLimit:
break

# keep deleting until the current db size falls within size limit
while totalSizeOfDB > p.sizeLimit:
# to shread/delete messsges, get the total row/message count
let numMessagesRes = await driver.getMessagesCount()
if numMessagesRes.isErr():
Expand All @@ -82,17 +81,5 @@ method execute*(p: SizeRetentionPolicy,
let resVaccum = await driver.performVacuum()
if resVaccum.isErr():
return err("vacuumming failed: " & resVaccum.error)

# get the db size again for the loop condition check
pageCount = (await driver.getPagesCount()).valueOr:
return err("failed to get Pages count: " & $error)

pageSizeRes = await driver.getPagesSize()
pageSize = int64(pageSizeRes.valueOr(0) div 1024)

if pageSize == 0:
return err("failed to get Page size: " & pageSizeRes.error)

totalSizeOfDB = float(pageSize * pageCount)/1024.0

return ok()

0 comments on commit 5022d3e

Please sign in to comment.