bool ISTree::modify(int _key, const char* _str, unsigned _len) { if (_key < 0) { printf("error in ISTree-modify: empty string\n"); return false; } this->CopyToTransfer(_str, _len, 2); //not check value const Bstr* val = &(this->transfer[2]); this->request = 0; int store; ISNode* ret = this->find(_key, &store, true); if (ret == NULL || store == -1 || _key != ret->getKey(store)) //tree is empty or not found { cerr<<"tree is empty or not found"<<endl; return false; } //cout<<"ISTree::modify() - key is found, now to remove"<<endl; unsigned len = ret->getValue(store)->getLen(); ret->setValue(val, store, true); //cout<<"value reset"<<endl; //cout<<"newlen: "<<val->getLen()<<" oldlen: "<<len<<endl; //request += (val->getLen() - len); this->request = val->getLen(); this->request -= len; ret->setDirty(); //cout<<"to request"<<endl; this->TSM->request(request); //cout<<"memory requested"<<endl; return true; }
bool ISTree::search(unsigned _key, char*& _str, unsigned& _len) { //DEBUG //if (_key < 0) //{ //printf("error in ISTree-search: empty string\n"); //return false; //} this->request = 0; int store; ISNode* ret = this->find(_key, &store, false); //cout<<"to find the position: "<<store<<endl; if (ret == NULL || store == -1 || _key != ret->getKey(store)) //tree is empty or not found { return false; } const Bstr* val = ret->getValue(store); //this->CopyToTransfer(val->getStr(), val->getLen(), 0); //not sum to request //_str = this->transfer[0].getStr(); //_len = this->transfer[0].getLen(); _str = val->getStr(); _len = val->getLen(); char* debug = new char[_len]; memcpy(debug, _str, _len); // debug[_len] = '\0'; _str = debug; //if(_key==62) //{ //cout<<"check in search: "<<string(_str, _len)<<endl; //} this->TSM->request(request); //if(_key==62) //{ //cout<<"check in search: "<<string(_str, _len)<<endl; //} return true; }
bool ISTree::search(int _key, char*& _str, int& _len) { if (_key < 0) { //printf("error in ISTree-search: empty string\n"); return false; } this->request = 0; int store; ISNode* ret = this->find(_key, &store, false); if (ret == NULL || store == -1 || _key != ret->getKey(store)) //tree is empty or not found { return false; } const Bstr* val = ret->getValue(store); this->CopyToTransfer(val->getStr(), val->getLen(), 0); //not sum to request _str = this->transfer[0].getStr(); _len = this->transfer[0].getLen(); this->TSM->request(request); return true; }
bool ISTree::insert(int _key, const char* _str, unsigned _len) { if (_key < 0) { printf("error in ISTree-insert: empty string\n"); return false; } this->CopyToTransfer(_str, _len, 2); const Bstr* val = &(this->transfer[2]); this->request = 0; ISNode* ret; if (this->root == NULL) //tree is empty { leaves_tail = leaves_head = root = new ISLeafNode; request += ISNode::LEAF_SIZE; this->height = 1; root->setHeight(1); //add to heap later } //this->prepare(this->root); //root must be in-mem if (root->getNum() == ISNode::MAX_KEY_NUM) { ISNode* father = new ISIntlNode; request += ISNode::INTL_SIZE; father->addChild(root, 0); ret = root->split(father, 0); if (ret->isLeaf() && ret->getNext() == NULL) this->leaves_tail = ret; if (ret->isLeaf()) request += ISNode::LEAF_SIZE; else request += ISNode::INTL_SIZE; this->height++; //height rises only when root splits //WARN: height area in Node: 4 bit! father->setHeight(this->height); //add to heap later this->TSM->updateHeap(ret, ret->getRank(), false); this->root = father; } ISNode* p = this->root; ISNode* q; int i; while (!p->isLeaf()) { //j = p->getNum(); //for(i = 0; i < j; ++i) //if(bstr < *(p->getKey(i))) //break; //NOTICE: using binary search is better here i = p->searchKey_less(_key); q = p->getChild(i); this->prepare(q); if (q->getNum() == ISNode::MAX_KEY_NUM) { ret = q->split(p, i); if (ret->isLeaf() && ret->getNext() == NULL) this->leaves_tail = ret; if (ret->isLeaf()) request += ISNode::LEAF_SIZE; else request += ISNode::INTL_SIZE; //BETTER: in loop may update multiple times this->TSM->updateHeap(ret, ret->getRank(), false); this->TSM->updateHeap(q, q->getRank(), true); this->TSM->updateHeap(p, p->getRank(), true); if (_key < p->getKey(i)) p = q; else p = ret; } else { p->setDirty(); this->TSM->updateHeap(p, p->getRank(), true); p = q; } } //j = p->getNum(); //for(i = 0; i < j; ++i) //if(bstr < *(p->getKey(i))) //break; i = p->searchKey_less(_key); //insert existing key is ok, but not inserted in //however, the tree-shape may change due to possible split in former code bool ifexist = false; if (i > 0 && _key == p->getKey(i - 1)) ifexist = true; else { p->addKey(_key, i); p->addValue(val, i, true); p->addNum(); request += val->getLen(); p->setDirty(); this->TSM->updateHeap(p, p->getRank(), true); //_key->clear(); //_value->clear(); } this->TSM->request(request); return !ifexist; //QUERY(which case:return false) }