18 package com.mysql.clusterj.tie;
20 import java.nio.ByteBuffer;
21 import java.nio.CharBuffer;
22 import java.util.List;
24 import com.mysql.ndbjtie.ndbapi.Ndb;
25 import com.mysql.ndbjtie.ndbapi.Ndb.Key_part_ptr;
26 import com.mysql.ndbjtie.ndbapi.Ndb.Key_part_ptrArray;
28 import com.mysql.ndbjtie.ndbapi.NdbErrorConst;
29 import com.mysql.ndbjtie.ndbapi.NdbTransaction;
30 import com.mysql.ndbjtie.ndbapi.NdbDictionary.Dictionary;
31 import com.mysql.ndbjtie.ndbapi.NdbDictionary.TableConst;
33 import com.mysql.clusterj.ClusterJDatastoreException;
34 import com.mysql.clusterj.ClusterJFatalInternalException;
35 import com.mysql.clusterj.core.store.ClusterConnection;
36 import com.mysql.clusterj.core.store.ClusterTransaction;
38 import com.mysql.clusterj.core.util.I18NHelper;
39 import com.mysql.clusterj.core.util.Logger;
40 import com.mysql.clusterj.core.util.LoggerFactoryService;
45 class DbImpl
implements com.mysql.clusterj.core.store.Db {
48 static final I18NHelper local = I18NHelper.getInstance(DbImpl.class);
51 static final Logger logger = LoggerFactoryService.getFactory()
52 .getInstance(com.mysql.clusterj.core.store.ClusterConnection.class);
58 private int errorBufferSize = 300;
61 private ByteBuffer errorBuffer = ByteBuffer.allocateDirect(errorBufferSize);
65 private int coordinatedTransactionIdBufferSize = 26;
68 private ByteBuffer coordinatedTransactionIdBuffer =
69 ByteBuffer.allocateDirect(coordinatedTransactionIdBufferSize);
73 private ByteBuffer partitionKeyScratchBuffer = ByteBuffer.allocateDirect(10000);
76 private BufferManager bufferManager =
new BufferManager();
79 private Dictionary ndbDictionary;
82 private DictionaryImpl dictionary;
85 private ClusterConnection clusterConnection;
87 public DbImpl(ClusterConnection clusterConnection,
Ndb ndb,
int maxTransactions) {
88 this.clusterConnection = clusterConnection;
90 int returnCode = ndb.
init(maxTransactions);
91 handleError(returnCode, ndb);
93 handleError(ndbDictionary, ndb);
94 this.dictionary =
new DictionaryImpl(ndbDictionary);
99 clusterConnection.close(
this);
102 public com.mysql.clusterj.core.store.Dictionary getDictionary() {
106 public ClusterTransaction startTransaction(
String joinTransactionId) {
107 return new ClusterTransactionImpl(
this, ndbDictionary, joinTransactionId);
110 protected void handleError(
int returnCode,
Ndb ndb) {
111 if (returnCode == 0) {
115 String detail = getNdbErrorDetail(ndbError);
116 Utility.throwError(returnCode, ndbError, detail);
120 protected void handleError(Object
object,
Ndb ndb) {
121 if (
object != null) {
125 String detail = getNdbErrorDetail(ndbError);
126 Utility.throwError(null, ndbError, detail);
130 public boolean isRetriable(ClusterJDatastoreException ex) {
131 return Utility.isRetriable(ex);
134 public String getNdbErrorDetail(NdbErrorConst ndbError) {
146 if (keyParts == null || keyParts.size() <= 0) {
147 throw new ClusterJFatalInternalException(
148 local.message(
"ERR_Key_Parts_Must_Not_Be_Null_Or_Zero_Length",
151 int keyPartsSize = keyParts.size();
153 TableConst
table = ndbDictionary.getTable(tableName);
154 handleError(table, ndb);
155 Key_part_ptrArray key_part_ptrArray = null;
156 if (keyPartsSize == 1) {
158 ByteBuffer buffer = keyParts.get(0).buffer;
159 int length = keyParts.get(0).length;
161 if (ndbTransaction == null) {
162 logger.warn(local.message(
"ERR_Transaction_Start_Failed",
163 tableName, buffer.position(), buffer.limit(), buffer.capacity(), length));
165 handleError (ndbTransaction, ndb);
166 return ndbTransaction;
168 key_part_ptrArray = Key_part_ptrArray.create(keyPartsSize + 1);
172 Key_part_ptr key_part_ptr;
173 for (
int i = 0;
i < keyPartsSize; ++
i) {
175 key_part_ptr = key_part_ptrArray.at(
i);
176 key_part_ptr.ptr(keyParts.get(
i).buffer);
177 key_part_ptr.len(keyParts.get(
i).length);
180 key_part_ptr = key_part_ptrArray.at(keyPartsSize);
181 key_part_ptr.ptr(null);
184 table, key_part_ptrArray,
185 partitionKeyScratchBuffer, partitionKeyScratchBuffer.capacity());
186 handleError (ndbTransaction, ndb);
187 return ndbTransaction;
190 Key_part_ptrArray.delete(key_part_ptrArray);
204 if (tableName == null) {
207 TableConst table= ndbDictionary.getTable(tableName);
210 handleError (result, ndb);
220 public ByteBuffer getCoordinatedTransactionIdBuffer() {
221 return coordinatedTransactionIdBuffer;
232 if (logger.isDetailEnabled()) logger.detail(
"CoordinatedTransactionId: "
233 + coordinatedTransactionId);
237 throw new ClusterJFatalInternalException(
"Not Implemented");
244 public BufferManager getBufferManager() {
245 return bufferManager;
257 ByteBuffer stringByteBuffer = null;
258 CharBuffer stringCharBuffer = null;
267 private static final int RESULT_DATA_BUFFER_INITIAL_SIZE = 8000;
270 private ByteBuffer resultDataBuffer = ByteBuffer.allocateDirect(RESULT_DATA_BUFFER_INITIAL_SIZE);
278 if (sizeNeeded > stringStorageBuffer.capacity()) {
279 if (logger.isDebugEnabled()) logger.debug(local.message(
"MSG_Reallocated_Byte_Buffer",
280 "string storage", stringStorageBuffer.capacity(), sizeNeeded));
282 stringStorageBuffer = ByteBuffer.allocateDirect(sizeNeeded);
284 stringStorageBuffer.limit(stringStorageBuffer.capacity());
294 stringByteBuffer.limit(0);
295 return stringByteBuffer;
297 int sizeNeeded = value.length() * 2;
299 stringCharBuffer.append(value);
301 stringByteBuffer.limit(stringCharBuffer.position() * 2);
302 return stringByteBuffer;
309 stringStorageBuffer.clear();
312 public ByteBuffer getStringStorageBuffer(
int sizeNeeded) {
314 return stringStorageBuffer;
317 public ByteBuffer getStringByteBuffer(
int sizeNeeded) {
319 return stringByteBuffer;
328 if (sizeNeeded > stringByteBufferCurrentSize) {
329 stringByteBufferCurrentSize = sizeNeeded;
330 stringByteBuffer = ByteBuffer.allocateDirect(stringByteBufferCurrentSize);
331 stringCharBuffer = stringByteBuffer.asCharBuffer();
333 if (stringByteBuffer == null) {
334 stringByteBuffer = ByteBuffer.allocateDirect(stringByteBufferCurrentSize);
335 stringCharBuffer = stringByteBuffer.asCharBuffer();
337 stringByteBuffer.clear();
338 stringCharBuffer.clear();
347 return stringCharBuffer;
356 if (sizeNeeded > resultDataBuffer.capacity()) {
357 if (logger.isDebugEnabled()) logger.debug(local.message(
"MSG_Reallocated_Byte_Buffer",
358 "result data", resultDataBuffer.capacity(), sizeNeeded));
360 resultDataBuffer = ByteBuffer.allocateDirect(sizeNeeded);
362 resultDataBuffer.clear();
363 return resultDataBuffer;