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.db4ounit.common.defragment;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026: import com.db4o.config.*;
027: import com.db4o.defragment.*;
028: import com.db4o.ext.*;
029: import com.db4o.internal.*;
030: import com.db4o.query.*;
031:
032: import db4ounit.*;
033:
034: public class SlotDefragmentFixture {
035:
036: public static final String PRIMITIVE_FIELDNAME = "_id";
037: public static final String WRAPPER_FIELDNAME = "_wrapper";
038: public static final String TYPEDOBJECT_FIELDNAME = "_next";
039:
040: public static class Data {
041:
042: public int _id;
043: public Integer _wrapper;
044: public Data _next;
045:
046: public Data(int id, Data next) {
047: _id = id;
048: _wrapper = new Integer(id);
049: _next = next;
050: }
051: }
052:
053: public static final int VALUE = 42;
054:
055: public static DefragmentConfig defragConfig(
056: boolean forceBackupDelete) {
057: DefragmentConfig defragConfig = new DefragmentConfig(
058: SlotDefragmentTestConstants.FILENAME,
059: SlotDefragmentTestConstants.BACKUPFILENAME);
060: defragConfig.forceBackupDelete(forceBackupDelete);
061: defragConfig.db4oConfig(db4oConfig());
062: return defragConfig;
063: }
064:
065: public static Configuration db4oConfig() {
066: Configuration db4oConfig = Db4o.newConfiguration();
067: db4oConfig.reflectWith(Platform4.reflectorForType(Data.class));
068: return db4oConfig;
069: }
070:
071: public static void createFile(String fileName) {
072: ObjectContainer db = Db4o.openFile(db4oConfig(), fileName);
073: Data data = null;
074: for (int value = VALUE - 1; value <= VALUE + 1; value++) {
075: data = new Data(value, data);
076: db.set(data);
077: }
078: db.close();
079: }
080:
081: public static void forceIndex() {
082: Configuration config = db4oConfig();
083: config.objectClass(Data.class).objectField(PRIMITIVE_FIELDNAME)
084: .indexed(true);
085: config.objectClass(Data.class).objectField(WRAPPER_FIELDNAME)
086: .indexed(true);
087: config.objectClass(Data.class).objectField(
088: TYPEDOBJECT_FIELDNAME).indexed(true);
089: ObjectContainer db = Db4o.openFile(config,
090: SlotDefragmentTestConstants.FILENAME);
091: Assert.isTrue(db.ext().storedClass(Data.class).storedField(
092: PRIMITIVE_FIELDNAME, Integer.TYPE).hasIndex());
093: Assert.isTrue(db.ext().storedClass(Data.class).storedField(
094: WRAPPER_FIELDNAME, Integer.class).hasIndex());
095: Assert.isTrue(db.ext().storedClass(Data.class).storedField(
096: TYPEDOBJECT_FIELDNAME, Data.class).hasIndex());
097: db.close();
098: }
099:
100: public static void assertIndex(String fieldName) throws IOException {
101: forceIndex();
102: DefragmentConfig defragConfig = new DefragmentConfig(
103: SlotDefragmentTestConstants.FILENAME,
104: SlotDefragmentTestConstants.BACKUPFILENAME);
105: defragConfig.db4oConfig(db4oConfig());
106: Defragment.defrag(defragConfig);
107: ObjectContainer db = Db4o.openFile(db4oConfig(),
108: SlotDefragmentTestConstants.FILENAME);
109: Query query = db.query();
110: query.constrain(Data.class);
111: query.descend(fieldName).constrain(new Integer(VALUE));
112: ObjectSet result = query.execute();
113: Assert.areEqual(1, result.size());
114: db.close();
115: }
116:
117: public static void assertDataClassKnown(boolean expected) {
118: ObjectContainer db = Db4o.openFile(db4oConfig(),
119: SlotDefragmentTestConstants.FILENAME);
120: try {
121: StoredClass storedClass = db.ext().storedClass(Data.class);
122: if (expected) {
123: Assert.isNotNull(storedClass);
124: } else {
125: Assert.isNull(storedClass);
126: }
127: } finally {
128: db.close();
129: }
130: }
131: }
|