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("VectorCompressFormat::Read");
C
Cai Yudong 已提交
42

G
groot 已提交
43
    const std::string full_file_path = file_path + VECTOR_COMPRESS_POSTFIX;
44 45
    if (!fs_ptr->reader_ptr_->Open(full_file_path)) {
        THROW_ERROR(SERVER_CANNOT_OPEN_FILE, "Fail to open vector compress file: " + full_file_path);
C
Cai Yudong 已提交
46 47
    }

48
    int64_t length = fs_ptr->reader_ptr_->Length();
C
Cai Yudong 已提交
49
    if (length <= 0) {
50
        THROW_ERROR(SERVER_UNEXPECTED_ERROR, "Invalid vector compress length: " + full_file_path);
C
Cai Yudong 已提交
51 52 53 54 55
    }

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

56 57 58
    fs_ptr->reader_ptr_->Seekg(0);
    fs_ptr->reader_ptr_->Read(compress->data.get(), length);
    fs_ptr->reader_ptr_->Close();
C
Cai Yudong 已提交
59 60 61

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

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

G
groot 已提交
70
    const std::string full_file_path = file_path + VECTOR_COMPRESS_POSTFIX;
71 72
    if (!fs_ptr->writer_ptr_->Open(full_file_path)) {
        THROW_ERROR(SERVER_CANNOT_OPEN_FILE, "Fail to open vector compress: " + full_file_path);
C
Cai Yudong 已提交
73 74
    }

75 76
    fs_ptr->writer_ptr_->Write(compress->data.get(), compress->size);
    fs_ptr->writer_ptr_->Close();
C
Cai Yudong 已提交
77 78 79

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

}  // namespace codec
}  // namespace milvus