Factories.cpp 3.6 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.
////////////////////////////////////////////////////////////////////////////////
6

7 8
#include "Factories.h"
#include "DBImpl.h"
S
starlord 已提交
9
#include "src/db/insert/MemManagerImpl.h"
10
#include "Exception.h"
11

12
#include <stdlib.h>
X
Xu Peng 已提交
13 14
#include <time.h>
#include <sstream>
15 16 17 18
#include <iostream>
#include <vector>
#include <assert.h>
#include <easylogging++.h>
Z
update  
zhiru 已提交
19
#include <regex>
20 21 22
#include <cstdlib>
#include <string>
#include <algorithm>
X
Xu Peng 已提交
23 24

namespace zilliz {
J
jinhai 已提交
25
namespace milvus {
X
Xu Peng 已提交
26 27 28 29 30 31 32 33 34 35
namespace engine {

DBMetaOptions DBMetaOptionsFactory::Build(const std::string& path) {
    auto p = path;
    if(p == "") {
        srand(time(nullptr));
        std::stringstream ss;
        ss << "/tmp/" << rand();
        p = ss.str();
    }
Z
zhiru 已提交
36

X
Xu Peng 已提交
37 38 39 40 41 42 43 44 45 46 47 48
    DBMetaOptions meta;
    meta.path = p;
    return meta;
}

Options OptionsFactory::Build() {
    auto meta = DBMetaOptionsFactory::Build();
    Options options;
    options.meta = meta;
    return options;
}

S
starlord 已提交
49
std::shared_ptr<meta::SqliteMetaImpl> DBMetaImplFactory::Build() {
X
Xu Peng 已提交
50
    DBMetaOptions options = DBMetaOptionsFactory::Build();
S
starlord 已提交
51
    return std::shared_ptr<meta::SqliteMetaImpl>(new meta::SqliteMetaImpl(options));
X
Xu Peng 已提交
52 53
}

Z
update  
zhiru 已提交
54
std::shared_ptr<meta::Meta> DBMetaImplFactory::Build(const DBMetaOptions& metaOptions,
Z
update  
zhiru 已提交
55
                                                     const int& mode) {
Z
update  
zhiru 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

    std::string uri = metaOptions.backend_uri;

    std::string dialectRegex = "(.*)";
    std::string usernameRegex = "(.*)";
    std::string passwordRegex = "(.*)";
    std::string hostRegex = "(.*)";
    std::string portRegex = "(.*)";
    std::string dbNameRegex = "(.*)";
    std::string uriRegexStr = dialectRegex + "\\:\\/\\/" +
                              usernameRegex + "\\:" +
                              passwordRegex + "\\@" +
                              hostRegex + "\\:" +
                              portRegex + "\\/" +
                              dbNameRegex;
    std::regex uriRegex(uriRegexStr);
    std::smatch pieces_match;

    if (std::regex_match(uri, pieces_match, uriRegex)) {
        std::string dialect = pieces_match[1].str();
        std::transform(dialect.begin(), dialect.end(), dialect.begin(), ::tolower);
        if (dialect.find("mysql") != std::string::npos) {
Z
update  
zhiru 已提交
78
            ENGINE_LOG_INFO << "Using MySQL";
79
            return std::make_shared<meta::MySQLMetaImpl>(metaOptions, mode);
Z
update  
zhiru 已提交
80 81
        } else if (dialect.find("sqlite") != std::string::npos) {
            ENGINE_LOG_INFO << "Using SQLite";
S
starlord 已提交
82
            return std::make_shared<meta::SqliteMetaImpl>(metaOptions);
Z
update  
zhiru 已提交
83
        } else {
Z
update  
zhiru 已提交
84
            ENGINE_LOG_ERROR << "Invalid dialect in URI: dialect = " << dialect;
Z
update  
zhiru 已提交
85 86
            throw InvalidArgumentException("URI dialect is not mysql / sqlite");
        }
Z
update  
zhiru 已提交
87
    } else {
Z
update  
zhiru 已提交
88 89
        ENGINE_LOG_ERROR << "Wrong URI format: URI = " << uri;
        throw InvalidArgumentException("Wrong URI format ");
Z
update  
zhiru 已提交
90 91 92
    }
}

S
starlord 已提交
93 94 95 96 97
//std::shared_ptr<DB> DBFactory::Build() {
//    auto options = OptionsFactory::Build();
//    auto db = DBFactory::Build(options);
//    return std::shared_ptr<DB>(db);
//}
98

G
groot 已提交
99 100
DB* DBFactory::Build(const Options& options) {
    return new DBImpl(options);
101 102
}

Z
zhiru 已提交
103 104
MemManagerAbstractPtr MemManagerFactory::Build(const std::shared_ptr<meta::Meta>& meta,
                                               const Options& options) {
S
starlord 已提交
105
    return std::make_shared<MemManagerImpl>(meta, options);
Z
zhiru 已提交
106 107
}

X
Xu Peng 已提交
108
} // namespace engine
J
jinhai 已提交
109
} // namespace milvus
X
Xu Peng 已提交
110
} // namespace zilliz