00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef Fennel_BTreeBuilder_Included
00025 #define Fennel_BTreeBuilder_Included
00026
00027 #include "fennel/btree/BTreeAccessBase.h"
00028
00029 #include <vector>
00030
00031 FENNEL_BEGIN_NAMESPACE
00032
00033 class BTreeBuildLevel;
00034 typedef boost::shared_ptr<BTreeBuildLevel> SharedBTreeBuildLevel;
00035
00036
00037
00049 class FENNEL_BTREE_EXPORT BTreeBuilder
00050 : public BTreeAccessBase
00051 {
00052
00053 friend class BTreeBuildLevel;
00054 friend class FixedBuildLevel;
00055 friend class VariableBuildLevel;
00056 friend class DynamicBuildLevel;
00057
00058 std::vector<SharedBTreeBuildLevel> levels;
00059
00060 SharedSegment pTempSegment;
00061
00062
00063
00064
00065
00066 static uint calculateChildEntriesPerNode(
00067 RecordNum parentLevelTotalEntries,
00068 RecordNum childLevelTotalEntries,
00069 RecordNum parentLevelProcessedEntries);
00070
00071 static uint calculateNodesOnLevel(
00072 uint nChildEntries,uint nEntriesPerChildNode);
00073
00074 uint getRootHeight()
00075 {
00076 return levels.size() - 1;
00077 }
00078
00079 void buildBalanced(
00080 ByteInputStream &sortedStream,
00081 uint iInputLevel,
00082 RecordNum nEntriesTotal,
00083 double fillFactor);
00084
00085 void buildTwoPass(
00086 ByteInputStream &sortedStream,
00087 RecordNum nEntries,
00088 double fillFactor);
00089
00090 void buildUnbalanced(
00091 ByteInputStream &sortedStream,
00092 RecordNum nEntries,
00093 double fillFactor);
00094
00095 void processInput(
00096 ByteInputStream &sortedStream);
00097
00098 inline BTreeBuildLevel &getLevel(uint i);
00099
00100 void growTree();
00101
00102 void swapRoot();
00103
00104 void truncateChildren(BTreeNode const &node);
00105 void truncateExternal(TupleProjection const &leafPageIdProj);
00106
00107 public:
00119 explicit BTreeBuilder(
00120 BTreeDescriptor const &descriptor,
00121 SharedSegment pTempSegment = SharedSegment());
00122 virtual ~BTreeBuilder();
00123
00136 void build(
00137 ByteInputStream &sortedStream,
00138 RecordNum nEntries,
00139 double fillFactor = 1.0);
00140
00146 void createEmptyRoot();
00147
00159 void truncate(
00160 bool rootless,
00161 TupleProjection const *pLeafPageIdProj = NULL);
00162 };
00163
00164 inline BTreeBuildLevel &BTreeBuilder::getLevel(uint i)
00165 {
00166 return *(levels[i]);
00167 }
00168
00169 FENNEL_END_NAMESPACE
00170
00171 #endif
00172
00173