Utils.cpp 7.7 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

Y
yukun 已提交
18 19
#include "examples/utils/Utils.h"
#include "examples/utils/TimeRecorder.h"
G
groot 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

#include <time.h>
#include <unistd.h>
#include <iostream>
#include <memory>
#include <utility>
#include <vector>

namespace milvus_sdk {

constexpr int64_t SECONDS_EACH_HOUR = 3600;

#define BLOCK_SPLITER std::cout << "===========================================" << std::endl;

std::string
Utils::CurrentTime() {
    time_t tt;
    time(&tt);
    tt = tt + 8 * SECONDS_EACH_HOUR;
    tm t;
    gmtime_r(&tt, &t);

    std::string str = std::to_string(t.tm_year + 1900) + "_" + std::to_string(t.tm_mon + 1) + "_" +
                      std::to_string(t.tm_mday) + "_" + std::to_string(t.tm_hour) + "_" + std::to_string(t.tm_min) +
                      "_" + std::to_string(t.tm_sec);

    return str;
}

std::string
Utils::CurrentTmDate(int64_t offset_day) {
    time_t tt;
    time(&tt);
    tt = tt + 8 * SECONDS_EACH_HOUR;
    tt = tt + 24 * SECONDS_EACH_HOUR * offset_day;
    tm t;
    gmtime_r(&tt, &t);

    std::string str =
        std::to_string(t.tm_year + 1900) + "-" + std::to_string(t.tm_mon + 1) + "-" + std::to_string(t.tm_mday);

    return str;
}

void
Utils::Sleep(int seconds) {
    std::cout << "Waiting " << seconds << " seconds ..." << std::endl;
    sleep(seconds);
}

const std::string&
Utils::GenTableName() {
    static std::string s_id("tbl_" + CurrentTime());
    return s_id;
}

std::string
Utils::MetricTypeName(const milvus::MetricType& metric_type) {
    switch (metric_type) {
        case milvus::MetricType::L2:
            return "L2 distance";
        case milvus::MetricType::IP:
            return "Inner product";
        default:
            return "Unknown metric type";
    }
}

std::string
Utils::IndexTypeName(const milvus::IndexType& index_type) {
    switch (index_type) {
Y
yudong.cai 已提交
91 92 93 94 95 96
        case milvus::IndexType::FLAT:
            return "FLAT";
        case milvus::IndexType::IVFFLAT:
            return "IVFFLAT";
        case milvus::IndexType::IVFSQ8:
            return "IVFSQ8";
Y
Yukikaze-CZR 已提交
97
        case milvus::IndexType::RNSG:
Y
yudong.cai 已提交
98 99 100
            return "NSG";
        case milvus::IndexType::IVFSQ8H:
            return "IVFSQ8H";
101 102 103 104 105 106
        case milvus::IndexType::IVFPQ:
            return "IVFPQ";
        case milvus::IndexType::SPTAGKDT:
            return "SPTAGKDT";
        case milvus::IndexType::SPTAGBKT:
            return "SPTAGBKT";
G
groot 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
        default:
            return "Unknown index type";
    }
}

void
Utils::PrintTableSchema(const milvus::TableSchema& tb_schema) {
    BLOCK_SPLITER
    std::cout << "Table name: " << tb_schema.table_name << std::endl;
    std::cout << "Table dimension: " << tb_schema.dimension << std::endl;
    std::cout << "Table index file size: " << tb_schema.index_file_size << std::endl;
    std::cout << "Table metric type: " << MetricTypeName(tb_schema.metric_type) << std::endl;
    BLOCK_SPLITER
}

void
Utils::PrintPartitionParam(const milvus::PartitionParam& partition_param) {
    BLOCK_SPLITER
    std::cout << "Table name: " << partition_param.table_name << std::endl;
    std::cout << "Partition name: " << partition_param.partition_name << std::endl;
    std::cout << "Partition tag: " << partition_param.partition_tag << std::endl;
    BLOCK_SPLITER
}

void
Utils::PrintIndexParam(const milvus::IndexParam& index_param) {
    BLOCK_SPLITER
    std::cout << "Index table name: " << index_param.table_name << std::endl;
    std::cout << "Index type: " << IndexTypeName(index_param.index_type) << std::endl;
    std::cout << "Index nlist: " << index_param.nlist << std::endl;
    BLOCK_SPLITER
}

void
Utils::BuildVectors(int64_t from, int64_t to, std::vector<milvus::RowRecord>& vector_record_array,
                    std::vector<int64_t>& record_ids, int64_t dimension) {
    if (to <= from) {
        return;
    }

    vector_record_array.clear();
    record_ids.clear();
    for (int64_t k = from; k < to; k++) {
        milvus::RowRecord record;
        record.data.resize(dimension);
        for (int64_t i = 0; i < dimension; i++) {
            record.data[i] = (float)(k % (i + 1));
        }

        vector_record_array.emplace_back(record);
        record_ids.push_back(k);
    }
}

void
Utils::PrintSearchResult(const std::vector<std::pair<int64_t, milvus::RowRecord>>& search_record_array,
G
groot 已提交
163
                         const milvus::TopKQueryResult& topk_query_result) {
G
groot 已提交
164
    BLOCK_SPLITER
165
    std::cout << "Returned result count: " << topk_query_result.size() << std::endl;
G
groot 已提交
166

167 168 169 170 171 172 173 174 175 176
    if (topk_query_result.size() != search_record_array.size()) {
        std::cout << "ERROR: Returned result count dones equal nq" << std::endl;
        return;
    }

    for (size_t i = 0; i < topk_query_result.size(); i++) {
        const milvus::QueryResult& one_result = topk_query_result[i];
        size_t topk = one_result.ids.size();
        auto search_id = search_record_array[i].first;
        std::cout << "No." << i << " vector " << search_id << " top " << topk << " search result:" << std::endl;
G
groot 已提交
177
        for (size_t j = 0; j < topk; j++) {
178
            std::cout << "\t" << one_result.ids[j] << "\t" << one_result.distances[j] << std::endl;
G
groot 已提交
179 180 181 182 183 184 185
        }
    }
    BLOCK_SPLITER
}

void
Utils::CheckSearchResult(const std::vector<std::pair<int64_t, milvus::RowRecord>>& search_record_array,
G
groot 已提交
186
                         const milvus::TopKQueryResult& topk_query_result) {
G
groot 已提交
187
    BLOCK_SPLITER
188
    size_t nq = topk_query_result.size();
G
groot 已提交
189
    for (size_t i = 0; i < nq; i++) {
190 191 192
        const milvus::QueryResult& one_result = topk_query_result[i];
        auto search_id = search_record_array[i].first;
        int64_t result_id = one_result.ids[0];
G
groot 已提交
193 194 195
        if (result_id != search_id) {
            std::cout << "The top 1 result is wrong: " << result_id << " vs. " << search_id << std::endl;
        } else {
196
            std::cout << "No." << i << " Check result successfully" << std::endl;
G
groot 已提交
197 198 199 200 201 202 203
        }
    }
    BLOCK_SPLITER
}

void
Utils::DoSearch(std::shared_ptr<milvus::Connection> conn, const std::string& table_name,
G
groot 已提交
204
                const std::vector<std::string>& partition_tags, int64_t top_k, int64_t nprobe,
G
groot 已提交
205
                const std::vector<std::pair<int64_t, milvus::RowRecord>>& search_record_array,
G
groot 已提交
206
                milvus::TopKQueryResult& topk_query_result) {
207
    topk_query_result.clear();
G
groot 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

    std::vector<milvus::Range> query_range_array;
    milvus::Range rg;
    rg.start_value = CurrentTmDate();
    rg.end_value = CurrentTmDate(1);
    query_range_array.emplace_back(rg);

    std::vector<milvus::RowRecord> record_array;
    for (auto& pair : search_record_array) {
        record_array.push_back(pair.second);
    }

    {
        BLOCK_SPLITER
        milvus_sdk::TimeRecorder rc("search");
G
groot 已提交
223
        milvus::Status stat =
G
groot 已提交
224
            conn->Search(table_name, partition_tags, record_array, query_range_array, top_k, nprobe, topk_query_result);
G
groot 已提交
225 226 227 228
        std::cout << "SearchVector function call status: " << stat.message() << std::endl;
        BLOCK_SPLITER
    }

G
groot 已提交
229 230
    PrintSearchResult(search_record_array, topk_query_result);
    CheckSearchResult(search_record_array, topk_query_result);
G
groot 已提交
231 232 233
}

}  // namespace milvus_sdk