1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002,2008 Oracle. All rights reserved. 5 * 6 * $Id: CompactConfig.java,v 12.8 2008/01/17 05:04:53 mjc Exp $ 7 */ 8 9package com.sleepycat.db; 10 11import com.sleepycat.db.internal.DbConstants; 12 13/** 14Configuration for {@link Database#compact} operations. 15*/ 16public class CompactConfig implements Cloneable { 17 /** 18 Default configuration used if null is passed to methods that create a 19 cursor. 20 */ 21 public static final CompactConfig DEFAULT = new CompactConfig(); 22 23 /* package */ 24 static CompactConfig checkNull(CompactConfig config) { 25 return (config == null) ? DEFAULT : config; 26 } 27 28 private int fillpercent = 0; 29 private boolean freeListOnly = false; 30 private boolean freeSpace = false; 31 private int maxPages = 0; 32 private int timeout = 0; 33 34 /** 35 Construct a default configuration object for compact operations. 36 */ 37 public CompactConfig() { 38 } 39 40 /** 41 Set the desired fill percentage. 42 If non-zero, the goal for filling pages, specified as a percentage 43 between 1 and 100. Any page in a Btree or Recno databases not at or 44 above this percentage full will be considered for compaction. The 45 default behavior is to consider every page for compaction, regardless 46 of its page fill percentage. 47 @param fillpercent 48 The desired fill percentage. 49 **/ 50 public void setFillPercent(final int fillpercent) { 51 this.fillpercent = fillpercent; 52 } 53 54 /** 55Return the the desired fill percentage. 56<p> 57This method may be called at any time during the life of the application. 58<p> 59@return 60The the desired fill percentage. 61 */ 62 public int getFillPercent() { 63 return fillpercent; 64 } 65 66 /** 67 Configure whether to skip page compaction, only returning pages 68 to the filesystem that are already free and at the end of the file. 69 This flag must be set if the database is a Hash access method database. 70 @param freeListOnly 71 Whether to skip page compaction 72 **/ 73 public void setFreeListOnly(boolean freeListOnly) { 74 this.freeListOnly = freeListOnly; 75 } 76 77 /** 78Return true if the whether to skip page compaction. 79<p> 80This method may be called at any time during the life of the application. 81<p> 82@return 83True if the whether to skip page compaction. 84 */ 85 public boolean getFreeListOnly() { 86 return freeListOnly; 87 } 88 89 /** 90 Return pages to the filesystem if possible. If this flag is not 91 specified, pages emptied as a result of compaction will be placed on the 92 free list for re-use, but not returned to the filesystem. 93 Note that only pages at the end of the file may be returned. Given the one 94 pass nature of the algorithm if a page near the end of the file is 95 logically near the begining of the btree it will inhibit returning pages to 96 the file system. 97 A second call to the method with a low fillfactor can be used to return 98 pages in such a situation. 99 @param freeSpace 100 Whether to return pages to the filesystem 101 **/ 102 public void setFreeSpace(boolean freeSpace) { 103 this.freeSpace = freeSpace; 104 } 105 106 /** 107Return true if the whether to return pages to the filesystem. 108<p> 109This method may be called at any time during the life of the application. 110<p> 111@return 112True if the whether to return pages to the filesystem. 113 */ 114 public boolean getFreeSpace() { 115 return freeSpace; 116 } 117 118 /** 119 Set the maximum number of pages to free. 120 @param maxPages 121 If non-zero, the call will return after that number of pages have been 122 freed. 123 **/ 124 public void setMaxPages(final int maxPages) { 125 this.maxPages = maxPages; 126 } 127 128 /** 129Return the the maximum number of pages to free. 130<p> 131This method may be called at any time during the life of the application. 132<p> 133@return 134The the maximum number of pages to free. 135 */ 136 public int getMaxPages() { 137 return maxPages; 138 } 139 140 /** 141 Set the lock timeout for implicit transactions. 142 If non-zero, and no transaction parameter was specified to 143 {@link Database#compact}, the lock timeout set for implicit 144 transactions, in microseconds. 145 @param timeout 146 the lock timeout set for implicit transactions, in microseconds. 147 **/ 148 public void setTimeout(final int timeout) { 149 this.timeout = timeout; 150 } 151 152 /** 153Return the the lock timeout set for implicit transactions, in microseconds. 154<p> 155This method may be called at any time during the life of the application. 156<p> 157@return 158The the lock timeout set for implicit transactions, in microseconds. 159 */ 160 public int getTimeout() { 161 return timeout; 162 } 163 164 /* package */ 165 int getFlags() { 166 int flags = 0; 167 flags |= freeListOnly ? DbConstants.DB_FREELIST_ONLY : 0; 168 flags |= freeSpace ? DbConstants.DB_FREE_SPACE : 0; 169 170 return flags; 171 } 172} 173