forked from pgvector/pgvector-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpqxx.hpp
More file actions
183 lines (144 loc) · 5.71 KB
/
pqxx.hpp
File metadata and controls
183 lines (144 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* pgvector-cpp v0.2.2
* https://github.com/pgvector/pgvector-cpp
* MIT License
*/
#pragma once
#include <cstddef>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
#include <pqxx/pqxx>
#include "halfvec.hpp"
#include "sparsevec.hpp"
#include "vector.hpp"
/// @cond
namespace pqxx {
template <> inline std::string const type_name<pgvector::Vector>{"vector"};
template <> struct nullness<pgvector::Vector> : pqxx::no_null<pgvector::Vector> {};
template <> struct string_traits<pgvector::Vector> {
static constexpr bool converts_to_string{true};
static constexpr bool converts_from_string{true};
static pgvector::Vector from_string(std::string_view text) {
if (text.front() != '[' || text.back() != ']') {
throw conversion_error("Malformed vector literal");
}
// TODO don't copy string
std::vector<float> result;
std::stringstream ss(std::string(text.substr(1, text.size() - 2)));
while (ss.good()) {
std::string substr;
getline(ss, substr, ',');
result.push_back(std::stof(substr));
}
return pgvector::Vector(result);
}
static zview to_buf(char* begin, char* end, const pgvector::Vector& value) {
char *const next = into_buf(begin, end, value);
return zview{begin, next - begin - 1};
}
static char* into_buf(char* begin, char* end, const pgvector::Vector& value) {
auto ret = string_traits<std::vector<float>>::into_buf(
begin, end, static_cast<std::vector<float>>(value));
// replace array brackets
*begin = '[';
*(ret - 2) = ']';
return ret;
}
static size_t size_buffer(const pgvector::Vector& value) noexcept {
return string_traits<std::vector<float>>::size_buffer(
static_cast<std::vector<float>>(value));
}
};
template <> inline std::string const type_name<pgvector::HalfVector>{"halfvec"};
template <> struct nullness<pgvector::HalfVector> : pqxx::no_null<pgvector::HalfVector> {};
template <> struct string_traits<pgvector::HalfVector> {
static constexpr bool converts_to_string{true};
static constexpr bool converts_from_string{true};
static pgvector::HalfVector from_string(std::string_view text) {
if (text.front() != '[' || text.back() != ']') {
throw conversion_error("Malformed halfvec literal");
}
// TODO don't copy string
std::vector<float> result;
std::stringstream ss(std::string(text.substr(1, text.size() - 2)));
while (ss.good()) {
std::string substr;
getline(ss, substr, ',');
result.push_back(std::stof(substr));
}
return pgvector::HalfVector(result);
}
static zview to_buf(char* begin, char* end, const pgvector::HalfVector& value) {
char *const next = into_buf(begin, end, value);
return zview{begin, next - begin - 1};
}
static char* into_buf(char* begin, char* end, const pgvector::HalfVector& value) {
auto ret = string_traits<std::vector<float>>::into_buf(
begin, end, static_cast<std::vector<float>>(value));
// replace array brackets
*begin = '[';
*(ret - 2) = ']';
return ret;
}
static size_t size_buffer(const pgvector::HalfVector& value) noexcept {
return string_traits<std::vector<float>>::size_buffer(
static_cast<std::vector<float>>(value));
}
};
template <> inline std::string const type_name<pgvector::SparseVector>{"sparsevec"};
template <> struct nullness<pgvector::SparseVector> : pqxx::no_null<pgvector::SparseVector> {};
template <> struct string_traits<pgvector::SparseVector> {
static constexpr bool converts_to_string{true};
// TODO add from_string
static constexpr bool converts_from_string{false};
static zview to_buf(char* begin, char* end, const pgvector::SparseVector& value) {
char *const next = into_buf(begin, end, value);
return zview{begin, next - begin - 1};
}
static char* into_buf(char* begin, char* end, const pgvector::SparseVector& value) {
int dimensions = value.dimensions();
auto indices = value.indices();
auto values = value.values();
size_t nnz = indices.size();
// important! size_buffer cannot throw an exception on overflow
// so perform this check before writing any data
if (nnz > 16000) {
throw conversion_overrun{"sparsevec cannot have more than 16000 dimensions"};
}
char *here = begin;
*here++ = '{';
for (size_t i = 0; i < nnz; i++) {
if (i != 0) {
*here++ = ',';
}
here = string_traits<int>::into_buf(here, end, indices[i] + 1) - 1;
*here++ = ':';
here = string_traits<float>::into_buf(here, end, values[i]) - 1;
}
*here++ = '}';
*here++ = '/';
here = string_traits<int>::into_buf(here, end, dimensions) - 1;
*here++ = '\0';
return here;
}
static size_t size_buffer(const pgvector::SparseVector& value) noexcept {
int dimensions = value.dimensions();
auto indices = value.indices();
auto values = value.values();
size_t nnz = indices.size();
// cannot throw an exception here on overflow
// so throw in into_buf
size_t size = 4; // {, }, /, and \0
size += string_traits<int>::size_buffer(dimensions);
for (size_t i = 0; i < nnz; i++) {
size += 2; // : and ,
size += string_traits<int>::size_buffer(indices[i]);
size += string_traits<float>::size_buffer(values[i]);
}
return size;
}
};
} // namespace pqxx
/// @endcond