1 /*
2  Copyright (c) 2014, 2016, 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, version 2.0,
6  as published by the Free Software Foundation.
7 
8  This program is also distributed with certain software (including
9  but not limited to OpenSSL) that is licensed under separate terms,
10  as designated in a particular file or component or in included license
11  documentation.  The authors of MySQL hereby grant you an additional
12  permission to link the program and your derivative works with the
13  separately licensed software that they have included with MySQL.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License, version 2.0, for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include <NdbApi.hpp>
26 
27 #include "adapter_global.h"
28 #include "js_wrapper_macros.h"
29 #include "Record.h"
30 #include "NativeMethodCall.h"
31 #include "NdbWrapperErrors.h"
32 #include "ScanOperation.h"
33 
34 using namespace v8;
35 
36 V8WrapperFn newScanOperation;
37 V8WrapperFn prepareAndExecute;
38 V8WrapperFn getOperationError;
39 V8WrapperFn scanNextResult;
40 V8WrapperFn scanFetchResults;
41 V8WrapperFn ScanOperation_close;
42 V8WrapperFn getNdbError;
43 V8WrapperFn ScanOp_readBlobResults;
44 
45 class ScanOperationEnvelopeClass : public Envelope {
46 public:
ScanOperationEnvelopeClass()47   ScanOperationEnvelopeClass() : Envelope("ScanOperation") {
48     addMethod("getNdbError", getNdbError<ScanOperation>);
49     addMethod("prepareAndExecute", prepareAndExecute);
50     addMethod("fetchResults", scanFetchResults);
51     addMethod("nextResult", scanNextResult);
52     addMethod("close", ScanOperation_close);
53     addMethod("readBlobResults", ScanOp_readBlobResults);
54   }
55 };
56 
57 ScanOperationEnvelopeClass ScanOperationEnvelope;
58 
getScanOperationEnvelope()59 Envelope * getScanOperationEnvelope() {
60   return & ScanOperationEnvelope;
61 }
62 
63 
64 // Constructor wrapper
newScanOperation(const Arguments & args)65 void newScanOperation(const Arguments &args) {
66   EscapableHandleScope scope(args.GetIsolate());
67   ScanOperation * s = new ScanOperation(args);
68   Local<Value> wrapper = ScanOperationEnvelope.wrap(s);
69   // freeFromGC: Disabled as it leads to segfaults during garbage collection
70   // ScanOperationEnvelope.freeFromGC(helper, wrapper);
71   args.GetReturnValue().Set(scope.Escape(wrapper));
72 }
73 
74 // void prepareAndExecute()
75 // ASYNC
prepareAndExecute(const Arguments & args)76 void prepareAndExecute(const Arguments &args) {
77   EscapableHandleScope scope(args.GetIsolate());
78   DEBUG_MARKER(UDEB_DEBUG);
79   REQUIRE_ARGS_LENGTH(1);
80   typedef NativeMethodCall_0_<int, ScanOperation> MCALL;
81   MCALL * mcallptr = new MCALL(& ScanOperation::prepareAndExecute, args);
82   mcallptr->errorHandler = getNdbErrorIfLessThanZero;
83   mcallptr->runAsync();
84 
85   args.GetReturnValue().SetUndefined();
86 }
87 
88 // void close()
89 // ASYNC
ScanOperation_close(const Arguments & args)90 void ScanOperation_close(const Arguments & args) {
91   typedef NativeVoidMethodCall_0_<ScanOperation> NCALL;
92   NCALL * ncallptr = new NCALL(& ScanOperation::close, args);
93   ncallptr->runAsync();
94   args.GetReturnValue().SetUndefined();
95 }
96 
97 // int nextResult(buffer)
98 // IMMEDIATE
scanNextResult(const Arguments & args)99 void scanNextResult(const Arguments & args) {
100   DEBUG_MARKER(UDEB_DETAIL);
101   EscapableHandleScope scope(args.GetIsolate());
102   typedef NativeMethodCall_1_<int, ScanOperation, char *> MCALL;
103   MCALL mcall(& ScanOperation::nextResult, args);
104   mcall.run();
105   args.GetReturnValue().Set(scope.Escape(mcall.jsReturnVal()));
106 }
107 
108 
109 // int fetchResults(buffer, forceSend, callback)
110 // ASYNC; CALLBACK GETS (Null-Or-Error, Int)
scanFetchResults(const Arguments & args)111 void scanFetchResults(const Arguments & args) {
112   DEBUG_MARKER(UDEB_DETAIL);
113   REQUIRE_ARGS_LENGTH(3);
114   typedef NativeMethodCall_2_<int, ScanOperation, char *, bool> MCALL;
115   MCALL * ncallptr = new MCALL(& ScanOperation::fetchResults, args);
116   ncallptr->errorHandler = getNdbErrorIfLessThanZero;
117   ncallptr->runAsync();
118   args.GetReturnValue().SetUndefined();
119 }
120 
ScanOp_readBlobResults(const Arguments & args)121 void ScanOp_readBlobResults(const Arguments & args) {
122   ScanOperation * op = unwrapPointer<ScanOperation *>(args.Holder());
123   op->readBlobResults(args);
124 }
125 
126 #define WRAP_CONSTANT(TARGET, X) DEFINE_JS_INT(TARGET, #X, NdbScanOperation::X)
127 
ScanHelper_initOnLoad(Handle<Object> target)128 void ScanHelper_initOnLoad(Handle<Object> target) {
129   Local<Object> scanObj = Object::New(Isolate::GetCurrent());
130   Local<String> scanKey = NEW_SYMBOL("Scan");
131   target->Set(scanKey, scanObj);
132 
133   DEFINE_JS_FUNCTION(scanObj, "create", newScanOperation);
134 
135   Local<Object> ScanHelper = Object::New(Isolate::GetCurrent());
136   Local<Object> ScanFlags =  Object::New(Isolate::GetCurrent());
137 
138   scanObj->Set(NEW_SYMBOL("helper"), ScanHelper);
139   scanObj->Set(NEW_SYMBOL("flags"), ScanFlags);
140 
141   WRAP_CONSTANT(ScanFlags, SF_TupScan);
142   WRAP_CONSTANT(ScanFlags, SF_DiskScan);
143   WRAP_CONSTANT(ScanFlags, SF_OrderBy);
144   WRAP_CONSTANT(ScanFlags, SF_OrderByFull);
145   WRAP_CONSTANT(ScanFlags, SF_Descending);
146   WRAP_CONSTANT(ScanFlags, SF_ReadRangeNo);
147   WRAP_CONSTANT(ScanFlags, SF_MultiRange);
148   WRAP_CONSTANT(ScanFlags, SF_KeyInfo);
149 
150   DEFINE_JS_INT(ScanHelper, "table_record", SCAN_TABLE_RECORD);
151   DEFINE_JS_INT(ScanHelper, "index_record", SCAN_INDEX_RECORD);
152   DEFINE_JS_INT(ScanHelper, "lock_mode", SCAN_LOCK_MODE);
153   DEFINE_JS_INT(ScanHelper, "bounds", SCAN_BOUNDS);
154   DEFINE_JS_INT(ScanHelper, "flags", SCAN_OPTION_FLAGS);
155   DEFINE_JS_INT(ScanHelper, "batch_size", SCAN_OPTION_BATCH_SIZE);
156   DEFINE_JS_INT(ScanHelper, "parallel", SCAN_OPTION_PARALLELISM);
157   DEFINE_JS_INT(ScanHelper, "filter_code", SCAN_FILTER_CODE);
158 }
159 
160