DBMetaImpl.cpp 24.9 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
#include "Utils.h"
X
Xu Peng 已提交
18 19 20 21

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

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

X
Xu Peng 已提交
26 27
inline auto StoragePrototype(const std::string& path) {
    return make_storage(path,
X
Xu Peng 已提交
28
            make_table("Group",
X
Xu Peng 已提交
29 30 31
                      make_column("id", &GroupSchema::id, primary_key()),
                      make_column("group_id", &GroupSchema::group_id, unique()),
                      make_column("dimension", &GroupSchema::dimension),
32
                      make_column("created_on", &GroupSchema::created_on),
X
Xu Peng 已提交
33 34 35 36 37 38 39
                      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)),
40
                      make_column("updated_time", &GroupFileSchema::updated_time),
41
                      make_column("created_on", &GroupFileSchema::created_on),
X
Xu Peng 已提交
42 43
                      make_column("date", &GroupFileSchema::date))
            );
X
Xu Peng 已提交
44 45 46

}

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

X
Xu Peng 已提交
50 51 52 53 54 55 56
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 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
std::string DBMetaImpl::GetGroupPath(const std::string& group_id) {
    return _options.path + "/" + group_id;
}

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 已提交
77 78
DBMetaImpl::DBMetaImpl(const DBMetaOptions& options_)
    : _options(options_) {
X
Xu Peng 已提交
79 80 81 82
    initialize();
}

Status DBMetaImpl::initialize() {
X
Xu Peng 已提交
83
    if (!boost::filesystem::is_directory(_options.path)) {
84 85 86 87 88
        auto ret = boost::filesystem::create_directory(_options.path);
        if (!ret) {
            LOG(ERROR) << "Create directory " << _options.path << " Error";
        }
        assert(ret);
X
Xu Peng 已提交
89
    }
X
Xu Peng 已提交
90

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

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

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

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

X
Xu Peng 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115
// PXU TODO: Temp solution. Will fix later
Status DBMetaImpl::delete_group_partitions(const std::string& group_id,
            const meta::DatesT& dates) {
    if (dates.size() == 0) {
        return Status::OK();
    }

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

X
Xu Peng 已提交
116
    auto yesterday = GetDateWithDelta(-1);
X
Xu Peng 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

    for (auto& date : dates) {
        if (date >= yesterday) {
            return Status::Error("Could not delete partitions with 2 days");
        }
    }

    try {
        ConnectorPtr->update_all(
                    set(
                        c(&GroupFileSchema::file_type) = (int)GroupFileSchema::TO_DELETE
                    ),
                    where(
                        c(&GroupFileSchema::group_id) == group_id and
                        in(&GroupFileSchema::date, dates)
                    ));
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
    }
    return Status::OK();
}

140 141 142 143 144 145 146 147 148
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 已提交
149
    group_info.created_on = utils::GetMicroSecTimeStamp();
X
Xu Peng 已提交
150

X
Xu Peng 已提交
151 152 153 154
    {
        try {
            auto id = ConnectorPtr->insert(group_info);
            group_info.id = id;
X
Xu Peng 已提交
155
            /* LOG(DEBUG) << "Add group " << id; */
X
Xu Peng 已提交
156 157 158
        } catch (...) {
            return Status::DBTransactionError("Add Group Error");
        }
X
Xu Peng 已提交
159
    }
160 161 162 163

    auto group_path = GetGroupPath(group_info.group_id);

    if (!boost::filesystem::is_directory(group_path)) {
164 165 166 167 168
        auto ret = boost::filesystem::create_directory(group_path);
        if (!ret) {
            LOG(ERROR) << "Create directory " << group_path << " Error";
        }
        assert(ret);
169 170
    }

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

X
Xu Peng 已提交
174
Status DBMetaImpl::get_group(GroupSchema& group_info) {
X
Xu Peng 已提交
175 176 177 178
    return get_group_no_lock(group_info);
}

Status DBMetaImpl::get_group_no_lock(GroupSchema& group_info) {
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    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 已提交
196
    }
X
Xu Peng 已提交
197

X
Xu Peng 已提交
198
    return Status::OK();
X
Xu Peng 已提交
199 200
}

X
Xu Peng 已提交
201
Status DBMetaImpl::has_group(const std::string& group_id, bool& has_or_not) {
202 203 204 205 206 207 208 209 210 211 212 213
    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 已提交
214
    }
X
Xu Peng 已提交
215
    return Status::OK();
X
Xu Peng 已提交
216 217
}

X
Xu Peng 已提交
218 219 220 221 222 223 224 225 226 227
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;
    }
228 229

    SimpleIDGenerator g;
230 231
    std::stringstream ss;
    ss << g.getNextIDNumber();
X
Xu Peng 已提交
232
    group_file.file_type = GroupFileSchema::NEW;
233
    group_file.file_id = ss.str();
X
Xu Peng 已提交
234
    group_file.dimension = group_info.dimension;
X
Xu Peng 已提交
235
    group_file.rows = 0;
X
Xu Peng 已提交
236
    group_file.created_on = utils::GetMicroSecTimeStamp();
X
Xu Peng 已提交
237
    group_file.updated_time = group_file.created_on;
X
Xu Peng 已提交
238
    GetGroupFilePath(group_file);
X
Xu Peng 已提交
239

X
Xu Peng 已提交
240 241 242 243
    {
        try {
            auto id = ConnectorPtr->insert(group_file);
            group_file.id = id;
X
Xu Peng 已提交
244
            /* LOG(DEBUG) << "Add group_file of file_id=" << group_file.file_id; */
X
Xu Peng 已提交
245 246 247
        } catch (...) {
            return Status::DBTransactionError("Add file Error");
        }
X
Xu Peng 已提交
248
    }
249 250 251 252

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

    if (!boost::filesystem::is_directory(partition_path)) {
253 254 255 256 257
        auto ret = boost::filesystem::create_directory(partition_path);
        if (!ret) {
            LOG(ERROR) << "Create directory " << partition_path << " Error";
        }
        assert(ret);
258 259
    }

X
Xu Peng 已提交
260
    return Status::OK();
X
Xu Peng 已提交
261 262
}

X
Xu Peng 已提交
263 264
Status DBMetaImpl::files_to_index(GroupFilesSchema& files) {
    files.clear();
X
Xu Peng 已提交
265

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    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 已提交
295
            }
296 297
            group_file.dimension = groups[group_file.group_id].dimension;
            files.push_back(group_file);
X
Xu Peng 已提交
298
        }
299 300 301
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
302
    }
X
Xu Peng 已提交
303

X
Xu Peng 已提交
304 305 306
    return Status::OK();
}

X
xj.lin 已提交
307
Status DBMetaImpl::files_to_search(const std::string &group_id,
X
Xu Peng 已提交
308
                                   const DatesT& partition,
X
xj.lin 已提交
309 310
                                   DatePartionedGroupFilesSchema &files) {
    files.clear();
X
Xu Peng 已提交
311 312 313
    DatesT today = {Meta::GetDate()};
    const DatesT& dates = (partition.empty() == true) ? today : partition;

314 315 316 317 318 319 320 321
    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 已提交
322
                                                 in(&GroupFileSchema::date, dates) and
323
                                                 (c(&GroupFileSchema::file_type) == (int) GroupFileSchema::RAW or
324
                                                     c(&GroupFileSchema::file_type) == (int) GroupFileSchema::TO_INDEX or
325 326 327 328 329 330 331 332
                                                     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 已提交
333

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
        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 已提交
349
        }
350 351 352
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
xj.lin 已提交
353 354 355 356 357
    }

    return Status::OK();
}

X
Xu Peng 已提交
358 359 360
Status DBMetaImpl::files_to_merge(const std::string& group_id,
        DatePartionedGroupFilesSchema& files) {
    files.clear();
X
Xu Peng 已提交
361

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    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 已提交
378

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
        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 已提交
394
        }
395 396 397
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
398 399 400
    }

    return Status::OK();
X
Xu Peng 已提交
401 402
}

403 404 405
Status DBMetaImpl::has_group_file(const std::string& group_id_,
                              const std::string& file_id_,
                              bool& has_or_not_) {
X
Xu Peng 已提交
406
    //PXU TODO
X
Xu Peng 已提交
407
    return Status::OK();
X
Xu Peng 已提交
408 409
}

410 411 412
Status DBMetaImpl::get_group_file(const std::string& group_id_,
                              const std::string& file_id_,
                              GroupFileSchema& group_file_info_) {
X
Xu Peng 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    try {
        auto files = ConnectorPtr->select(columns(&GroupFileSchema::id,
                                                   &GroupFileSchema::group_id,
                                                   &GroupFileSchema::file_id,
                                                   &GroupFileSchema::file_type,
                                                   &GroupFileSchema::rows,
                                                   &GroupFileSchema::date),
                                          where(c(&GroupFileSchema::file_id) == file_id_ and
                                                c(&GroupFileSchema::group_id) == group_id_
                                          ));
        assert(files.size() <= 1);
        if (files.size() == 1) {
            group_file_info_.id = std::get<0>(files[0]);
            group_file_info_.group_id = std::get<1>(files[0]);
            group_file_info_.file_id = std::get<2>(files[0]);
            group_file_info_.file_type = std::get<3>(files[0]);
            group_file_info_.rows = std::get<4>(files[0]);
            group_file_info_.date = std::get<5>(files[0]);
        } else {
            return Status::NotFound("GroupFile " + file_id_ + " not found");
        }
    } catch (std::exception &e) {
        LOG(DEBUG) << e.what();
        throw e;
    }

X
Xu Peng 已提交
439
    return Status::OK();
X
Xu Peng 已提交
440 441
}

442
Status DBMetaImpl::get_group_files(const std::string& group_id_,
443
                               const int date_delta_,
444
                               GroupFilesSchema& group_files_info_) {
X
Xu Peng 已提交
445
    // PXU TODO
X
Xu Peng 已提交
446
    return Status::OK();
X
Xu Peng 已提交
447 448
}

X
Xu Peng 已提交
449 450 451 452 453 454 455 456 457 458 459
// PXU TODO: Support Swap
Status DBMetaImpl::archive_files() {
    auto& criterias = _options.archive_conf.GetCriterias();
    if (criterias.size() == 0) {
        return Status::OK();
    }

    for (auto kv : criterias) {
        auto& criteria = kv.first;
        auto& limit = kv.second;
        if (criteria == "days") {
460 461 462
            long usecs = 3600*24*limit*1000000UL;
            long now = utils::GetMicroSecTimeStamp();
            LOG(DEBUG) << "Limit " << limit << " TimeLimit "  << now - usecs;
X
Xu Peng 已提交
463 464 465 466 467 468 469
            try
            {
                ConnectorPtr->update_all(
                        set(
                            c(&GroupFileSchema::file_type) = (int)GroupFileSchema::TO_DELETE
                           ),
                        where(
470
                            c(&GroupFileSchema::created_on) < (long)(now - usecs) and
X
Xu Peng 已提交
471 472 473 474 475 476 477 478
                            c(&GroupFileSchema::file_type) != (int)GroupFileSchema::TO_DELETE
                            ));
            } catch (std::exception & e) {
                LOG(DEBUG) << e.what();
                throw e;
            }
        }
        if (criteria == "disk") {
479
            size_t G = 1024*1024*1024UL;
X
Xu Peng 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
            long unsigned int sum = 0;
            try {
                auto sum_c = ConnectorPtr->sum(
                        &GroupFileSchema::rows,
                        where(
                            c(&GroupFileSchema::file_type) != (int)GroupFileSchema::TO_DELETE
                        ));
                sum = *sum_c;
            } catch (std::exception & e) {
                LOG(DEBUG) << e.what();
                throw e;
            }
            // PXU TODO: refactor rows
            auto to_delete = sum - limit*G/sizeof(float);
            discard_files_of_size(to_delete);
        }
    }

    return Status::OK();
}

Status DBMetaImpl::discard_files_of_size(long to_discard_size) {
    if (to_discard_size <= 0) {
        return Status::OK();
    }
    try {
        auto selected = ConnectorPtr->select(columns(&GroupFileSchema::id,
                                                   &GroupFileSchema::rows),
                                          where(c(&GroupFileSchema::file_type) != (int)GroupFileSchema::TO_DELETE),
                                          order_by(&GroupFileSchema::id),
                                          limit(10));
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
        std::vector<int> ids;

        for (auto& file : selected) {
            if (to_discard_size <= 0) break;
            GroupFileSchema group_file;
            group_file.id = std::get<0>(file);
            group_file.rows = std::get<1>(file);
            ids.push_back(group_file.id);
            to_discard_size -= group_file.rows;
        }

        if (ids.size() == 0) {
            return Status::OK();
        }

        ConnectorPtr->update_all(
                    set(
                        c(&GroupFileSchema::file_type) = (int)GroupFileSchema::TO_DELETE
                    ),
                    where(
                        in(&GroupFileSchema::id, ids)
                    ));
X
Xu Peng 已提交
533 534 535 536 537 538 539

    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
    }


540
    return discard_files_of_size(to_discard_size);
X
Xu Peng 已提交
541 542
}

543
Status DBMetaImpl::update_group_file(GroupFileSchema& group_file) {
X
Xu Peng 已提交
544
    group_file.updated_time = utils::GetMicroSecTimeStamp();
545
    try {
546
        ConnectorPtr->update(group_file);
547 548 549 550
    } 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 已提交
551
    }
X
Xu Peng 已提交
552
    return Status::OK();
X
Xu Peng 已提交
553 554
}

555
Status DBMetaImpl::update_files(GroupFilesSchema& files) {
556 557 558
    try {
        auto commited = ConnectorPtr->transaction([&] () mutable {
            for (auto& file : files) {
X
Xu Peng 已提交
559
                file.updated_time = utils::GetMicroSecTimeStamp();
560 561 562 563 564 565
                ConnectorPtr->update(file);
            }
            return true;
        });
        if (!commited) {
            return Status::DBTransactionError("Update files Error");
X
Xu Peng 已提交
566
        }
567 568 569
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
570
    }
571 572 573
    return Status::OK();
}

X
Xu Peng 已提交
574
Status DBMetaImpl::cleanup_ttl_files(uint16_t seconds) {
X
Xu Peng 已提交
575
    auto now = utils::GetMicroSecTimeStamp();
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    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 已提交
601
            /* LOG(DEBUG) << "Removing deleted id=" << group_file.id << " location=" << group_file.location << std::endl; */
X
Xu Peng 已提交
602
        }
603 604 605
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
606 607 608 609 610
    }

    return Status::OK();
}

X
Xu Peng 已提交
611
Status DBMetaImpl::cleanup() {
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
    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 已提交
637
            /* LOG(DEBUG) << "Removing id=" << group_file.id << " location=" << group_file.location << std::endl; */
X
Xu Peng 已提交
638
        }
639 640 641
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
642 643 644 645 646
    }

    return Status::OK();
}

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

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
    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 已提交
668

669
        result /= group_info.dimension;
X
Xu Peng 已提交
670

671 672 673
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
674 675 676 677
    }
    return Status::OK();
}

X
Xu Peng 已提交
678 679 680 681 682 683 684
Status DBMetaImpl::drop_all() {
    if (boost::filesystem::is_directory(_options.path)) {
        boost::filesystem::remove_all(_options.path);
    }
    return Status::OK();
}

X
Xu Peng 已提交
685 686 687 688
DBMetaImpl::~DBMetaImpl() {
    cleanup();
}

689
} // namespace meta
X
Xu Peng 已提交
690 691 692
} // namespace engine
} // namespace vecwise
} // namespace zilliz