📄 testblobs.cpp
字号:
CHK(setBlobValue(g_bh1, tup.m_blob1) == 0); if (! g_opt.m_oneblob) CHK(setBlobValue(g_bh2, tup.m_blob2) == 0); return 0;}static intgetBlobValue(NdbBlob* h, const Bval& v){ bool null = (v.m_val == 0); DBG("getValue " << h->getColumn()->getName() << " buflen=" << v.m_buflen); CHK(h->getValue(v.m_buf, v.m_buflen) == 0); return 0;}static intgetBlobValue(const Tup& tup){ CHK(getBlobValue(g_bh1, tup.m_blob1) == 0); if (! g_opt.m_oneblob) CHK(getBlobValue(g_bh2, tup.m_blob2) == 0); return 0;}static intverifyBlobValue(NdbBlob* h, const Bval& v){ bool null = (v.m_val == 0); bool isNull; unsigned len; if (null) { isNull = false; CHK(h->getNull(isNull) == 0 && isNull == true); CHK(getBlobLength(h, len) == 0 && len == 0); } else { isNull = true; CHK(h->getNull(isNull) == 0 && isNull == false); CHK(getBlobLength(h, len) == 0 && len == v.m_len); for (unsigned i = 0; i < v.m_len; i++) CHK(v.m_val[i] == v.m_buf[i]); } return 0;}static intverifyBlobValue(const Tup& tup){ CHK(verifyBlobValue(g_bh1, tup.m_blob1) == 0); if (! g_opt.m_oneblob) CHK(verifyBlobValue(g_bh2, tup.m_blob2) == 0); return 0;}// readData / writeDatastatic intwriteBlobData(NdbBlob* h, const Bval& v){ bool null = (v.m_val == 0); bool isNull; unsigned len; DBG("write " << h->getColumn()->getName() << " len=" << v.m_len << " null=" << null); if (null) { CHK(h->setNull() == 0); isNull = false; CHK(h->getNull(isNull) == 0 && isNull == true); CHK(getBlobLength(h, len) == 0 && len == 0); } else { CHK(h->truncate(v.m_len) == 0); unsigned n = 0; do { unsigned m = g_opt.m_full ? v.m_len : urandom(v.m_len + 1); if (m > v.m_len - n) m = v.m_len - n; DBG("write pos=" << n << " cnt=" << m); CHK(h->writeData(v.m_val + n, m) == 0); n += m; } while (n < v.m_len); assert(n == v.m_len); isNull = true; CHK(h->getNull(isNull) == 0 && isNull == false); CHK(getBlobLength(h, len) == 0 && len == v.m_len); } return 0;}static intwriteBlobData(const Tup& tup){ CHK(writeBlobData(g_bh1, tup.m_blob1) == 0); if (! g_opt.m_oneblob) CHK(writeBlobData(g_bh2, tup.m_blob2) == 0); return 0;}static intreadBlobData(NdbBlob* h, const Bval& v){ bool null = (v.m_val == 0); bool isNull; unsigned len; DBG("read " << h->getColumn()->getName() << " len=" << v.m_len << " null=" << null); if (null) { isNull = false; CHK(h->getNull(isNull) == 0 && isNull == true); CHK(getBlobLength(h, len) == 0 && len == 0); } else { isNull = true; CHK(h->getNull(isNull) == 0 && isNull == false); CHK(getBlobLength(h, len) == 0 && len == v.m_len); v.trash(); unsigned n = 0; while (n < v.m_len) { unsigned m = g_opt.m_full ? v.m_len : urandom(v.m_len + 1); if (m > v.m_len - n) m = v.m_len - n; DBG("read pos=" << n << " cnt=" << m); const unsigned m2 = m; CHK(h->readData(v.m_buf + n, m) == 0); CHK(m2 == m); n += m; } assert(n == v.m_len); // need to execute to see the data CHK(g_con->execute(NoCommit) == 0); for (unsigned i = 0; i < v.m_len; i++) CHK(v.m_val[i] == v.m_buf[i]); } return 0;}static intreadBlobData(const Tup& tup){ CHK(readBlobData(g_bh1, tup.m_blob1) == 0); if (! g_opt.m_oneblob) CHK(readBlobData(g_bh2, tup.m_blob2) == 0); return 0;}// hooksstatic NdbBlob::ActiveHook blobWriteHook;static intblobWriteHook(NdbBlob* h, void* arg){ DBG("blobWriteHook"); Bval& v = *(Bval*)arg; CHK(writeBlobData(h, v) == 0); return 0;}static intsetBlobWriteHook(NdbBlob* h, Bval& v){ DBG("setBlobWriteHook"); CHK(h->setActiveHook(blobWriteHook, &v) == 0); return 0;}static intsetBlobWriteHook(Tup& tup){ CHK(setBlobWriteHook(g_bh1, tup.m_blob1) == 0); if (! g_opt.m_oneblob) CHK(setBlobWriteHook(g_bh2, tup.m_blob2) == 0); return 0;}static NdbBlob::ActiveHook blobReadHook;// no PK yet to identify tuple so just read the valuestatic intblobReadHook(NdbBlob* h, void* arg){ DBG("blobReadHook"); Bval& v = *(Bval*)arg; unsigned len; CHK(getBlobLength(h, len) == 0); v.alloc(len); Uint32 maxlen = 0xffffffff; CHK(h->readData(v.m_buf, maxlen) == 0); DBG("read " << maxlen << " bytes"); CHK(len == maxlen); return 0;}static intsetBlobReadHook(NdbBlob* h, Bval& v){ DBG("setBlobReadHook"); CHK(h->setActiveHook(blobReadHook, &v) == 0); return 0;}static intsetBlobReadHook(Tup& tup){ CHK(setBlobReadHook(g_bh1, tup.m_blob1) == 0); if (! g_opt.m_oneblob) CHK(setBlobReadHook(g_bh2, tup.m_blob2) == 0); return 0;}// verify blob datastatic intverifyHeadInline(const Bcol& c, const Bval& v, NdbRecAttr* ra){ if (v.m_val == 0) { CHK(ra->isNULL() == 1); } else { CHK(ra->isNULL() == 0); const NdbBlob::Head* head = (const NdbBlob::Head*)ra->aRef(); CHK(head->length == v.m_len); const char* data = (const char*)(head + 1); for (unsigned i = 0; i < head->length && i < c.m_inline; i++) CHK(data[i] == v.m_val[i]); } return 0;}static intverifyHeadInline(const Tup& tup){ DBG("verifyHeadInline pk1=" << hex << tup.m_pk1); CHK((g_con = g_ndb->startTransaction()) != 0); CHK((g_opr = g_con->getNdbOperation(g_opt.m_tname)) != 0); CHK(g_opr->readTuple() == 0); CHK(g_opr->equal("PK1", tup.m_pk1) == 0); if (g_opt.m_pk2len != 0) CHK(g_opr->equal("PK2", tup.m_pk2) == 0); NdbRecAttr* ra1; NdbRecAttr* ra2; CHK((ra1 = g_opr->getValue("BL1")) != 0); if (! g_opt.m_oneblob) CHK((ra2 = g_opr->getValue("BL2")) != 0); if (tup.m_exists) { CHK(g_con->execute(Commit) == 0); DBG("verifyHeadInline BL1"); CHK(verifyHeadInline(g_opt.m_blob1, tup.m_blob1, ra1) == 0); if (! g_opt.m_oneblob) { DBG("verifyHeadInline BL2"); CHK(verifyHeadInline(g_opt.m_blob2, tup.m_blob2, ra2) == 0); } } else { CHK(g_con->execute(Commit) == -1 && g_con->getNdbError().code == 626); } g_ndb->closeTransaction(g_con); g_opr = 0; g_con = 0; return 0;}static intverifyBlobTable(const Bcol& b, const Bval& v, Uint32 pk1, bool exists){ DBG("verify " << b.m_btname << " pk1=" << hex << pk1); NdbRecAttr* ra_pk; NdbRecAttr* ra_part; NdbRecAttr* ra_data; CHK((g_con = g_ndb->startTransaction()) != 0); CHK((g_ops = g_con->getNdbScanOperation(b.m_btname)) != 0); CHK(g_ops->readTuples() == 0); CHK((ra_pk = g_ops->getValue("PK")) != 0); CHK((ra_part = g_ops->getValue("PART")) != 0); CHK((ra_data = g_ops->getValue("DATA")) != 0); CHK(g_con->execute(NoCommit) == 0); unsigned partcount; if (! exists || v.m_len <= b.m_inline) partcount = 0; else partcount = (v.m_len - b.m_inline + b.m_partsize - 1) / b.m_partsize; char* seen = new char [partcount]; memset(seen, 0, partcount); while (1) { int ret; CHK((ret = g_ops->nextResult()) == 0 || ret == 1); if (ret == 1) break; if (pk1 != ra_pk->u_32_value()) continue; Uint32 part = ra_part->u_32_value(); DBG("part " << part << " of " << partcount); const char* data = ra_data->aRef(); CHK(part < partcount && ! seen[part]); seen[part] = 1; unsigned n = b.m_inline + part * b.m_partsize; assert(exists && v.m_val != 0 && n < v.m_len); unsigned m = v.m_len - n; if (m > b.m_partsize) m = b.m_partsize; CHK(memcmp(data, v.m_val + n, m) == 0); } for (unsigned i = 0; i < partcount; i++) CHK(seen[i] == 1); g_ndb->closeTransaction(g_con); g_ops = 0; g_con = 0; return 0;}static intverifyBlobTable(const Tup& tup){ CHK(verifyBlobTable(g_opt.m_blob1, tup.m_blob1, tup.m_pk1, tup.m_exists) == 0); if (! g_opt.m_oneblob) CHK(verifyBlobTable(g_opt.m_blob2, tup.m_blob2, tup.m_pk1, tup.m_exists) == 0); return 0;}static intverifyBlob(){ for (unsigned k = 0; k < g_opt.m_rows; k++) { const Tup& tup = g_tups[k]; DBG("verifyBlob pk1=" << hex << tup.m_pk1); CHK(verifyHeadInline(tup) == 0); CHK(verifyBlobTable(tup) == 0); } return 0;}// operationsstatic const char* stylename[3] = { "style=getValue/setValue", "style=setActiveHook", "style=readData/writeData"};// pk opsstatic intinsertPk(int style){ DBG("--- insertPk " << stylename[style] << " ---"); unsigned n = 0; CHK((g_con = g_ndb->startTransaction()) != 0); for (unsigned k = 0; k < g_opt.m_rows; k++) { Tup& tup = g_tups[k]; DBG("insertPk pk1=" << hex << tup.m_pk1); CHK((g_opr = g_con->getNdbOperation(g_opt.m_tname)) != 0); CHK(g_opr->insertTuple() == 0); CHK(g_opr->equal("PK1", tup.m_pk1) == 0); if (g_opt.m_pk2len != 0) CHK(g_opr->equal("PK2", tup.m_pk2) == 0); CHK(getBlobHandles(g_opr) == 0); if (style == 0) { CHK(setBlobValue(tup) == 0); } else if (style == 1) { // non-nullable must be set CHK(g_bh1->setValue("", 0) == 0); CHK(setBlobWriteHook(tup) == 0); } else { // non-nullable must be set CHK(g_bh1->setValue("", 0) == 0); CHK(g_con->execute(NoCommit) == 0); CHK(writeBlobData(tup) == 0); } if (++n == g_opt.m_batch) { CHK(g_con->execute(Commit) == 0); g_ndb->closeTransaction(g_con); CHK((g_con = g_ndb->startTransaction()) != 0); n = 0; } g_opr = 0; tup.m_exists = true; } if (n != 0) { CHK(g_con->execute(Commit) == 0); n = 0; } g_ndb->closeTransaction(g_con); g_con = 0; return 0;}static intreadPk(int style){ DBG("--- readPk " << stylename[style] << " ---"); for (unsigned k = 0; k < g_opt.m_rows; k++) { Tup& tup = g_tups[k]; DBG("readPk pk1=" << hex << tup.m_pk1); CHK((g_con = g_ndb->startTransaction()) != 0); CHK((g_opr = g_con->getNdbOperation(g_opt.m_tname)) != 0); CHK(g_opr->readTuple() == 0); CHK(g_opr->equal("PK1", tup.m_pk1) == 0); if (g_opt.m_pk2len != 0) CHK(g_opr->equal("PK2", tup.m_pk2) == 0); CHK(getBlobHandles(g_opr) == 0); if (style == 0) { CHK(getBlobValue(tup) == 0); } else if (style == 1) { CHK(setBlobReadHook(tup) == 0); } else { CHK(g_con->execute(NoCommit) == 0); CHK(readBlobData(tup) == 0); } CHK(g_con->execute(Commit) == 0); if (style == 0 || style == 1) { CHK(verifyBlobValue(tup) == 0); } g_ndb->closeTransaction(g_con); g_opr = 0; g_con = 0; } return 0;}static intupdatePk(int style){ DBG("--- updatePk " << stylename[style] << " ---"); for (unsigned k = 0; k < g_opt.m_rows; k++) { Tup& tup = g_tups[k]; DBG("updatePk pk1=" << hex << tup.m_pk1); CHK((g_con = g_ndb->startTransaction()) != 0); CHK((g_opr = g_con->getNdbOperation(g_opt.m_tname)) != 0); CHK(g_opr->updateTuple() == 0); CHK(g_opr->equal("PK1", tup.m_pk1) == 0); if (g_opt.m_pk2len != 0) CHK(g_opr->equal("PK2", tup.m_pk2) == 0); CHK(getBlobHandles(g_opr) == 0); if (style == 0) { CHK(setBlobValue(tup) == 0); } else if (style == 1) { CHK(setBlobWriteHook(tup) == 0); } else { CHK(g_con->execute(NoCommit) == 0); CHK(writeBlobData(tup) == 0); } CHK(g_con->execute(Commit) == 0); g_ndb->closeTransaction(g_con); g_opr = 0; g_con = 0; tup.m_exists = true; } return 0;}static intwritePk(int style){ DBG("--- writePk " << stylename[style] << " ---"); for (unsigned k = 0; k < g_opt.m_rows; k++) { Tup& tup = g_tups[k]; DBG("writePk pk1=" << hex << tup.m_pk1); CHK((g_con = g_ndb->startTransaction()) != 0); CHK((g_opr = g_con->getNdbOperation(g_opt.m_tname)) != 0); CHK(g_opr->writeTuple() == 0); CHK(g_opr->equal("PK1", tup.m_pk1) == 0); if (g_opt.m_pk2len != 0) CHK(g_opr->equal("PK2", tup.m_pk2) == 0); CHK(getBlobHandles(g_opr) == 0); if (style == 0) { CHK(setBlobValue(tup) == 0); } else if (style == 1) { // non-nullable must be set CHK(g_bh1->setValue("", 0) == 0); CHK(setBlobWriteHook(tup) == 0); } else { // non-nullable must be set CHK(g_bh1->setValue("", 0) == 0); CHK(g_con->execute(NoCommit) == 0); CHK(writeBlobData(tup) == 0); } CHK(g_con->execute(Commit) == 0); g_ndb->closeTransaction(g_con); g_opr = 0; g_con = 0; tup.m_exists = true; } return 0;}static intdeletePk()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -