summaryrefslogtreecommitdiff
path: root/ndb/src/old_files/newtonapi/dba_bulkread.cpp
blob: 1f75037046b35dee5aaee39c83ed1dbedf0dfd89 (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
/* 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 "dba_internal.hpp"

struct DBA__BulkReadData {
  const DBA_Binding_t * const * pBindings; // The bindings
  DBA_BulkReadResultSet_t * pData;       // The data 
  
  int NbRows;                               // NbRows per binding
  int NbBindings;                           // NbBindings
  int TotalRows;                            // Total rows (NbRows*NbBindings)
  
  DBA_AsyncCallbackFn_t CbFunc;             // Users callback
  DBA_ReqId_t RequestId;                    // Users request id
  DBA_Error_t Status;                       // Request status
  DBA_ErrorCode_t ErrorCode;                /**< Request error
					       Only valid if request is
					       aborted */
  
  int RowsSubmitted;                        // No of read sent to NDB
  int RowsAcknowledged;                     // No of read responses
  int OpPerTrans;                           // Operations per transaction
  
  struct Index {
    int binding;
    int row;
    int datarow;

    void init() { row = binding = datarow = 0;}
    void next(int rows) { 
      datarow++; row++; 
      if(row == rows){ row = 0; binding++; } 
    }
  };
  Index lastSend;
  Index nextSend;
  
  /**
   * If "simple" bulkread
   * use this storage
   */
  const DBA_Binding_t    * bindings[1];

  DBA__BulkReadData() {
    RequestId = DBA_INVALID_REQID;
  }
  void ProcessBulkRead();
  bool ProcessCallback(int errorCode, NdbConnection * connection);
};

static
void 
NewtonCallback(int errorCode,
	       NdbConnection * connection,
	       void * anyObject){

  DBA__BulkReadData * brd = (DBA__BulkReadData*)anyObject;
  
  brd->ProcessCallback(errorCode, connection);

  DBA__TheNdb->closeTransaction(connection);

  if(brd->RowsSubmitted == brd->TotalRows){
    
    /**
     * The entire bulk read is finished,
     * call users callback
     */
    DBA_ReqId_t reqId = brd->RequestId;
    
    // Invalidate BulkReadData
    brd->RequestId = DBA_INVALID_REQID;

    brd->CbFunc(reqId, brd->Status, brd->ErrorCode);
    return;
  }
  
  brd->ProcessBulkRead();
}

/**
 * A BulkReadData structure
 */
static DBA__BulkReadData theBRD;

#define CHECK_BINDINGS(Bindings) \
  if(!DBA__ValidBinding(Bindings)){ \
    DBA__SetLatestError(DBA_APPLICATION_ERROR, 0, "Invalid bindings"); \
    return DBA_INVALID_REQID; \
  } 

#define CHECK_BINDINGS2(Bindings, NbBindings) \
  if(!DBA__ValidBindings(Bindings, NbBindings)){ \
    DBA__SetLatestError(DBA_APPLICATION_ERROR, 0, "Invalid bindings"); \
    return DBA_INVALID_REQID; \
  }

DBA_ReqId_t
DBA_BulkReadRows(const DBA_Binding_t * pBindings,
		 DBA_BulkReadResultSet_t pData[],
		 int NbRows,
		 DBA_AsyncCallbackFn_t CbFunc ){

  CHECK_BINDINGS(pBindings);

  DBA__BulkReadData * brd = &theBRD;
  
  NdbMutex_Lock(DBA__TheNewtonMutex);

  if(brd->RequestId != DBA_INVALID_REQID){
    DBA__SetLatestError(DBA_ERROR, 0,
			"DBA only permits 1 concurrent bulkread");
    
    NdbMutex_Unlock(DBA__TheNewtonMutex);
    return DBA_ERROR;
  }
  
  theBRD.RequestId = 1;
  
  /**
   * 
   */
  brd->bindings[0] = pBindings;
  brd->pBindings   = brd->bindings;
  brd->pData       = pData;

  /**
   * Control data
   */
  brd->NbRows         = NbRows;
  brd->NbBindings     = 1;
  brd->TotalRows      = NbRows;
  brd->CbFunc         = CbFunc;
  brd->Status         = DBA_NO_ERROR;
  brd->ErrorCode      = 0;
  brd->OpPerTrans     = DBA__BulkReadCount;

  brd->RowsSubmitted    = 0;
  brd->RowsAcknowledged = 0;

  brd->lastSend.init();
  brd->nextSend.init();

  brd->ProcessBulkRead();
  NdbMutex_Unlock(DBA__TheNewtonMutex);
  
  return brd->RequestId;
}

DBA_ReqId_t
DBA_BulkMultiReadRows(const DBA_Binding_t * const * pBindings,
		      DBA_BulkReadResultSet_t pData[],
		      int NbBindings,
		      int NbRows,
		      DBA_AsyncCallbackFn_t CbFunc ){

  CHECK_BINDINGS2(pBindings, NbBindings);

  DBA__BulkReadData * brd = &theBRD;

  NdbMutex_Lock(DBA__TheNewtonMutex);
  
  if(brd->RequestId != DBA_INVALID_REQID){
    DBA__SetLatestError(DBA_ERROR, 0,
			"DBA only permits 1 concurrent bulkread");

    NdbMutex_Unlock(DBA__TheNewtonMutex);
    return DBA_ERROR;
  }
  
  brd->RequestId = 1;
  
  /**
   * 
   */
  brd->pBindings  = pBindings;
  brd->pData      = pData;

  /**
   * Control data
   */
  brd->NbRows        = NbRows;
  brd->NbBindings    = NbBindings;
  brd->TotalRows     = (NbRows * NbBindings);
  brd->CbFunc        = CbFunc;
  brd->Status        = DBA_NO_ERROR;
  brd->ErrorCode     = 0;
  brd->OpPerTrans    = DBA__BulkReadCount;

  brd->RowsSubmitted    = 0;
  brd->RowsAcknowledged = 0;
  
  brd->lastSend.init();
  brd->nextSend.init();
  
  brd->ProcessBulkRead();
  
  NdbMutex_Unlock(DBA__TheNewtonMutex);  

  return brd->RequestId;
}

bool
DBA__BulkReadData::ProcessCallback(int errorCode, NdbConnection * con){

  Index tmp = lastSend;
  const NdbOperation * op = con->getNextCompletedOperation(0);
  
  for(int i = 0; i<OpPerTrans && RowsAcknowledged < RowsSubmitted; i++){
    require(op != 0);
    if(op->getNdbError().code == 0)
      pData[tmp.datarow].RowFoundIndicator = 1;
    else
      pData[tmp.datarow].RowFoundIndicator = 0;

    RowsAcknowledged++;
    tmp.next(NbRows);
    op = con->getNextCompletedOperation(op);
  }
  return true;
}

void
DBA__BulkReadData::ProcessBulkRead(){

  NdbConnection * con = DBA__TheNdb->startTransaction();

  Index tmp = nextSend;

  for(int i = 0; i<OpPerTrans && RowsSubmitted < TotalRows; i++){
    
    const DBA_Binding_t * binding = pBindings[tmp.binding];
    void * data = pData[tmp.datarow].DataPtr;
    
    NdbOperation  * op  = con->getNdbOperation(binding->tableName);
    
    op->simpleRead();
    
    require(DBA__EqualGetValue(op, binding, data));
    
    RowsSubmitted++;
    tmp.next(NbRows);
  }

  con->executeAsynchPrepare(Commit,
			    NewtonCallback,
			    (void*)this,
			    CommitAsMuchAsPossible);

  lastSend = nextSend;
  nextSend = tmp;
}