Skip to content

Commit

Permalink
feat: support RESTful, add CreateImportJobs, ListImportJobs, GetImpor…
Browse files Browse the repository at this point in the history
…tJobProgress

Signed-off-by: Ruichen Bao <[email protected]>
  • Loading branch information
brcarry committed Jan 25, 2025
1 parent 4491781 commit 086e1ac
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ set_option_category("Thirdparty")
define_option_string(MILVUS_WITH_GRPC "Using gRPC from" "module" "package" "module")
define_option_string(MILVUS_WITH_ZLIB "Using Zlib from" "module" "package" "module")
define_option_string(MILVUS_WITH_NLOHMANN_JSON "nlohmann json from" "module" "package" "module")
define_option_string(MILVUS_WITH_CPP_HTTPLIB "cpp-httplib from" "module" "package" "module")
define_option_string(MILVUS_WITH_BROTLI "Using Brotli from" "module" "package" "module")
define_option_string(MILVUS_WITH_GTEST "Using GTest from" "module" "package" "module")


Expand Down
40 changes: 40 additions & 0 deletions cmake/ThirdPartyPackages.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ include(FetchContent)
set(GRPC_VERSION 1.49.1)
set(NLOHMANN_JSON_VERSION 3.11.3)
set(GOOGLETEST_VERSION 1.12.1)
set(CPP_HTTPLIB_VERSION 0.18.5)
set(BROTLI_VERSION 1.1.0)
Set(FETCHCONTENT_QUIET FALSE)

# grpc
Expand Down Expand Up @@ -50,6 +52,23 @@ FetchContent_Declare(
GIT_PROGRESS TRUE
)

# cpp-httplib
FetchContent_Declare(
cpp-httplib
GIT_REPOSITORY https://github.com/yhirose/cpp-httplib.git
GIT_TAG v${CPP_HTTPLIB_VERSION}
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)

# brotli
FetchContent_Declare(
brotli
GIT_REPOSITORY https://github.com/google/brotli.git
GIT_TAG v${BROTLI_VERSION}
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)

# grpc
if ("${MILVUS_WITH_GRPC}" STREQUAL "package")
Expand Down Expand Up @@ -88,3 +107,24 @@ else ()
add_subdirectory(${nlohmann_json_SOURCE_DIR} ${nlohmann_json_BINARY_DIR} EXCLUDE_FROM_ALL)
endif ()
endif ()

# cpp-httplib
if ("${MILVUS_WITH_CPP_HTTPLIB}" STREQUAL "package")
find_package(cpp-httplib REQUIRED)
else ()
if (NOT cpp-httplib_POPULATED)
FetchContent_Populate(cpp-httplib)
add_subdirectory(${cpp-httplib_SOURCE_DIR} ${cpp-httplib_BINARY_DIR} EXCLUDE_FROM_ALL)
include_directories(${cpp-httplib_SOURCE_DIR})
endif ()
endif ()

# brotli
if ("${MILVUS_WITH_BROTLI}" STREQUAL "package")
find_package(Brotli REQUIRED)
else ()
if (NOT brotli_POPULATED)
FetchContent_Populate(brotli)
add_subdirectory(${brotli_SOURCE_DIR} ${brotli_BINARY_DIR} EXCLUDE_FROM_ALL)
endif ()
endif ()
87 changes: 87 additions & 0 deletions src/impl/BulkImport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "milvus/BulkImport.h"

namespace milvus {

nlohmann::json
BulkImport::CreateImportJobs(const std::string& url, const std::string& collection_name,

Check warning on line 6 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L6

Added line #L6 was not covered by tests
const std::vector<std::string>& files, const std::string& db_name,
const std::string& api_key, const std::string& partition_name,
const nlohmann::json& options) {
httplib::Client client(url);

Check warning on line 10 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L10

Added line #L10 was not covered by tests

nlohmann::json request_payload = {
{"dbName", db_name},
{"collectionName", collection_name},
{"files", nlohmann::json::array({files})},
};

Check warning on line 16 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L15-L16

Added lines #L15 - L16 were not covered by tests

if (!partition_name.empty()) {
request_payload["partitionName"] = partition_name;

Check warning on line 19 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L18-L19

Added lines #L18 - L19 were not covered by tests
}

if (!options.empty()) {
if (options.contains("timeout") && !options["timeout"].is_null()) {
request_payload["options"] = options;

Check warning on line 24 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L22-L24

Added lines #L22 - L24 were not covered by tests
}
}

std::string request_url = "/v2/vectordb/jobs/import/create";

Check warning on line 28 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L28

Added line #L28 was not covered by tests
httplib::Headers headers = {
{"Authorization", "Bearer " + api_key},
};
std::string body = request_payload.dump();

Check warning on line 32 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L30-L32

Added lines #L30 - L32 were not covered by tests

auto res = client.Post(request_url, headers, body, "application/json");
if (res && res->status == 200) {
return nlohmann::json::parse(res->body);

Check warning on line 36 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L34-L36

Added lines #L34 - L36 were not covered by tests
} else {
return nullptr;

Check warning on line 38 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L38

Added line #L38 was not covered by tests
}
}

Check warning on line 40 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L40

Added line #L40 was not covered by tests

nlohmann::json
BulkImport::ListImportJobs(const std::string& url, const std::string& collection_name, const std::string& db_name,

Check warning on line 43 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L43

Added line #L43 was not covered by tests
const std::string& api_key) {
httplib::Client client(url);

Check warning on line 45 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L45

Added line #L45 was not covered by tests

nlohmann::json request_payload = {
{"collectionName", collection_name},
{"dbName", db_name},
};

Check warning on line 50 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L50

Added line #L50 was not covered by tests

std::string request_url = "/v2/vectordb/jobs/import/list";

Check warning on line 52 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L52

Added line #L52 was not covered by tests
httplib::Headers headers = {
{"Authorization", "Bearer " + api_key},
};
std::string body = request_payload.dump();

Check warning on line 56 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L54-L56

Added lines #L54 - L56 were not covered by tests

auto res = client.Post(request_url, headers, body, "application/json");
if (res && res->status == 200) {
return nlohmann::json::parse(res->body);

Check warning on line 60 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L58-L60

Added lines #L58 - L60 were not covered by tests
} else {
return nullptr;

Check warning on line 62 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L62

Added line #L62 was not covered by tests
}
}

Check warning on line 64 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L64

Added line #L64 was not covered by tests

nlohmann::json
BulkImport::GetImportJobProgress(const std::string& url, const std::string& job_id, const std::string& db_name,

Check warning on line 67 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L67

Added line #L67 was not covered by tests
const std::string& api_key) {
httplib::Client client(url);

Check warning on line 69 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L69

Added line #L69 was not covered by tests

nlohmann::json payload = {{"dbName", db_name}, {"jobID", job_id}};

Check warning on line 71 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L71

Added line #L71 was not covered by tests

std::string request_url = "/v2/vectordb/jobs/import/get_progress";

Check warning on line 73 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L73

Added line #L73 was not covered by tests
httplib::Headers headers = {
{"Authorization", "Bearer " + api_key},
};
std::string body = payload.dump();

Check warning on line 77 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L75-L77

Added lines #L75 - L77 were not covered by tests

auto res = client.Post(request_url, headers, body, "application/json");
if (res && res->status == 200) {
return nlohmann::json::parse(res->body);

Check warning on line 81 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L79-L81

Added lines #L79 - L81 were not covered by tests
} else {
return nullptr;

Check warning on line 83 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L83

Added line #L83 was not covered by tests
}
}

Check warning on line 85 in src/impl/BulkImport.cpp

View check run for this annotation

Codecov / codecov/patch

src/impl/BulkImport.cpp#L85

Added line #L85 was not covered by tests

} // namespace milvus
27 changes: 27 additions & 0 deletions src/include/milvus/BulkImport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <httplib.h>

#include <nlohmann/json.hpp>
#include <string>
#include <vector>

namespace milvus {

class BulkImport {
public:
static nlohmann::json
CreateImportJobs(const std::string& url, const std::string& collection_name, const std::vector<std::string>& files,
const std::string& db_name = "default", const std::string& api_key = "",
const std::string& partition_name = "", const nlohmann::json& options = nlohmann::json{});

static nlohmann::json
ListImportJobs(const std::string& url, const std::string& collection_name, const std::string& db_name = "default",
const std::string& api_key = "");

static nlohmann::json
GetImportJobProgress(const std::string& url, const std::string& job_id, const std::string& db_name = "default",
const std::string& api_key = "");
};

} // namespace milvus
2 changes: 2 additions & 0 deletions src/include/milvus/MilvusClientV2.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#pragma once

#include <httplib.h>

#include <memory>

#include "Status.h"
Expand Down

0 comments on commit 086e1ac

Please sign in to comment.