summaryrefslogtreecommitdiff
path: root/storage/ndb/src/old_files/rep/storage/GCIContainer.cpp
blob: c161db0769b6080a2f82bc1c3dc83ed880430622 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program 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, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include "GCIContainer.hpp"
#include <NdbOut.hpp>
#include <NdbMem.h>
#include <new>

#include <rep/rep_version.hpp>

//#define GCICONTAINER_DEBUG

/*****************************************************************************
 * Constructors / Destructors
 *****************************************************************************/

GCIContainer::GCIContainer(Uint32 maxNoOfIds) 
{
  m_maxNoOfIds = maxNoOfIds;

  gciRange = new GCIRange[maxNoOfIds * sizeof(GCIRange)];
  
  for(Uint32 i = 0; i < maxNoOfIds; i++) {
    gciRange[i].m_firstGCI = 1;  // The empty interval = [1,0]
    gciRange[i].m_lastGCI = 0;
  }
  theMutexPtr = NdbMutex_Create();
}

GCIContainer::~GCIContainer() 
{
  for(Uint32 i=0; i < m_bufferList.size(); i++) {
    delete m_bufferList[i];
    m_bufferList[i] = 0;
  }

  m_bufferList=0;
  delete [] gciRange;
  NdbMutex_Destroy(theMutexPtr);
}

/*****************************************************************************
 * GCIBuffer Create / Destroy
 *****************************************************************************/

GCIBuffer * 
GCIContainer::createGCIBuffer(Uint32 gci, Uint32 id) 
{
  GCIBuffer * buf = new GCIBuffer(gci, id);
  if (buf == NULL) REPABORT("Could not allocate new buffer");
  
  m_bufferList.push_back(buf, true);
  
#ifdef GCICONTAINER_DEBUG
  ndbout_c("GCIContainer: New buffer created (GCI: %d, Id: %d)", gci, id);
#endif
  return buf;
}

/**
 * Delete all GCI buffers strictly less than "gci"
 */
void
GCIContainer::destroyGCIBuffersBeforeGCI(Uint32 gci, Uint32 id) 
{
  for(Uint32 i = 0 ; i < m_bufferList.size(); i++) {
    if(m_bufferList[i]->getGCI() < gci) {
#ifdef GCICONTAINER_DEBUG
      ndbout_c("GCIContainer: Destroying buffer (GCI: %d, id: %d)",
	       m_bufferList[i]->getGCI(), id);
#endif
      destroyGCIBuffer(i, id);
    }
  }
}

/**
 * Delete one GCI Buffer
 */
bool
GCIContainer::destroyGCIBuffer(Uint32 gci, Uint32 id) 
{
  m_bufferList.lock();
  for(Uint32 i = 0 ; i < m_bufferList.size(); i++) {
    if((m_bufferList[i]->getGCI() == gci) && 
       (m_bufferList[i]->getId() == id)) {

      /**
       * Delete the GCI Buffer
       */      
      delete m_bufferList[i];
      m_bufferList[i] = 0;

      /**
       * Remove from the list of buffers stored in GCIContainer
       */
      m_bufferList.erase(i,false); 
      m_bufferList.unlock();

      /**
       * Set info
       */
      NdbMutex_Lock(theMutexPtr);
      if(gciRange[id].m_firstGCI != gci)
	RLOG(("WARNING! Buffer %d deleted from [%d-%d]",
	      gci, gciRange[id].m_firstGCI, gciRange[id].m_lastGCI));
      
      gciRange[id].m_firstGCI++;

      /**
       * Normalize empty interval to [1,0]
       */
      if (gciRange[id].m_firstGCI > gciRange[id].m_lastGCI){
	gciRange[id].m_firstGCI = 1;
	gciRange[id].m_lastGCI = 0;
      }
      NdbMutex_Unlock(theMutexPtr);
      return true;
    }
  }
  m_bufferList.unlock();
  return false;
}

/*****************************************************************************
 * GCIBuffer interface
 *****************************************************************************/

GCIBuffer * 
GCIContainer::getGCIBuffer(Uint32 gci, Uint32 id) 
{
  GCIBuffer * gciBuffer = 0;

  m_bufferList.lock();
  for(Uint32 i=0; i < m_bufferList.size(); i++) {
    gciBuffer = m_bufferList[i];
    if((gciBuffer->getGCI() == gci) && (gciBuffer->getId() == id)) {
      m_bufferList.unlock();
      return gciBuffer;
    }
  }
  m_bufferList.unlock();
  return 0;
}

void
GCIContainer::setCompleted(Uint32 gci, Uint32 id) 
{
  GCIBuffer * gciBuffer = getGCIBuffer(gci, id);
  if(gciBuffer == 0) gciBuffer = createGCIBuffer(gci, id);

  gciBuffer->setComplete();
  
#ifdef GCICONTAINER_DEBUG
  ndbout_c("GCIContainer: Buffer completely stored in GCIContainer (GCI: %d)",
	   gci);
#endif
  
  NdbMutex_Lock(theMutexPtr);
  
  /**
   * If this is the first GCI Buffer to be completed
   * then both first and last must be updated.
   * Subsequently, only the last value must be updated.
   */
  if(gciRange[id].m_firstGCI == 1 && gciRange[id].m_lastGCI == 0) {
    gciRange[id].m_firstGCI =  gci;
    gciRange[id].m_lastGCI =  gci;
  } else {
    if (gci != gciRange[id].m_lastGCI + 1) {
      RLOG(("WARNING! Non-consequtive buffer %u completed [%u-%u])",
	    gci, gciRange[id].m_firstGCI, gciRange[id].m_lastGCI));
    }
    gciRange[id].m_lastGCI = gci;
  }  
  NdbMutex_Unlock(theMutexPtr);
}

void
GCIContainer::getAvailableGCIBuffers(Uint32 id,	Uint32 * first, Uint32 * last) 
{
  NdbMutex_Lock(theMutexPtr);
  *first = gciRange[id].m_firstGCI;
  *last = gciRange[id].m_lastGCI;
  NdbMutex_Unlock(theMutexPtr);
}

/*****************************************************************************
 * Inserts
 *****************************************************************************/
void
GCIContainer::insertMetaRecord(Uint32 id, Uint32 tableId, 
			       class LinearSectionPtr ptr[3], Uint32 gci) 
{
  /**********************************************************
   * 1. Find correct GCI Buffer (Doesn't exist?  Create one)
   **********************************************************/
  GCIBuffer * gciBuffer = getGCIBuffer(gci, id);
  if(gciBuffer == 0) gciBuffer = createGCIBuffer(gci, id);

  /**********************************
   * 2. Insert record into GCIBuffer
   **********************************/
  gciBuffer->insertMetaRecord(tableId, ptr);
}

void
GCIContainer::insertLogRecord(Uint32 id, Uint32 tableId, Uint32 operation,
			      class LinearSectionPtr ptr[3], Uint32 gci) 
{
  /*********************************************************
   * 1. Find correct GCI Buffer (doesn't exist? create one)
   *********************************************************/
  GCIBuffer * gciBuffer = getGCIBuffer(gci, id);
  if(gciBuffer == 0) gciBuffer = createGCIBuffer(gci, id);
  /**********************************
   * 2. Insert record into GCIBuffer
   **********************************/
  gciBuffer->insertLogRecord(tableId, operation, ptr);
}

void
GCIContainer::insertPage(Uint32 gci, Uint32 id,
			 char * dataPtr, Uint32 dataBLen) 
{
  /*********************************************************
   * 1. Find correct GCI Buffer (doesn't exist? create one)
   *********************************************************/
  GCIBuffer * gciBuffer = getGCIBuffer(gci, id);
  if(gciBuffer == 0) gciBuffer = createGCIBuffer(gci, id);

  /********************************
   * 2. Insert page into GCIBuffer
   ********************************/
  gciBuffer->insertPage(gci, dataPtr, dataBLen);
}

bool
GCIContainer::reset() 
{
  /**
   * Clear the intervals
   */ 
  for(Uint32 i = 0; i < m_maxNoOfIds; i++) {
    gciRange[i].m_firstGCI = 1;  // The empty interval = [1,0]
    gciRange[i].m_lastGCI = 0;
  }

  /**
   * Destroy ALL gci buffers for ALL ids
   */
  for(Uint32 i=0; i < m_bufferList.size(); i++) {
    delete m_bufferList[i];
    m_bufferList[i] = 0;
  }
  m_bufferList.clear();

  return true;
}