00001 /* 00002 // $Id: //open/dev/fennel/ftrs/BTreeInsertExecStream.cpp#9 $ 00003 // Fennel is a library of data storage and processing components. 00004 // Copyright (C) 2005-2009 The Eigenbase Project 00005 // Copyright (C) 2005-2009 SQLstream, Inc. 00006 // Copyright (C) 2005-2009 LucidEra, Inc. 00007 // Portions Copyright (C) 2004-2009 John V. Sichi 00008 // 00009 // This program is free software; you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by the Free 00011 // Software Foundation; either version 2 of the License, or (at your option) 00012 // any later version approved by The Eigenbase Project. 00013 // 00014 // This program is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with this program; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 */ 00023 00024 #include "fennel/common/CommonPreamble.h" 00025 #include "fennel/ftrs/BTreeInsertExecStream.h" 00026 #include "fennel/btree/BTreeBuilder.h" 00027 #include "fennel/btree/BTreeWriter.h" 00028 #include "fennel/tuple/TupleDescriptor.h" 00029 #include "fennel/exec/ExecStreamBufAccessor.h" 00030 00031 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/ftrs/BTreeInsertExecStream.cpp#9 $"); 00032 00033 void BTreeInsertExecStream::prepare(BTreeInsertExecStreamParams const ¶ms) 00034 { 00035 BTreeExecStream::prepare(params); 00036 ConduitExecStream::prepare(params); 00037 distinctness = params.distinctness; 00038 monotonic = params.monotonic; 00039 00040 dynamicBTree = (rootPageIdParamId > DynamicParamId(0)); 00041 truncateOnRestart = false; 00042 assert(treeDescriptor.tupleDescriptor == pInAccessor->getTupleDesc()); 00043 } 00044 00045 void BTreeInsertExecStream::getResourceRequirements( 00046 ExecStreamResourceQuantity &minQuantity, 00047 ExecStreamResourceQuantity &optQuantity) 00048 { 00049 BTreeExecStream::getResourceRequirements(minQuantity,optQuantity); 00050 00051 // max number of pages locked during tree update (REVIEW), 00052 // including BTreeWriter's private scratch page 00053 minQuantity.nCachePages += 5; 00054 00055 // TODO: use opt to govern prefetch and come up with a good formula 00056 optQuantity = minQuantity; 00057 } 00058 00059 void BTreeInsertExecStream::open(bool restart) 00060 { 00061 BTreeExecStream::open(restart); 00062 ConduitExecStream::open(restart); 00063 00064 if (dynamicBTree) { 00065 buildTree(restart); 00066 } 00067 00068 if (restart) { 00069 return; 00070 } 00071 00072 if (rootPageIdParamId > DynamicParamId(0)) { 00073 StandardTypeDescriptorFactory stdTypeFactory; 00074 TupleAttributeDescriptor attrDesc = 00075 TupleAttributeDescriptor( 00076 stdTypeFactory.newDataType(STANDARD_TYPE_UINT_64)); 00077 pDynamicParamManager->createParam(rootPageIdParamId, attrDesc); 00078 TupleDatum rootPageIdDatum; 00079 rootPageIdDatum.pData = (PConstBuffer) &(treeDescriptor.rootPageId); 00080 rootPageIdDatum.cbData = sizeof(PageId); 00081 pDynamicParamManager->writeParam(rootPageIdParamId, rootPageIdDatum); 00082 } 00083 00084 // NOTE: do this last so that rootPageId is available 00085 pWriter = newWriter(monotonic); 00086 } 00087 00088 void BTreeInsertExecStream::buildTree(bool restart) 00089 { 00090 if (restart) { 00091 if (truncateOnRestart) { 00092 truncateTree(false); 00093 } 00094 } else { 00095 BTreeBuilder builder( 00096 treeDescriptor, 00097 treeDescriptor.segmentAccessor.pSegment); 00098 builder.createEmptyRoot(); 00099 treeDescriptor.rootPageId = builder.getRootPageId(); 00100 } 00101 } 00102 00103 void BTreeInsertExecStream::truncateTree(bool rootless) 00104 { 00105 if (treeDescriptor.rootPageId == NULL_PAGE_ID) { 00106 // nothing to do 00107 assert(rootless); 00108 return; 00109 } 00110 BTreeBuilder builder( 00111 treeDescriptor, 00112 treeDescriptor.segmentAccessor.pSegment); 00113 builder.truncate(rootless); 00114 if (rootless) { 00115 treeDescriptor.rootPageId = NULL_PAGE_ID; 00116 } 00117 } 00118 00119 ExecStreamResult BTreeInsertExecStream::execute( 00120 ExecStreamQuantum const &quantum) 00121 { 00122 ExecStreamResult rc = precheckConduitBuffers(); 00123 if (rc != EXECRC_YIELD) { 00124 if (rc == EXECRC_EOS) { 00125 // Terminate search so other streams can read from the btree 00126 pWriter->endSearch(); 00127 pOutAccessor->markEOS(); 00128 } 00129 return rc; 00130 } 00131 00132 uint nTuples = 0; 00133 00134 for (;;) { 00135 PConstBuffer pTupleBuf = pInAccessor->getConsumptionStart(); 00136 uint cb = pWriter->insertTupleFromBuffer(pTupleBuf,distinctness); 00137 pInAccessor->consumeData(pTupleBuf + cb); 00138 ++nTuples; 00139 if (nTuples > quantum.nTuplesMax) { 00140 return EXECRC_QUANTUM_EXPIRED; 00141 } 00142 if (!pInAccessor->demandData()) { 00143 return EXECRC_BUF_UNDERFLOW; 00144 } 00145 } 00146 } 00147 00148 void BTreeInsertExecStream::closeImpl() 00149 { 00150 BTreeExecStream::closeImpl(); 00151 pWriter.reset(); 00152 pBTreeAccessBase.reset(); 00153 ConduitExecStream::closeImpl(); 00154 if (dynamicBTree) { 00155 truncateTree(true); 00156 } 00157 } 00158 00159 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/ftrs/BTreeInsertExecStream.cpp#9 $"); 00160 00161 // End BTreeInsertExecStream.cpp