Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::format instead of string concatenation #6

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,20 @@ 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
run: cmake -S . -B build -DCMAKE_CXX_STANDARD=20

- name: Build
run: cmake --build build
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/ChromaDB/Client/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <unordered_map>
#include <unordered_set>
#include <regex>
#include <format>

namespace chromadb {

Expand Down
1 change: 1 addition & 0 deletions include/ChromaDB/Resources/Collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include <memory>
#include <map>
#include <format>

namespace chromadb {

Expand Down
2 changes: 1 addition & 1 deletion src/ChromaDB/Client/ChromaApiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 20 additions & 20 deletions src/ChromaDB/Client/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace chromadb {
{
try
{
m_ChromaApiClient.Get("/tenants/" + m_Tenant);
m_ChromaApiClient.Get(std::format("/tenants/{}" ,m_Tenant));
}
catch (ChromaException)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand All @@ -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<std::string, std::string> metadata;
if (!json["metadata"].is_null())
Expand Down Expand Up @@ -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<Collection> collections;
for (const auto& collection : json)
Expand All @@ -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)
{
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<EmbeddingResource> results;
for (size_t i = 0; i < response["ids"].size(); i++)
Expand Down Expand Up @@ -418,7 +418,7 @@ namespace chromadb {
std::vector<QueryResponseResource> Client::Query(const Collection& collection, const std::vector<std::string>& queryTexts, const std::vector<std::vector<double>>& queryEmbeddings, size_t nResults, const std::vector<std::string>& 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");
Expand Down Expand Up @@ -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<QueryResponseResource> queryResponses;
for (size_t i = 0; i < response["ids"].size(); i++)
Expand Down Expand Up @@ -513,7 +513,7 @@ namespace chromadb {
Client::ValidationResult Client::Validate(const Collection& collection, const std::vector<std::string>& ids, const std::vector<std::vector<double>>& embeddings, const std::vector<std::unordered_map<std::string, std::string>>& metadata, const std::vector<std::string>& 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)
{
Expand Down Expand Up @@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion src/ChromaDB/Resources/Collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading