void GetTableCall::run() { DEBUG_PRINT("GetTableCall::run() [%s.%s]", arg1, arg2); return_val = -1; /* dbName is optional; if not present, set it from ndb database name */ if(strlen(dbName)) { ndb->setDatabaseName(dbName); } else { dbName = ndb->getDatabaseName(); } dict = ndb->getDictionary(); ndb_table = dict->getTable(tableName); if(ndb_table) { /* Ndb object used to create NdbRecords and to cache auto-increment values */ per_table_ndb = new Ndb(& ndb->get_ndb_cluster_connection()); DEBUG_PRINT("per_table_ndb %s.%s %p\n", dbName, tableName, per_table_ndb); per_table_ndb->init(); /* List the indexes */ return_val = dict->listIndexes(idx_list, tableName); } if(return_val == 0) { /* Fetch the indexes now. These calls may perform network IO, populating the (connection) global and (Ndb) local dictionary caches. Later, in the JavaScript main thread, we will call getIndex() again knowing that the caches are populated. */ for(unsigned int i = 0 ; i < idx_list.count ; i++) { const NdbDictionary::Index * idx = dict->getIndex(idx_list.elements[i].name, tableName); /* It is possible to get an index for a recently dropped table rather than the desired table. This is a known bug likely to be fixed later. */ const char * idx_table_name = idx->getTable(); const NdbDictionary::Table * idx_table = dict->getTable(idx_table_name); if(idx_table == 0 || idx_table->getObjectVersion() != ndb_table->getObjectVersion()) { dict->invalidateIndex(idx); idx = dict->getIndex(idx_list.elements[i].name, tableName); } } } else { DEBUG_PRINT("listIndexes() returned %i", return_val); ndbError = & dict->getNdbError(); return; } /* List the foreign keys and keep the list around for doAsyncCallback to create js objects * Currently there is no listForeignKeys so we use the more generic listDependentObjects * specifying the table metadata object. */ return_val = dict->listDependentObjects(fk_list, *ndb_table); if (return_val == 0) { /* Fetch the foreign keys and associated parent tables now. * These calls may perform network IO, populating * the (connection) global and (Ndb) local dictionary caches. Later, * in the JavaScript main thread, we will call getForeignKey() again knowing * that the caches are populated. * We only care about foreign keys where this table is the child table, not the parent table. */ 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; get the parent table ++fk_count; DEBUG_PRINT("Getting ParentTable"); splitter.splitName(fk.getParentTable()); ndb->setDatabaseName(splitter.part1); // temp for next call const NdbDictionary::Table * parent_table = dict->getTable(splitter.part3); ndb->setDatabaseName(dbName); // back to expected value DEBUG_PRINT("Parent table getTable returned %s", parent_table->getName()); } } } } else { DEBUG_PRINT("listDependentObjects() returned %i", return_val); ndbError = & dict->getNdbError(); } }
/* 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); }
/* 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); }