Examples › Socket Examples
socket_vector_client
Legacy: examples_socket/vector_client/testprog.cpp · Modern: examples_modern/socket/vector_client/modern.cpp
Modern equivalent of the vector client example: VECTOR_INFO and VECTOR_SEARCH on a DataReel 7.x object server over mutual TLS (gxv7ObjectClient, dr7features.h). The connection settings are one struct filled from the command line with designated initializers (C++20), the client is closed by an RAII guard, the query is a std::vector<float> passed as a std::span, and the results are printed with a range-for over std::span(results).first(count).
It needs a running 7.x object server and certificates, as the legacy example does (see examples_socket/vector_client/run_vector_client.sh): modern HOST PORT SERVER_NAME CERT KEY CA DB_NAME With no arguments it prints the usage and exits 0.
Legacy calls in this program
| Legacy | Modern C++ | Standard | Notes |
|---|---|---|---|
| gxString / UString | std::string, std::string_view | C++98 / C++17 | string_view looks at text without copying it. |
| Atoi(), Atol(), Atof(), atoi() | std::from_chars() into std::optional<T>; std::stoi() | C++17 | Overflow and junk are reported, not turned into 0. |
| SetWidth(), SetFill(), Precision(), sprintf() | std::format("{:*>10} {:.3f}", ...) | C++20 | C++17 and older: <iomanip> setw, setfill, setprecision. |
| new / delete | automatic objects, containers, std::unique_ptr / std::make_unique | C++11 / C++14 | Ownership is in the type, and release() shows a hand-over. |
| gxDatabase | keep gxDatabase: a stack object, errors as exceptions, static_assert(std::is_trivially_copyable_v<Record>) | C++17 | The standard library has no database file; the file format is DataReel's. |
| gxv7VectorIndex, gxv7ObjRepl, gxv7ObjectClient | keep them; std::span, <random>, std::ranges for checks | C++20 | Needs DATAREEL7_FEATURES_DIR. |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_socket/vector_client/testprog.cpp 171 lines
#include "gxdlcode.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include "gxdbase.h" #include "gxstring.h" #include "dr7features.h" static int failures = 0; static void check(int ok, const char *what) { printf("%s %s\n", ok ? "PASS" : "FAIL", what); fflush(stdout); if(!ok) failures++; } const unsigned DIM = 16; const unsigned DAYS = 3; const unsigned PER_DAY = 300; static void day_vector(unsigned day, unsigned i, float *v) { for(unsigned d = 0; d < DIM; d++) v[d] = (float)sin(day * 1.7 + i * 0.13 + d * 0.9); } static void query_vector(unsigned q, float *v) { for(unsigned d = 0; d < DIM; d++) v[d] = (float)cos(q * 0.77 + d * 0.41); } static int make(const char *dir) { gxString manifest = dir; manifest << "/news.vixset"; gxv7VectorSet set; int rv = set.Create(manifest.c_str(), "news", DIM, gxv7FLOAT32, gxv7L2, "news-test"); check(rv == gxv7_NO_ERROR, "vector set created"); for(unsigned day = 0; day < DAYS; day++) { char rel[32]; sprintf(rel, "day%u.vix", day); gxString path = dir; path << "/" << rel; gxv7VectorIndex v; int ok = v.Create(path.c_str(), DIM, gxv7FLOAT32, gxv7L2, 16, "news-test") == gxv7_NO_ERROR; float vec[DIM]; for(unsigned i = 0; i < PER_DAY && ok; i++) { day_vector(day, i, vec); unsigned tags[gxv7TagCount] = { i % 5, 0, 20261110 + day, 0 }; if(v.Insert((long long)(day * 100000 + i), vec, tags) != gxv7_NO_ERROR) ok = 0; } v.Close(); if(ok) ok = set.AddFile(rel) == gxv7_NO_ERROR; check(ok, "day file written and added"); } set.Close(); gxString dbname = dir; dbname << "/news.gxd"; gxDatabase db; check(db.Create(dbname.c_str()) == gxDBASE_NO_ERROR, "6.x database news.gxd created"); db.Close(); return failures ? 1 : 0; } // One line per result: id distance file static gxString lines(const gxv7Result *r, unsigned n, const gxString *names) { gxString s; char buf[128]; for(unsigned i = 0; i < n; i++) { sprintf(buf, "%lld %.9g ", r[i].id, r[i].distance); s << buf << names[i] << "\n"; } return s; } static int do_check(int argc, char **argv) { gxv7ObjectClient cli; int rv = cli.Connect(argv[2], atoi(argv[3]), argv[4], argv[5], argv[6], argv[7], argv[8]); check(rv == 0, "connected over mutual TLS, channel opened"); if(rv != 0) { printf(" %s\n", cli.Error().c_str()); return 1; } gxString manifest = argv[9]; manifest << "/news.vixset"; gxv7VectorSet local; check(local.Open(manifest.c_str()) == gxv7_NO_ERROR, "local vector set opened"); gxv7VectorInfo info; rv = cli.VectorInfo(info); printf(" VECTOR_INFO: version %u, dim %u, type %d, metric %d, %u files, %llu nodes, %llu deleted, spec \"%s\"\n", info.version, info.dim, (int)info.type, (int)info.metric, info.files, info.nodes, info.deleted, info.spec.c_str()); check(rv == 0 && info.version == 1 && info.dim == DIM && info.type == gxv7FLOAT32 && info.metric == gxv7L2 && info.files == DAYS && info.nodes == DAYS * PER_DAY && info.spec == gxString("news-test"), "VECTOR_INFO describes the set"); float q[DIM]; gxv7Result rr[10], lr[10]; gxString rn[10], ln[10]; int same = 0; for(unsigned qi = 0; qi < 6; qi++) { query_vector(qi, q); unsigned nr = 0, nl = 0; int a = cli.VectorSearch(q, DIM, 10, rr, nr); int b = local.Search(q, 10, lr, nl); for(unsigned i = 0; i < nr; i++) rn[i] = cli.FileName(rr[i].file); for(unsigned i = 0; i < nl; i++) ln[i] = local.FilePath(lr[i].file); gxString x = lines(rr, nr, rn), y = lines(lr, nl, ln); if(a == 0 && b == gxv7_NO_ERROR && nr == 10 && x == y) same++; else printf(" query %u differs:\n%s local:\n%s", qi, x.c_str(), y.c_str()); } check(same == 6, "6 remote searches equal the local searches (ids, distances, order, files)"); // A day range: only day 1, the other two files skipped query_vector(2, q); unsigned nr = 0, nl = 0, skipped = 0, missing = 0; int a = cli.VectorSearch(q, DIM, 10, rr, nr, 64, 0, 1, 20261111, 20261111, &skipped, &missing); int b = local.Search(q, 10, lr, nl, 64, 0, 1, 20261111, 20261111); int day_ok = (a == 0 && b == gxv7_NO_ERROR && nr == 10 && nr == nl && skipped == 2 && missing == 0); for(unsigned i = 0; i < nr && day_ok; i++) { if(rr[i].tags[2] != 20261111 || rr[i].id != lr[i].id || cli.FileName(rr[i].file) != gxString("day1.vix")) day_ok = 0; } check(day_ok, "day range search: day 1 only, 2 files skipped, equal to local"); // A tag filter gxv7Filter flt; flt.Tag(0, 3); a = cli.VectorSearch(q, DIM, 10, rr, nr, 64, &flt); b = local.Search(q, 10, lr, nl, 64, &flt); int flt_ok = (a == 0 && b == gxv7_NO_ERROR && nr == 10 && nr == nl); for(unsigned i = 0; i < nr && flt_ok; i++) if(rr[i].tags[0] != 3 || rr[i].id != lr[i].id) flt_ok = 0; check(flt_ok, "tag filter search equal to local"); // Refused requests: a wrong dimension, k too large float big[DIM + 1]; for(unsigned d = 0; d <= DIM; d++) big[d] = 0.1f; check(cli.VectorSearch(big, DIM + 1, 10, rr, nr) != 0, "a query of the wrong dimension is refused"); gxv7Result *many = new gxv7Result[1001]; check(cli.VectorSearch(q, DIM, 1001, many, nr) != 0, "k over 1000 is refused"); delete[] many; check(cli.VectorSearch(q, DIM, 3, rr, nr) == 0 && nr == 3, "the channel still answers after refusals"); cli.Close(); local.Close(); return failures ? 1 : 0; } int main(int argc, char **argv) { if(argc == 3 && strcmp(argv[1], "make") == 0) return make(argv[2]); if(argc == 10 && strcmp(argv[1], "check") == 0) { int rv = do_check(argc, argv); if(failures) printf("%d check(s) FAILED\n", failures); else printf("All checks passed\n"); return rv; } if(argc == 9 && strcmp(argv[1], "refused") == 0) { gxv7ObjectClient cli; int rv = cli.Connect(argv[2], atoi(argv[3]), argv[4], argv[5], argv[6], argv[7], argv[8]); if(rv == 0) { gxv7VectorInfo info; rv = cli.VectorInfo(info); } printf("%s: %s\n", rv == 0 ? "ACCEPTED" : "REFUSED", rv == 0 ? "" : cli.Error().c_str()); return rv == 0 ? 1 : 0; } printf("Usage: %s make DIR | check HOST PORT SERVER_NAME CERT KEY CA DB_NAME DIR |\n" " refused HOST PORT SERVER_NAME CERT KEY CA DB_NAME\n", argv[0]); return 2; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++20)
examples_modern/socket/vector_client/modern.cpp 51 lines
#include <cstdlib> #include <iostream> #include <span> #include <string> #include <vector> #include "dr7features.h" struct Settings { std::string host, server_name, cert, key, ca, db; int port = 0; }; int main(int argc, char **argv) { if(argc != 8) { std::cout << "Usage: " << argv[0] << " HOST PORT SERVER_NAME CERT KEY CA DB_NAME\n" << "(needs a DataReel 7.x object server; see run_vector_client.sh)\n"; return 0; } const Settings s{.host = argv[1], .server_name = argv[3], .cert = argv[4], .key = argv[5], .ca = argv[6], .db = argv[7], .port = std::atoi(argv[2])}; gxv7ObjectClient client; struct Close { gxv7ObjectClient &c; ~Close() { c.Close(); } } guard{client}; if(client.Connect(s.host.c_str(), s.port, s.server_name.c_str(), s.cert.c_str(), s.key.c_str(), s.ca.c_str(), s.db.c_str()) != 0) { std::cout << "Connect refused: " << client.Error().c_str() << "\n"; return 1; } gxv7VectorInfo info; if(client.VectorInfo(info) != 0) { std::cout << client.Error().c_str() << "\n"; return 1; } std::cout << "Server vectors: dim " << info.dim << "\n"; std::vector<float> query(info.dim, 0.5f); std::vector<gxv7Result> results(10); unsigned count = 0; if(client.VectorSearch(std::span(query).data(), unsigned(query.size()), unsigned(results.size()), results.data(), count) != 0) { std::cout << client.Error().c_str() << "\n"; return 1; } for(const auto &r : std::span(results).first(count)) std::cout << "id " << r.id << " distance " << r.distance << " file " << client.FileName(r.file).c_str() << "\n"; return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DDATAREEL7_FEATURES_DIR=/path/to/datareel-7.4 -DCMAKE_CXX_STANDARD=20 cmake --build build-modern --target socket_vector_client ./build-modern/socket_vector_client
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
Usage: build-modern/cxx20/socket_vector_client HOST PORT SERVER_NAME CERT KEY CA DB_NAME (needs a DataReel 7.x object server; see run_vector_client.sh)