| package edu.uci.ics.hyracks.storage.am.btree; |
| |
| import java.io.DataOutput; |
| import java.nio.ByteBuffer; |
| import java.util.Random; |
| import java.util.logging.Level; |
| |
| import org.junit.Test; |
| |
| import edu.uci.ics.hyracks.api.comm.IFrameTupleAccessor; |
| import edu.uci.ics.hyracks.api.context.IHyracksTaskContext; |
| import edu.uci.ics.hyracks.api.dataflow.value.IBinaryComparatorFactory; |
| import edu.uci.ics.hyracks.api.dataflow.value.ISerializerDeserializer; |
| import edu.uci.ics.hyracks.api.dataflow.value.ITypeTraits; |
| import edu.uci.ics.hyracks.api.dataflow.value.RecordDescriptor; |
| import edu.uci.ics.hyracks.data.std.accessors.PointableBinaryComparatorFactory; |
| import edu.uci.ics.hyracks.data.std.primitive.IntegerPointable; |
| import edu.uci.ics.hyracks.dataflow.common.comm.io.ArrayTupleBuilder; |
| import edu.uci.ics.hyracks.dataflow.common.comm.io.FrameTupleAccessor; |
| import edu.uci.ics.hyracks.dataflow.common.comm.io.FrameTupleAppender; |
| import edu.uci.ics.hyracks.dataflow.common.data.accessors.FrameTupleReference; |
| import edu.uci.ics.hyracks.dataflow.common.data.marshalling.IntegerSerializerDeserializer; |
| import edu.uci.ics.hyracks.storage.am.btree.api.IBTreeInteriorFrame; |
| import edu.uci.ics.hyracks.storage.am.btree.api.IBTreeLeafFrame; |
| import edu.uci.ics.hyracks.storage.am.btree.frames.BTreeNSMInteriorFrameFactory; |
| import edu.uci.ics.hyracks.storage.am.btree.frames.BTreeNSMLeafFrameFactory; |
| import edu.uci.ics.hyracks.storage.am.btree.impls.BTree; |
| import edu.uci.ics.hyracks.storage.am.btree.util.AbstractBTreeTest; |
| import edu.uci.ics.hyracks.storage.am.common.TestOperationCallback; |
| import edu.uci.ics.hyracks.storage.am.common.api.IFreePageManager; |
| import edu.uci.ics.hyracks.storage.am.common.api.ITreeIndexAccessor; |
| import edu.uci.ics.hyracks.storage.am.common.api.ITreeIndexFrameFactory; |
| import edu.uci.ics.hyracks.storage.am.common.api.ITreeIndexMetaDataFrame; |
| import edu.uci.ics.hyracks.storage.am.common.api.ITreeIndexMetaDataFrameFactory; |
| import edu.uci.ics.hyracks.storage.am.common.api.TreeIndexException; |
| import edu.uci.ics.hyracks.storage.am.common.frames.LIFOMetaDataFrameFactory; |
| import edu.uci.ics.hyracks.storage.am.common.freepage.LinkedListFreePageManager; |
| import edu.uci.ics.hyracks.storage.am.common.tuples.TypeAwareTupleWriterFactory; |
| import edu.uci.ics.hyracks.storage.am.common.util.TreeIndexBufferCacheWarmup; |
| import edu.uci.ics.hyracks.storage.am.common.util.TreeIndexStats; |
| import edu.uci.ics.hyracks.storage.am.common.util.TreeIndexStatsGatherer; |
| import edu.uci.ics.hyracks.storage.common.buffercache.IBufferCache; |
| import edu.uci.ics.hyracks.storage.common.file.IFileMapProvider; |
| import edu.uci.ics.hyracks.test.support.TestStorageManagerComponentHolder; |
| import edu.uci.ics.hyracks.test.support.TestUtils; |
| |
| @SuppressWarnings("rawtypes") |
| public class BTreeStatsTest extends AbstractBTreeTest { |
| private static final int PAGE_SIZE = 4096; |
| private static final int NUM_PAGES = 1000; |
| private static final int MAX_OPEN_FILES = 10; |
| private static final int HYRACKS_FRAME_SIZE = 128; |
| |
| private final IHyracksTaskContext ctx = TestUtils.create(HYRACKS_FRAME_SIZE); |
| |
| @Test |
| public void test01() throws Exception { |
| |
| TestStorageManagerComponentHolder.init(PAGE_SIZE, NUM_PAGES, MAX_OPEN_FILES); |
| IBufferCache bufferCache = harness.getBufferCache(); |
| IFileMapProvider fmp = harness.getFileMapProvider(); |
| |
| // declare fields |
| int fieldCount = 2; |
| ITypeTraits[] typeTraits = new ITypeTraits[fieldCount]; |
| typeTraits[0] = IntegerPointable.TYPE_TRAITS; |
| typeTraits[1] = IntegerPointable.TYPE_TRAITS; |
| |
| // declare keys |
| int keyFieldCount = 1; |
| IBinaryComparatorFactory[] cmpFactories = new IBinaryComparatorFactory[keyFieldCount]; |
| cmpFactories[0] = PointableBinaryComparatorFactory.of(IntegerPointable.FACTORY); |
| |
| TypeAwareTupleWriterFactory tupleWriterFactory = new TypeAwareTupleWriterFactory(typeTraits); |
| ITreeIndexFrameFactory leafFrameFactory = new BTreeNSMLeafFrameFactory(tupleWriterFactory); |
| ITreeIndexFrameFactory interiorFrameFactory = new BTreeNSMInteriorFrameFactory(tupleWriterFactory); |
| ITreeIndexMetaDataFrameFactory metaFrameFactory = new LIFOMetaDataFrameFactory(); |
| |
| IBTreeLeafFrame leafFrame = (IBTreeLeafFrame) leafFrameFactory.createFrame(); |
| IBTreeInteriorFrame interiorFrame = (IBTreeInteriorFrame) interiorFrameFactory.createFrame(); |
| ITreeIndexMetaDataFrame metaFrame = metaFrameFactory.createFrame(); |
| |
| IFreePageManager freePageManager = new LinkedListFreePageManager(bufferCache, 0, metaFrameFactory); |
| |
| BTree btree = new BTree(bufferCache, fmp, freePageManager, interiorFrameFactory, leafFrameFactory, |
| cmpFactories, fieldCount, harness.getFileReference()); |
| btree.create(); |
| btree.activate(); |
| |
| Random rnd = new Random(); |
| rnd.setSeed(50); |
| |
| long start = System.currentTimeMillis(); |
| |
| if (LOGGER.isLoggable(Level.INFO)) { |
| LOGGER.info("INSERTING INTO TREE"); |
| } |
| |
| ByteBuffer frame = ctx.allocateFrame(); |
| FrameTupleAppender appender = new FrameTupleAppender(ctx.getFrameSize()); |
| ArrayTupleBuilder tb = new ArrayTupleBuilder(fieldCount); |
| DataOutput dos = tb.getDataOutput(); |
| |
| ISerializerDeserializer[] recDescSers = { IntegerSerializerDeserializer.INSTANCE, |
| IntegerSerializerDeserializer.INSTANCE }; |
| RecordDescriptor recDesc = new RecordDescriptor(recDescSers); |
| IFrameTupleAccessor accessor = new FrameTupleAccessor(ctx.getFrameSize(), recDesc); |
| accessor.reset(frame); |
| FrameTupleReference tuple = new FrameTupleReference(); |
| |
| ITreeIndexAccessor indexAccessor = btree.createAccessor(TestOperationCallback.INSTANCE, |
| TestOperationCallback.INSTANCE); |
| // 10000 |
| for (int i = 0; i < 100000; i++) { |
| |
| int f0 = rnd.nextInt() % 100000; |
| int f1 = 5; |
| |
| tb.reset(); |
| IntegerSerializerDeserializer.INSTANCE.serialize(f0, dos); |
| tb.addFieldEndOffset(); |
| IntegerSerializerDeserializer.INSTANCE.serialize(f1, dos); |
| tb.addFieldEndOffset(); |
| |
| appender.reset(frame, true); |
| appender.append(tb.getFieldEndOffsets(), tb.getByteArray(), 0, tb.getSize()); |
| |
| tuple.reset(accessor, 0); |
| |
| if (LOGGER.isLoggable(Level.INFO)) { |
| if (i % 10000 == 0) { |
| long end = System.currentTimeMillis(); |
| LOGGER.info("INSERTING " + i + " : " + f0 + " " + f1 + " " + (end - start)); |
| } |
| } |
| |
| try { |
| indexAccessor.insert(tuple); |
| } catch (TreeIndexException e) { |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| int fileId = fmp.lookupFileId(harness.getFileReference()); |
| TreeIndexStatsGatherer statsGatherer = new TreeIndexStatsGatherer(bufferCache, freePageManager, fileId, |
| btree.getRootPageId()); |
| TreeIndexStats stats = statsGatherer.gatherStats(leafFrame, interiorFrame, metaFrame); |
| if (LOGGER.isLoggable(Level.INFO)) { |
| LOGGER.info("\n" + stats.toString()); |
| } |
| |
| TreeIndexBufferCacheWarmup bufferCacheWarmup = new TreeIndexBufferCacheWarmup(bufferCache, freePageManager, |
| fileId); |
| bufferCacheWarmup.warmup(leafFrame, metaFrame, new int[] { 1, 2 }, new int[] { 2, 5 }); |
| |
| btree.deactivate(); |
| btree.destroy(); |
| bufferCache.close(); |
| } |
| } |