/** * Lay nhung ItemID co luot viewscount lon nhat. * @param number * @return vector<string> */ vector<string> ItemDB::getListTopItemID(int64_t number) { int n = grassDB.count(); vector < string> itemsReturn; vector < pair<int, string > > allItems; if (n == 0) { return itemsReturn; } DB::Cursor* cur = grassDB.cursor(); cur->jump(); string ckey, cvalue; while (cur->get(&ckey, &cvalue, true)) { try { Item item = convertJsonToItem(cvalue); allItems.push_back(std::make_pair(item.likeCounts, ckey)); } catch (char* str) { continue; //cout << "error make pair:" << str << endl; } } sort(allItems.begin(), allItems.end(), boost::bind(&std::pair<int, string>::first, _1) > boost::bind(&std::pair<int, string>::first, _2) ); int64_t temp = allItems.size(); if (number > temp) { number = temp; } for (int64_t i = 0; i < number; i++) { itemsReturn.push_back(allItems[i].second); } return itemsReturn; }
vector<Tag> TagDB::getAllTag() { vector<Tag> listTag; DB::Cursor* cur = grassDB.cursor(); cur->jump(); string ckey, cvalue; while (cur->get(&ckey, &cvalue, true)) { Tag tag = convertJsonToTag(cvalue); tag.tagID = ckey; listTag.push_back(tag); } delete cur; //cout << "t" << listTag.size() << endl; return listTag; }
vector<Item> ItemDB::getItemsPageKeyword(string keyWord, int64_t pageNumber, int64_t itemNumber) { vector<Item> lItem; if (pageNumber < 1) { poco_error(*logger, "getItemsPage: pageNumber < 0"); return lItem; } if (itemNumber < 1) { poco_error(*logger, "getItemsPage: numberItems < 0"); return lItem; } int64_t size = grassDB.count(); int totalPageNumber; if (size % itemNumber == 0) totalPageNumber = size / itemNumber; else totalPageNumber = size / itemNumber + 1; if (pageNumber > totalPageNumber) { poco_warning_f2(*logger, "getItemsPage: pageNumber = %d > totalPageNumber = %d", pageNumber, totalPageNumber); pageNumber = totalPageNumber; } int64_t first = (pageNumber - 1) * itemNumber; int64_t last = pageNumber * itemNumber; int count = 0; string key, value; DB::Cursor* cur = grassDB.cursor(); cur->jump(); Item item; while (cur->get(&key, &value, true)) { if (Utils::findStringInString(value, keyWord)) { count++; if (count < first) continue; item = convertJsonToItem(value); item.itemID = key; lItem.push_back(item); if (count >= last) break; } } delete cur; return lItem; }
/** * Lấy listItem mà content có chứa từ khóa keyword. * @param keyword * @return vector<Item> */ vector<Item> ItemDB::getItemKeyword(string keyword) { vector<Item> result; if (keyword.empty()) { poco_error(*logger, "getItemKeyword: keyword is empty."); return result; } DB::Cursor* cur = grassDB.cursor(); cur->jump(); string ckey, cvalue; while (cur->get(&ckey, &cvalue, true)) { if (Utils::findStringInString(cvalue, keyword)) { Item item = getItemFromItemID(ckey); item.itemID = ckey; result.push_back(item); } } delete cur; return result; }
// main routine int main(int argc, char** argv) { // create the database object TreeDB db; // open the database if (!db.open("casket.kch", PolyDB::OWRITER | PolyDB::OCREATE)) { cerr << "open error: " << db.error().name() << endl; } // store records if (!db.set("foo", "hop") || !db.set("bar", "step") || !db.set("baz", "jump")) { cerr << "set error: " << db.error().name() << endl; } // retrieve a record string value; if (db.get("foo", &value)) { cout << value << endl; } else { cerr << "get error: " << db.error().name() << endl; } // traverse records DB::Cursor* cur = db.cursor(); cur->jump(); string ckey, cvalue; while (cur->get(&ckey, &cvalue, true)) { cout << ckey << ":" << cvalue << endl; } delete cur; // close the database if (!db.close()) { cerr << "close error: " << db.error().name() << endl; } return 0; }
vector<Tag> TagDB::getTagKeyword(string keyword) { vector<Tag> result; if (keyword.empty()) { poco_error(*logger, "getTagKeyword: keyword is NULL."); return result; } DB::Cursor* cur = grassDB.cursor(); cur->jump(); string ckey, cvalue; while (cur->get(&ckey, &cvalue, true)) { if (Utils::findStringInStringForTag(cvalue, keyword)) { Tag tag = convertJsonToTag(cvalue); tag.tagID = ckey; result.push_back(tag); } } delete cur; return result; }
/** * Lay list number Item theo keyword. * @param keyword * @param numberItems * @return vector<Item> */ vector<Item> ItemDB::getItemKeyword(string keyword, int32_t numberItems) { vector<Item> result; if (keyword.empty()) { poco_error(*logger, "getItemKeyword: keyword is empty."); return result; } int32_t i = 0; DB::Cursor* cur = grassDB.cursor(); cur->jump(); string ckey, cvalue; while (cur->get(&ckey, &cvalue, true)) { if (Utils::findStringInString(cvalue, keyword)) { i++; Item item = convertJsonToItem(cvalue); item.itemID = ckey; result.push_back(item); } if (i == numberItems) break; } delete cur; return result; }
void scanAscending(RecordListResponse& _return, TreeDB* db, const std::string& startKey, const bool startKeyIncluded, const std::string& endKey, const bool endKeyIncluded, const int32_t maxRecords, const int32_t maxBytes) { int numBytes = 0; DB::Cursor* cursor = db->cursor(); _return.responseCode = ResponseCode::ScanEnded; if (!cursor->jump(startKey)) { delete cursor; return; } string key, value; while (cursor->get(&key, &value, true /* step */)) { if (!startKeyIncluded && key == startKey) { continue; } if (!endKey.empty()) { if (endKeyIncluded && endKey < key) { break; } if (!endKeyIncluded && endKey <= key) { break; } } Record record; record.key = key; record.value = value; numBytes += record.key.size() + record.value.size(); _return.records.push_back(record); if (_return.records.size() >= (uint32_t)maxRecords || numBytes >= maxBytes) { _return.responseCode = ResponseCode::Success; break; } } delete cursor; }
IteratorTripleID *TriplesKyoto::search(TripleID &pattern) { #if 1 cout << endl << "DB size: " << db.count() << endl; DB::Cursor *cur = db.cursor(); cur->jump(); size_t count = 0; size_t total = db.count(); while(count<total-1) { size_t fsize; TripleID *ptr = (TripleID*)cur->get_key(&fsize, true); cout << "Triple: " << *ptr << endl; count++; } cout << "Iterated" << endl; delete cur; db.close(); cout << "Closed" << endl; exit(0); #endif string patternString = pattern.getPatternString(); if(patternString=="???") { return new TriplesKyotoIterator(&db); } else { return new SequentialSearchIteratorTripleID(pattern, new TriplesKyotoIterator(&db)); } }
/** * Get random number item trong ItemDB, neu number lon hon so record thi se set number=so record * @param number * @return vector<Item> */ vector<Item> ItemDB::getAllItems(int64_t number) { vector<Item> result; if (number == 0 || number<-1) return result; int64_t size = grassDB.count(); if (number > size || number == -1) number = size; if (number == size) { DB::Cursor* cur = grassDB.cursor(); cur->jump(); string itemID, content; while (cur->get(&itemID, &content, true)) { Item item = convertJsonToItem(content); item.itemID = itemID; result.push_back(item); } } else { int64_t index = Utils::getRandomNumber(size); while (grassDB.check(Utils::convertIntToString(index)) == -1) index = Utils::getRandomNumber(size); string itemID, content; string keyStart = Utils::convertIntToString(index); DB::Cursor* cur = grassDB.cursor(); cur->jump(keyStart); int64_t i = 0; while (i < number) { if (!cur->get(&itemID, &content, true)) cur->jump(); Item item = convertJsonToItem(content); item.itemID = itemID; result.push_back(item); i++; } } //poco_information_f1(*logger, "getAllItems: Get %d items.", number); return result; }
unsigned char *next() { cur->get_key(&key, true); count++; return (unsigned char*)(key.c_str()); }
KyotoDictIterator(DB *db) : db(db), cur(db->cursor()), count(0) { cur->jump(); }
/** * Lấy List Item theo kiểu phân trang, nếu tagID==-1 thì sẽ lấy trong ItemDB, * tagID>1 thì sẽ lấy Item thuộc tagID đó. * @param pageNumber * @param itemNumber * @param tagID * @return vector<Item> */ vector<Item> ItemDB::getItemsPage(int64_t pageNumber, int32_t numberItems, string tagID, ItemTagDB& itemTagDB) { vector<Item> lItem; if (pageNumber < 1) { poco_error(*logger, "getItemsPage: pageNumber < 0"); return lItem; } if (numberItems < 1) { poco_error(*logger, "getItemsPage: numberItems < 0"); return lItem; } Item item; if (tagID == "-1") { int64_t size = grassDB.count(); int totalPageNumber; if (size % numberItems == 0) totalPageNumber = size / numberItems; else totalPageNumber = size / numberItems + 1; if (pageNumber > totalPageNumber) { poco_warning_f2(*logger, "getItemsPage: pageNumber = %d > totalPageNumber = %d", pageNumber, totalPageNumber); pageNumber = totalPageNumber; } int64_t last = pageNumber * numberItems; int64_t first = (pageNumber - 1) * numberItems - 1; int64_t i = 0; string key, value; DB::Cursor* cur = grassDB.cursor(); cur->jump(); while (i < last) { cur->get(&key, &value, true); if (i > first) { item = convertJsonToItem(value); item.itemID = key; lItem.push_back(item); } i++; } delete cur; } else { vector<string> lItemID = itemTagDB.getAllItemsIdHaveTag(tagID); int64_t sizeLItemID = lItemID.size(); int totalPageNumber; if (sizeLItemID % numberItems == 0) totalPageNumber = sizeLItemID / numberItems; else totalPageNumber = sizeLItemID / numberItems + 1; if (pageNumber > totalPageNumber) { poco_warning_f2(*logger, "getItemsPage: pageNumber = %d > totalPageNumber = %d", pageNumber, totalPageNumber); pageNumber = totalPageNumber; } int64_t last = pageNumber * numberItems; int64_t first = (pageNumber - 1) * numberItems - 1; int64_t i = 0; if (!lItemID.empty()) { for (i = first + 1; i < last; i++) { if (i == sizeLItemID) return lItem; lItem.push_back(getItemFromItemID(lItemID[i])); } } } return lItem; }