int runDDL(NDBT_Context* ctx, NDBT_Step* step){ Ndb* pNdb= GETNDB(step); NdbDictionary::Dictionary* pDict = pNdb->getDictionary(); const int tables = NDBT_Tables::getNumTables(); while(!ctx->isTestStopped()) { const int tab_no = rand() % (tables); NdbDictionary::Table tab = *NDBT_Tables::getTable(tab_no); BaseString name= tab.getName(); name.appfmt("-%d", step->getStepNo()); tab.setName(name.c_str()); if(pDict->createTable(tab) == 0) { HugoTransactions hugoTrans(* pDict->getTable(name.c_str())); if (hugoTrans.loadTable(pNdb, 10000) != 0){ return NDBT_FAILED; } while(pDict->dropTable(tab.getName()) != 0 && pDict->getNdbError().code != 4009) g_err << pDict->getNdbError() << endl; sleep(1); } } return NDBT_OK; }
int CMT_createTableHook(Ndb* ndb, NdbDictionary::Table& table, int when, void* arg) { if (when == 0) { Uint32 num = ((Uint32*) arg)[0]; Uint32 fragCount = ((Uint32*) arg)[1]; /* Substitute a unique name */ char buf[100]; BaseString::snprintf(buf, sizeof(buf), "%s_%u", table.getName(), num); table.setName(buf); if (fragCount > 0) table.setFragmentCount(fragCount); ndbout << "Creating " << buf << " with fragment count " << fragCount << endl; } return 0; }
static int dropEvent(Ndb *pNdb, const NdbDictionary::Table &tab) { char eventName[1024]; sprintf(eventName,"%s_EVENT",tab.getName()); NdbDictionary::Dictionary *myDict = pNdb->getDictionary(); if (!myDict) { g_err << "Dictionary not found " << pNdb->getNdbError().code << " " << pNdb->getNdbError().message << endl; return NDBT_FAILED; } if (myDict->dropEvent(eventName)) { g_err << "Failed to drop event: " << myDict->getNdbError().code << " : " << myDict->getNdbError().message << endl; return NDBT_FAILED; } return NDBT_OK; }
int create100Tables(NDBT_Context* ctx, NDBT_Step* step) { Ndb* pNdb = GETNDB(step); const NdbDictionary::Table* pTab= ctx->getTab(); /* Run as a 'T1' testcase - do nothing for other tables */ if (strcmp(pTab->getName(), "T1") != 0) return NDBT_OK; for (Uint32 t=0; t < 100; t++) { char tabnameBuff[10]; snprintf(tabnameBuff, sizeof(tabnameBuff), "TAB%u", t); NdbDictionary::Table tab; tab.setName(tabnameBuff); NdbDictionary::Column pk; pk.setName("PK"); pk.setType(NdbDictionary::Column::Varchar); pk.setLength(20); pk.setNullable(false); pk.setPrimaryKey(true); tab.addColumn(pk); pNdb->getDictionary()->dropTable(tab.getName()); if(pNdb->getDictionary()->createTable(tab) != 0) { ndbout << "Create table failed with error : " << pNdb->getDictionary()->getNdbError().code << " " << pNdb->getDictionary()->getNdbError().message << endl; return NDBT_FAILED; } ndbout << "Created table " << tabnameBuff << endl; } return NDBT_OK; }
/* doAsyncCallback() runs in the main thread. We don't want it to block. TODO: verify whether any IO is done by checking WaitMetaRequestCount at the start and end. */ void GetTableCall::doAsyncCallback(Local<Object> ctx) { HandleScope scope; DEBUG_PRINT("GetTableCall::doAsyncCallback: return_val %d", return_val); /* User callback arguments */ Handle<Value> cb_args[2]; cb_args[0] = Null(); cb_args[1] = Null(); /* TableMetadata = { database : "" , // Database name name : "" , // Table Name columns : [] , // ordered array of DBColumn objects indexes : [] , // array of DBIndex objects partitionKey : [] , // ordered array of column numbers in the partition key }; */ if(ndb_table && ! return_val) { Local<Object> table = NdbDictTableEnv.newWrapper(); wrapPointerInObject(ndb_table, NdbDictTableEnv, table); // database table->Set(String::NewSymbol("database"), String::New(arg1)); // name table->Set(String::NewSymbol("name"), String::New(ndb_table->getName())); // columns Local<Array> columns = Array::New(ndb_table->getNoOfColumns()); for(int i = 0 ; i < ndb_table->getNoOfColumns() ; i++) { Handle<Object> col = buildDBColumn(ndb_table->getColumn(i)); columns->Set(i, col); } table->Set(String::NewSymbol("columns"), columns); // indexes (primary key & secondary) Local<Array> js_indexes = Array::New(idx_list.count + 1); js_indexes->Set(0, buildDBIndex_PK()); // primary key for(unsigned int i = 0 ; i < idx_list.count ; i++) { // secondary indexes const NdbDictionary::Index * idx = arg0->dict->getIndex(idx_list.elements[i].name, arg2); js_indexes->Set(i+1, buildDBIndex(idx)); } table->Set(String::NewSymbol("indexes"), js_indexes, ReadOnly); // partitionKey // Table Record (implementation artifact; not part of spec) DEBUG_PRINT("Creating Table Record"); Record * rec = new Record(arg0->dict, ndb_table->getNoOfColumns()); for(int i = 0 ; i < ndb_table->getNoOfColumns() ; i++) { rec->addColumn(ndb_table->getColumn(i)); } rec->completeTableRecord(ndb_table); table->Set(String::NewSymbol("record"), Record_Wrapper(rec)); // User Callback cb_args[1] = table; } else { cb_args[0] = String::New(arg0->dict->getNdbError().message); } callback->Call(ctx, 2, cb_args); }
/* doAsyncCallback() runs in the main thread. We don't want it to block. TODO: verify whether any IO is done by checking WaitMetaRequestCount at the start and end. */ void GetTableCall::doAsyncCallback(Local<Object> ctx) { const char *tableName; HandleScope scope; DEBUG_PRINT("GetTableCall::doAsyncCallback: return_val %d", return_val); /* User callback arguments */ Handle<Value> cb_args[2]; cb_args[0] = Null(); cb_args[1] = Null(); /* TableMetadata = { database : "" , // Database name name : "" , // Table Name columns : [] , // ordered array of DBColumn objects indexes : [] , // array of DBIndex objects partitionKey : [] , // ordered array of column numbers in the partition key }; */ if(ndb_table && ! return_val) { Local<Object> table = NdbDictTableEnv.newWrapper(); const NdbDictionary::Table * js_ndb_table = ndb_table; wrapPointerInObject(js_ndb_table, NdbDictTableEnv, table); // database table->Set(String::NewSymbol("database"), String::New(arg1)); // name tableName = ndb_table->getName(); table->Set(String::NewSymbol("name"), String::New(tableName)); // partitionKey int nPartitionKeys = 0; Handle<Array> partitionKeys = Array::New(); table->Set(String::NewSymbol("partitionKey"), partitionKeys); // columns Local<Array> columns = Array::New(ndb_table->getNoOfColumns()); for(int i = 0 ; i < ndb_table->getNoOfColumns() ; i++) { const NdbDictionary::Column *ndb_col = ndb_table->getColumn(i); Handle<Object> col = buildDBColumn(ndb_col); columns->Set(i, col); if(ndb_col->getPartitionKey()) { /* partition key */ partitionKeys->Set(nPartitionKeys++, String::New(ndb_col->getName())); } } table->Set(String::NewSymbol("columns"), columns); // indexes (primary key & secondary) Local<Array> js_indexes = Array::New(idx_list.count + 1); js_indexes->Set(0, buildDBIndex_PK()); // primary key for(unsigned int i = 0 ; i < idx_list.count ; i++) { // secondary indexes const NdbDictionary::Index * idx = dict->getIndex(idx_list.elements[i].name, arg2); js_indexes->Set(i+1, buildDBIndex(idx)); } table->Set(String::NewSymbol("indexes"), js_indexes, ReadOnly); // Table Record (implementation artifact; not part of spec) DEBUG_PRINT("Creating Table Record"); Record * rec = new Record(dict, ndb_table->getNoOfColumns()); for(int i = 0 ; i < ndb_table->getNoOfColumns() ; i++) { rec->addColumn(ndb_table->getColumn(i)); } rec->completeTableRecord(ndb_table); table->Set(String::NewSymbol("record"), Record_Wrapper(rec)); // foreign keys (only foreign keys for which this table is the child) // now create the javascript foreign key metadata objects for dictionary objects cached earlier Local<Array> js_fks = Array::New(fk_count); int fk_number = 0; for(unsigned int i = 0 ; i < fk_list.count ; i++) { NdbDictionary::ForeignKey fk; if (fk_list.elements[i].type == NdbDictionary::Object::ForeignKey) { const char * fk_name = fk_list.elements[i].name; int fkGetCode = dict->getForeignKey(fk, fk_name); DEBUG_PRINT("getForeignKey for %s returned %i", fk_name, fkGetCode); // see if the foreign key child table is this table if(splitNameMatchesDbAndTable(fk.getChildTable())) { // the foreign key child table is this table; build the fk object DEBUG_PRINT("Adding foreign key for %s at %i", fk.getName(), fk_number); js_fks->Set(fk_number++, buildDBForeignKey(&fk)); } } } table->Set(String::NewSymbol("foreignKeys"), js_fks, ReadOnly); // Autoincrement Cache Impl (also not part of spec) if(per_table_ndb) { table->Set(String::NewSymbol("per_table_ndb"), Ndb_Wrapper(per_table_ndb)); } // User Callback cb_args[1] = table; } else { cb_args[0] = NdbError_Wrapper(* ndbError); } callback->Call(ctx, 2, cb_args); }
static int createEvent(Ndb *pNdb, const NdbDictionary::Table &tab, bool merge_events = true, bool report = true) { char eventName[1024]; sprintf(eventName,"%s_EVENT",tab.getName()); NdbDictionary::Dictionary *myDict = pNdb->getDictionary(); if (!myDict) { g_err << "Dictionary not found " << pNdb->getNdbError().code << " " << pNdb->getNdbError().message << endl; return NDBT_FAILED; } myDict->dropEvent(eventName); NdbDictionary::Event myEvent(eventName); myEvent.setTable(tab.getName()); myEvent.addTableEvent(NdbDictionary::Event::TE_ALL); for(int a = 0; a < tab.getNoOfColumns(); a++) { myEvent.addEventColumn(a); } myEvent.mergeEvents(merge_events); if (report) myEvent.setReport(NdbDictionary::Event::ER_SUBSCRIBE); int res = myDict->createEvent(myEvent); // Add event to database if (res == 0) myEvent.print(); else if (myDict->getNdbError().classification == NdbError::SchemaObjectExists) { g_info << "Event creation failed event exists\n"; res = myDict->dropEvent(eventName); if (res) { g_err << "Failed to drop event: " << myDict->getNdbError().code << " : " << myDict->getNdbError().message << endl; return NDBT_FAILED; } // try again res = myDict->createEvent(myEvent); // Add event to database if (res) { g_err << "Failed to create event (1): " << myDict->getNdbError().code << " : " << myDict->getNdbError().message << endl; return NDBT_FAILED; } } else { g_err << "Failed to create event (2): " << myDict->getNdbError().code << " : " << myDict->getNdbError().message << endl; return NDBT_FAILED; } return NDBT_OK; }
/* doAsyncCallback() runs in the main thread. We don't want it to block. TODO: verify whether any IO is done by checking WaitMetaRequestCount at the start and end. */ void GetTableCall::doAsyncCallback(Local<Object> ctx) { const char *ndbTableName; EscapableHandleScope scope(isolate); DEBUG_PRINT("GetTableCall::doAsyncCallback: return_val %d", return_val); /* User callback arguments */ Handle<Value> cb_args[2]; cb_args[0] = Null(isolate); cb_args[1] = Null(isolate); /* TableMetadata = { database : "" , // Database name name : "" , // Table Name columns : [] , // ordered array of DBColumn objects indexes : [] , // array of DBIndex objects partitionKey : [] , // ordered array of column numbers in the partition key sparseContainer : null // default column for sparse fields }; */ if(ndb_table && ! return_val) { Local<Object> table = NdbDictTableEnv.wrap(ndb_table)->ToObject(); // database table->Set(SYMBOL(isolate, "database"), String::NewFromUtf8(isolate, arg1)); // name ndbTableName = ndb_table->getName(); table->Set(SYMBOL(isolate, "name"), String::NewFromUtf8(isolate, ndbTableName)); // partitionKey int nPartitionKeys = 0; Handle<Array> partitionKeys = Array::New(isolate); table->Set(SYMBOL(isolate, "partitionKey"), partitionKeys); // sparseContainer table->Set(SYMBOL(isolate,"sparseContainer"), Null(isolate)); // columns Local<Array> columns = Array::New(isolate, ndb_table->getNoOfColumns()); for(int i = 0 ; i < ndb_table->getNoOfColumns() ; i++) { const NdbDictionary::Column *ndb_col = ndb_table->getColumn(i); Handle<Object> col = buildDBColumn(ndb_col); columns->Set(i, col); if(ndb_col->getPartitionKey()) { /* partition key */ partitionKeys->Set(nPartitionKeys++, String::NewFromUtf8(isolate, ndb_col->getName())); } if( ! strcmp(ndb_col->getName(), "SPARSE_FIELDS") && ( (! strncmp(getColumnType(ndb_col), "VARCHAR", 7) && (getEncoderCharsetForColumn(ndb_col)->isUnicode)) || ( ! strncmp(getColumnType(ndb_col), "VARBINARY", 9) || ! strncmp(getColumnType(ndb_col), "JSON", 4)))) { table->Set(SYMBOL(isolate,"sparseContainer"), String::NewFromUtf8(isolate, ndb_col->getName())); } } table->Set(SYMBOL(isolate, "columns"), columns); // indexes (primary key & secondary) Local<Array> js_indexes = Array::New(isolate, idx_list.count + 1); js_indexes->Set(0, buildDBIndex_PK()); // primary key for(unsigned int i = 0 ; i < idx_list.count ; i++) { // secondary indexes const NdbDictionary::Index * idx = dict->getIndex(idx_list.elements[i].name, arg2); js_indexes->Set(i+1, buildDBIndex(idx)); } SET_RO_PROPERTY(table, SYMBOL(isolate, "indexes"), js_indexes); // foreign keys (only foreign keys for which this table is the child) // now create the javascript foreign key metadata objects for dictionary objects cached earlier Local<Array> js_fks = Array::New(isolate, fk_count); int fk_number = 0; for(unsigned int i = 0 ; i < fk_list.count ; i++) { NdbDictionary::ForeignKey fk; if (fk_list.elements[i].type == NdbDictionary::Object::ForeignKey) { const char * fk_name = fk_list.elements[i].name; int fkGetCode = dict->getForeignKey(fk, fk_name); DEBUG_PRINT("getForeignKey for %s returned %i", fk_name, fkGetCode); // see if the foreign key child table is this table if(splitNameMatchesDbAndTable(fk.getChildTable())) { // the foreign key child table is this table; build the fk object DEBUG_PRINT("Adding foreign key for %s at %i", fk.getName(), fk_number); js_fks->Set(fk_number++, buildDBForeignKey(&fk)); } } } SET_RO_PROPERTY(table, SYMBOL(isolate, "foreignKeys"), js_fks); // Autoincrement Cache Impl (also not part of spec) if(per_table_ndb) { table->Set(SYMBOL(isolate, "per_table_ndb"), Ndb_Wrapper(per_table_ndb)); } // User Callback cb_args[1] = table; } else { cb_args[0] = NdbError_Wrapper(* ndbError); } ToLocal(& callback)->Call(ctx, 2, cb_args); }