コード例 #1
0
    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;
    }