MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bulk_copy.cpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 
20 #include <NdbOut.hpp>
21 #include <NdbApi.hpp>
22 #include <NdbSleep.h>
23 #include <NDBT.hpp>
24 
25 #include <getarg.h>
26 
27 
28 int setValuesFromLine(NdbOperation* pOp,
29  const NdbDictionary::Table* pTab,
30  char* line){
31 
32  int check = 0;
33  char* p = line;
34  char* pn;
35  char buf[8000];
36  // Loop through each attribute in this table
37  for (int a = 0; a<pTab->getNoOfColumns(); a++){
38 
39  pn = p;
40  while (*pn != ';')
41  pn++;
42 
43  memset(buf, 0, sizeof(buf));
44  strncpy(buf, p, pn-p);
45  // ndbout << a << ": " << buf << endl;
46  const NdbDictionary::Column* attr = pTab->getColumn(a);
47  switch (attr->getType()){
49  Int32 sval;
50  if (sscanf(buf, "%d", &sval) == 0)
51  return -2;
52  check = pOp->setValue(a, sval);
53  break;
54 
56  Uint32 uval;
57  if (sscanf(buf, "%u", &uval) == 0)
58  return -2;
59  check = pOp->setValue(a, uval);
60  break;
61 
63  char buf2[8000];
64  char* p2;
65  memset(buf2, 0, sizeof(buf));
66  p2 = &buf2[0];
67  while(*p != ';'){
68  *p2 = *p;
69  p++;p2++;
70  };
71  *p2 = 0;
72  check = pOp->setValue(a, buf2);
73  break;
74 
75  default:
76  check = -2;
77  break;
78  }
79 
80  // Move pointer to after next ";"
81  while (*p != ';')
82  p++;
83  p++;
84 
85  }
86 
87  return check;
88 }
89 
90 
91 int insertLine(Ndb* pNdb,
92  const NdbDictionary::Table* pTab,
93  char* line){
94  int check;
95  int retryAttempt = 0;
96  int retryMax = 5;
97  NdbConnection *pTrans;
98  NdbOperation *pOp;
99 
100  while (retryAttempt < retryMax){
101 
102  pTrans = pNdb->startTransaction();
103  if (pTrans == NULL) {
104  ERR(pNdb->getNdbError());
105  NdbSleep_MilliSleep(50);
106  retryAttempt++;
107  continue;
108  }
109 
110  pOp = pTrans->getNdbOperation(pTab->getName());
111  if (pOp == NULL) {
112  ERR(pTrans->getNdbError());
113  pNdb->closeTransaction(pTrans);
114  return -1;
115  }
116 
117  check = pOp->insertTuple();
118  if( check == -1 ) {
119  ERR(pTrans->getNdbError());
120  pNdb->closeTransaction(pTrans);
121  return -1;
122  }
123 
124  check = setValuesFromLine(pOp,
125  pTab,
126  line);
127  if (check == -2){
128  pNdb->closeTransaction(pTrans);
129  return -2;
130  }
131  if( check == -1 ) {
132  ERR(pTrans->getNdbError());
133  pNdb->closeTransaction(pTrans);
134  return -1;
135  }
136 
137 
138  // Execute the transaction and insert the record
139  check = pTrans->execute( Commit );
140  if(check == -1 ) {
141  const NdbError err = pTrans->getNdbError();
142  pNdb->closeTransaction(pTrans);
143 
144  switch(err.status){
145  case NdbError::Success:
146  ERR(err);
147  ndbout << "ERROR: NdbError reports success when transcaction failed" << endl;
148  return -1;
149  break;
150 
152  ERR(err);
153  NdbSleep_MilliSleep(50);
154  retryAttempt++;
155  continue;
156  break;
157 
159  ERR(err);
160  return -1;
161  break;
162 
164  switch (err.classification){
166  // Tuple already existed, OK in this application, but should be reported
167  ndbout << err.code << " " << err.message << endl;
168  break;
169  default:
170  ERR(err);
171  return -1;
172  break;
173  }
174  break;
175  }
176  }
177  else{
178 
179  pNdb->closeTransaction(pTrans);
180  }
181  return 0;
182  }
183  return check;
184 }
185 
186 int insertFile(Ndb* pNdb,
187  const NdbDictionary::Table* pTab,
188  const char* fileName){
189 
190  const int MAX_LINE_LEN = 8000;
191  char line[MAX_LINE_LEN];
192  int lineNo = 0;
193 
194  FILE* instr = fopen(fileName, "r");
195  if (instr == NULL){
196  ndbout << "Coul'd not open " << fileName << endl;
197  return -1;
198  }
199 
200  while(fgets(line, MAX_LINE_LEN, instr)){
201  lineNo++;
202 
203  if (line[strlen(line)-1] == '\n') {
204  line[strlen(line)-1] = '\0';
205  }
206 
207  int check = insertLine(pNdb, pTab, line);
208  if (check == -2){
209  ndbout << "Wrong format in input data file, line: " << lineNo << endl;
210  fclose(instr);
211  return -1;
212  }
213  if (check == -1){
214  fclose(instr);
215  return -1;
216 
217  }
218  }
219 
220  fclose(instr);
221  return 0;
222 }
223 
224 
225 int main(int argc, const char** argv){
226  ndb_init();
227 
228  const char* _tabname = NULL;
229  int _help = 0;
230 
231  struct getargs args[] = {
232  { "usage", '?', arg_flag, &_help, "Print help", "" }
233  };
234  int num_args = sizeof(args) / sizeof(args[0]);
235  int optind = 0;
236  char desc[] =
237  "tabname\n"\
238  "This program will bulk copy data from a file to a table in Ndb.\n";
239 
240  if(getarg(args, num_args, argc, argv, &optind) ||
241  argv[optind] == NULL || _help) {
242  arg_printusage(args, num_args, argv[0], desc);
243  return NDBT_ProgramExit(NDBT_WRONGARGS);
244  }
245  _tabname = argv[optind];
246  ndbout << "Tablename: " << _tabname << endl;
247 
248  // Connect to Ndb
249  Ndb MyNdb( "TEST_DB" );
250 
251  if(MyNdb.init() != 0){
252  ERR(MyNdb.getNdbError());
253  return NDBT_ProgramExit(NDBT_FAILED);
254  }
255 
256  // Connect to Ndb and wait for it to become ready
257  while(MyNdb.waitUntilReady() != 0)
258  ndbout << "Waiting for ndb to become ready..." << endl;
259 
260  // Check if table exists in db
261  const NdbDictionary::Table* pTab = MyNdb.getDictionary()->getTable(_tabname);
262  if(pTab == NULL){
263  ndbout << " Table " << _tabname << " does not exist!" << endl;
264  return NDBT_ProgramExit(NDBT_WRONGARGS);
265  }
266 
267  char buf[255];
268  BaseString::snprintf(buf, sizeof(buf), "%s.data", (const char*)_tabname);
269  if (insertFile(&MyNdb, pTab, buf) != 0){
270  return NDBT_ProgramExit(NDBT_FAILED);
271  }
272 
273  return NDBT_ProgramExit(NDBT_OK);
274 
275 }
276 
277 
278