Beispiel #1
0
/**
 * 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;
}
Beispiel #2
0
/**
 * 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;
}
Beispiel #3
0
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;
}
Beispiel #4
0
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;
}
    void scanDescending(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 (endKey.empty()) {
            if (!cursor->jump_back()) {
                delete cursor;
                return;
            }
        } else {
            if (!cursor->jump_back(endKey)) {
                delete cursor;
                return;
            }
        }
        string key, value;
        while (cursor->get(&key, &value, false /* step */)) {
            if (!endKeyIncluded && key == endKey) {
                cursor->step_back();
                continue;
            }
            if (startKeyIncluded && startKey > key) {
                break;
            }
            if (!startKeyIncluded && startKey >= 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;
            }
            cursor->step_back();
        }
        delete cursor;
    }
Beispiel #6
0
/**
 * 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;
}
Beispiel #7
0
// 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;
}
Beispiel #8
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;

}
Beispiel #9
0
/**
 * 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;
}
Beispiel #10
0
/**
 * 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;
}