MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DbtuxBuild.cpp
1 /*
2  Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "Dbtux.hpp"
19 
21 {
22  Uint32 indexId;
23  Uint32 tableId;
24  Uint32 fragId;
25  Dbtup* tup_ptr;
26  Dbtux::TuxCtx * tux_ctx_ptr;
27  NdbMutex * alloc_mutex_ptr;
28 };
29 
30 Uint32
31 Dbtux_mt_buildIndexFragment_wrapper_C(void * obj)
32 {
34 }
35 
36 Uint32
38 {
39  mt_BuildIndxReq* req = reinterpret_cast<mt_BuildIndxReq*>(obj);
40  TuxCtx * tux_ctx = reinterpret_cast<TuxCtx*>(req->mem_buffer);
41  {
45  Uint32 * ptr = reinterpret_cast<Uint32*>(req->mem_buffer);
46  ptr += (sizeof(* tux_ctx) + 3) / 4;
47 
48  tux_ctx->jamBuffer = (EmulatedJamBuffer*)ptr;
49  tux_ctx->jamBuffer->theEmulatedJamIndex = 0;
50  ptr += (sizeof(EmulatedJamBuffer) + 3) / 4;
51  tux_ctx->c_searchKey = ptr;
52  ptr += MaxAttrDataSize;
53  tux_ctx->c_entryKey = ptr;
54  ptr += MaxAttrDataSize;
55  tux_ctx->c_dataBuffer = ptr;
56  ptr += MaxAttrDataSize;
57 #ifdef VM_TRACE
58  tux_ctx->c_debugBuffer = (char*)ptr;
59  ptr += (DebugBufferBytes + 3) / 4;
60 #endif
61  if (!(UintPtr(ptr) - UintPtr(req->mem_buffer) <= req->buffer_size))
62  abort();
63  }
64 
65  mt_BuildIndxCtx ctx;
66  ctx.indexId = req->indexId;
67  ctx.tableId = req->tableId;
68  ctx.fragId = req->fragId;
69  ctx.tux_ctx_ptr = tux_ctx;
70  ctx.tup_ptr = reinterpret_cast<Dbtup*>(req->tup_ptr);
71 
72  Dbtux* tux = reinterpret_cast<Dbtux*>(req->tux_ptr);
73  return tux->mt_buildIndexFragment(&ctx);
74 }
75 
76 Uint32 // error code
77 Dbtux::mt_buildIndexFragment(mt_BuildIndxCtx* req)
78 {
79  IndexPtr indexPtr;
80  c_indexPool.getPtr(indexPtr, req->indexId);
81  ndbrequire(indexPtr.p->m_tableId == req->tableId);
82  // get base fragment id and extra bits
83  const Uint32 fragId = req->fragId;
84  // get the fragment
85  FragPtr fragPtr;
86  findFrag(*indexPtr.p, fragId, fragPtr);
87  ndbrequire(fragPtr.i != RNIL);
88  Frag& frag = *fragPtr.p;
89 
90  TuxCtx & ctx = * (TuxCtx*)req->tux_ctx_ptr;
91 
92  Local_key pos;
93  Uint32 fragPtrI;
94  int err = req->tup_ptr->mt_scan_init(req->tableId, req->fragId,
95  &pos, &fragPtrI);
96  bool moveNext = false;
97  while (globalData.theRestartFlag != perform_stop &&
98  err == 0 &&
99  (err = req->tup_ptr->mt_scan_next(req->tableId,
100  fragPtrI, &pos, moveNext)) == 0)
101  {
102  moveNext = true;
103 
104  // set up search entry
105  TreeEnt ent;
106  ent.m_tupLoc = TupLoc(pos.m_page_no, pos.m_page_idx);
107  ent.m_tupVersion = pos.m_file_no; // used for version
108 
109  // set up and read search key
110  KeyData searchKey(indexPtr.p->m_keySpec, false, 0);
111  searchKey.set_buf(ctx.c_searchKey, MaxAttrDataSize << 2);
112  readKeyAttrs(ctx, frag, ent, searchKey, indexPtr.p->m_numAttrs);
113 
114  if (unlikely(! indexPtr.p->m_storeNullKey) &&
115  searchKey.get_null_cnt() == indexPtr.p->m_numAttrs) {
116  jam();
117  continue;
118  }
119 
120  TreePos treePos;
121  bool ok = searchToAdd(ctx, frag, searchKey, ent, treePos);
122  ndbrequire(ok);
123 
124  /*
125  * At most one new node is inserted in the operation. Pre-allocate
126  * it so that the operation cannot fail.
127  */
128  if (frag.m_freeLoc == NullTupLoc)
129  {
130  jam();
131  NodeHandle node(frag);
132  err = -(int)allocNode(ctx, node);
133 
134  if (err != 0)
135  {
136  break;
137  }
138  frag.m_freeLoc = node.m_loc;
139  ndbrequire(frag.m_freeLoc != NullTupLoc);
140  }
141  treeAdd(ctx, frag, treePos, ent);
142  frag.m_entryCount++;
143  frag.m_entryBytes += searchKey.get_data_len();
144  frag.m_entryOps++;
145  }
146 
147  if (err < 0)
148  {
149  return -err;
150  }
151 
152  return 0;
153 };