Package org.helidb.impl.txn.log

Source Code of org.helidb.impl.txn.log.LoggingTxnDbOnConstantRecordSizeBPlusTreeBackendTest

/* 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.impl.txn.log;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

import org.entityfs.Directory;
import org.entityfs.EFile;
import org.entityfs.support.log.LogAdapterHolder;
import org.entityfs.support.log.StdOutLogAdapter;
import org.entityfs.util.Directories;
import org.helidb.CRSSortedBackendDBTester;
import org.helidb.Database;
import org.helidb.backend.bpluscrs.ConstantRecordSizeBPlusTreeBackend;
import org.helidb.lang.serializer.CharacterNullSerializer;
import org.helidb.lang.serializer.CharacterSerializer;
import org.helidb.lang.serializer.IntegerNullSerializer;
import org.helidb.lang.serializer.LongSerializer;
import org.helidb.txn.TransactionalDatabase;
import org.helidb.util.bplus.FileBackedNodeRepository;
import org.helidb.util.bplus.KeyAndValue;
import org.helidb.util.bplus.LruCacheNodeRepository;
import org.helidb.util.bplus.NodeRepository;
import org.helidb.util.bplus.NumberOfRecordsNodeSizeStrategy;
import org.junit.Test;

public class LoggingTxnDbOnConstantRecordSizeBPlusTreeBackendTest extends AbstractLoggingTxnDbOnConstantRecordSizeBackendTest<KeyAndValue<Integer, Long>>
{
  private final Map<Database<?, ?>, EFile[]> m_dbFiles = new HashMap<Database<?, ?>, EFile[]>();

  private LoggingTransactionalDatabase<Integer, Long, KeyAndValue<Integer, Long>> createDbUsingFiles(EFile f, EFile logf)
  {
    LogAdapterHolder lah = new LogAdapterHolder(new StdOutLogAdapter());
    lah.setLevel(Level.SEVERE);
    NodeRepository<Integer> nr = new LruCacheNodeRepository<Integer, Long>(new FileBackedNodeRepository<Integer, Long>(f, false, 0, new NumberOfRecordsNodeSizeStrategy(4), true, IntegerNullSerializer.INSTANCE, LongSerializer.INSTANCE, 4, 8192, null, lah), 10);
    ConstantRecordSizeBPlusTreeBackend<Integer, Long> backend = new ConstantRecordSizeBPlusTreeBackend<Integer, Long>(nr, false, lah);
    LoggingTransactionalDatabase<Integer, Long, KeyAndValue<Integer, Long>> res = new LoggingTransactionalDatabase<Integer, Long, KeyAndValue<Integer, Long>>(backend, logf, IntegerNullSerializer.INSTANCE, LongSerializer.INSTANCE, lah);
    m_dbFiles.put(res, new EFile[] { f, logf });
    return res;
  }

  @Override
  protected LoggingTransactionalDatabase<Integer, Long, KeyAndValue<Integer, Long>> createDatabaseWoTxnInDirectory(Directory dir)
  {
    EFile f = Directories.newFile(dir, "db");
    EFile logf = Directories.newFile(dir, "log");
    return createDbUsingFiles(f, logf);
  }

  @Override
  protected LoggingTransactionalDatabase<Integer, Long, KeyAndValue<Integer, Long>> createNewDatabaseUsingSameFiles(TransactionalDatabase<Integer, Long> otherDb)
  {
    EFile[] files = m_dbFiles.get(otherDb);
    return createDbUsingFiles(files[0], files[1]);
  }

  @Override
  protected LoggingTransactionalDatabase<Integer, Long, KeyAndValue<Integer, Long>> createNewDatabaseUsingSameFilesButTruncateRollbackLog(TransactionalDatabase<Integer, Long> otherDb, int howMuchToTruncate)
  {
    EFile[] files = m_dbFiles.get(otherDb);
    return createDbUsingFiles(files[0], truncate(files[1], howMuchToTruncate));
  }

  private LoggingTransactionalDatabase<Character, Character, KeyAndValue<Character, Character>> createCharacterDbUsingFiles(EFile f, EFile logf)
  {
    LogAdapterHolder lah = new LogAdapterHolder(new StdOutLogAdapter());
    lah.setLevel(Level.SEVERE);
    NodeRepository<Character> nr = new LruCacheNodeRepository<Character, Character>(new FileBackedNodeRepository<Character, Character>(f, false, 0, new NumberOfRecordsNodeSizeStrategy(2), true, CharacterNullSerializer.INSTANCE, CharacterSerializer.INSTANCE, 4, 8192, null, lah), 10);
    ConstantRecordSizeBPlusTreeBackend<Character, Character> backend = new ConstantRecordSizeBPlusTreeBackend<Character, Character>(nr, false, lah);
    LoggingTransactionalDatabase<Character, Character, KeyAndValue<Character, Character>> res = new LoggingTransactionalDatabase<Character, Character, KeyAndValue<Character, Character>>(backend, logf, CharacterNullSerializer.INSTANCE, CharacterSerializer.INSTANCE, lah);
    m_dbFiles.put(res, new EFile[] { f, logf });
    return res;
  }

  @Override
  protected TransactionalDatabase<Character, Character> createCharacterDatabaseWoTxnInDirectory(Directory dir)
  {
    EFile f = Directories.newFile(dir, "db");
    EFile logf = Directories.newFile(dir, "log");
    return createCharacterDbUsingFiles(f, logf);
  }

  @Test
  public void testCursor()
  {
    Database<Integer, Long> db = createDatabase();
    try
    {
      CRSSortedBackendDBTester.testCursor(db);
    }
    finally
    {
      tearDownDatabase(db);
    }
  }

  @Test
  public void testCursorIterator()
  {
    Database<Integer, Long> db = createDatabase();
    try
    {
      CRSSortedBackendDBTester.testCursorIterator(db);
    }
    finally
    {
      tearDownDatabase(db);
    }
  }

  @Test
  public void testCursorBackwardsIterator()
  {
    Database<Integer, Long> db = createDatabase();
    try
    {
      CRSSortedBackendDBTester.testCursorBackwardsIterator(db);
    }
    finally
    {
      tearDownDatabase(db);
    }
  }
}
TOP

Related Classes of org.helidb.impl.txn.log.LoggingTxnDbOnConstantRecordSizeBPlusTreeBackendTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.