From 9f95fa5101f8c493b1d238a073fc289daad991c1 Mon Sep 17 00:00:00 2001 From: BlackyDrum Date: Sat, 21 Sep 2024 19:39:19 +0200 Subject: [PATCH 1/2] Use std::format instead of string concatenation --- .github/workflows/tests.yml | 12 +++++++- CMakeLists.txt | 2 +- README.md | 2 +- include/ChromaDB/Client/Client.h | 1 + include/ChromaDB/Resources/Collection.h | 1 + src/ChromaDB/Client/ChromaApiClient.cpp | 2 +- src/ChromaDB/Client/Client.cpp | 40 ++++++++++++------------- src/ChromaDB/Resources/Collection.cpp | 2 +- 8 files changed, 37 insertions(+), 25 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index fbfc752..10f4135 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -35,7 +35,17 @@ jobs: run: git submodule update --init --recursive - name: Install dependencies - run: sudo apt-get update && sudo apt-get install -y cmake libssl-dev + run: | + sudo apt-get update + sudo apt-get install -y software-properties-common + sudo add-apt-repository ppa:ubuntu-toolchain-r/test + sudo apt-get update + sudo apt-get install -y gcc-13 g++-13 cmake libssl-dev + + - name: Set GCC 13 as default + run: | + sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 60 + sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 60 - name: Configure CMake run: cmake -S . -B build diff --git a/CMakeLists.txt b/CMakeLists.txt index d509502..0845e77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10) project(chromadb VERSION 0.4.2) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED True) if (MSVC) diff --git a/README.md b/README.md index b307eee..adc047e 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ This guide will help you build and install the ChromaDB library and run an examp ### Prerequisites - CMake (version 3.10 or higher) -- A C++ compiler (supporting C++17) +- A C++ compiler (supporting C++20) - OpenSSL development libraries ### Building ChromaDB Library diff --git a/include/ChromaDB/Client/Client.h b/include/ChromaDB/Client/Client.h index aa5eb65..33b84b8 100644 --- a/include/ChromaDB/Client/Client.h +++ b/include/ChromaDB/Client/Client.h @@ -27,6 +27,7 @@ #include #include #include +#include namespace chromadb { diff --git a/include/ChromaDB/Resources/Collection.h b/include/ChromaDB/Resources/Collection.h index 8335a0e..7ada347 100644 --- a/include/ChromaDB/Resources/Collection.h +++ b/include/ChromaDB/Resources/Collection.h @@ -7,6 +7,7 @@ #include #include #include +#include namespace chromadb { diff --git a/src/ChromaDB/Client/ChromaApiClient.cpp b/src/ChromaDB/Client/ChromaApiClient.cpp index 891cae8..c92ef3e 100644 --- a/src/ChromaDB/Client/ChromaApiClient.cpp +++ b/src/ChromaDB/Client/ChromaApiClient.cpp @@ -5,7 +5,7 @@ namespace chromadb { ChromaApiClient::ChromaApiClient(const std::string& scheme, const std::string& host, const std::string& port, const std::string& authToken) : m_Scheme(scheme), m_Host(host), m_Port(port), m_AuthToken(authToken) { - m_BaseUrl = m_Scheme + "://" + m_Host + ":" + m_Port; + m_BaseUrl = std::format("{}://{}:{}", m_Scheme, m_Host, m_Port); } nlohmann::json ChromaApiClient::Get(const std::string& endpoint) diff --git a/src/ChromaDB/Client/Client.cpp b/src/ChromaDB/Client/Client.cpp index 36851e2..c809817 100644 --- a/src/ChromaDB/Client/Client.cpp +++ b/src/ChromaDB/Client/Client.cpp @@ -12,7 +12,7 @@ namespace chromadb { { try { - m_ChromaApiClient.Get("/tenants/" + m_Tenant); + m_ChromaApiClient.Get(std::format("/tenants/{}" ,m_Tenant)); } catch (ChromaException) { @@ -21,7 +21,7 @@ namespace chromadb { try { - m_ChromaApiClient.Get("/databases/" + m_Database + "?tenant=" + m_Tenant); + m_ChromaApiClient.Get(std::format("/databases/{}?tenant={}", m_Database, m_Tenant)); } catch (ChromaException) { @@ -53,7 +53,7 @@ namespace chromadb { try { - m_ChromaApiClient.Post("/databases?tenant=" + m_Tenant, json); + m_ChromaApiClient.Post(std::format("/databases?tenant={}", m_Tenant), json); } catch (ChromaException& e) { @@ -81,7 +81,7 @@ namespace chromadb { version.erase(std::remove_if(version.begin(), version.end(), [](char c) { return c == '"'; - }), version.end()); + }), version.end()); return version; } @@ -115,7 +115,7 @@ namespace chromadb { try { - m_ChromaApiClient.Post("/collections?tenant=" + m_Tenant + "&database=" + m_Database, json); + m_ChromaApiClient.Post(std::format("/collections?tenant={}&database={}", m_Tenant, m_Database), json); } catch (ChromaException& e) { @@ -129,7 +129,7 @@ namespace chromadb { { try { - auto json = m_ChromaApiClient.Get("/collections/" + name + "?tenant=" + m_Tenant + "&database=" + m_Database); + auto json = m_ChromaApiClient.Get(std::format("/collections/{}?tenant={}&database={}", name, m_Tenant, m_Database)); std::unordered_map metadata; if (!json["metadata"].is_null()) @@ -159,7 +159,7 @@ namespace chromadb { { try { - auto json = m_ChromaApiClient.Get("/collections?tenant=" + m_Tenant + "&database=" + m_Database); + auto json = m_ChromaApiClient.Get(std::format("/collections?tenant={}&database={}", m_Tenant, m_Database)); std::vector collections; for (const auto& collection : json) @@ -183,7 +183,7 @@ namespace chromadb { { try { - return m_ChromaApiClient.Get("/count_collections?tenant=" + m_Tenant + "&database=" + m_Database); + return m_ChromaApiClient.Get(std::format("/count_collections?tenant={}&database={}", m_Tenant, m_Database)); } catch (ChromaException& e) { @@ -195,7 +195,7 @@ namespace chromadb { { try { - m_ChromaApiClient.Delete("/collections/" + collection.GetName() + "?tenant=" + m_Tenant + "&database=" + m_Database); + m_ChromaApiClient.Delete(std::format("/collections/{}?tenant={}&database={}", collection.GetName(), m_Tenant, m_Database)); collection.m_IsDeleted = true; } @@ -223,7 +223,7 @@ namespace chromadb { try { - m_ChromaApiClient.Put("/collections/" + oldCollection.GetId() + "?tenant=" + m_Tenant + "&database=" + m_Database, json); + m_ChromaApiClient.Put(std::format("/collections/{}?tenant={}&database={}", oldCollection.GetId(), m_Tenant, m_Database), json); } catch (ChromaException& e) { @@ -252,7 +252,7 @@ namespace chromadb { try { - m_ChromaApiClient.Post("/collections/" + collection.GetId() + "/add", json); + m_ChromaApiClient.Post(std::format("/collections/{}/add", collection.GetId()), json); } catch (ChromaException& e) { @@ -279,7 +279,7 @@ namespace chromadb { try { - m_ChromaApiClient.Post("/collections/" + collection.GetId() + "/update", json); + m_ChromaApiClient.Post(std::format("/collections/{}/update", collection.GetId()), json); } catch (ChromaException& e) { @@ -306,7 +306,7 @@ namespace chromadb { try { - m_ChromaApiClient.Post("/collections/" + collection.GetId() + "/upsert", json); + m_ChromaApiClient.Post(std::format("/collections/{}/upsert", collection.GetId()), json); } catch (ChromaException& e) { @@ -318,7 +318,7 @@ namespace chromadb { { try { - return m_ChromaApiClient.Get("/collections/" + collection.GetId() + "/count"); + return m_ChromaApiClient.Get(std::format("/collections/{}/count", collection.GetId())); } catch (ChromaInvalidCollectionException& e) { @@ -346,7 +346,7 @@ namespace chromadb { try { - m_ChromaApiClient.Post("/collections/" + collection.GetId() + "/delete", json); + m_ChromaApiClient.Post(std::format("/collections/{}/delete", collection.GetId()), json); } catch (ChromaInvalidCollectionException& e) { @@ -381,7 +381,7 @@ namespace chromadb { try { - auto response = m_ChromaApiClient.Post("/collections/" + collection.GetId() + "/get", json); + auto response = m_ChromaApiClient.Post(std::format("/collections/{}/get", collection.GetId()), json); std::vector results; for (size_t i = 0; i < response["ids"].size(); i++) @@ -418,7 +418,7 @@ namespace chromadb { std::vector Client::Query(const Collection& collection, const std::vector& queryTexts, const std::vector>& queryEmbeddings, size_t nResults, const std::vector& include, const nlohmann::json& where_document, const nlohmann::json& where) { if (collection.GetIsDeleted()) - throw ChromaInvalidCollectionException("Collection " + collection.GetName() + " has already been deleted"); + throw ChromaInvalidCollectionException(std::format("Collection {} has already been deleted", collection.GetName())); if (!((!queryEmbeddings.empty() && queryTexts.empty()) || (queryEmbeddings.empty() && !queryTexts.empty()) || (queryEmbeddings.empty() && queryTexts.empty()))) throw ChromaInvalidArgumentException("You must provide only one of queryEmbeddings or queryTexts"); @@ -457,7 +457,7 @@ namespace chromadb { try { - auto response = m_ChromaApiClient.Post("/collections/" + collection.GetId() + "/query", json); + auto response = m_ChromaApiClient.Post(std::format("/collections/{}/query", collection.GetId()), json); std::vector queryResponses; for (size_t i = 0; i < response["ids"].size(); i++) @@ -513,7 +513,7 @@ namespace chromadb { Client::ValidationResult Client::Validate(const Collection& collection, const std::vector& ids, const std::vector>& embeddings, const std::vector>& metadata, const std::vector& documents, bool requireEmbeddingsOrDocuments) { if (collection.GetIsDeleted()) - throw ChromaInvalidCollectionException("Collection " + collection.GetName() + " has already been deleted"); + throw ChromaInvalidCollectionException(std::format("Collection {} has already been deleted", collection.GetName())); if (requireEmbeddingsOrDocuments) { @@ -557,7 +557,7 @@ namespace chromadb { duplicates.insert(id); } - throw ChromaInvalidArgumentException("Expected IDs to be unique, found duplicates for: " + Utils::Join(duplicates, ", ")); + throw ChromaInvalidArgumentException(std::format("Expected IDs to be unique, found duplicates for: {}", Utils::Join(duplicates, ", "))); } return { validatedIds, finalEmbeddings, metadata, documents }; diff --git a/src/ChromaDB/Resources/Collection.cpp b/src/ChromaDB/Resources/Collection.cpp index a541b92..0011637 100644 --- a/src/ChromaDB/Resources/Collection.cpp +++ b/src/ChromaDB/Resources/Collection.cpp @@ -10,7 +10,7 @@ namespace chromadb { void Collection::CheckDeleted() const { if (m_IsDeleted) - throw ChromaInvalidCollectionException("Collection " + m_Name + " has already been deleted"); + throw ChromaInvalidCollectionException(std::format("Collection {} has already been deleted", m_Name)); } } // namespace chromadb \ No newline at end of file From 43e79fbaa19e9325a764c1c50d57b415bd66e4e3 Mon Sep 17 00:00:00 2001 From: BlackyDrum <111639941+BlackyDrum@users.noreply.github.com> Date: Sat, 21 Sep 2024 19:42:38 +0200 Subject: [PATCH 2/2] Update tests.yml --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 10f4135..da4a3a5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -48,7 +48,7 @@ jobs: sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 60 - name: Configure CMake - run: cmake -S . -B build + run: cmake -S . -B build -DCMAKE_CXX_STANDARD=20 - name: Build run: cmake --build build