DBMetaImpl.cpp 22.1 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
    if (!boost::filesystem::is_directory(_options.path)) {
89 90 91 92 93
        auto ret = boost::filesystem::create_directory(_options.path);
        if (!ret) {
            LOG(ERROR) << "Create directory " << _options.path << " Error";
        }
        assert(ret);
X
Xu Peng 已提交
94
    }
X
Xu Peng 已提交
95

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

X
Xu Peng 已提交
98
    ConnectorPtr->sync_schema();
99
    ConnectorPtr->open_forever(); // thread safe option
100
    ConnectorPtr->pragma.journal_mode(journal_mode::WAL); // WAL => write ahead log
X
Xu Peng 已提交
101

X
Xu Peng 已提交
102 103
    cleanup();

X
Xu Peng 已提交
104
    return Status::OK();
X
Xu Peng 已提交
105 106
}

X
Xu Peng 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120
// 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 已提交
121
    auto yesterday = GetDateWithDelta(-1);
X
Xu Peng 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

    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();
}

145 146 147 148 149 150 151 152 153
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 已提交
154

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

    auto group_path = GetGroupPath(group_info.group_id);

    if (!boost::filesystem::is_directory(group_path)) {
168 169 170 171 172
        auto ret = boost::filesystem::create_directory(group_path);
        if (!ret) {
            LOG(ERROR) << "Create directory " << group_path << " Error";
        }
        assert(ret);
173 174
    }

X
Xu Peng 已提交
175
    return Status::OK();
X
Xu Peng 已提交
176 177
}

X
Xu Peng 已提交
178
Status DBMetaImpl::get_group(GroupSchema& group_info) {
X
Xu Peng 已提交
179 180 181 182
    return get_group_no_lock(group_info);
}

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

X
Xu Peng 已提交
202
    return Status::OK();
X
Xu Peng 已提交
203 204
}

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

X
Xu Peng 已提交
222 223 224 225 226 227 228 229 230 231
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;
    }
232 233

    SimpleIDGenerator g;
234 235
    std::stringstream ss;
    ss << g.getNextIDNumber();
X
Xu Peng 已提交
236
    group_file.file_type = GroupFileSchema::NEW;
237
    group_file.file_id = ss.str();
X
Xu Peng 已提交
238
    group_file.dimension = group_info.dimension;
X
Xu Peng 已提交
239
    group_file.rows = 0;
240
    group_file.updated_time = GetMicroSecTimeStamp(); //ConnectorPtr->select(datetime("now", "localtime +1 hour")).front();
X
Xu Peng 已提交
241
    GetGroupFilePath(group_file);
X
Xu Peng 已提交
242

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

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

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

X
Xu Peng 已提交
263
    return Status::OK();
X
Xu Peng 已提交
264 265
}

X
Xu Peng 已提交
266 267
Status DBMetaImpl::files_to_index(GroupFilesSchema& files) {
    files.clear();
X
Xu Peng 已提交
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 295 296 297
    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 已提交
298
            }
299 300
            group_file.dimension = groups[group_file.group_id].dimension;
            files.push_back(group_file);
X
Xu Peng 已提交
301
        }
302 303 304
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
305
    }
X
Xu Peng 已提交
306

X
Xu Peng 已提交
307 308 309
    return Status::OK();
}

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

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

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

    return Status::OK();
}

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

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

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

    return Status::OK();
X
Xu Peng 已提交
404 405
}

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

413 414 415
Status DBMetaImpl::get_group_file(const std::string& group_id_,
                              const std::string& file_id_,
                              GroupFileSchema& group_file_info_) {
X
Xu Peng 已提交
416 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 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 已提交
442
    return Status::OK();
X
Xu Peng 已提交
443 444
}

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

452
Status DBMetaImpl::update_group_file(GroupFileSchema& group_file) {
453
    group_file.updated_time = GetMicroSecTimeStamp();
454
    try {
455 456 457 458 459 460 461 462
        ConnectorPtr->update(group_file);
        /* auto commited = ConnectorPtr->transaction([&] () mutable { */
        /*     ConnectorPtr->update(group_file); */
        /*     return true; */
        /* }); */
        /* if (!commited) { */
        /*     return Status::DBTransactionError("Update file Error"); */
        /* } */
463 464 465 466
    } 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 已提交
467
    }
X
Xu Peng 已提交
468
    return Status::OK();
X
Xu Peng 已提交
469 470
}

471
Status DBMetaImpl::update_files(GroupFilesSchema& files) {
472 473 474 475 476 477 478 479 480 481
    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 已提交
482
        }
483 484 485
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
486
    }
487 488 489
    return Status::OK();
}

X
Xu Peng 已提交
490 491
Status DBMetaImpl::cleanup_ttl_files(uint16_t seconds) {
    auto now = GetMicroSecTimeStamp();
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    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 已提交
517
            /* LOG(DEBUG) << "Removing deleted id=" << group_file.id << " location=" << group_file.location << std::endl; */
X
Xu Peng 已提交
518
        }
519 520 521
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
522 523 524 525 526
    }

    return Status::OK();
}

X
Xu Peng 已提交
527
Status DBMetaImpl::cleanup() {
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    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 已提交
553
            /* LOG(DEBUG) << "Removing id=" << group_file.id << " location=" << group_file.location << std::endl; */
X
Xu Peng 已提交
554
        }
555 556 557
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
558 559 560 561 562
    }

    return Status::OK();
}

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

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    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 已提交
584

585
        result /= group_info.dimension;
X
Xu Peng 已提交
586

587 588 589
    } catch (std::exception & e) {
        LOG(DEBUG) << e.what();
        throw e;
X
Xu Peng 已提交
590 591 592 593
    }
    return Status::OK();
}

X
Xu Peng 已提交
594 595 596 597 598 599 600
Status DBMetaImpl::drop_all() {
    if (boost::filesystem::is_directory(_options.path)) {
        boost::filesystem::remove_all(_options.path);
    }
    return Status::OK();
}

X
Xu Peng 已提交
601 602 603 604
DBMetaImpl::~DBMetaImpl() {
    cleanup();
}

605
} // namespace meta
X
Xu Peng 已提交
606 607 608
} // namespace engine
} // namespace vecwise
} // namespace zilliz