VectorCompressFormat.cpp 3.1 KB
Newer Older
C
Cai Yudong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 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.

#include <boost/filesystem.hpp>
#include <memory>

G
groot 已提交
21
#include "codecs/VectorCompressFormat.h"
C
Cai Yudong 已提交
22 23 24 25 26 27 28 29
#include "knowhere/common/BinarySet.h"
#include "utils/Exception.h"
#include "utils/Log.h"
#include "utils/TimeRecorder.h"

namespace milvus {
namespace codec {

G
groot 已提交
30 31 32
const char* VECTOR_COMPRESS_POSTFIX = ".cmp";

std::string
G
groot 已提交
33
VectorCompressFormat::FilePostfix() {
G
groot 已提交
34 35 36 37
    std::string str = VECTOR_COMPRESS_POSTFIX;
    return str;
}

C
Cai Yudong 已提交
38
void
G
groot 已提交
39 40
VectorCompressFormat::Read(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path,
                           knowhere::BinaryPtr& compress) {
G
groot 已提交
41
    milvus::TimeRecorder recorder("SSVectorCompressFormat::Read");
C
Cai Yudong 已提交
42

G
groot 已提交
43 44 45
    const std::string full_file_path = file_path + VECTOR_COMPRESS_POSTFIX;
    if (!fs_ptr->reader_ptr_->open(full_file_path)) {
        LOG_ENGINE_ERROR_ << "Fail to open vector compress: " << full_file_path;
C
Cai Yudong 已提交
46 47 48 49 50
        return;
    }

    int64_t length = fs_ptr->reader_ptr_->length();
    if (length <= 0) {
G
groot 已提交
51
        LOG_ENGINE_ERROR_ << "Invalid vector compress length: " << full_file_path;
C
Cai Yudong 已提交
52 53 54 55 56 57 58 59 60 61 62 63
        return;
    }

    compress->data = std::shared_ptr<uint8_t[]>(new uint8_t[length]);
    compress->size = length;

    fs_ptr->reader_ptr_->seekg(0);
    fs_ptr->reader_ptr_->read(compress->data.get(), length);
    fs_ptr->reader_ptr_->close();

    double span = recorder.RecordSection("End");
    double rate = length * 1000000.0 / span / 1024 / 1024;
G
groot 已提交
64
    LOG_ENGINE_DEBUG_ << "SSVectorCompressFormat::Read(" << full_file_path << ") rate " << rate << "MB/s";
C
Cai Yudong 已提交
65 66 67
}

void
G
groot 已提交
68 69
VectorCompressFormat::Write(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path,
                            const knowhere::BinaryPtr& compress) {
G
groot 已提交
70
    milvus::TimeRecorder recorder("SSVectorCompressFormat::Write");
C
Cai Yudong 已提交
71

G
groot 已提交
72 73 74
    const std::string full_file_path = file_path + VECTOR_COMPRESS_POSTFIX;
    if (!fs_ptr->writer_ptr_->open(full_file_path)) {
        LOG_ENGINE_ERROR_ << "Fail to open vector compress: " << full_file_path;
C
Cai Yudong 已提交
75 76 77 78 79 80 81 82
        return;
    }

    fs_ptr->writer_ptr_->write(compress->data.get(), compress->size);
    fs_ptr->writer_ptr_->close();

    double span = recorder.RecordSection("End");
    double rate = compress->size * 1000000.0 / span / 1024 / 1024;
G
groot 已提交
83
    LOG_ENGINE_DEBUG_ << "SSVectorCompressFormat::Write(" << full_file_path << ") rate " << rate << "MB/s";
C
Cai Yudong 已提交
84 85 86 87
}

}  // namespace codec
}  // namespace milvus