1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: VerifyConfig.java,v 12.7 2008/01/17 05:04:53 mjc Exp $
7 */
8
9package com.sleepycat.db;
10
11import com.sleepycat.db.internal.DbConstants;
12
13/**
14Specifies the attributes of a verification operation.
15*/
16public class VerifyConfig {
17    /**
18    Default configuration used if null is passed to
19    {@link com.sleepycat.db.Database#verify Database.verify}.
20    */
21    public static final VerifyConfig DEFAULT = new VerifyConfig();
22
23    /* package */
24    static VerifyConfig checkNull(VerifyConfig config) {
25        return (config == null) ? DEFAULT : config;
26    }
27
28    private boolean aggressive = false;
29    private boolean noOrderCheck = false;
30    private boolean orderCheckOnly = false;
31    private boolean salvage = false;
32    private boolean printable = false;
33
34    /**
35    An instance created using the default constructor is initialized
36    with the system's default settings.
37    */
38    public VerifyConfig() {
39    }
40
41    /**
42    Configure {@link com.sleepycat.db.Database#verify Database.verify} to output <b>all</b> the
43    key/data pairs in the file that can be found.
44    <p>
45    By default, {@link com.sleepycat.db.Database#verify Database.verify} does not assume corruption.
46    For example, if a key/data pair on a page is marked as deleted, it
47    is not then written to the output file.  When {@link com.sleepycat.db.Database#verify Database.verify} is configured with this method, corruption is assumed, and
48    any key/data pair that can be found is written.  In this case,
49    key/data pairs that are corrupted or have been deleted may appear
50    in the output (even if the file being salvaged is in no way
51    corrupt), and the output will almost certainly require editing
52    before being loaded into a database.
53    <p>
54    @param aggressive
55    If true, configure {@link com.sleepycat.db.Database#verify Database.verify} to output <b>all</b>
56    the key/data pairs in the file that can be found.
57    */
58    public void setAggressive(final boolean aggressive) {
59        this.aggressive = aggressive;
60    }
61
62    /**
63Return true if the     {@link com.sleepycat.db.Database#verify Database.verify} is configured to output
64    <b>all</b> the key/data pairs in the file that can be found.
65<p>
66This method may be called at any time during the life of the application.
67<p>
68@return
69True if the     {@link com.sleepycat.db.Database#verify Database.verify} is configured to output
70    <b>all</b> the key/data pairs in the file that can be found.
71    */
72    public boolean getAggressive() {
73        return aggressive;
74    }
75
76    /**
77    Configure {@link com.sleepycat.db.Database#verify Database.verify} to skip the database checks for
78    btree and duplicate sort order and for hashing.
79    <p>
80    {@link com.sleepycat.db.Database#verify Database.verify} normally verifies that btree keys and
81    duplicate items are correctly sorted, and hash keys are correctly
82    hashed.  If the file being verified contains multiple databases
83    using differing sorting or hashing algorithms, some of them must
84    necessarily fail database verification because only one sort order
85    or hash function can be specified before {@link com.sleepycat.db.Database#verify Database.verify}
86    is called.  To verify files with multiple databases having differing
87    sorting orders or hashing functions, first perform verification of
88    the file as a whole using {@link com.sleepycat.db.Database#verify Database.verify} configured with
89    {@link com.sleepycat.db.VerifyConfig#setNoOrderCheck VerifyConfig.setNoOrderCheck}, and then individually verify
90    the sort order and hashing function for each database in the file
91    using 4_link(Database, verify) configured with {@link com.sleepycat.db.VerifyConfig#setOrderCheckOnly VerifyConfig.setOrderCheckOnly}.
92    <p>
93    @param noOrderCheck
94    If true, configure {@link com.sleepycat.db.Database#verify Database.verify} to skip the database
95    checks for btree and duplicate sort order and for hashing.
96    */
97    public void setNoOrderCheck(final boolean noOrderCheck) {
98        this.noOrderCheck = noOrderCheck;
99    }
100
101    /**
102Return true if the     {@link com.sleepycat.db.Database#verify Database.verify} is configured to skip the
103    database checks for btree and duplicate sort order and for hashing.
104<p>
105This method may be called at any time during the life of the application.
106<p>
107@return
108True if the     {@link com.sleepycat.db.Database#verify Database.verify} is configured to skip the
109    database checks for btree and duplicate sort order and for hashing.
110    */
111    public boolean getNoOrderCheck() {
112        return printable;
113    }
114
115    /**
116    Configure {@link com.sleepycat.db.Database#verify Database.verify} to do database checks for btree
117    and duplicate sort order and for hashing, skipped by verification
118    operations configured by {@link com.sleepycat.db.VerifyConfig#setNoOrderCheck VerifyConfig.setNoOrderCheck}.
119    <p>
120    When this flag is specified, a database name must be specified to
121    {@link com.sleepycat.db.Database#verify Database.verify}, indicating the database in the physical
122    file which is to be checked.
123    <p>
124    This configuration is only safe to use on databases that have
125    already successfully been verified with {@link com.sleepycat.db.VerifyConfig#setNoOrderCheck VerifyConfig.setNoOrderCheck} configured.
126    <p>
127    @param orderCheckOnly
128    If true, configure {@link com.sleepycat.db.Database#verify Database.verify} to do database checks
129    for btree and duplicate sort order and for hashing, skipped by
130    verification operations configured by {@link com.sleepycat.db.VerifyConfig#setNoOrderCheck VerifyConfig.setNoOrderCheck}.
131    */
132    public void setOrderCheckOnly(final boolean orderCheckOnly) {
133        this.orderCheckOnly = orderCheckOnly;
134    }
135
136    /**
137Return true if the     {@link com.sleepycat.db.Database#verify Database.verify} is configured to do database
138    checks for btree and duplicate sort order and for hashing, skipped
139    by verification operations configured by {@link com.sleepycat.db.VerifyConfig#setNoOrderCheck VerifyConfig.setNoOrderCheck}.
140<p>
141This method may be called at any time during the life of the application.
142<p>
143@return
144True if the     {@link com.sleepycat.db.Database#verify Database.verify} is configured to do database
145    checks for btree and duplicate sort order and for hashing, skipped
146    by verification operations configured by {@link com.sleepycat.db.VerifyConfig#setNoOrderCheck VerifyConfig.setNoOrderCheck}.
147    */
148    public boolean getOrderCheckOnly() {
149        return orderCheckOnly;
150    }
151
152    /**
153    Configure {@link com.sleepycat.db.Database#verify Database.verify} to use printing characters to
154    where possible.
155    <p>
156    This method is only meaningful when combined with
157    {@link com.sleepycat.db.VerifyConfig#setSalvage VerifyConfig.setSalvage}.
158    <p>
159    This configuration permits users to use standard text editors and
160    tools to modify the contents of databases or selectively remove data
161    from salvager output.
162    <p>
163    Note: different systems may have different notions about what characters
164    are considered <em>printing characters</em>, and databases dumped in
165    this manner may be less portable to external systems.
166    <p>
167    @param printable
168    If true, configure {@link com.sleepycat.db.Database#verify Database.verify} to use printing
169    characters to where possible.
170    */
171    public void setPrintable(final boolean printable) {
172        this.printable = printable;
173    }
174
175    /**
176Return true if the     {@link com.sleepycat.db.Database#verify Database.verify} is configured to use printing
177    characters to where possible.
178<p>
179This method may be called at any time during the life of the application.
180<p>
181@return
182True if the     {@link com.sleepycat.db.Database#verify Database.verify} is configured to use printing
183    characters to where possible.
184    */
185    public boolean getPrintable() {
186        return printable;
187    }
188
189    /**
190    Configure {@link com.sleepycat.db.Database#verify Database.verify} to write the key/data pairs from
191    all databases in the file to the file stream named by the outfile
192    parameter.
193    <p>
194    The output format is the same as that specified for the db_dump
195    utility, and can be used as input for the db_load utility.
196    <p>
197    Because the key/data pairs are output in page order as opposed to the
198    sort order used by db_dump, using {@link com.sleepycat.db.Database#verify Database.verify} to dump
199    key/data pairs normally produces less than optimal loads for Btree
200    databases.
201    <p>
202    @param salvage
203    If true, configure {@link com.sleepycat.db.Database#verify Database.verify} to write the key/data
204    pairs from all databases in the file to the file stream named by the
205    outfile parameter.
206    */
207    public void setSalvage(final boolean salvage) {
208        this.salvage = salvage;
209    }
210
211    /**
212Return true if the     {@link com.sleepycat.db.Database#verify Database.verify} is configured to write the
213    key/data pairs from all databases in the file to the file stream
214    named by the outfile parameter..
215<p>
216This method may be called at any time during the life of the application.
217<p>
218@return
219True if the     {@link com.sleepycat.db.Database#verify Database.verify} is configured to write the
220    key/data pairs from all databases in the file to the file stream
221    named by the outfile parameter..
222    */
223    public boolean getSalvage() {
224        return salvage;
225    }
226
227    int getFlags() {
228        int flags = 0;
229        flags |= aggressive ? DbConstants.DB_AGGRESSIVE : 0;
230        flags |= noOrderCheck ? DbConstants.DB_NOORDERCHK : 0;
231        flags |= orderCheckOnly ? DbConstants.DB_ORDERCHKONLY : 0;
232        flags |= salvage ? DbConstants.DB_SALVAGE : 0;
233        flags |= printable ? DbConstants.DB_PRINTABLE : 0;
234
235        return flags;
236    }
237}
238