1 
2 /*
3  Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights
4  reserved.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License, version 2.0,
8  as published by the Free Software Foundation.
9 
10  This program is also distributed with certain software (including
11  but not limited to OpenSSL) that is licensed under separate terms,
12  as designated in a particular file or component or in included license
13  documentation.  The authors of MySQL hereby grant you an additional
14  permission to link the program and your derivative works with the
15  separately licensed software that they have included with MySQL.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  GNU General Public License, version 2.0, for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25  02110-1301  USA
26  */
27 
28 #include <my_config.h>
29 
30 #include "Operation.h"
31 #include "TabSeparatedValues.h"
32 
33 
34 /*
35    Class Operation originated as a header-only class and formed a bridge
36    between the "high-level" application code in ndb_worker.cc and the
37    low-level details of using the NDB API.
38 
39    This proved difficult to debug.  On many platforms, gdb's support for
40    inlined functions from header-only classes is poor.
41 
42    A few methods are always compiled here.
43 */
44 
45 
Operation(QueryPlan * p,int o,char * kbuf)46 Operation::Operation(QueryPlan *p, int o, char *kbuf) : key_buffer(kbuf),
47                                                         plan(p),
48                                                         op(o)
49 {
50   set_default_record();
51 }
52 
Operation(workitem * i,Uint32 mask)53 Operation::Operation(workitem *i, Uint32 mask) : key_buffer(i->ndb_key_buffer),
54                                                  plan(i->plan),
55                                                  op(i->base.verb)
56 {
57   set_default_record();
58   if(mask) {
59     row_mask[0] = mask & 0x000000FF;
60     row_mask[1] = mask & 0x0000FF00;
61     row_mask[2] = mask & 0x00FF0000;
62     row_mask[3] = mask & 0xFF000000;
63   }
64 }
65 
Operation(QueryPlan * p,char * buf)66 Operation::Operation(QueryPlan *p, char * buf) : buffer(buf),
67                                                  plan(p),
68                                                  op(OP_READ)
69 {
70   set_default_record();
71 }
72 
73 
set_default_record()74 void Operation::set_default_record() {
75   row_mask[3] = row_mask[2] = row_mask[1] = row_mask[0] = 0;
76   key_mask[3] = key_mask[2] = key_mask[1] = key_mask[0] = 0;
77   read_mask_ptr = 0;
78 
79   if(op == OP_READ) record = plan->val_record;
80   else if(op == OP_FLUSH) record = plan->key_record;  // scanning delete
81   else record = plan->row_record;
82 }
83 
84 
85 /* Methods for reading columns from the response */
86 
87 
getStringValueNoCopy(int idx,char ** dstptr,size_t * lenptr) const88 bool Operation::getStringValueNoCopy(int idx, char **dstptr,
89                                      size_t *lenptr) const {
90   if(record->isNull(idx, buffer)) {
91     *dstptr = 0;
92     *lenptr = 0;
93     return true;
94   }
95   return record->decodeNoCopy(idx, dstptr, lenptr, buffer);
96 }
97 
98 
copyValue(int idx,char * dest) const99 size_t Operation::copyValue(int idx, char *dest) const {
100   if(record->isNull(idx, buffer)) {
101     *dest = 0;
102     return 0;
103   }
104   return record->decodeCopy(idx, dest, buffer);
105 }
106 
107 
108 /* NdbTransaction method wrappers */
109 
startTransaction(Ndb * db) const110 NdbTransaction * Operation::startTransaction(Ndb *db) const {
111   char hash_buffer[512];
112   return db->startTransaction(plan->key_record->ndb_record, key_buffer,
113                               hash_buffer, 512);
114 }
115 
scanIndex(NdbTransaction * tx,NdbIndexScanOperation::IndexBound * bound)116 NdbIndexScanOperation * Operation::scanIndex(NdbTransaction *tx,
117                                              NdbIndexScanOperation::IndexBound *bound) {
118   /* MUST BE ORDERED ASC; used by configuration to read key_prefixes */
119   NdbScanOperation::ScanOptions opts;
120   opts.optionsPresent = NdbScanOperation::ScanOptions::SO_SCANFLAGS;
121   opts.scan_flags = NdbScanOperation::SF_OrderBy;
122 
123   return tx->scanIndex(plan->key_record->ndb_record,            // scan key
124                        plan->row_record->ndb_record,            // row record
125                        NdbOperation::LM_Read,                   // lock mode
126                        (const unsigned char*) 0,                // result mask
127                        bound,                                   // bound
128                        & opts,
129                        sizeof(opts));
130 }
131 
132 
setKey(int nparts,const char * dbkey,size_t key_len)133 bool Operation::setKey(int nparts, const char *dbkey, size_t key_len ) {
134   bool r = true;
135 
136   clearKeyNullBits();
137   if(nparts > 1) {
138     TabSeparatedValues tsv(dbkey, nparts, key_len);
139     int idx = 0;
140     do {
141       if(tsv.getLength()) {
142         DEBUG_PRINT("Set key part %d [%.*s]", idx, tsv.getLength(), tsv.getPointer());
143         if(! setKeyPart(COL_STORE_KEY+idx, tsv.getPointer(), tsv.getLength()))
144           return false;
145       }
146       else {
147         DEBUG_PRINT("Set key part NULL: %d ", idx);
148         setKeyPartNull(COL_STORE_KEY+idx);
149       }
150       idx++;
151     } while (tsv.advance());
152   }
153   else {
154     r = setKeyPart(COL_STORE_KEY, dbkey, key_len);
155   }
156   return r;
157 }
158 
159 
setFieldsInRow(int offset,const char * desc,int nparts,const char * val,size_t len)160 bool Operation::setFieldsInRow(int offset, const char * desc,
161                                int nparts, const char *val, size_t len ) {
162   bool r = true;
163 
164   if(nparts > 1) {
165     TabSeparatedValues tsv(val, nparts, len);
166     int idx = 0;
167     do {
168       if(tsv.getLength()) {
169         DEBUG_PRINT("Set %s part %d [%.*s]", desc, idx, tsv.getLength(), tsv.getPointer());
170         if(! setColumn(offset+idx, tsv.getPointer(), tsv.getLength()))
171           return false;
172       }
173       else {
174         DEBUG_PRINT("Set %s part NULL: %d ", desc, idx);
175         setColumnNull(offset+idx);
176       }
177       idx++;
178     } while (tsv.advance());
179   }
180   else {
181     r = setColumn(offset, val, len);
182   }
183   return r;
184 }
185 
186 
187