summaryrefslogtreecommitdiffstats
path: root/files/reprepro/snapshots/time_based/tails-compact-reprepro-db
blob: 4992b54cbacee3c1b2515ecdddf7fcc86584597f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/python3

import argparse
import os.path

from bsddb3 import db

def print_stats(path, d):
    d.stat_print()
    print()

parser = argparse.ArgumentParser()
parser.add_argument('--path', required=True,
                    help="Path to the file that contains the database")
parser.add_argument('--db', required=True,
                    help="Name of the database (for example: references)")
parser.add_argument('--compact-fillpercent', default=90, type=int,
                    help="Any page in the database below this percentage full is considered for compaction")
args = parser.parse_args()

size_before = os.path.getsize(args.path)
print("Before compacting: {size} bytes".format(size=size_before))

d = db.DB()
d.open(args.path, dbname=args.db)
for i in range(0, 10):
    d.compact(compact_fillpercent=args.compact_fillpercent,
              flags=db.DB_FREE_SPACE)
    print("After pass {n}: {size} bytes"
          .format(n=i, size=os.path.getsize(args.path)))
d.close()

size_after = os.path.getsize(args.path)
print("⇒ saved {size} bytes ({percent}%)"
      .format(size=size_before - size_after,
              percent=int(100*(size_before - size_after)/size_before)
      ))