Factories.cpp 4.0 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"
Z
zhiru 已提交
9 10
#include "MemManager.h"
#include "NewMemManager.h"
11
#include "Exception.h"
12

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

namespace zilliz {
J
jinhai 已提交
26
namespace milvus {
X
Xu Peng 已提交
27 28 29 30 31 32 33 34 35 36
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 已提交
37

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

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

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

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

    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 已提交
79
            ENGINE_LOG_INFO << "Using MySQL";
Z
update  
zhiru 已提交
80
            return std::make_shared<meta::MySQLMetaImpl>(meta::MySQLMetaImpl(metaOptions, mode));
Z
update  
zhiru 已提交
81
        } else if (dialect.find("sqlite") != std::string::npos) {
Z
update  
zhiru 已提交
82
            ENGINE_LOG_INFO << "Using SQLite";
Z
zhiru 已提交
83
            return std::make_shared<meta::DBMetaImpl>(meta::DBMetaImpl(metaOptions));
Z
update  
zhiru 已提交
84
        } else {
Z
update  
zhiru 已提交
85
            ENGINE_LOG_ERROR << "Invalid dialect in URI: dialect = " << dialect;
Z
update  
zhiru 已提交
86 87
            throw InvalidArgumentException("URI dialect is not mysql / sqlite");
        }
Z
update  
zhiru 已提交
88
    } else {
Z
update  
zhiru 已提交
89 90
        ENGINE_LOG_ERROR << "Wrong URI format: URI = " << uri;
        throw InvalidArgumentException("Wrong URI format ");
Z
update  
zhiru 已提交
91 92 93
    }
}

G
groot 已提交
94
std::shared_ptr<DB> DBFactory::Build() {
95
    auto options = OptionsFactory::Build();
G
groot 已提交
96
    auto db = DBFactory::Build(options);
97 98 99
    return std::shared_ptr<DB>(db);
}

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

Z
zhiru 已提交
104 105
MemManagerAbstractPtr MemManagerFactory::Build(const std::shared_ptr<meta::Meta>& meta,
                                               const Options& options) {
106 107 108 109 110 111 112 113 114 115
    if (const char* env = getenv("MILVUS_USE_OLD_MEM_MANAGER")) {
        std::string env_str = env;
        std::transform(env_str.begin(), env_str.end(), env_str.begin(), ::toupper);
        if (env_str == "ON") {
            return std::make_shared<MemManager>(meta, options);
        }
        else {
            return std::make_shared<NewMemManager>(meta, options);
        }
    }
Z
update  
zhiru 已提交
116
    return std::make_shared<NewMemManager>(meta, options);
Z
zhiru 已提交
117 118
}

X
Xu Peng 已提交
119
} // namespace engine
J
jinhai 已提交
120
} // namespace milvus
X
Xu Peng 已提交
121
} // namespace zilliz