001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.defragment;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.ext.*;
026: import com.db4o.internal.*;
027:
028: /**
029: * Configuration for a defragmentation run.
030: *
031: * @see Defragment
032: */
033: public class DefragmentConfig {
034:
035: public static final boolean DEBUG = false;
036:
037: public final static String BACKUP_SUFFIX = "backup";
038:
039: private String _origPath;
040: private String _backupPath;
041: private String _tempPath;
042: private ContextIDMapping _mapping;
043: private Configuration _config;
044:
045: private StoredClassFilter _storedClassFilter = null;
046: private boolean _forceBackupDelete = false;
047:
048: private int _objectCommitFrequency;
049:
050: /**
051: * Creates a configuration for a defragmentation run. The backup and mapping
052: * file paths are generated from the original path by appending the default
053: * suffixes. All properties other than the provided paths are set to FALSE
054: * by default.
055: *
056: * @param origPath The path to the file to be defragmented. Must exist and must be
057: * a valid yap file.
058: */
059: public DefragmentConfig(String origPath) {
060: this (origPath, origPath + "." + BACKUP_SUFFIX);
061: }
062:
063: /**
064: * Creates a configuration for a defragmentation run with in-memory mapping.
065: * All properties other than the provided paths are set to FALSE by default.
066: *
067: * @param origPath The path to the file to be defragmented. Must exist and must be
068: * a valid yap file.
069: * @param backupPath The path to the backup of the original file. No file should
070: * exist at this position, otherwise it will be OVERWRITTEN if forceBackupDelete()
071: * is set to true!
072: */
073: public DefragmentConfig(String origPath, String backupPath) {
074: this (origPath, backupPath, new TreeIDMapping());
075: }
076:
077: /**
078: * Creates a configuration for a defragmentation run. All properties other
079: * than the provided paths are set to FALSE by default.
080: *
081: * @param origPath The path to the file to be defragmented. Must exist and must be
082: * a valid yap file.
083: * @param backupPath The path to the backup of the original file. No file should
084: * exist at this position, otherwise it will be OVERWRITTEN if forceBackupDelete()
085: * is set to true!
086: * @param mapping The intermediate mapping used internally.
087: */
088: public DefragmentConfig(String origPath, String backupPath,
089: ContextIDMapping mapping) {
090: _origPath = origPath;
091: _backupPath = backupPath;
092: _mapping = mapping;
093: }
094:
095: /**
096: * @return The path to the file to be defragmented.
097: */
098: public String origPath() {
099: return _origPath;
100: }
101:
102: /**
103: * @return The path to the backup of the original file.
104: */
105: public String backupPath() {
106: return _backupPath;
107: }
108:
109: /**
110: * @return The intermediate mapping used internally. For internal use only.
111: */
112: public ContextIDMapping mapping() {
113: return _mapping;
114: }
115:
116: /**
117: * @return The {@link StoredClassFilter} used to select stored class extents to
118: * be included into the defragmented file.
119: */
120: public StoredClassFilter storedClassFilter() {
121: return (_storedClassFilter == null ? NULLFILTER
122: : _storedClassFilter);
123: }
124:
125: /**
126: * @param storedClassFilter The {@link StoredClassFilter} used to select stored class extents to
127: * be included into the defragmented file.
128: */
129: public void storedClassFilter(StoredClassFilter storedClassFilter) {
130: _storedClassFilter = storedClassFilter;
131: }
132:
133: /**
134: * @return true, if an existing backup file should be deleted, false otherwise.
135: */
136: public boolean forceBackupDelete() {
137: return _forceBackupDelete;
138: }
139:
140: /**
141: * @param forceBackupDelete true, if an existing backup file should be deleted, false otherwise.
142: */
143: public void forceBackupDelete(boolean forceBackupDelete) {
144: _forceBackupDelete = forceBackupDelete;
145: }
146:
147: /**
148: * @return The db4o {@link com.db4o.config.Configuration Configuration} to be applied
149: * during the defragment process.
150: */
151: public Configuration db4oConfig() {
152: if (_config == null) {
153: _config = vanillaDb4oConfig(1);
154: }
155: return _config;
156: }
157:
158: /**
159: * @param config The db4o {@link com.db4o.config.Configuration Configuration} to be applied
160: * during the defragment process.
161: */
162: public void db4oConfig(Configuration config) {
163: _config = config;
164: }
165:
166: public int objectCommitFrequency() {
167: return _objectCommitFrequency;
168: }
169:
170: /**
171: * @param objectCommitFrequency The number of processed object (slots) that should trigger an
172: * intermediate commit of the target file. Default: 0, meaning: never.
173: */
174: public void objectCommitFrequency(int objectCommitFrequency) {
175: _objectCommitFrequency = objectCommitFrequency;
176: }
177:
178: /**
179: * Instruct the defragment process to upgrade the source file to the current db4o
180: * version prior to defragmenting it. Use this option if your source file has been created
181: * with an older db4o version than the one you are using.
182: * @param tempPath The location for an intermediate, upgraded version of the source file.
183: */
184: public void upgradeFile(String tempPath) {
185: _tempPath = tempPath;
186: }
187:
188: public boolean fileNeedsUpgrade() {
189: return _tempPath != null;
190: }
191:
192: public String tempPath() {
193: return (_tempPath != null ? _tempPath : _backupPath);
194: }
195:
196: public int blockSize() {
197: return ((Config4Impl) db4oConfig()).blockSize();
198: }
199:
200: protected static class NullFilter implements StoredClassFilter {
201: public boolean accept(StoredClass storedClass) {
202: return true;
203: }
204: }
205:
206: private final static StoredClassFilter NULLFILTER = new NullFilter();
207:
208: public static Configuration vanillaDb4oConfig(int blockSize) {
209: Configuration config = Db4o.newConfiguration();
210: config.weakReferences(false);
211: config.blockSize(blockSize);
212: return config;
213: }
214:
215: public Configuration clonedDb4oConfig() {
216: return (Configuration) ((Config4Impl) db4oConfig())
217: .deepClone(null);
218: }
219:
220: }
|