Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
/// \file model_package.h
/// \brief Public C API for the ONNX Runtime Model Package library.
///
/// A model package is a directory with a top-level `manifest.json` that
/// declares a set of components; each component declares a set of variants;
/// each variant points at a directory containing the model files and may
/// carry executor-specific configuration under per-namespace
/// `executor_info` entries.
///
/// Error handling: functions that can fail return `ModelPackageStatus*`.
/// A `nullptr` return indicates success. Use `ModelPackageStatus_Message`,
/// `ModelPackageStatus_Code`, and `ModelPackageStatus_Release` to inspect and
/// release statuses.
///
/// Object lifetime: every `const char*` and every `const ModelPackageInfo*`
/// (and its sub-arrays) returned by this API is owned by the `ModelPackage`
/// handle and remains valid until the next mutation of that scope or until
/// the package is closed. Mutations invalidate cached pointers in the mutated
/// scope and its descendants; callers must re-read `ModelPackage_Info()`
/// after any mutation.
 
#pragma once
 
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
 
#include "model_package_api.h"
 
#ifdef __cplusplus
extern "C" {
#endif
 
// The library is consumed as source (compiled into each consumer's own binary),
// so the structs below have no binary boundary to maintain: there is no
// struct_size/ABI versioning. Compatibility with on-disk packages is governed
// solely by `schema_version` (see ModelPackageInfo).
 
// ─────────────────────────────────────────────────────────────────────────────
// Opaque handle
// ─────────────────────────────────────────────────────────────────────────────
 
typedef struct ModelPackage ModelPackage;
 
// ─────────────────────────────────────────────────────────────────────────────
// Status helpers
// ─────────────────────────────────────────────────────────────────────────────
 
/// Get the error message from a status object. Returns NULL if `status` is NULL.
/// The returned string is owned by the status object.
MODEL_PACKAGE_API const char* ModelPackageStatus_Message(const ModelPackageStatus*);
/// Get the categorical error code. Returns `MODEL_PACKAGE_OK` when `status` is NULL.
MODEL_PACKAGE_API ModelPackageErrorCode ModelPackageStatus_Code(const ModelPackageStatus*);
/// Release a status object. Safe to call with NULL.
MODEL_PACKAGE_API void ModelPackageStatus_Release(ModelPackageStatus*);
 
// ─────────────────────────────────────────────────────────────────────────────
// Lifecycle
// ─────────────────────────────────────────────────────────────────────────────
 
typedef struct ModelPackageOpenOptions {
  bool allow_external_paths;   ///< default false; unlocks absolute paths and `..` segments
  bool follow_symlinks;        ///< default true
  bool strict_unknown_fields;  ///< default true; relax to round-trip newer schemas
} ModelPackageOpenOptions;
 
/// Open an existing model package directory. `opts` may be NULL for defaults.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_Open(const char* package_root,
                                                        const ModelPackageOpenOptions* opts,
                                                        ModelPackage** out);
 
/// Create a new empty in-memory package for from-scratch authoring.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_New(ModelPackage** out);
 
/// Release a ModelPackage handle and all its caches. Safe on NULL.
MODEL_PACKAGE_API void ModelPackage_Close(ModelPackage* pkg);
 
// ─────────────────────────────────────────────────────────────────────────────
// Data model — POD structs read from ModelPackage_Info()
// ─────────────────────────────────────────────────────────────────────────────
 
typedef struct ModelExecutorInfoEntry {
  const char* namespace_key;  ///< executor namespace name (e.g. "ort")
  const char* json;           ///< canonical JSON value as string (object, array, etc.)
} ModelExecutorInfoEntry;
 
typedef struct ModelVariantInfo {
  const char* name;
  /// Resolved absolute path to the variant's on-disk directory, or NULL when
  /// no directory has been declared and the default location does not exist.
  const char* variant_directory;
  const char* ep;                        ///< NULL when unset
  const char* device;                    ///< NULL when unset
  const char* compatibility_string;      ///< NULL when unset
  const char* additional_metadata_json;  ///< NULL when unset
  size_t num_executor_infos;
  const ModelExecutorInfoEntry* executor_infos;
} ModelVariantInfo;
 
typedef struct ModelComponentInfo {
  const char* name;
  const char* additional_metadata_json;  ///< NULL when unset
  size_t num_variants;
  const ModelVariantInfo* variants;
} ModelComponentInfo;
 
typedef struct ModelSharedAssetInfo {
  const char* uri;            ///< "sha256:<hex>"
  const char* resolved_path;  ///< absolute on-disk directory path
} ModelSharedAssetInfo;
 
typedef struct ModelPackageInfo {
  int64_t schema_version_major;          ///< parsed from on-disk "<major>.<minor>"; gates compatibility
  int64_t schema_version_minor;          ///< informational; indicates which optional fields may be present
  const char* package_name;              ///< NULL when unset
  const char* package_version;           ///< NULL when unset
  const char* description;               ///< NULL when unset
  const char* layout;                    ///< "portable" or "installed"
  const char* additional_metadata_json;  ///< NULL when unset
  size_t num_components;
  const ModelComponentInfo* components;
  size_t num_shared_assets;
  const ModelSharedAssetInfo* shared_assets;
} ModelPackageInfo;
 
/// Return the package-level info tree. Pointer is owned by the package and is
/// invalidated by any mutation.
MODEL_PACKAGE_API const ModelPackageInfo* ModelPackage_Info(const ModelPackage* pkg);
 
// ─────────────────────────────────────────────────────────────────────────────
// Convenience lookups
// ─────────────────────────────────────────────────────────────────────────────
 
/// Find a component by name. Returns NULL when not found.
MODEL_PACKAGE_API const ModelComponentInfo* ModelPackage_FindComponent(const ModelPackageInfo*,
                                                                       const char* name);
/// Find a variant within a component by name. Returns NULL when not found.
MODEL_PACKAGE_API const ModelVariantInfo* ModelComponentInfo_FindVariant(const ModelComponentInfo*,
                                                                         const char* name);
/// Find an executor_info entry by namespace. Returns NULL when not declared.
MODEL_PACKAGE_API const ModelExecutorInfoEntry* ModelVariantInfo_FindExecutorInfo(
    const ModelVariantInfo*, const char* namespace_key);
 
// ─────────────────────────────────────────────────────────────────────────────
// Round-trip JSON getters
// ─────────────────────────────────────────────────────────────────────────────
 
/// Get the canonical schema-shaped JSON for the named component. Preserves
/// fields unknown to this build. The returned pointer is owned by the package.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_GetComponentJson(const ModelPackage*,
                                                                    const char* component_name,
                                                                    const char** out_json);
 
/// Get the canonical schema-shaped JSON for the named variant.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_GetVariantJson(const ModelPackage*,
                                                                  const char* component_name,
                                                                  const char* variant_name,
                                                                  const char** out_json);
 
// ─────────────────────────────────────────────────────────────────────────────
// Asset resolution + hashing
// ─────────────────────────────────────────────────────────────────────────────
 
/// Resolve a `sha256:<hex>` URI to an on-disk directory. Errors with
/// `MODEL_PACKAGE_ERR_ASSET_MISSING` when not resolvable.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_ResolveAssetUri(const ModelPackage*,
                                                                   const char* uri,
                                                                   const char** out_path);
 
/// Resolve a string reference using the model package's path resolution rules.
/// `input` may be:
///   - `sha256:<hex>`               -> shared-asset folder
///   - `sha256:<hex>/sub/path`      -> file or subdir inside a shared-asset folder
///                                     (sub/path is resolved with portable-mode
///                                     confinement under the asset folder: no
///                                     absolute, no `..`)
///   - relative path                -> resolved against `base_dir` (or
///                                     `package_root` when `base_dir == NULL`),
///                                     confined to `package_root` in portable layout
///   - absolute path / `..` segments -> only allowed in installed layout, or in
///                                     any layout when the package was opened with
///                                     `ModelPackageOpenOptions.allow_external_paths`
///
/// `must_exist` controls whether a missing target is `MODEL_PACKAGE_ERR_NOT_FOUND`
/// or the lexically-normalized path is returned anyway.
/// On success `*out_path` points to a NUL-terminated thread-local string; copy
/// it if you need it to outlive the next `ModelPackage_ResolveStringRef` call on
/// the same thread.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_ResolveStringRef(const ModelPackage*,
                                                                    const char* base_dir,
                                                                    const char* input,
                                                                    bool must_exist,
                                                                    const char** out_path);
 
/// Compute the canonical `sha256:<hex>` URI for a directory. On success,
/// `*out_uri` is set to a NUL-terminated string owned by an internal
/// thread-local slot; the caller must copy if it must outlive the next call
/// on the same thread.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_ComputeDirectoryHash(const char* source_dir,
                                                                        const char** out_uri);
 
// ─────────────────────────────────────────────────────────────────────────────
// Authoring — mutation API
// ─────────────────────────────────────────────────────────────────────────────
//
// Each mutation invalidates info pointers in the mutated scope and its
// descendants. Strict unknown-field rejection follows the open-time option
// `strict_unknown_fields` (default true).
 
/// Set or replace an inline component. `component_json` must be a JSON object
/// matching the component schema. An existing component with the same name is
/// replaced.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_SetComponentInline(ModelPackage*,
                                                                      const char* name,
                                                                      const char* component_json);
 
/// Set or replace an external component. `path` is recorded in the manifest
/// (relative to package_root, or absolute in installed layout). If the file
/// exists, it is loaded; otherwise the component is initialized empty
/// (`{"variants": {}}`). `path` may be a directory (resolves to
/// `<dir>/component.json`).
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_SetComponentExternal(ModelPackage*,
                                                                        const char* name,
                                                                        const char* path);
 
/// Remove a component by name. No-op when the name is not present.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_RemoveComponent(ModelPackage*, const char* name);
 
/// Upsert a variant inside a component. `variant_json` must be a JSON object
/// matching the variant schema. The library does not validate that
/// `variant_directory` exists on disk; executors are responsible for resolving
/// their own file references at load time.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_SetVariant(ModelPackage*,
                                                              const char* component_name,
                                                              const char* variant_name,
                                                              const char* variant_json);
 
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_RemoveVariant(ModelPackage*,
                                                                 const char* component_name,
                                                                 const char* variant_name);
 
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_SetVariantExecutorInfoInline(ModelPackage*,
                                                                                const char* component,
                                                                                const char* variant,
                                                                                const char* ns,
                                                                                const char* info_json);
 
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_SetVariantExecutorInfoExternal(ModelPackage*,
                                                                                  const char* component,
                                                                                  const char* variant,
                                                                                  const char* ns,
                                                                                  const char* path);
 
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_RemoveVariantExecutorInfo(ModelPackage*,
                                                                             const char* component,
                                                                             const char* variant,
                                                                             const char* ns);
 
/// Add a content-addressed shared asset. When `expected_uri_or_null` is
/// non-NULL, the computed URI must match (reproducible-build check). With
/// `copy_in == false`, an override path is stored in the manifest; this is
/// rejected eagerly in portable layout. With `copy_in == true`, the source
/// directory is staged for copy at `_Commit` time.
///
/// `out_uri` is set to a NUL-terminated string owned by the package. The
/// pointer is only guaranteed to remain valid until the next mutation
/// (any ModelPackage_Set*, ModelPackage_Remove*, ModelPackage_AddSharedAsset,
/// or ModelPackage_Commit call), since those calls may rebuild the
/// shared-asset table or rehash the pending-copies map. Callers that need to
/// retain the URI must copy it into their own storage.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_AddSharedAsset(ModelPackage*,
                                                                  const char* source_dir,
                                                                  const char* expected_uri_or_null,
                                                                  bool copy_in,
                                                                  const char** out_uri);
 
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_RemoveSharedAsset(ModelPackage*, const char* uri);
 
/// Set or clear package-level metadata. Any argument may be NULL to leave the
/// existing value untouched. Passing an empty string clears the field.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_SetMetadata(ModelPackage*,
                                                               const char* name_or_null,
                                                               const char* version_or_null,
                                                               const char* description_or_null);
 
/// Set the layout. Valid values: "portable" or "installed".
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_SetLayout(ModelPackage*, const char* layout);
 
/// Set or clear `additional_metadata` at a given scope.
///   scope = "manifest"  — component_or_null and variant_or_null must be NULL
///   scope = "component" — component_or_null is required, variant_or_null is NULL
///   scope = "variant"   — component_or_null and variant_or_null are both required
/// `json_or_null == NULL` clears the field at that scope.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_SetAdditionalMetadataJson(ModelPackage*,
                                                                             const char* scope,
                                                                             const char* component_or_null,
                                                                             const char* variant_or_null,
                                                                             const char* json_or_null);
 
// ─────────────────────────────────────────────────────────────────────────────
// Commit / Prune / Validate
// ─────────────────────────────────────────────────────────────────────────────
 
typedef enum {
  MODEL_PACKAGE_WRITE_PRESERVE = 0,  ///< each component/executor-info keeps its current shape
  MODEL_PACKAGE_WRITE_DENSE = 1,     ///< flatten all external components inline
} ModelPackageWriteMode;
 
/// Persist the in-memory model to disk. `dest_root_or_null == NULL` commits
/// in-place at `package_root`. Otherwise `dest_root` must be empty or
/// nonexistent and the entire package is materialized there (self-contained
/// "save as"). On a successful dest_root commit, the package's root is
/// updated to `dest_root` so subsequent in-place commits go there.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_Commit(ModelPackage*,
                                                          const char* dest_root_or_null,
                                                          ModelPackageWriteMode mode);
 
/// Reclaim stale `.tmp.<suffix>` staging directories under
/// `<package_root>/shared_assets/` (left by interrupted commits, after a grace
/// window) and tracked orphan variant/component directories left behind by
/// RemoveVariant, RemoveComponent, SetVariant or SetComponentExternal. Only
/// paths registered through this API and inside `package_root` are touched.
/// Content-addressed shared-asset (`sha256-<hex>`) directories are never removed
/// — use ModelPackage_RemoveSharedAsset to reclaim those.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_Prune(ModelPackage*);
 
typedef enum {
  MODEL_PACKAGE_VALIDATE_SCHEMA = 1 << 0,
  MODEL_PACKAGE_VALIDATE_PATHS = 1 << 1,
  MODEL_PACKAGE_VALIDATE_ASSET_REHASH = 1 << 2,
  MODEL_PACKAGE_VALIDATE_UNKNOWN_FIELDS = 1 << 3,
  MODEL_PACKAGE_VALIDATE_ALL = ~0,
} ModelPackageValidateFlags;
 
/// Run structural and reachability checks. `*out_report_json` is set to a
/// JSON string owned by the package describing findings:
///   `{"errors": [{"code": "...", "message": "..."}, ...],
///     "warnings": [...]}`
/// Returns non-NULL status when any error-level finding fired; warnings alone
/// still return success.
MODEL_PACKAGE_API ModelPackageStatus* ModelPackage_Validate(ModelPackage*,
                                                            int flags,
                                                            const char** out_report_json);
 
#ifdef __cplusplus
}  // extern "C"
#endif