DBMetaImpl.cpp 19.4 KB
Newer Older
X
Xu Peng 已提交
1 2 3 4 5
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/
X
Xu Peng 已提交
6

X
Xu Peng 已提交
7
#include <unistd.h>
X
Xu Peng 已提交
8 9
#include <sstream>
#include <iostream>
X
Xu Peng 已提交
10
#include <boost/filesystem.hpp>
11
#include <chrono>
X
Xu Peng 已提交
12
#include <fstream>
13
#include <sqlite_orm.h>
14
#include <easylogging++.h>
X
Xu Peng 已提交
15 16
#include "DBMetaImpl.h"
#include "IDGenerator.h"
X
Xu Peng 已提交
17 18 19 20

namespace zilliz {
namespace vecwise {
namespace engine {
21
namespace meta {
X
Xu Peng 已提交
22

X
Xu Peng 已提交
23 24
using namespace sqlite_orm;

X
Xu Peng 已提交
25 26
inline auto StoragePrototype(const std::string& path) {
    return make_storage(path,
X
Xu Peng 已提交
27
            make_table("Group",
X
Xu Peng 已提交
28 29 30
                      make_column("id", &GroupSchema::id, primary_key()),
                      make_column("group_id", &GroupSchema::group_id, unique()),
                      make_column("dimension", &GroupSchema::dimension),
X
Xu Peng 已提交
31 32 33 34 35 36 37
                      make_column("files_cnt", &GroupSchema::files_cnt, default_value(0))),
            make_table("GroupFile",
                      make_column("id", &GroupFileSchema::id, primary_key()),
                      make_column("group_id", &GroupFileSchema::group_id),
                      make_column("file_id", &GroupFileSchema::file_id),
                      make_column("file_type", &GroupFileSchema::file_type),
                      make_column("rows", &GroupFileSchema::rows, default_value(0)),
38
                      make_column("updated_time", &GroupFileSchema::updated_time),
X
Xu Peng 已提交
39 40
                      make_column("date", &GroupFileSchema::date))
            );
X
Xu Peng 已提交
41 42 43

}

X
Xu Peng 已提交
44
using ConnectorT = decltype(StoragePrototype("/tmp/dummy.sqlite3"));
X
Xu Peng 已提交
45 46
static std::unique_ptr<ConnectorT> ConnectorPtr;

X
Xu Peng 已提交
47 48 49 50 51 52 53
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 已提交
54 55 56 57
std::string DBMetaImpl::GetGroupPath(const std::string& group_id) {
    return _options.path + "/" + group_id;
}

58 59 60 61 62 63 64 65
long DBMetaImpl::GetMicroSecTimeStamp() {
    auto now = std::chrono::system_clock::now();
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(
            now.time_since_epoch()).count();

    return micros;
}

X
Xu Peng 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
std::string DBMetaImpl::GetGroupDatePartitionPath(const std::string& group_id, DateT& date) {
    std::stringstream ss;
    ss << GetGroupPath(group_id) << "/" << date;
    return ss.str();
}

void DBMetaImpl::GetGroupFilePath(GroupFileSchema& group_file) {
    if (group_file.date == EmptyDate) {
        group_file.date = Meta::GetDate();
    }
    std::stringstream ss;
    ss << GetGroupDatePartitionPath(group_file.group_id, group_file.date)
       << "/" << group_file.file_id;
    group_file.location = ss.str();
}

X
Xu Peng 已提交
82 83
DBMetaImpl::DBMetaImpl(const DBMetaOptions& options_)
    : _options(options_) {
X
Xu Peng 已提交
84 85 86 87
    initialize();
}

Status DBMetaImpl::initialize() {
X
Xu Peng 已提交
88 89
    if (!boost::filesystem::is_directory(_options.path)) {
        assert(boost::filesystem::create_directory(_options.path));
X
Xu Peng 已提交
90
    }
X
Xu Peng 已提交
91

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

X
Xu Peng 已提交
94
    ConnectorPtr->sync_schema();
95
    ConnectorPtr->open_forever(); // thread safe option
96
    ConnectorPtr->pragma.journal_mode(journal_mode::WAL); // WAL => write ahead log
X
Xu Peng 已提交
97

X
Xu Peng 已提交
98 99
    cleanup();

X
Xu Peng 已提交
100
    return Status::OK();
X
Xu Peng 已提交
101 102
}

103 104 105 106 107 108 109 110 111
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 已提交
112

X
Xu Peng 已提交
113 114 115 116
    {
        try {
            auto id = ConnectorPtr->insert(group_info);
            group_info.id = id;
X
Xu Peng 已提交
117
            /* LOG(DEBUG) << "Add group " << id; */
X
Xu Peng 已提交
118 119 120
        } catch (...) {
            return Status::DBTransactionError("Add Group Error");
        }
X
Xu Peng 已提交
121
    }
122 123 124 125 126 127 128

    auto group_path = GetGroupPath(group_info.group_id);

    if (!boost::filesystem::is_directory(group_path)) {
        assert(boost::filesystem::create_directory(group_path));
    }

X
Xu Peng 已提交
129
    return Status::OK();
X
Xu Peng 已提交
130 131
}

X
Xu Peng 已提交
132
Status DBMetaImpl::get_group(GroupSchema& group_info) {
X
Xu Peng 已提交
133 134 135 136
    return get_group_no_lock(group_info);
}

Status DBMetaImpl::get_group_no_lock(GroupSchema& group_info) {
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    try {
        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");
        }
    } catch (std::exception &e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
154
    }
X
Xu Peng 已提交
155

X
Xu Peng 已提交
156
    return Status::OK();
X
Xu Peng 已提交
157 158
}

X
Xu Peng 已提交
159
Status DBMetaImpl::has_group(const std::string& group_id, bool& has_or_not) {
160 161 162 163 164 165 166 167 168 169 170 171
    try {
        auto groups = ConnectorPtr->select(columns(&GroupSchema::id),
                                          where(c(&GroupSchema::group_id) == group_id));
        assert(groups.size() <= 1);
        if (groups.size() == 1) {
            has_or_not = true;
        } else {
            has_or_not = false;
        }
    } catch (std::exception &e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
172
    }
X
Xu Peng 已提交
173
    return Status::OK();
X
Xu Peng 已提交
174 175
}

X
Xu Peng 已提交
176 177 178 179 180 181 182 183 184 185
Status DBMetaImpl::add_group_file(GroupFileSchema& group_file) {
    if (group_file.date == EmptyDate) {
        group_file.date = Meta::GetDate();
    }
    GroupSchema group_info;
    group_info.group_id = group_file.group_id;
    auto status = get_group(group_info);
    if (!status.ok()) {
        return status;
    }
186 187

    SimpleIDGenerator g;
188 189
    std::stringstream ss;
    ss << g.getNextIDNumber();
X
Xu Peng 已提交
190
    group_file.file_type = GroupFileSchema::NEW;
191
    group_file.file_id = ss.str();
X
Xu Peng 已提交
192
    group_file.dimension = group_info.dimension;
X
Xu Peng 已提交
193
    group_file.rows = 0;
194
    group_file.updated_time = GetMicroSecTimeStamp(); //ConnectorPtr->select(datetime("now", "localtime +1 hour")).front();
X
Xu Peng 已提交
195
    GetGroupFilePath(group_file);
X
Xu Peng 已提交
196

X
Xu Peng 已提交
197 198 199 200
    {
        try {
            auto id = ConnectorPtr->insert(group_file);
            group_file.id = id;
X
Xu Peng 已提交
201
            /* LOG(DEBUG) << "Add group_file of file_id=" << group_file.file_id; */
X
Xu Peng 已提交
202 203 204
        } catch (...) {
            return Status::DBTransactionError("Add file Error");
        }
X
Xu Peng 已提交
205
    }
206 207 208 209 210 211 212

    auto partition_path = GetGroupDatePartitionPath(group_file.group_id, group_file.date);

    if (!boost::filesystem::is_directory(partition_path)) {
        assert(boost::filesystem::create_directory(partition_path));
    }

X
Xu Peng 已提交
213
    return Status::OK();
X
Xu Peng 已提交
214 215
}

X
Xu Peng 已提交
216 217
Status DBMetaImpl::files_to_index(GroupFilesSchema& files) {
    files.clear();
X
Xu Peng 已提交
218

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    try {
        auto selected = ConnectorPtr->select(columns(&GroupFileSchema::id,
                                                   &GroupFileSchema::group_id,
                                                   &GroupFileSchema::file_id,
                                                   &GroupFileSchema::file_type,
                                                   &GroupFileSchema::rows,
                                                   &GroupFileSchema::date),
                                          where(c(&GroupFileSchema::file_type) == (int)GroupFileSchema::TO_INDEX));

        std::map<std::string, GroupSchema> groups;

        for (auto& file : selected) {
            GroupFileSchema group_file;
            group_file.id = std::get<0>(file);
            group_file.group_id = std::get<1>(file);
            group_file.file_id = std::get<2>(file);
            group_file.file_type = std::get<3>(file);
            group_file.rows = std::get<4>(file);
            group_file.date = std::get<5>(file);
            GetGroupFilePath(group_file);
            auto groupItr = groups.find(group_file.group_id);
            if (groupItr == groups.end()) {
                GroupSchema group_info;
                group_info.group_id = group_file.group_id;
                auto status = get_group_no_lock(group_info);
                if (!status.ok()) {
                    return status;
                }
                groups[group_file.group_id] = group_info;
X
Xu Peng 已提交
248
            }
249 250
            group_file.dimension = groups[group_file.group_id].dimension;
            files.push_back(group_file);
X
Xu Peng 已提交
251
        }
252 253 254
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
255
    }
X
Xu Peng 已提交
256

X
Xu Peng 已提交
257 258 259
    return Status::OK();
}

X
xj.lin 已提交
260
Status DBMetaImpl::files_to_search(const std::string &group_id,
X
Xu Peng 已提交
261
                                   const DatesT& partition,
X
xj.lin 已提交
262 263
                                   DatePartionedGroupFilesSchema &files) {
    files.clear();
X
Xu Peng 已提交
264 265 266
    DatesT today = {Meta::GetDate()};
    const DatesT& dates = (partition.empty() == true) ? today : partition;

267 268 269 270 271 272 273 274
    try {
        auto selected = ConnectorPtr->select(columns(&GroupFileSchema::id,
                                                     &GroupFileSchema::group_id,
                                                     &GroupFileSchema::file_id,
                                                     &GroupFileSchema::file_type,
                                                     &GroupFileSchema::rows,
                                                     &GroupFileSchema::date),
                                             where(c(&GroupFileSchema::group_id) == group_id and
X
Xu Peng 已提交
275
                                                 in(&GroupFileSchema::date, dates) and
276
                                                 (c(&GroupFileSchema::file_type) == (int) GroupFileSchema::RAW or
277
                                                     c(&GroupFileSchema::file_type) == (int) GroupFileSchema::TO_INDEX or
278 279 280 281 282 283 284 285
                                                     c(&GroupFileSchema::file_type) == (int) GroupFileSchema::INDEX)));

        GroupSchema group_info;
        group_info.group_id = group_id;
        auto status = get_group_no_lock(group_info);
        if (!status.ok()) {
            return status;
        }
X
xj.lin 已提交
286

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
        for (auto& file : selected) {
            GroupFileSchema group_file;
            group_file.id = std::get<0>(file);
            group_file.group_id = std::get<1>(file);
            group_file.file_id = std::get<2>(file);
            group_file.file_type = std::get<3>(file);
            group_file.rows = std::get<4>(file);
            group_file.date = std::get<5>(file);
            group_file.dimension = group_info.dimension;
            GetGroupFilePath(group_file);
            auto dateItr = files.find(group_file.date);
            if (dateItr == files.end()) {
                files[group_file.date] = GroupFilesSchema();
            }
            files[group_file.date].push_back(group_file);
X
xj.lin 已提交
302
        }
303 304 305
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
xj.lin 已提交
306 307 308 309 310
    }

    return Status::OK();
}

X
Xu Peng 已提交
311 312 313
Status DBMetaImpl::files_to_merge(const std::string& group_id,
        DatePartionedGroupFilesSchema& files) {
    files.clear();
X
Xu Peng 已提交
314

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    try {
        auto selected = ConnectorPtr->select(columns(&GroupFileSchema::id,
                                                   &GroupFileSchema::group_id,
                                                   &GroupFileSchema::file_id,
                                                   &GroupFileSchema::file_type,
                                                   &GroupFileSchema::rows,
                                                   &GroupFileSchema::date),
                                          where(c(&GroupFileSchema::file_type) == (int)GroupFileSchema::RAW and
                                                c(&GroupFileSchema::group_id) == group_id));

        GroupSchema group_info;
        group_info.group_id = group_id;
        auto status = get_group_no_lock(group_info);
        if (!status.ok()) {
            return status;
        }
X
Xu Peng 已提交
331

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
        for (auto& file : selected) {
            GroupFileSchema group_file;
            group_file.id = std::get<0>(file);
            group_file.group_id = std::get<1>(file);
            group_file.file_id = std::get<2>(file);
            group_file.file_type = std::get<3>(file);
            group_file.rows = std::get<4>(file);
            group_file.date = std::get<5>(file);
            group_file.dimension = group_info.dimension;
            GetGroupFilePath(group_file);
            auto dateItr = files.find(group_file.date);
            if (dateItr == files.end()) {
                files[group_file.date] = GroupFilesSchema();
            }
            files[group_file.date].push_back(group_file);
X
Xu Peng 已提交
347
        }
348 349 350
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
351 352 353
    }

    return Status::OK();
X
Xu Peng 已提交
354 355
}

356 357 358
Status DBMetaImpl::has_group_file(const std::string& group_id_,
                              const std::string& file_id_,
                              bool& has_or_not_) {
X
Xu Peng 已提交
359
    //PXU TODO
X
Xu Peng 已提交
360
    return Status::OK();
X
Xu Peng 已提交
361 362
}

363 364 365
Status DBMetaImpl::get_group_file(const std::string& group_id_,
                              const std::string& file_id_,
                              GroupFileSchema& group_file_info_) {
X
Xu Peng 已提交
366
    //PXU TODO
X
Xu Peng 已提交
367
    return Status::OK();
X
Xu Peng 已提交
368 369
}

370
Status DBMetaImpl::get_group_files(const std::string& group_id_,
371
                               const int date_delta_,
372
                               GroupFilesSchema& group_files_info_) {
X
Xu Peng 已提交
373
    // PXU TODO
X
Xu Peng 已提交
374
    return Status::OK();
X
Xu Peng 已提交
375 376
}

377
Status DBMetaImpl::update_group_file(GroupFileSchema& group_file) {
378
    group_file.updated_time = GetMicroSecTimeStamp();
379
    try {
380 381 382 383 384 385 386 387
        ConnectorPtr->update(group_file);
        /* auto commited = ConnectorPtr->transaction([&] () mutable { */
        /*     ConnectorPtr->update(group_file); */
        /*     return true; */
        /* }); */
        /* if (!commited) { */
        /*     return Status::DBTransactionError("Update file Error"); */
        /* } */
388 389 390 391
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        LOG(DEBUG) << "id= " << group_file.id << " file_id=" << group_file.file_id;
        throw e;
X
Xu Peng 已提交
392
    }
X
Xu Peng 已提交
393
    return Status::OK();
X
Xu Peng 已提交
394 395
}

396
Status DBMetaImpl::update_files(GroupFilesSchema& files) {
397 398 399 400 401 402 403 404 405 406
    try {
        auto commited = ConnectorPtr->transaction([&] () mutable {
            for (auto& file : files) {
                file.updated_time = GetMicroSecTimeStamp();
                ConnectorPtr->update(file);
            }
            return true;
        });
        if (!commited) {
            return Status::DBTransactionError("Update files Error");
X
Xu Peng 已提交
407
        }
408 409 410
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
411
    }
412 413 414
    return Status::OK();
}

X
Xu Peng 已提交
415 416
Status DBMetaImpl::cleanup_ttl_files(uint16_t seconds) {
    auto now = GetMicroSecTimeStamp();
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    try {
        auto selected = ConnectorPtr->select(columns(&GroupFileSchema::id,
                                                   &GroupFileSchema::group_id,
                                                   &GroupFileSchema::file_id,
                                                   &GroupFileSchema::file_type,
                                                   &GroupFileSchema::rows,
                                                   &GroupFileSchema::date),
                                          where(c(&GroupFileSchema::file_type) == (int)GroupFileSchema::TO_DELETE and
                                                c(&GroupFileSchema::updated_time) > now - 1000000*seconds));

        GroupFilesSchema updated;

        for (auto& file : selected) {
            GroupFileSchema group_file;
            group_file.id = std::get<0>(file);
            group_file.group_id = std::get<1>(file);
            group_file.file_id = std::get<2>(file);
            group_file.file_type = std::get<3>(file);
            group_file.rows = std::get<4>(file);
            group_file.date = std::get<5>(file);
            GetGroupFilePath(group_file);
            if (group_file.file_type == GroupFileSchema::TO_DELETE) {
                boost::filesystem::remove(group_file.location);
            }
            ConnectorPtr->remove<GroupFileSchema>(group_file.id);
X
Xu Peng 已提交
442
            /* LOG(DEBUG) << "Removing deleted id=" << group_file.id << " location=" << group_file.location << std::endl; */
X
Xu Peng 已提交
443
        }
444 445 446
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
447 448 449 450 451
    }

    return Status::OK();
}

X
Xu Peng 已提交
452
Status DBMetaImpl::cleanup() {
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    try {
        auto selected = ConnectorPtr->select(columns(&GroupFileSchema::id,
                                                   &GroupFileSchema::group_id,
                                                   &GroupFileSchema::file_id,
                                                   &GroupFileSchema::file_type,
                                                   &GroupFileSchema::rows,
                                                   &GroupFileSchema::date),
                                          where(c(&GroupFileSchema::file_type) == (int)GroupFileSchema::TO_DELETE or
                                                c(&GroupFileSchema::file_type) == (int)GroupFileSchema::NEW));

        GroupFilesSchema updated;

        for (auto& file : selected) {
            GroupFileSchema group_file;
            group_file.id = std::get<0>(file);
            group_file.group_id = std::get<1>(file);
            group_file.file_id = std::get<2>(file);
            group_file.file_type = std::get<3>(file);
            group_file.rows = std::get<4>(file);
            group_file.date = std::get<5>(file);
            GetGroupFilePath(group_file);
            if (group_file.file_type == GroupFileSchema::TO_DELETE) {
                boost::filesystem::remove(group_file.location);
            }
            ConnectorPtr->remove<GroupFileSchema>(group_file.id);
X
Xu Peng 已提交
478
            /* LOG(DEBUG) << "Removing id=" << group_file.id << " location=" << group_file.location << std::endl; */
X
Xu Peng 已提交
479
        }
480 481 482
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
483 484 485 486 487
    }

    return Status::OK();
}

X
Xu Peng 已提交
488 489
Status DBMetaImpl::count(const std::string& group_id, long& result) {

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    try {
        auto selected = ConnectorPtr->select(columns(&GroupFileSchema::rows,
                                                   &GroupFileSchema::date),
                                          where((c(&GroupFileSchema::file_type) == (int)GroupFileSchema::RAW or
                                                 c(&GroupFileSchema::file_type) == (int)GroupFileSchema::TO_INDEX or
                                                 c(&GroupFileSchema::file_type) == (int)GroupFileSchema::INDEX) and
                                                c(&GroupFileSchema::group_id) == group_id));

        GroupSchema group_info;
        group_info.group_id = group_id;
        auto status = get_group_no_lock(group_info);
        if (!status.ok()) {
            return status;
        }

        result = 0;
        for (auto& file : selected) {
            result += std::get<0>(file);
        }
X
Xu Peng 已提交
509

510
        result /= group_info.dimension;
X
Xu Peng 已提交
511

512 513 514
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
515 516 517 518
    }
    return Status::OK();
}

X
Xu Peng 已提交
519 520 521 522 523 524 525
Status DBMetaImpl::drop_all() {
    if (boost::filesystem::is_directory(_options.path)) {
        boost::filesystem::remove_all(_options.path);
    }
    return Status::OK();
}

X
Xu Peng 已提交
526 527 528 529
DBMetaImpl::~DBMetaImpl() {
    cleanup();
}

530
} // namespace meta
X
Xu Peng 已提交
531 532 533
} // namespace engine
} // namespace vecwise
} // namespace zilliz