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.
////////////////////////////////////////////////////////////////////////////////
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 58
std::shared_ptr<meta::Meta> DBMetaImplFactory::Build(const DBMetaOptions& metaOptions) {

    std::string uri = metaOptions.backend_uri;
Z
zhiru 已提交
59 60 61 62 63
//    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 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

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

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

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

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