DBMetaImpl.cpp 6.8 KB
Newer Older
X
Xu Peng 已提交
1 2
#include <sys/stat.h>
#include <unistd.h>
X
Xu Peng 已提交
3 4
#include <sstream>
#include <iostream>
X
Xu Peng 已提交
5
#include <boost/filesystem.hpp>
X
Xu Peng 已提交
6
#include <fstream>
X
Xu Peng 已提交
7
#include <sqlite_orm/sqlite_orm.h>
X
Xu Peng 已提交
8 9
#include "DBMetaImpl.h"
#include "IDGenerator.h"
X
Xu Peng 已提交
10 11 12 13

namespace zilliz {
namespace vecwise {
namespace engine {
14
namespace meta {
X
Xu Peng 已提交
15

X
Xu Peng 已提交
16 17
using namespace sqlite_orm;

X
Xu Peng 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30
inline auto StoragePrototype(const std::string& path) {
    return make_storage(path,
            make_table("Groups",
                      make_column("id", &GroupSchema::id, primary_key()),
                      make_column("group_id", &GroupSchema::group_id, unique()),
                      make_column("dimension", &GroupSchema::dimension),
                      make_column("files_cnt", &GroupSchema::files_cnt, default_value(0))));

}

using ConnectorT = decltype(StoragePrototype(""));
static std::unique_ptr<ConnectorT> ConnectorPtr;

X
Xu Peng 已提交
31 32 33 34 35 36 37
long GetFileSize(const std::string& filename)
{
    struct stat stat_buf;
    int rc = stat(filename.c_str(), &stat_buf);
    return rc == 0 ? stat_buf.st_size : -1;
}

X
Xu Peng 已提交
38 39
DBMetaImpl::DBMetaImpl(const DBMetaOptions& options_)
    : _options(options_) {
X
Xu Peng 已提交
40 41 42 43
    initialize();
}

Status DBMetaImpl::initialize() {
X
Xu Peng 已提交
44 45
    if (!boost::filesystem::is_directory(_options.path)) {
        assert(boost::filesystem::create_directory(_options.path));
X
Xu Peng 已提交
46
    }
X
Xu Peng 已提交
47

X
Xu Peng 已提交
48
    ConnectorPtr = std::make_unique<ConnectorT>(StoragePrototype(_options.path+"/meta.sqlite"));
X
Xu Peng 已提交
49

X
Xu Peng 已提交
50
    ConnectorPtr->sync_schema();
X
Xu Peng 已提交
51

X
Xu Peng 已提交
52
    return Status::OK();
X
Xu Peng 已提交
53 54
}

55 56 57 58 59 60 61 62 63
Status DBMetaImpl::add_group(GroupSchema& group_info) {
    if (group_info.group_id == "") {
        std::stringstream ss;
        SimpleIDGenerator g;
        ss << g.getNextIDNumber();
        group_info.group_id = ss.str();
    }
    group_info.files_cnt = 0;
    group_info.id = -1;
X
Xu Peng 已提交
64
    try {
65
        auto id = ConnectorPtr->insert(group_info);
X
Xu Peng 已提交
66
        std::cout << "id=" << id << std::endl;
67
        group_info.id = id;
X
Xu Peng 已提交
68
    } catch(std::system_error& e) {
69
        return Status::GroupError("Add Group " + group_info.group_id + " Error");
X
Xu Peng 已提交
70
    }
X
Xu Peng 已提交
71
    return Status::OK();
X
Xu Peng 已提交
72 73
}

X
Xu Peng 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87
Status DBMetaImpl::get_group(GroupSchema& group_info) {
    auto groups = ConnectorPtr->select(columns(&GroupSchema::id,
                                              &GroupSchema::group_id,
                                              &GroupSchema::files_cnt,
                                              &GroupSchema::dimension),
                                      where(c(&GroupSchema::group_id) == group_info.group_id));
    assert(groups.size() <= 1);
    if (groups.size() == 1) {
        group_info.id = std::get<0>(groups[0]);
        group_info.files_cnt = std::get<2>(groups[0]);
        group_info.dimension = std::get<3>(groups[0]);
    } else {
        return Status::NotFound("Group " + group_info.group_id + " not found");
    }
X
Xu Peng 已提交
88

X
Xu Peng 已提交
89 90
    std::cout << __func__ << ": gid=" << group_info.group_id
        << " dimension=" << group_info.dimension << std::endl;
X
Xu Peng 已提交
91
    return Status::OK();
X
Xu Peng 已提交
92 93
}

94
Status DBMetaImpl::has_group(const std::string& group_id_, bool& has_or_not_) {
X
Xu Peng 已提交
95
    //PXU TODO
X
Xu Peng 已提交
96
    return Status::OK();
X
Xu Peng 已提交
97 98
}

99
Status DBMetaImpl::add_group_file(const std::string& group_id,
X
Xu Peng 已提交
100 101
                              GroupFileSchema& group_file_info,
                              GroupFileSchema::FILE_TYPE file_type) {
102 103 104 105 106
    return add_group_file(group_id, Meta::GetDate(), group_file_info);
}

Status DBMetaImpl::add_group_file(const std::string& group_id,
                              DateT date,
X
Xu Peng 已提交
107 108
                              GroupFileSchema& group_file_info,
                              GroupFileSchema::FILE_TYPE file_type) {
X
Xu Peng 已提交
109
    //PXU TODO
110 111
    std::stringstream ss;
    SimpleIDGenerator g;
X
Xu Peng 已提交
112
    std::string suffix = (file_type == GroupFileSchema::RAW) ? ".raw" : ".index";
X
Xu Peng 已提交
113 114
    /* ss << "/tmp/test/" << date */
    ss << _options.path << "/" << date
115
                       << "/" << g.getNextIDNumber()
X
Xu Peng 已提交
116
                       << suffix;
117 118 119 120
    group_file_info.group_id = "1";
    group_file_info.dimension = 64;
    group_file_info.location = ss.str();
    group_file_info.date = date;
X
Xu Peng 已提交
121
    return Status::OK();
X
Xu Peng 已提交
122 123
}

X
Xu Peng 已提交
124 125 126 127
Status DBMetaImpl::files_to_index(GroupFilesSchema& files) {
    // PXU TODO
    files.clear();
    std::stringstream ss;
X
Xu Peng 已提交
128 129
    /* ss << "/tmp/test/" << Meta::GetDate(); */
    ss << _options.path << "/" << Meta::GetDate();
X
Xu Peng 已提交
130 131 132
    boost::filesystem::path path(ss.str().c_str());
    boost::filesystem::directory_iterator end_itr;
    for (boost::filesystem::directory_iterator itr(path); itr != end_itr; ++itr) {
X
Xu Peng 已提交
133
        /* std::cout << itr->path().string() << std::endl; */
X
Xu Peng 已提交
134 135
        GroupFileSchema f;
        f.location = itr->path().string();
X
Xu Peng 已提交
136 137 138 139 140
        std::string suffixStr = f.location.substr(f.location.find_last_of('.') + 1);
        if (suffixStr == "index") continue;
        if (1024*1024*1000 >= GetFileSize(f.location)) continue;
        std::cout << "[About to index] " << f.location << std::endl;
        f.date = Meta::GetDate();
X
Xu Peng 已提交
141 142 143 144 145
        files.push_back(f);
    }
    return Status::OK();
}

X
Xu Peng 已提交
146 147 148 149 150
Status DBMetaImpl::files_to_merge(const std::string& group_id,
        DatePartionedGroupFilesSchema& files) {
    //PXU TODO
    files.clear();
    std::stringstream ss;
X
Xu Peng 已提交
151 152
    /* ss << "/tmp/test/" << Meta::GetDate(); */
    ss << _options.path << "/" << Meta::GetDate();
X
Xu Peng 已提交
153 154 155 156 157 158
    boost::filesystem::path path(ss.str().c_str());
    boost::filesystem::directory_iterator end_itr;
    GroupFilesSchema gfiles;
    DateT date = Meta::GetDate();
    files[date] = gfiles;
    for (boost::filesystem::directory_iterator itr(path); itr != end_itr; ++itr) {
X
Xu Peng 已提交
159
        /* std::cout << itr->path().string() << std::endl; */
X
Xu Peng 已提交
160 161
        GroupFileSchema f;
        f.location = itr->path().string();
X
Xu Peng 已提交
162 163 164
        std::string suffixStr = f.location.substr(f.location.find_last_of('.') + 1);
        if (suffixStr == "index") continue;
        if (1024*1024*1000 < GetFileSize(f.location)) continue;
X
Xu Peng 已提交
165
        std::cout << "About to merge " << f.location << std::endl;
X
Xu Peng 已提交
166 167 168 169
        files[date].push_back(f);
    }

    return Status::OK();
X
Xu Peng 已提交
170 171
}

172 173 174
Status DBMetaImpl::has_group_file(const std::string& group_id_,
                              const std::string& file_id_,
                              bool& has_or_not_) {
X
Xu Peng 已提交
175
    //PXU TODO
X
Xu Peng 已提交
176
    return Status::OK();
X
Xu Peng 已提交
177 178
}

179 180 181
Status DBMetaImpl::get_group_file(const std::string& group_id_,
                              const std::string& file_id_,
                              GroupFileSchema& group_file_info_) {
X
Xu Peng 已提交
182
    //PXU TODO
X
Xu Peng 已提交
183
    return Status::OK();
X
Xu Peng 已提交
184 185
}

186
Status DBMetaImpl::get_group_files(const std::string& group_id_,
187
                               const int date_delta_,
188
                               GroupFilesSchema& group_files_info_) {
X
Xu Peng 已提交
189
    // PXU TODO
X
Xu Peng 已提交
190
    return Status::OK();
X
Xu Peng 已提交
191 192
}

X
Xu Peng 已提交
193
Status DBMetaImpl::update_group_file(const GroupFileSchema& group_file_) {
X
Xu Peng 已提交
194
    //PXU TODO
X
Xu Peng 已提交
195
    return Status::OK();
X
Xu Peng 已提交
196 197
}

198 199 200 201 202 203
Status DBMetaImpl::update_files(const GroupFilesSchema& files) {
    //PXU TODO
    return Status::OK();
}

} // namespace meta
X
Xu Peng 已提交
204 205 206
} // namespace engine
} // namespace vecwise
} // namespace zilliz