Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
#ifndef DBMS_VECTOR_REST_INTERNAL_HPP
#define DBMS_VECTOR_REST_INTERNAL_HPP
 
/*
 * DBMS_VECTOR / DBMS_VECTOR_CHAIN shared REST/credential core.
 *
 * One implementation of the provider transport used by every REST-backed
 * API of both packages (UTL_TO_EMBEDDING/UTL_TO_EMBEDDINGS today,
 * UTL_TO_GENERATE_TEXT later): params parsing (provider/url/model/
 * credential_name/transfer_timeout), the CHECK_REST_PARAMS pre-flight,
 * credential resolution to a bearer key, the libcurl JSON POST, passthrough
 * of non-reserved params into request bodies (REMOVE_ORACLE_KEYS parity)
 * and the GET_PROVIDER_ERROR response parser.  Business logic must not be
 * duplicated into dbms_vector.cpp or dbms_vector_chain.cpp.
 */
 
#include <string>
 
extern "C"
{
 
#include "postgres.h"
#include "utils/jsonb.h"
 
} // extern "C"
 
/*
 * Oracle DEFAULT_TRANSFER_TIMEOUT := 60 (seconds); applies to the whole
 * REST request (connect + transfer), 0 disables the timeout.
 */
#define DBMS_VECTOR_DEFAULT_TRANSFER_TIMEOUT 60
 
/// Recognized REST params of the provider dispatch layer.
struct DbmsVectorRestParams
{
    std::string provider = "database";
    std::string url;
    std::string model;
    std::string credential_name;
    int32_t transfer_timeout = DBMS_VECTOR_DEFAULT_TRANSFER_TIMEOUT;
};
 
/// One completed HTTP exchange (status 0 means curl reported no status).
struct DbmsVectorRestResponse
{
    long status_code = 0;
    std::string body;
};
 
/*
 * Parse the jsonb params of a REST call: provider (default 'database',
 * lowercased), url, model, credential_name and transfer_timeout
 * (default 60 s).  A nullptr params keeps every default.
 */
void dbms_vector_rest_parse_params(Jsonb *params,
                                   DbmsVectorRestParams &out);
 
/*
 * CHECK_REST_PARAMS parity (pls:3639): url, model and credential_name are
 * required.  A missing/unusable credential is reported by the lookup helper
 * with the ORA-20001 parity error.
 */
void dbms_vector_rest_check_params(const DbmsVectorRestParams &params);
 
/*
 * Keys consumed by the dispatch layer and never forwarded to the provider
 * (Oracle REMOVE_ORACLE_KEYS, pls:3664).
 */
bool dbms_vector_rest_is_reserved_key(const char *key, int len);
 
/*
 * Append every non-reserved key/value pair of params to an object under
 * construction (WJB_KEY/WJB_VALUE pairs; the caller owns the WJB_BEGIN/
 * END_OBJECT brackets).  This is the passthrough half of request-body
 * builders: reserved keys are stripped exactly once, here.
 */
void dbms_vector_rest_push_passthrough(JsonbInState *state, Jsonb *params);
 
/*
 * POST body as JSON with a bearer key (Oracle HTTP_SEND_REQUEST +
 * Authorization header parity).  Any transport failure raises; HTTP error
 * statuses do not - the body is left to the response parser so the caller
 * reports "provider returned an error" like Oracle does.
 */
DbmsVectorRestResponse dbms_vector_rest_call(
    const DbmsVectorRestParams &params, const std::string &api_key,
    const char *body);
 
/*
 * GET_PROVIDER_ERROR analog (pls:3731): extract a human-readable message
 * from $.message / $.error.message / $.error[0] / $.error when the output is
 * a short (<512 bytes) JSON document; otherwise return the raw output.
 */
std::string dbms_vector_rest_provider_error(const char *output);
 
/*
 * Raw stored params of a credential (secrets included) for internal REST
 * consumers.  The SQL boundary (dbms_vector_get_credential,
 * SYS.all_vector_credentials) redacts the secret values; this accessor is
 * deliberately not SQL-exposed.  Honors OWNER.NAME qualification and the
 * owner-or-superuser access rule, returns a copy allocated in the caller's
 * memory context, or raises the ORA-20001 parity error.
 */
Jsonb *dbms_vector_credential_lookup(const std::string &credential_name);
 
/*
 * Map the stored credential params onto the REST bearer key.  Oracle
 * providers always send 'Authorization: Bearer <KEY>' (pls:2253, 2335), so
 * only the access_token form carries a usable key; the username/password
 * and OCI forms raise a clear local error instead of a doomed HTTP call.
 */
std::string dbms_vector_rest_credential_key(Jsonb *credential,
                                            const std::string
                                                &credential_name);
 
#endif // DBMS_VECTOR_REST_INTERNAL_HPP