string HTWorker::remove_shared(const ZPack &zpack) { string result; if (zpack.key().empty()) return Const::ZSC_REC_EMPTYKEY; //-1 string key = zpack.key(); int ret = PMAP->remove(key); if (ret != 0) { printf("thread[%lu] DB Error: fail to remove, rcode = %d\n", pthread_self(), ret); fflush(stdout); result = Const::ZSC_REC_NONEXISTKEY; //-92 } else { if (_instant_swap) { PMAP->writeFileFG(); } result = Const::ZSC_REC_SUCC; //0, succeed. } return result; }
string HTWorker::state_change_callback_internal(const ZPack &zpack) { string result; if (zpack.key().empty()) return Const::ZSC_REC_EMPTYKEY; //-1 string key = zpack.key(); string *ret = PMAP->get(key); if (ret == NULL) { printf("thread[%lu] DB Error: lookup found nothing\n", pthread_self()); fflush(stdout); result = Const::ZSC_REC_NONEXISTKEY; } else { ZPack rltpack; rltpack.ParseFromString(*ret); if (zpack.val() == rltpack.val()) { result = Const::ZSC_REC_SUCC; //0, succeed. } else { result = Const::ZSC_REC_SCCBPOLLTRY; } } return result; }
string HTWorker::lookup_shared(const ZPack &zpack) { string result; if (zpack.key().empty()) return Const::ZSC_REC_EMPTYKEY; //-1 string key = zpack.key(); string *ret = PMAP->get(key); if (ret == NULL) { printf("thread[%lu] DB Error: lookup found nothing\n", pthread_self()); fflush(stdout); result = Const::ZSC_REC_NONEXISTKEY; result.append("Empty"); } else { result = Const::ZSC_REC_SUCC; result.append(*ret); } return result; }
string HTWorker::append_shared(const ZPack &zpack) { string result; if (zpack.key().empty()) return Const::ZSC_REC_EMPTYKEY; //-1 string key = zpack.key(); int ret = PMAP->append(key, zpack.SerializeAsString()); if (ret != 0) { printf("thread[%lu] DB Error: fail to append, rcode = %d\n", pthread_self(), ret); fflush(stdout); result = Const::ZSC_REC_NONEXISTKEY; //-92 } else { if (_instant_swap) { PMAP->writeFile(); //PMAP->flushDbfile(); } result = Const::ZSC_REC_SUCC; //0, succeed. } return result; }
string HTWorker::run(const char *buf) { string result; ZPack zpack; //string str(buf); string* str =(string*) buf; zpack.ParseFromString(*str); if(ZPack_Pack_type_BATCH_REQ == zpack.pack_type()){//batch cout << "HTWrorker::run(): ZPack_Pack_type_BATCH_REQ received."<< endl; cout << "Batch contains "<< zpack.batch_item_size() << " items."<<endl; cout <<"zpack.key: "<< zpack.key() <<endl; cout <<"zpack.batch_item(i).val: "<<zpack.batch_item(0).val() << endl<< endl; result = Const::ZSC_REC_UOPC; // "OK"; }else if(ZPack_Pack_type_SINGLE == zpack.pack_type()){//single if (zpack.opcode() == Const::ZSC_OPC_LOOKUP) { result = lookup(zpack); } else if (zpack.opcode() == Const::ZSC_OPC_INSERT) { result = insert(zpack); } else if (zpack.opcode() == Const::ZSC_OPC_APPEND) { result = append(zpack); } else if (zpack.opcode() == Const::ZSC_OPC_CMPSWP) { result = compare_swap(zpack); } else if (zpack.opcode() == Const::ZSC_OPC_REMOVE) { result = remove(zpack); } else if (zpack.opcode() == Const::ZSC_OPC_STCHGCB) { result = state_change_callback(zpack); } else { result = Const::ZSC_REC_UOPC; } } return result; }
string HTWorker::compare_swap(const ZPack &zpack) { if (zpack.key().empty()) return Const::ZSC_REC_EMPTYKEY; //-1 string result = compare_swap_internal(zpack); string lkpresult = lookup_shared(zpack); result.append(erase_status_code(lkpresult)); #ifdef SCCB _stub->sendBack(_addr, result.data(), result.size()); return ""; #else return result; #endif }