1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2009 Oracle.  All rights reserved.
5 *
6 */
7using System;
8using System.Collections.Generic;
9using System.Text;
10using BerkeleyDB.Internal;
11
12namespace BerkeleyDB {
13    /// <summary>
14    /// A class to represent configuration settings for
15    /// <see cref="BTreeDatabase.Compact"/> and
16    /// <see cref="RecnoDatabase.Compact"/>.
17    /// </summary>
18    public class CompactConfig {
19        private DB_COMPACT cdata;
20
21        private bool fillPctSet;
22        private uint fillpct;
23        private bool pgsSet;
24        private uint pgs;
25        private bool tmoutSet;
26        private uint tmout;
27        /// <summary>
28        /// Return the database key marking the end of the compaction operation
29        /// in a Btree or Recno database. This is generally the first key of the
30        /// page where the operation stopped.
31        /// </summary>
32        public bool returnEnd;
33        /// <summary>
34        /// If non-null, the starting point for compaction. Compaction will
35        /// start at the smallest key greater than or equal to
36        /// <paramref name="start"/>. If null, compaction will start at the
37        /// beginning of the database.
38        /// </summary>
39        public DatabaseEntry start;
40        /// <summary>
41        /// If non-null, the stopping point for compaction. Compaction will stop
42        /// at the page with the smallest key greater than
43        /// <paramref name="stop"/>. If null, compaction will stop at the end of
44        /// the database.
45        /// </summary>
46        public DatabaseEntry stop;
47        /// <summary>
48        /// If true, return pages to the filesystem when possible. If false,
49        /// pages emptied as a result of compaction will be placed on the free
50        /// list for re-use, but never returned to the filesystem.
51        /// </summary>
52        /// <remarks>
53        /// Note that only pages at the end of a file can be returned to the
54        /// filesystem. Because of the one-pass nature of the compaction
55        /// algorithm, any unemptied page near the end of the file inhibits
56        /// returning pages to the file system. A repeated call to
57        /// <see cref="BTreeDatabase.Compact"/> or
58        /// <see cref="RecnoDatabase.Compact"/> with a low
59        /// <see cref="FillPercentage"/> may be used to return pages in this
60        /// case.
61        /// </remarks>
62        public bool TruncatePages;
63
64        static internal DB_COMPACT getDB_COMPACT(CompactConfig compactData) {
65            if (compactData == null)
66                return null;
67
68            if (compactData.cdata == null)
69                compactData.doCompaction();
70            return compactData.cdata;
71        }
72
73        /// <summary>
74        /// Create a new CompactConfig object
75        /// </summary>
76        public CompactConfig() { }
77
78        /// <summary>
79        /// If non-zero, this provides the goal for filling pages, specified as
80        /// a percentage between 1 and 100. Any page not at or above this
81        /// percentage full will be considered for compaction. The default
82        /// behavior is to consider every page for compaction, regardless of its
83        /// page fill percentage.
84        /// </summary>
85        public uint FillPercentage {
86            get { return fillpct; }
87            set {
88                fillpct = value;
89                fillPctSet = true;
90            }
91        }
92        /// <summary>
93        /// If non-zero, compaction will complete after the specified number of
94        /// pages have been freed.
95        /// </summary>
96        public uint Pages {
97            get { return pgs; }
98            set {
99                pgs = value;
100                pgsSet = true;
101            }
102        }
103        /// <summary>
104        /// If non-zero, and no <see cref="Transaction"/> is specified, this
105        /// parameter identifies the lock timeout used for implicit
106        /// transactions, in microseconds.
107        /// </summary>
108        public uint Timeout {
109            get { return tmout; }
110            set {
111                tmout = value;
112                tmoutSet = true;
113            }
114        }
115
116        private void doCompaction() {
117            cdata = new DB_COMPACT();
118
119            if (fillPctSet)
120                cdata.compact_fillpercent = fillpct;
121            if (pgsSet)
122                cdata.compact_pages = pgs;
123            if (tmoutSet)
124                cdata.compact_timeout = tmout;
125        }
126
127        internal uint flags {
128            get {
129                uint ret = 0;
130                ret |= TruncatePages ? DbConstants.DB_FREE_SPACE : 0;
131                return ret;
132            }
133        }
134    }
135}