xref: /3.0.3-GA/ep-engine/management/cbcompact (revision 220e2365)
1#!/usr/bin/env python
2
3# Example Usage for local host..
4# install/bin/cbcompact 127.0.0.1:12000 compact -b beer-sample 799
5#   where 12000 is memcached port and 799 is vbucket id
6
7import clitool
8from time import sleep
9import sys
10import mc_bin_client
11
12def cmd(f):
13    """Decorate a function with code to authenticate based on 1-2
14    arguments past the normal number of arguments."""
15
16    def g(*args, **kwargs):
17        mc = args[0]
18        vbucket = int(args[1])
19        n = f.func_code.co_argcount
20        if len(args) > n:
21            print "Too many args, given %s, but expected a maximum of %s"\
22                    % (list(args[1:]), n - 1)
23            sys.exit(1)
24
25        bucket = kwargs.get('bucketName', None)
26        password = kwargs.get('password', None) or ""
27        purgeBeforeTs = long(kwargs.get('purgeBeforeTs', None) or 0)
28        purgeBeforeSeq = long(kwargs.get('purgeBeforeSeq', None) or 0)
29        dropDeletes = int(kwargs.get('dropDeletes', None) or 0)
30
31        if bucket:
32            try:
33                mc.sasl_auth_plain(bucket, password)
34            except mc_bin_client.MemcachedError:
35                print "Authentication error for %s" % bucket
36                sys.exit(1)
37
38        f(mc, vbucket, purgeBeforeTs, purgeBeforeSeq, dropDeletes)
39    return g
40
41@cmd
42def compact(mc, vbucket, purgeBeforeTs, purgeBeforeSeq, dropDeletes):
43     try:
44        return mc.compact_db(vbucket, purgeBeforeTs, purgeBeforeSeq,
45                             dropDeletes)
46     except:
47         print "Unable to compact '%d %d %d %d' in requested engine."\
48             % (vbucket, purgeBeforeTs, purgeBeforeSeq, dropDeletes)
49
50def main():
51    c = clitool.CliTool()
52
53    c.addCommand('compact', compact, 'compact vbucketid')
54    c.addOption('-b', 'bucketName',
55                'the bucket to get stats from (Default: default)')
56    c.addOption('-p', 'password', 'the password for the bucket if one exists')
57    c.addOption('--purge-before', 'purgeBeforeTs',
58                'purge documents before this timestamp')
59    c.addOption('--purge-only-upto-seq', 'purgeBeforeSeq',
60                'purge only upto this document sequence number')
61    c.addFlag('--dropdeletes', 'dropDeletes', 'drop deleted items')
62
63    c.execute()
64
65if __name__ == '__main__':
66    main()
67