/* HeliDB -- A simple database for Java, http://www.helidb.org
* Copyright (C) 2008, 2009 Karl Gustafsson
*
* This file is a part of HeliDB.
*
* HeliDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeliDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.helidb.resources.perf;
import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.entityfs.support.log.LogAdapterHolder;
import org.entityfs.util.io.ReadWritableFileAdapter;
import org.helidb.Database;
import org.helidb.backend.DatabaseBackend;
import org.helidb.backend.heap.HeapBackend;
import org.helidb.backend.index.bplus.BPlusTreeIndexBackend;
import org.helidb.impl.simple.SimpleDatabase;
import org.helidb.lang.hasher.StringToBigIntegerHasher;
import org.helidb.lang.serializer.FixedSizeBigIntegerNullSerializer;
import org.helidb.lang.serializer.LongSerializer;
import org.helidb.lang.serializer.StringSerializer;
import org.helidb.test.support.FileSupport;
import org.helidb.util.bplus.BPlusTree;
import org.helidb.util.bplus.FileBackedNodeRepository;
import org.helidb.util.bplus.FixedSizeNodeSizeStrategy;
import org.helidb.util.bplus.LruCacheNodeRepository;
import org.helidb.util.bplus.NodeRepository;
public class SimpleDatabaseHeapWBPlusTreeIndexConfiguration extends AbstractTestConfiguration implements VRSTestConfiguration
{
private final int m_bTreeLruNodeCacheSize;
private final int m_bTreeNodeSize;
private Collection<SimpleDatabase<String, String, Long>> m_databases;
private Map<Integer, File[]> m_dbFiles;
public SimpleDatabaseHeapWBPlusTreeIndexConfiguration(int noOfDatabases, int noOfBaseRecords, int noOfAdditionalRecords, int bTreeLruNodeCacheSize, int bTreeNodeSize)
{
super(noOfDatabases, noOfBaseRecords, noOfAdditionalRecords);
m_bTreeLruNodeCacheSize = bTreeLruNodeCacheSize;
m_bTreeNodeSize = bTreeNodeSize;
}
public String getGraphFileNamePrefix()
{
return "s_h_bpti_" + m_bTreeNodeSize + "_" + m_bTreeLruNodeCacheSize + "_";
}
public String getDatabaseImplementationName()
{
return "SimpleDatabase";
}
public String[] getBackendImplementationNames()
{
return new String[] { "BPlusTreeIndexBackend", "HeapBackend" };
}
public String getAdditionalInfo()
{
return "nd sz: " + m_bTreeNodeSize + (m_bTreeLruNodeCacheSize > 0 ? " cache: " + m_bTreeLruNodeCacheSize + "nds" : "");
}
public Collection<? extends Database<String, String>> getDatabases()
{
return m_databases;
}
public String getHeader()
{
return "Simple database with heap backend with B+ tree index. B+ Tree LRU node cache " + m_bTreeLruNodeCacheSize + " entries. B+ Tree node size " + m_bTreeNodeSize + " bytes";
}
public void setup(LogAdapterHolder lah)
{
final int noOfDatabases = getNoOfDatabases();
m_databases = new ArrayList<SimpleDatabase<String, String, Long>>(noOfDatabases);
m_dbFiles = new HashMap<Integer, File[]>(noOfDatabases);
for (int i = 0; i < noOfDatabases; i++)
{
File f = FileSupport.createTempFile();
File treef = FileSupport.createTempFile();
DatabaseBackend<String, String, Long> b = new HeapBackend<String, String>(new ReadWritableFileAdapter(f), false, StringSerializer.INSTANCE, StringSerializer.INSTANCE, 0, 8192, lah);
// Maximum pointer needed: approx 1,000,000 * node size
// approx: 1,000,000 * 20 = 20,000,000 < 4294967296 =
// 2 ^ (4 * 8). i.e pointer size = 4 bytes
NodeRepository<BigInteger> nr = new FileBackedNodeRepository<BigInteger, Long>(new ReadWritableFileAdapter(treef), false, 0, new FixedSizeNodeSizeStrategy(m_bTreeNodeSize), true, new FixedSizeBigIntegerNullSerializer(STRING_HASH_SIZE + 1), LongSerializer.INSTANCE, 4, 8192, null, lah);
if (m_bTreeLruNodeCacheSize > 0)
{
nr = new LruCacheNodeRepository<BigInteger, Long>(nr, m_bTreeLruNodeCacheSize);
}
BPlusTree<BigInteger, Long> btree = new BPlusTree<BigInteger, Long>(nr, lah);
// Only use 7 bits of the most significant byte when hashing to
// avoid collisions with the serialized null value
b = new BPlusTreeIndexBackend<String, String, BigInteger, Long>(b, false, btree, new StringToBigIntegerHasher(STRING_HASH_SIZE, 7), lah);
SimpleDatabase<String, String, Long> db = new SimpleDatabase<String, String, Long>(b, lah);
m_dbFiles.put(System.identityHashCode(db), new File[] { f, treef });
m_databases.add(db);
}
}
public void tearDown()
{
for (Database<?, ?> db : m_databases)
{
db.close();
File[] files = m_dbFiles.get(System.identityHashCode(db));
for (File f : files)
{
deleteFile(f);
}
}
m_dbFiles = null;
m_databases = null;
}
}