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.jre11.defragment;
022:
023: import java.io.*;
024: import java.util.*;
025:
026: import com.db4o.*;
027: import com.db4o.config.*;
028: import com.db4o.db4ounit.common.defragment.*;
029: import com.db4o.defragment.*;
030: import com.db4o.internal.*;
031: import com.db4o.query.*;
032:
033: import db4ounit.*;
034:
035: public class SlotDefragmentVectorTestCase implements TestCase {
036:
037: public static class Holder {
038: public Vector _vector;
039:
040: public Holder(Vector vector) {
041: this ._vector = vector;
042: }
043: }
044:
045: public static class Data {
046: public int _id;
047:
048: public Data(int id) {
049: this ._id = id;
050: }
051: }
052:
053: private static final int NUMVECTORS = 10;
054: private static final int NUMENTRIES = 10;
055:
056: public void testVectorDefragment() throws Exception {
057: store();
058: defrag();
059: query();
060: }
061:
062: private void query() {
063: ObjectContainer db = Db4o.openFile(configuration(),
064: SlotDefragmentTestConstants.FILENAME);
065: Query query = db.query();
066: query.constrain(Holder.class);
067: ObjectSet result = query.execute();
068: Assert.areEqual(NUMVECTORS + 1, result.size());
069: db.close();
070: }
071:
072: private Configuration configuration() {
073: Configuration config = Db4o.newConfiguration();
074: config.reflectWith(Platform4.reflectorForType(Data.class));
075: return config;
076: }
077:
078: private void defrag() throws IOException {
079: DefragmentConfig config = new DefragmentConfig(
080: SlotDefragmentTestConstants.FILENAME);
081: config.db4oConfig(configuration());
082: config.forceBackupDelete(true);
083: Defragment.defrag(config);
084: }
085:
086: private void store() {
087: new File(SlotDefragmentTestConstants.FILENAME).delete();
088: ObjectContainer db = Db4o.openFile(configuration(),
089: SlotDefragmentTestConstants.FILENAME);
090: for (int vectorIdx = 0; vectorIdx < NUMVECTORS; vectorIdx++) {
091: Vector vector = new Vector(NUMENTRIES);
092: for (int entryIdx = 0; entryIdx < NUMENTRIES; entryIdx++) {
093: Object obj = (entryIdx % 2 == 0 ? (Object) new Data(
094: entryIdx) : (Object) String.valueOf(entryIdx));
095: vector.addElement(obj);
096: }
097: db.set(new Holder(vector));
098: }
099: db.set(new Holder(new Vector(0)));
100: db.close();
101: }
102: }
|