Factories.cpp 3.7 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.
////////////////////////////////////////////////////////////////////////////////
Z
zhiru 已提交
6
#include <stdlib.h>
7 8 9
#include "Factories.h"
#include "DBImpl.h"

X
Xu Peng 已提交
10 11
#include <time.h>
#include <sstream>
12 13 14 15
#include <iostream>
#include <vector>
#include <assert.h>
#include <easylogging++.h>
Z
update  
zhiru 已提交
16 17
#include <regex>
#include "Exception.h"
X
Xu Peng 已提交
18 19

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

//    std::string uri;
//    const char* uri_p = getenv("MILVUS_DB_META_URI");
//    if (uri_p) {
//        uri = uri_p;
//    }

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

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

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

Z
update  
zhiru 已提交
56 57
std::shared_ptr<meta::Meta> DBMetaImplFactory::Build(const DBMetaOptions& metaOptions,
                                                     const std::string& mode) {
Z
update  
zhiru 已提交
58 59

    std::string uri = metaOptions.backend_uri;
Z
zhiru 已提交
60 61 62 63 64
//    if (uri.empty()) {
//        //Default to sqlite if uri is empty
////        return std::make_shared<meta::DBMetaImpl>(new meta::DBMetaImpl(metaOptions));
//        return std::shared_ptr<meta::DBMetaImpl>(new meta::DBMetaImpl(metaOptions));
//    }
Z
update  
zhiru 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

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

G
groot 已提交
103
std::shared_ptr<DB> DBFactory::Build() {
104
    auto options = OptionsFactory::Build();
G
groot 已提交
105
    auto db = DBFactory::Build(options);
106 107 108
    return std::shared_ptr<DB>(db);
}

G
groot 已提交
109 110
DB* DBFactory::Build(const Options& options) {
    return new DBImpl(options);
111 112
}

X
Xu Peng 已提交
113
} // namespace engine
J
jinhai 已提交
114
} // namespace milvus
X
Xu Peng 已提交
115
} // namespace zilliz