database.proto 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. // Smartbotic Database Service
  2. // Document-oriented storage with persistence and replication
  3. syntax = "proto3";
  4. package smartbotic.databasepb;
  5. // Error code range: 6000-6999 for Database errors
  6. // 6000: Generic database error
  7. // 6001: Collection not found
  8. // 6002: Document not found
  9. // 6003: Document already exists
  10. // 6004: Version conflict
  11. // 6005: Invalid query
  12. // 6006: File not found
  13. // 6007: File too large
  14. // 6008: Encryption error
  15. // 6009: Replication error
  16. // 6010: Version not found
  17. // ===== Main Database Service =====
  18. // v2.12.0 - REFUSAL DETAIL. Any refused write may carry trailing metadata
  19. // `sbdb-error-detail-bin`: UTF-8 JSON (gRPC base64s a -bin value on the wire)
  20. // that says WHY in a form a program can branch on, whether the refusal came as
  21. // an error status or in-band (status OK, success=false). Keys, all optional:
  22. //
  23. // reason "unique_violation" | "missing_parent" | "delete_restricted"
  24. // | "read_only" | "memory_pressure" | "view_write"
  25. // | "not_found" | "version_conflict" | "invalid_increment"
  26. // | "duplicate_id" (2.12.1) | "file_too_large"
  27. // | "mime_type_not_allowed" (2.12.3)
  28. // code the gRPC status code number - present for in-band
  29. // refusals, whose status is OK
  30. // fields the field(s) concerned: the constrained field(s) of a
  31. // unique violation, the reference field of a missing
  32. // parent, the field an increment could not be applied to
  33. // values object, field -> the value that collided / was refused
  34. // conflicting_id unique violation: the document already holding the value
  35. // relation missing parent: the relation that refused the write
  36. // current_version version conflict: the document's current version
  37. //
  38. // Absent on a server older than 2.12.0. The reason exists because a status
  39. // code alone is ambiguous: FAILED_PRECONDITION means a missing parent, a
  40. // restricted delete AND read-only mode.
  41. service DatabaseService {
  42. // Document operations
  43. rpc Insert(InsertRequest) returns (InsertResponse);
  44. rpc Get(GetRequest) returns (GetResponse);
  45. rpc Update(UpdateRequest) returns (UpdateResponse);
  46. rpc Upsert(UpsertRequest) returns (UpsertResponse);
  47. rpc Delete(DeleteRequest) returns (DeleteResponse);
  48. rpc PatchDocument(PatchDocumentRequest) returns (PatchDocumentResponse);
  49. rpc Exists(ExistsRequest) returns (ExistsResponse);
  50. // Version history operations
  51. rpc GetVersionHistory(GetVersionHistoryRequest) returns (GetVersionHistoryResponse);
  52. rpc GetDocumentVersion(GetDocumentVersionRequest) returns (GetDocumentVersionResponse);
  53. rpc RestoreVersion(RestoreVersionRequest) returns (RestoreVersionResponse);
  54. rpc RestoreToDate(RestoreToDateRequest) returns (RestoreToDateResponse);
  55. // Batch operations
  56. rpc BatchInsert(BatchInsertRequest) returns (BatchInsertResponse);
  57. rpc BatchGet(BatchGetRequest) returns (BatchGetResponse);
  58. rpc BatchDelete(BatchDeleteRequest) returns (BatchDeleteResponse);
  59. // Query operations
  60. rpc Find(FindRequest) returns (FindResponse);
  61. rpc Count(CountRequest) returns (CountResponse);
  62. rpc SimilaritySearch(SimilaritySearchRequest) returns (SimilaritySearchResponse);
  63. // v2.13.0 - stream one collection out, documents plus (optionally) their
  64. // stored vectors. See ExportRequest for the contract.
  65. rpc ExportCollection(ExportRequest) returns (stream ExportChunk);
  66. // Set operations (Redis compatibility)
  67. rpc SetAdd(SetAddRequest) returns (SetAddResponse);
  68. rpc SetRemove(SetRemoveRequest) returns (SetRemoveResponse);
  69. rpc SetMembers(SetMembersRequest) returns (SetMembersResponse);
  70. rpc SetIsMember(SetIsMemberRequest) returns (SetIsMemberResponse);
  71. // Collection management
  72. rpc CreateCollection(CreateCollectionRequest) returns (CreateCollectionResponse);
  73. rpc DropCollection(DropCollectionRequest) returns (DropCollectionResponse);
  74. rpc ListCollections(ListCollectionsRequest) returns (ListCollectionsResponse);
  75. rpc GetCollectionInfo(GetCollectionInfoRequest) returns (GetCollectionInfoResponse);
  76. // v2.3 — Project management. Projects are top-level namespaces over
  77. // collections. The "default" project is implicit and always exists.
  78. rpc ListProjects(ListProjectsRequest) returns (ListProjectsResponse);
  79. rpc CreateProject(CreateProjectRequest) returns (CreateProjectResponse);
  80. rpc DropProject(DropProjectRequest) returns (DropProjectResponse);
  81. // View management
  82. rpc CreateView(CreateViewRequest) returns (CreateViewResponse);
  83. rpc DropView(DropViewRequest) returns (DropViewResponse);
  84. rpc ListViews(ListViewsRequest) returns (ListViewsResponse);
  85. rpc GetViewInfo(GetViewInfoRequest) returns (GetViewInfoResponse);
  86. // v2.11.0 T6b — relation (referential integrity) management. Admin-only:
  87. // `_relations` is a system collection and a relation names another
  88. // collection's schema, so declaring one is not ordinary per-collection
  89. // write access.
  90. rpc CreateRelation(CreateRelationRequest) returns (CreateRelationResponse);
  91. rpc DropRelation(DropRelationRequest) returns (DropRelationResponse);
  92. rpc ListRelations(ListRelationsRequest) returns (ListRelationsResponse);
  93. rpc GetRelationInfo(GetRelationInfoRequest) returns (GetRelationInfoResponse);
  94. // v2.11.0 T5 — "what would happen if I deleted this?" without deleting
  95. // it. Read-only and gated as an ordinary per-collection read, not admin.
  96. rpc DescribeDelete(DescribeDeleteRequest) returns (DescribeDeleteResponse);
  97. // v2.11.0 T7 — "does this relation's reverse index actually match live
  98. // data?" A relation declared over a collection that already had rows
  99. // before v2.11.0 T7 shipped (or one whose sub-db was lost/restored from
  100. // an older snapshot) can carry postings for parent ids that no longer
  101. // exist. This walks the reverse index and probes each parent id;
  102. // mutates nothing. ⚠ Cost is proportional to the number of DISTINCT
  103. // parents referenced, not collection size, but on a very large child
  104. // collection that is still a full index walk - treat this as an
  105. // operator/migration tool, not something to call on a hot path.
  106. // Admin-only, like the other relation-management RPCs above (NOT gated
  107. // as a per-collection read on `child`, despite being read-only and
  108. // reporting facts about that collection's data): answering requires
  109. // resolving the relation first to learn its `child`, and gating after
  110. // that lookup would make a nonexistent-relation response distinguishable
  111. // from an existing-but-unauthorized one - a probe-able existence oracle,
  112. // which is exactly what gate() exists to prevent elsewhere.
  113. rpc CheckRelation(CheckRelationRequest) returns (CheckRelationResponse);
  114. // Collection configuration
  115. rpc ConfigureCollection(ConfigureCollectionRequest) returns (ConfigureCollectionResponse);
  116. // v2.9.0 — secondary indexes. Declaration is explicit per collection: an
  117. // index costs write throughput, and indexing every field would build useless
  118. // ones (a low-cardinality field like a status enum is slower through an
  119. // index than a scan).
  120. rpc CreateIndex(CreateIndexRequest) returns (CreateIndexResponse);
  121. rpc DropIndex(DropIndexRequest) returns (DropIndexResponse);
  122. rpc ListIndexes(ListIndexesRequest) returns (ListIndexesResponse);
  123. // v2.10.0 — the distinct values an indexed field holds, with row counts.
  124. // Answered from the index, one step per distinct value rather than per row.
  125. rpc GetIndexValues(GetIndexValuesRequest) returns (GetIndexValuesResponse);
  126. rpc GetCollectionConfig(GetCollectionConfigRequest) returns (GetCollectionConfigResponse);
  127. rpc MigrateCollectionTimestamps(MigrateCollectionTimestampsRequest) returns (MigrateCollectionTimestampsResponse);
  128. // File operations
  129. rpc UploadFile(stream FileChunk) returns (UploadFileResponse);
  130. rpc DownloadFile(DownloadFileRequest) returns (stream FileChunk);
  131. rpc DeleteFile(DeleteFileRequest) returns (DeleteFileResponse);
  132. rpc GetFileInfo(GetFileInfoRequest) returns (FileInfo);
  133. rpc ListFiles(ListFilesRequest) returns (ListFilesResponse);
  134. rpc SetFileTtl(SetFileTtlRequest) returns (SetFileTtlResponse);
  135. // Event subscription
  136. rpc Subscribe(SubscribeRequest) returns (stream DatabaseEvent);
  137. // Health and stats
  138. rpc HealthCheck(HealthCheckRequest) returns (HealthCheckResponse);
  139. // v2.15.0 - what this server is and which features it has, so a client can
  140. // refuse a call the server would misunderstand instead of sending it. A
  141. // pre-2.15.0 server answers UNIMPLEMENTED.
  142. rpc GetServerInfo(GetServerInfoRequest) returns (GetServerInfoResponse);
  143. rpc GetStats(GetStatsRequest) returns (GetStatsResponse);
  144. rpc GetMemoryStats(GetMemoryStatsRequest) returns (GetMemoryStatsResponse);
  145. // Read-only runtime state control
  146. rpc SetReadOnly(SetReadOnlyRequest) returns (SetReadOnlyResponse);
  147. rpc GetReadOnlyStatus(GetReadOnlyStatusRequest) returns (GetReadOnlyStatusResponse);
  148. }
  149. // ===== Replication Service =====
  150. service DatabaseReplication {
  151. // Bidirectional streaming for real-time sync
  152. rpc SyncStream(stream ReplicationMessage) returns (stream ReplicationMessage);
  153. // Pull missed entries (for recovery)
  154. rpc GetEntriesSince(GetEntriesRequest) returns (stream ReplicationEntry);
  155. // Get current node state
  156. rpc GetNodeState(GetNodeStateRequest) returns (NodeState);
  157. }
  158. // ===== Document Types =====
  159. message Document {
  160. string id = 1;
  161. string collection = 2;
  162. bytes data = 3; // JSON-encoded document data
  163. uint64 version = 4;
  164. uint64 created_at = 5; // Timestamp in milliseconds
  165. uint64 updated_at = 6;
  166. uint64 expires_at = 7; // 0 = no expiration
  167. string node_id = 8; // Origin node for replication
  168. bool encrypted = 9;
  169. repeated string encrypted_fields = 10;
  170. string created_by = 11; // User ID who created the document
  171. string updated_by = 12; // User ID who last updated the document
  172. // v2.13.0 - the stored vector as raw float32 little-endian bytes. Set ONLY
  173. // when the request asked for vectors (GetRequest/FindRequest
  174. // include_vector, ExportRequest include_vectors) and a vector is stored.
  175. bytes vector = 13;
  176. }
  177. // ===== Document Operations =====
  178. message InsertRequest {
  179. string collection = 1;
  180. bytes data = 2; // JSON document
  181. string id = 3; // Optional, auto-generated if empty
  182. uint32 ttl_seconds = 4; // 0 = use collection default
  183. string actor = 5; // User ID performing the operation (for audit)
  184. }
  185. message InsertResponse {
  186. string id = 1;
  187. uint64 version = 2;
  188. }
  189. message GetRequest {
  190. string collection = 1;
  191. string id = 2;
  192. bool include_vector = 3; // v2.13.0 - fill Document.vector
  193. }
  194. message GetResponse {
  195. Document document = 1;
  196. bool found = 2;
  197. }
  198. message UpdateRequest {
  199. string collection = 1;
  200. string id = 2;
  201. bytes data = 3;
  202. uint64 expected_version = 4; // 0 = no version check (force update)
  203. string actor = 5; // User ID performing the operation (for audit)
  204. // v2.8.0 — change the document's expiry as part of the update.
  205. //
  206. // `optional` gives three distinct meanings, which a plain uint32 cannot:
  207. // absent -> leave the existing expiry alone (the default, and what an
  208. // update did implicitly once it stopped WIPING the expiry)
  209. // 0 -> clear the expiry: the document becomes permanent
  210. // N -> expire N seconds from now
  211. //
  212. // Before this, ttl_seconds existed only on Insert/Upsert/BatchInsert, so
  213. // changing a TTL meant rewriting the whole document via upsert.
  214. optional uint32 ttl_seconds = 6;
  215. }
  216. message UpdateResponse {
  217. uint64 new_version = 1;
  218. bool success = 2;
  219. string error = 3;
  220. // v2.12.0 - when success=false, the gRPC status code this refusal WOULD
  221. // carry: NOT_FOUND, ABORTED (version conflict), FAILED_PRECONDITION
  222. // (read-only), RESOURCE_EXHAUSTED (memory pressure), INVALID_ARGUMENT
  223. // (write to a view). These refusals stay in-band (status OK) so clients
  224. // older than 2.12.0 see exactly what they always saw; 0 from an older
  225. // server. The trailing `sbdb-error-detail-bin` adds the reason.
  226. int32 error_code = 4;
  227. }
  228. message UpsertRequest {
  229. string collection = 1;
  230. bytes data = 2;
  231. string id = 3;
  232. uint32 ttl_seconds = 4;
  233. string actor = 5; // User ID performing the operation (for audit)
  234. }
  235. message UpsertResponse {
  236. string id = 1;
  237. uint64 version = 2;
  238. bool inserted = 3; // true if inserted, false if updated
  239. }
  240. message DeleteRequest {
  241. string collection = 1;
  242. string id = 2;
  243. // v2.12.0 - who is deleting, as `actor` is on every other write. Reported
  244. // on the resulting DatabaseEvent (and on the events of any documents a
  245. // relation cascade deletes or nulls because of it). Not stored: the
  246. // document is gone, and its last version in history keeps the updated_by
  247. // of whoever last WROTE it, which is not who deleted it.
  248. string actor = 3;
  249. }
  250. message DeleteResponse {
  251. bool deleted = 1;
  252. }
  253. message PatchDocumentRequest {
  254. string collection = 1;
  255. string id = 2;
  256. bytes patch_json = 3; // JSON fields to merge into existing document
  257. string actor = 4; // User ID performing the operation (for audit)
  258. // v2.8.0 — same three-way semantics as UpdateRequest.ttl_seconds: absent
  259. // leaves the expiry alone, 0 clears it, N sets N seconds from now.
  260. optional uint32 ttl_seconds = 5;
  261. // v2.12.0 - apply only if the document is still at this version: a
  262. // conditional PARTIAL update. updateIfVersion replaces the whole body, so a
  263. // caller that only wants to change one field ("claim this task") would
  264. // have to write back everything it read, reverting any field a concurrent
  265. // writer changed meanwhile. A mismatch is refused with status ABORTED,
  266. // reason "version_conflict" and the current version in the detail.
  267. // Absent = unconditional, as before.
  268. optional uint64 expected_version = 6;
  269. // v2.12.0 - ATOMIC INCREMENT. A JSON object of field -> number to add, in
  270. // the same locked read-modify-write as the merge, so concurrent increments
  271. // never lose an update. Dotted paths descend objects (creating them).
  272. // - an absent or null field counts as 0;
  273. // - integer + integer stays an integer (overflow is refused), anything
  274. // else is a double;
  275. // - a non-numeric stored value, a non-numeric delta, or a field that
  276. // patch_json also sets is refused with INVALID_ARGUMENT, reason
  277. // "invalid_increment", and the field in the detail.
  278. // Nothing is written when any increment is refused. patch_json may be
  279. // empty when only incrementing. The new values come back in
  280. // PatchDocumentResponse.incremented_json.
  281. bytes increments_json = 7;
  282. }
  283. message PatchDocumentResponse {
  284. uint64 new_version = 1;
  285. bool success = 2;
  286. string error = 3;
  287. // v2.12.0 - as UpdateResponse.error_code.
  288. int32 error_code = 4;
  289. // v2.12.0 - JSON object, field -> value AFTER the increments, for every
  290. // field in increments_json. Empty when there were none.
  291. bytes incremented_json = 5;
  292. }
  293. message ExistsRequest {
  294. string collection = 1;
  295. string id = 2;
  296. }
  297. message ExistsResponse {
  298. bool exists = 1;
  299. }
  300. // ===== Version History Operations =====
  301. message DocumentVersionEntry {
  302. uint64 version = 1;
  303. bytes data = 2; // JSON-encoded document data at this version
  304. uint64 timestamp = 3; // When this version was created (updatedAt)
  305. string updated_by = 4;
  306. bool encrypted = 5;
  307. repeated string encrypted_fields = 6;
  308. }
  309. message GetVersionHistoryRequest {
  310. string collection = 1;
  311. string id = 2;
  312. uint32 limit = 3; // 0 = all versions
  313. uint32 offset = 4;
  314. }
  315. message GetVersionHistoryResponse {
  316. repeated DocumentVersionEntry versions = 1;
  317. uint64 current_version = 2; // 0 if document is deleted
  318. uint64 total_count = 3;
  319. bool document_deleted = 4;
  320. }
  321. message GetDocumentVersionRequest {
  322. string collection = 1;
  323. string id = 2;
  324. uint64 version = 3;
  325. }
  326. message GetDocumentVersionResponse {
  327. DocumentVersionEntry version_entry = 1;
  328. bool found = 2;
  329. }
  330. message RestoreVersionRequest {
  331. string collection = 1;
  332. string id = 2;
  333. uint64 version = 3; // Version number to restore
  334. string actor = 4; // User performing the restore
  335. }
  336. message RestoreVersionResponse {
  337. uint64 new_version = 1; // The newly created version number
  338. bool success = 2;
  339. string error = 3;
  340. }
  341. message RestoreToDateRequest {
  342. string collection = 1;
  343. string id = 2;
  344. uint64 timestamp = 3; // Find version active at this time (ms since epoch)
  345. string actor = 4;
  346. }
  347. message RestoreToDateResponse {
  348. uint64 restored_version = 1; // Which old version was restored from
  349. uint64 new_version = 2; // The newly created version number
  350. bool success = 3;
  351. string error = 4;
  352. }
  353. // ===== Batch Operations =====
  354. message BatchInsertRequest {
  355. string collection = 1;
  356. repeated BatchInsertItem items = 2;
  357. string actor = 3; // User ID performing the operation (for audit)
  358. }
  359. message BatchInsertItem {
  360. bytes data = 1;
  361. string id = 2;
  362. uint32 ttl_seconds = 3;
  363. }
  364. message BatchInsertResponse {
  365. repeated string ids = 1;
  366. uint32 success_count = 2;
  367. uint32 error_count = 3;
  368. }
  369. message BatchGetRequest {
  370. string collection = 1;
  371. repeated string ids = 2;
  372. }
  373. message BatchGetResponse {
  374. repeated Document documents = 1;
  375. }
  376. message BatchDeleteRequest {
  377. string collection = 1;
  378. repeated string ids = 2;
  379. string actor = 3; // v2.12.0 - as DeleteRequest.actor, for every id
  380. }
  381. message BatchDeleteResponse {
  382. uint64 deleted_count = 1;
  383. // v2.12.0 - the ids actually deleted, in request order. An id that is
  384. // absent here did not exist. If the call fails partway (a relation
  385. // refuses one id), the status is an error and no response is delivered:
  386. // ids before it may already be gone - re-read to learn which.
  387. repeated string deleted_ids = 2;
  388. }
  389. // ===== Query Operations =====
  390. message FindRequest {
  391. string collection = 1;
  392. repeated Filter filters = 2;
  393. Sort sort = 3;
  394. uint32 limit = 4; // Default 100
  395. uint32 offset = 5;
  396. repeated string projection = 6; // Fields to include (empty = all)
  397. bool include_vector = 7; // v2.13.0 - fill Document.vector on each result
  398. }
  399. message Filter {
  400. string field = 1;
  401. FilterOp op = 2;
  402. bytes value = 3; // JSON-encoded value
  403. }
  404. enum FilterOp {
  405. FILTER_OP_UNSPECIFIED = 0;
  406. FILTER_OP_EQ = 1;
  407. FILTER_OP_NE = 2;
  408. FILTER_OP_GT = 3;
  409. FILTER_OP_GTE = 4;
  410. FILTER_OP_LT = 5;
  411. FILTER_OP_LTE = 6;
  412. FILTER_OP_IN = 7;
  413. FILTER_OP_CONTAINS = 8;
  414. FILTER_OP_EXISTS = 9;
  415. FILTER_OP_REGEX = 10;
  416. FILTER_OP_SEARCH = 11; // Full-text search across ID and string fields
  417. }
  418. message Sort {
  419. string field = 1;
  420. bool descending = 2;
  421. }
  422. message FindResponse {
  423. repeated Document documents = 1;
  424. uint64 total_count = 2;
  425. bool has_more = 3;
  426. // WAL fallback metrics (for queries that include evicted documents)
  427. bool used_wal_fallback = 4; // True if WAL was scanned for evicted docs
  428. uint32 memory_match_count = 5; // Documents matched from memory
  429. uint32 wal_match_count = 6; // Documents matched from WAL
  430. uint64 memory_search_micros = 7; // Time spent searching memory (µs)
  431. uint64 wal_search_micros = 8; // Time spent loading/searching WAL (µs)
  432. }
  433. message CountRequest {
  434. string collection = 1;
  435. repeated Filter filters = 2;
  436. }
  437. message CountResponse {
  438. uint64 count = 1;
  439. }
  440. // ===== Export (v2.13.0) =====
  441. //
  442. // One collection, never a project: the unit is a collection named exactly as
  443. // every other call names one, so a caller whose tenancy is a collection-name
  444. // prefix cannot be handed another tenant's rows. Gated as an ordinary READ of
  445. // that collection (column masks and row predicates apply), not admin.
  446. //
  447. // The stream is: one header, then row batches, then one trailer.
  448. // - One stream is one LMDB read transaction: an exact snapshot, rows in id
  449. // order, none skipped or repeated. header.rows_at_snapshot counts the rows
  450. // the stream will send if it completes, so rows_sent == rows_at_snapshot
  451. // checks it.
  452. // - Resuming with after_id = trailer.last_id starts a NEW snapshot strictly
  453. // after that id: it never repeats a row, but can miss one inserted during
  454. // the gap with an id that sorts before the cursor.
  455. // - A stream ends early (complete=false, last_id set) when it reaches the
  456. // server's time cap (header.max_stream_seconds) - that is the resume case.
  457. // - A row larger than the server's send limit ends the stream with
  458. // RESOURCE_EXHAUSTED, reason row_too_large, conflicting_id = that row; the
  459. // rows before it were sent. It is never skipped silently.
  460. // - A stream that ends without a trailer did not complete. Treat it as
  461. // failed; a client must never read a stream without a trailer as whole.
  462. // - A restored row gets FRESH metadata: insert stamps _created_at,
  463. // _updated_at, _version and _created_by itself. The originals are in each
  464. // exported Document; keep them there, or copy them into your own field
  465. // before importing if you need them as data.
  466. message ExportRequest {
  467. string collection = 1;
  468. // Include each row's vector (Document.vector) and its vector_state.
  469. // Refused with INVALID_ARGUMENT (reason no_vector_index) on a collection
  470. // with vector_dimension 0, which cannot hold vectors.
  471. bool include_vectors = 2;
  472. string after_id = 3; // resume cursor; empty = from the start
  473. uint32 batch_rows = 4; // rows per ExportRows; 0 = default; clamped, never refused
  474. }
  475. enum VectorState {
  476. VECTOR_STATE_UNSPECIFIED = 0; // include_vectors was false
  477. VECTOR_STATE_PRESENT = 1; // vector stored; Document.vector holds it
  478. VECTOR_STATE_NONE = 2; // written without a vector
  479. VECTOR_STATE_MISSING = 3; // written WITH a vector, and its entry is gone
  480. VECTOR_STATE_UNKNOWN = 4; // no entry, and the row predates the marker (pre-2.13.0)
  481. }
  482. message ExportHeader {
  483. string collection = 1;
  484. uint32 vector_dimension = 2;
  485. string vector_dtype = 3; // "float32-le"
  486. uint64 rows_at_snapshot = 4;
  487. uint32 batch_rows_cap = 5; // the most rows a batch holds (requested, clamped); a batch also closes at 8 MB, so it may hold fewer
  488. uint32 max_stream_seconds = 6;
  489. }
  490. message ExportRow {
  491. Document document = 1; // same shape as a Get result
  492. VectorState vector_state = 2;
  493. }
  494. message ExportRows {
  495. repeated ExportRow rows = 1;
  496. }
  497. message ExportTrailer {
  498. uint64 rows_sent = 1;
  499. string last_id = 2; // id of the last row sent; the resume cursor
  500. bool complete = 3; // true only when the end of the collection was reached
  501. }
  502. message ExportChunk {
  503. oneof chunk {
  504. ExportHeader header = 1;
  505. ExportRows rows = 2;
  506. ExportTrailer trailer = 3;
  507. }
  508. }
  509. message SimilaritySearchRequest {
  510. string collection = 1;
  511. repeated float query_vector = 2;
  512. uint32 top_k = 3;
  513. float min_score = 4;
  514. // v2.14.0 - only rows matching EVERY filter are candidates (same grammar
  515. // and semantics as FindRequest.filters). The answer is the exact top-K
  516. // among matching rows; min_score then trims. A pre-2.14.0 server ignores
  517. // this field and answers UNFILTERED, which is why the response says
  518. // whether the filters were applied.
  519. repeated Filter filters = 5;
  520. }
  521. message SimilaritySearchResponse {
  522. repeated SimilarityResult results = 1;
  523. // v2.14.0 - true when the server applied `filters`. A pre-2.14.0 server
  524. // never sets it, so a client that sent filters and reads false must NOT
  525. // use the results: they are unfiltered.
  526. bool filters_applied = 2;
  527. }
  528. message SimilarityResult {
  529. string id = 1;
  530. float score = 2;
  531. bytes data = 3;
  532. }
  533. // ===== Set Operations =====
  534. message SetAddRequest {
  535. string collection = 1;
  536. string set_id = 2;
  537. string member = 3;
  538. }
  539. message SetAddResponse {
  540. bool added = 1; // false if already exists
  541. }
  542. message SetRemoveRequest {
  543. string collection = 1;
  544. string set_id = 2;
  545. string member = 3;
  546. }
  547. message SetRemoveResponse {
  548. bool removed = 1;
  549. }
  550. message SetMembersRequest {
  551. string collection = 1;
  552. string set_id = 2;
  553. }
  554. message SetMembersResponse {
  555. repeated string members = 1;
  556. }
  557. message SetIsMemberRequest {
  558. string collection = 1;
  559. string set_id = 2;
  560. string member = 3;
  561. }
  562. message SetIsMemberResponse {
  563. bool is_member = 1;
  564. }
  565. // ===== Collection Management =====
  566. message CreateCollectionRequest {
  567. string name = 1;
  568. CollectionOptions options = 2;
  569. }
  570. message CollectionOptions {
  571. bool auto_create_id = 1;
  572. uint32 default_ttl_seconds = 2;
  573. bool encrypted = 3;
  574. repeated string sensitive_fields = 4;
  575. uint32 max_versions = 5; // Max version history per document (0 = unlimited)
  576. uint32 vector_dimension = 6; // Dimension of vectors stored in this collection (0 = not a vector collection)
  577. // NEW in v1.7.0 — eviction priority for the collection.
  578. // UNSPECIFIED is treated as NORMAL for backward compatibility.
  579. MemoryPriority memory_priority = 7;
  580. }
  581. enum MemoryPriority {
  582. MEMORY_PRIORITY_UNSPECIFIED = 0; // treated as NORMAL
  583. MEMORY_PRIORITY_LOW = 1;
  584. MEMORY_PRIORITY_NORMAL = 2;
  585. MEMORY_PRIORITY_HIGH = 3;
  586. }
  587. message CreateCollectionResponse {
  588. bool created = 1;
  589. }
  590. message DropCollectionRequest {
  591. string name = 1;
  592. }
  593. message DropCollectionResponse {
  594. bool dropped = 1;
  595. }
  596. message ListCollectionsRequest {
  597. // v2.8.0 — restrict the listing to one project namespace, mirroring
  598. // ListViewsRequest. Empty lists every project (operator/CLI use); clients
  599. // always set it, so a workspace never sees another workspace's collection
  600. // names. A collection name is information about the shape of someone else's
  601. // data even when its contents are unreachable.
  602. string project = 1;
  603. }
  604. message ListCollectionsResponse {
  605. repeated string names = 1;
  606. }
  607. message GetCollectionInfoRequest {
  608. string name = 1;
  609. }
  610. message GetCollectionInfoResponse {
  611. CollectionInfo info = 1;
  612. bool found = 2;
  613. }
  614. // What a collection IS: its creation-time options and its size.
  615. //
  616. // Settings changeable after creation are NOT here - timestamp_precision,
  617. // versioning_enabled and relations_enforced are read with GetCollectionConfig
  618. // (see CollectionConfig), which always populates all three on a reply. Their
  619. // absence from this message does not mean they cannot be read.
  620. message CollectionInfo {
  621. string name = 1;
  622. uint64 document_count = 2;
  623. uint64 size_bytes = 3;
  624. CollectionOptions options = 4;
  625. uint64 created_at = 5;
  626. uint64 updated_at = 6;
  627. uint32 vector_dimension = 7; // Dimension of vectors stored in this collection (0 = not a vector collection)
  628. }
  629. // ===== Project Management (v2.3) =====
  630. //
  631. // Projects are top-level namespaces over collections. Collections live
  632. // inside a project; wire-form "my_app:users" addresses collection "users"
  633. // in project "my_app", bare "users" addresses ("default", "users"). The
  634. // "default" project is implicit and always present.
  635. //
  636. // Stage F ships the RPC surface backed by a filesystem-only placeholder
  637. // (`<dataDir>/projects/<name>/env/`). Stage B replaces the placeholder
  638. // with the real ProjectStore-backed implementation.
  639. message ListProjectsRequest {}
  640. message ListProjectsResponse {
  641. // Alphabetical. Always includes "default".
  642. repeated string projects = 1;
  643. }
  644. message CreateProjectRequest {
  645. string name = 1;
  646. }
  647. message CreateProjectResponse {
  648. // true = newly created, false = already existed (idempotent) OR validation error.
  649. bool created = 1;
  650. // Populated on validation failure (e.g. invalid name). Empty on success
  651. // and on the idempotent "already existed" path.
  652. string error = 2;
  653. }
  654. message DropProjectRequest {
  655. string name = 1;
  656. }
  657. message DropProjectResponse {
  658. bool dropped = 1;
  659. // Populated when the drop is refused — e.g. name == "default",
  660. // unknown project, or filesystem error.
  661. string error = 2;
  662. }
  663. // ===== File Operations =====
  664. message FileChunk {
  665. oneof content {
  666. FileMetadata metadata = 1; // First chunk includes metadata
  667. bytes data = 2; // Subsequent chunks are data
  668. }
  669. }
  670. message FileMetadata {
  671. string id = 1; // Set by server on upload response
  672. string name = 2;
  673. string mime_type = 3;
  674. uint64 size = 4;
  675. string file_type = 5; // "plugin", "document", "generated"
  676. string related_id = 6; // plugin_id, conversation_id, etc.
  677. map<string, string> metadata = 7; // Custom metadata
  678. bool is_public = 8; // Visibility flag
  679. // v2.6.0 — owning project. Empty means "default", matching the rule for
  680. // bare collection names. Files are namespaced so that per-project access
  681. // policy has something to attach to.
  682. string project = 9;
  683. // v2.8.0 — relative time-to-live in seconds, applied at upload. Converted
  684. // once to an absolute expiry server-side, so a file's lifetime does not
  685. // restart when it is read or on restart.
  686. //
  687. // `optional` is load-bearing: ABSENT means "inherit the configured default
  688. // for this file type", while an explicitly SET 0 means "never expire" and
  689. // overrides that default. A plain uint32 cannot express the difference,
  690. // which would make it impossible to opt one file out of a retention policy.
  691. optional uint32 ttl_seconds = 10;
  692. }
  693. message UploadFileResponse {
  694. string id = 1;
  695. uint64 size = 2;
  696. string checksum = 3; // SHA-256
  697. bool deduplicated = 4; // True if blob already existed
  698. }
  699. message DownloadFileRequest {
  700. string id = 1;
  701. string project = 2; // v2.6.0 — empty means "default"
  702. }
  703. message DeleteFileRequest {
  704. string id = 1;
  705. string project = 2; // v2.6.0 — empty means "default"
  706. }
  707. message DeleteFileResponse {
  708. bool deleted = 1;
  709. }
  710. message GetFileInfoRequest {
  711. string id = 1;
  712. string project = 2; // v2.6.0 — empty means "default"
  713. }
  714. message FileInfo {
  715. string id = 1;
  716. string name = 2;
  717. string mime_type = 3;
  718. uint64 size = 4;
  719. string file_type = 5;
  720. string related_id = 6;
  721. string checksum = 7;
  722. uint64 created_at = 8;
  723. map<string, string> metadata = 9;
  724. bool is_public = 10; // Visibility flag
  725. uint32 ref_count = 11; // Blob reference count (informational)
  726. string project = 12; // v2.6.0 — owning project
  727. uint64 expires_at = 13; // v2.8.0 — absolute expiry ms, 0 = never
  728. }
  729. message ListFilesRequest {
  730. string file_type = 1; // Filter by type (optional)
  731. string related_id = 2; // Filter by related ID (optional)
  732. uint32 limit = 3;
  733. uint32 offset = 4;
  734. string checksum = 5; // Filter by checksum (for dedup queries)
  735. string name = 6; // Filter by exact filename (optional)
  736. string project = 7; // v2.6.0 — empty means "default"
  737. }
  738. // v2.8.0 — change the expiry of a file that is already stored. A file's TTL was
  739. // otherwise fixed at upload.
  740. message SetFileTtlRequest {
  741. string id = 1;
  742. string project = 2; // empty means "default"
  743. // Absent is invalid here - the call exists to change the expiry, so it must
  744. // say what to. 0 clears the expiry (keep indefinitely); N expires the file N
  745. // seconds from now.
  746. uint32 ttl_seconds = 3;
  747. }
  748. message SetFileTtlResponse {
  749. bool updated = 1; // false if no such file in that project
  750. uint64 expires_at = 2; // the resulting absolute expiry, 0 = never
  751. }
  752. message ListFilesResponse {
  753. repeated FileInfo files = 1;
  754. uint64 total_count = 2;
  755. bool has_more = 3;
  756. }
  757. // ===== Event Subscription =====
  758. message SubscribeRequest {
  759. repeated string collections = 1; // Empty = all collections
  760. repeated string patterns = 2; // Glob patterns (e.g., "assistants:*")
  761. bool include_data = 3; // Include document data in events
  762. }
  763. message DatabaseEvent {
  764. EventType type = 1;
  765. string collection = 2;
  766. string document_id = 3;
  767. uint64 timestamp = 4;
  768. string node_id = 5;
  769. bytes data = 6; // JSON document (if include_data)
  770. // v2.12.0 - who caused this change: the `actor` the write or delete
  771. // request carried. Empty when the request carried none, and for changes
  772. // the database makes itself (TTL expiry). Sent whether or not
  773. // include_data is set.
  774. //
  775. // For a document a relation cascade deleted or nulled, it is the actor of
  776. // the delete that triggered the cascade - the cascade is that caller's
  777. // doing, not whoever last wrote the child.
  778. //
  779. // This is what makes a write-triggered-write loop guard possible: a
  780. // subscriber that reacts to events by writing can tag its own writes and
  781. // recognise them here. Before this an event said nothing about its
  782. // origin; insert/update provenance had to be read back from version
  783. // history after the fact (racing any later write), and a delete's was not
  784. // recorded anywhere.
  785. string actor = 7;
  786. }
  787. enum EventType {
  788. EVENT_UNKNOWN = 0;
  789. EVENT_INSERT = 1;
  790. EVENT_UPDATE = 2;
  791. EVENT_DELETE = 3;
  792. EVENT_EXPIRE = 4;
  793. EVENT_INVALIDATE = 5;
  794. // NEW in v1.7.0 — memory-pressure observability events.
  795. EVENT_MEMORY_PRESSURE_HIGH = 6; // Emitted when pressure reaches hard threshold
  796. EVENT_MEMORY_EVICTION_BURST = 7; // Emitted when eviction runs a large burst
  797. }
  798. // ===== Replication =====
  799. message ReplicationEntry {
  800. uint64 sequence = 1;
  801. uint64 global_timestamp = 2;
  802. string node_id = 3;
  803. OperationType op = 4;
  804. string collection = 5;
  805. string document_id = 6;
  806. uint64 document_version = 7;
  807. bytes data = 8;
  808. }
  809. enum OperationType {
  810. OP_UNKNOWN = 0;
  811. OP_INSERT = 1;
  812. OP_UPDATE = 2;
  813. OP_DELETE = 3;
  814. OP_UPSERT = 4;
  815. OP_CREATE_COLLECTION = 5;
  816. OP_DROP_COLLECTION = 6;
  817. }
  818. message ReplicationMessage {
  819. oneof content {
  820. ReplicationEntry entry = 1;
  821. Heartbeat heartbeat = 2;
  822. WatermarkUpdate watermark = 3;
  823. SyncRequest sync_request = 4;
  824. }
  825. }
  826. message Heartbeat {
  827. string node_id = 1;
  828. uint64 timestamp = 2;
  829. uint64 sequence = 3;
  830. }
  831. message WatermarkUpdate {
  832. string node_id = 1;
  833. uint64 sequence = 2;
  834. }
  835. message SyncRequest {
  836. uint64 from_sequence = 1;
  837. }
  838. message GetEntriesRequest {
  839. uint64 from_sequence = 1;
  840. uint32 limit = 2;
  841. }
  842. message GetNodeStateRequest {
  843. }
  844. message NodeState {
  845. string node_id = 1;
  846. uint64 current_sequence = 2;
  847. map<string, uint64> peer_watermarks = 3;
  848. uint64 document_count = 4;
  849. repeated string collections = 5;
  850. bool healthy = 6;
  851. // Per-collection sequence tracking for collection discovery
  852. map<string, uint64> collection_sequences = 7;
  853. }
  854. // ===== Health and Stats =====
  855. message HealthCheckRequest {
  856. }
  857. // v2.15.0 - capability negotiation.
  858. //
  859. // `capabilities` names features, not versions: a client gates a feature on its
  860. // NAME being present, so a patched or backported server reports exactly what it
  861. // can do. Names are stable forever and never reused; a feature that changes
  862. // meaning gets a new name. The list a server sends is in
  863. // service/src/server_capabilities.hpp.
  864. //
  865. // Clients send their own library version in the request metadata key
  866. // `sbdb-client-version` on every call, so a server can keep an old behaviour
  867. // for an old client if a meaning ever has to change. `client_version` echoes
  868. // what this call carried (empty if none).
  869. message GetServerInfoRequest {
  870. }
  871. message GetServerInfoResponse {
  872. string version = 1; // e.g. "2.15.0"
  873. string commit = 2; // short git commit, or "unknown"
  874. repeated string capabilities = 3;
  875. string client_version = 4;
  876. }
  877. message HealthCheckResponse {
  878. bool healthy = 1;
  879. uint64 uptime_seconds = 2;
  880. uint64 document_count = 3;
  881. uint64 memory_used_bytes = 4;
  882. uint64 wal_size_bytes = 5;
  883. repeated PeerHealth peers = 6;
  884. }
  885. message PeerHealth {
  886. string node_id = 1;
  887. bool connected = 2;
  888. uint64 latency_ms = 3;
  889. uint64 lag_entries = 4;
  890. }
  891. message GetStatsRequest {
  892. }
  893. message GetStatsResponse {
  894. uint64 total_documents = 1;
  895. uint64 total_collections = 2;
  896. uint64 memory_used_bytes = 3;
  897. uint64 wal_sequence = 4;
  898. uint64 wal_size_bytes = 5;
  899. uint64 snapshot_count = 6;
  900. uint64 last_snapshot_sequence = 7;
  901. uint64 insert_count = 8;
  902. uint64 update_count = 9;
  903. uint64 delete_count = 10;
  904. uint64 query_count = 11;
  905. // Memory eviction stats
  906. uint64 evicted_documents = 12; // Documents currently evicted to disk
  907. uint64 total_evictions = 13; // Total eviction operations performed
  908. uint64 recovery_count = 14; // Documents recovered from eviction
  909. // Memory configuration
  910. uint64 max_memory_bytes = 15; // Configured max memory limit
  911. uint32 eviction_threshold_percent = 16; // Start evicting at this % of max
  912. uint32 eviction_target_percent = 17; // Evict down to this % of max
  913. // Operation timing (microseconds) - for performance monitoring
  914. uint64 get_count = 18; // Number of get operations
  915. uint64 get_total_micros = 19; // Total time spent in get operations
  916. uint64 get_max_micros = 20; // Max single get operation time
  917. uint64 insert_total_micros = 21;
  918. uint64 insert_max_micros = 22;
  919. uint64 update_total_micros = 23;
  920. uint64 update_max_micros = 24;
  921. uint64 query_total_micros = 25;
  922. uint64 query_max_micros = 26;
  923. }
  924. // ===== View Operations =====
  925. message ViewDefinition {
  926. string name = 1; // view name (globally unique, cannot start with _)
  927. string collection = 2; // target real collection (must NOT be another view)
  928. repeated string include = 3; // field paths to include (dot-notation for nested)
  929. repeated string exclude = 4; // field paths to exclude (ignored if include is non-empty)
  930. repeated Filter where = 5; // baked-in filters (AND-merged with caller filters)
  931. Sort default_sort = 6; // baked-in default sort (used when caller doesn't specify)
  932. uint64 created_at = 7;
  933. uint64 updated_at = 8;
  934. }
  935. message CreateViewRequest {
  936. string name = 1;
  937. string collection = 2;
  938. repeated string include = 3;
  939. repeated string exclude = 4;
  940. repeated Filter where = 5;
  941. Sort default_sort = 6;
  942. }
  943. message CreateViewResponse {
  944. bool success = 1;
  945. string error = 2;
  946. }
  947. message DropViewRequest {
  948. string name = 1;
  949. }
  950. message DropViewResponse {
  951. bool success = 1;
  952. string error = 2;
  953. }
  954. message ListViewsRequest {
  955. // Restrict the listing to one project namespace. Empty lists every
  956. // project (operator/CLI use). Clients always set it so a workspace
  957. // never sees another workspace's views.
  958. string project = 1;
  959. }
  960. message ListViewsResponse {
  961. repeated ViewDefinition views = 1;
  962. }
  963. message GetViewInfoRequest {
  964. string name = 1;
  965. }
  966. message GetViewInfoResponse {
  967. ViewDefinition view = 1;
  968. bool found = 2;
  969. }
  970. // ===== Relation (Referential Integrity) Operations — v2.11.0 T6b =====
  971. //
  972. // A relation says: documents in `child` reference documents in `parent`
  973. // through `child_field` (a dot-path that may resolve to a single id or an
  974. // array of ids). Declarations are persisted in the global `_relations`
  975. // system collection (mirrors `_views`) and are project-qualified like any
  976. // other collection-carrying name. Management is admin-only.
  977. message RelationDefinition {
  978. string name = 1; // relation name, project-qualified
  979. string child = 2; // collection holding the reference, project-qualified
  980. string child_field = 3; // dot-path on the child; may resolve to an array of ids
  981. string parent = 4; // collection being referenced, project-qualified
  982. // "restrict" (default) | "cascade" | "set_null" | "no_action".
  983. //
  984. // ALL FOUR ARE ENFORCED as of v2.11.0, and an unrecognised value is
  985. // REFUSED by CreateRelation rather than coerced to "restrict".
  986. // restrict - the parent delete fails FAILED_PRECONDITION while any
  987. // child still references it.
  988. // cascade - DESTRUCTIVE: children referencing the parent are deleted
  989. // atomically with it. If child_field resolves to an ARRAY,
  990. // the parent id is pulled from the array and the child is
  991. // KEPT (cascade and set_null collapse there). Refused if a
  992. // child is itself protected by its own restrict relation.
  993. // Not recursive.
  994. // set_null - DESTRUCTIVE: the child's reference field is set to null
  995. // and the child is kept. Arrays behave as under cascade.
  996. // no_action - the delete proceeds, the reference is left dangling.
  997. string on_delete = 5;
  998. // ENFORCED as of v2.11.0: a write to the child naming a parent that does
  999. // not exist is rejected with FAILED_PRECONDITION. An empty-string
  1000. // reference is a hard rejection.
  1001. bool validate_on_write = 6;
  1002. uint64 created_at = 7;
  1003. uint64 updated_at = 8;
  1004. }
  1005. message CreateRelationRequest {
  1006. string name = 1;
  1007. string child = 2;
  1008. string child_field = 3;
  1009. string parent = 4;
  1010. string on_delete = 5; // empty defaults to "restrict"
  1011. bool validate_on_write = 6;
  1012. }
  1013. message CreateRelationResponse {
  1014. bool success = 1;
  1015. string error = 2;
  1016. // v2.11.0 T7 — rows indexed by the bootstrap scan. Declaring a relation
  1017. // over a collection that already has rows backfills the reverse index
  1018. // immediately (mirrors CreateIndexResponse.rows_indexed), so enforcement
  1019. // covers pre-existing children from the moment the relation is declared,
  1020. // not only writes made afterwards.
  1021. uint64 rows_indexed = 3;
  1022. }
  1023. message DropRelationRequest {
  1024. string name = 1;
  1025. }
  1026. message DropRelationResponse {
  1027. bool success = 1;
  1028. string error = 2;
  1029. }
  1030. message ListRelationsRequest {
  1031. // Restrict the listing to one project namespace. Empty lists every
  1032. // project (operator/CLI use). Clients always send their own project so
  1033. // one workspace never enumerates another's relations.
  1034. string project = 1;
  1035. }
  1036. message ListRelationsResponse {
  1037. repeated RelationDefinition relations = 1;
  1038. }
  1039. message GetRelationInfoRequest {
  1040. string name = 1;
  1041. }
  1042. message GetRelationInfoResponse {
  1043. RelationDefinition relation = 1;
  1044. bool found = 2;
  1045. }
  1046. // v2.11.0 T5 — "what would happen if I deleted this?", answered without
  1047. // deleting anything. A relational database exposes its constraints through
  1048. // DDL you can read; a document store has none, so this has to be a query.
  1049. // Gated as an ordinary READ on `collection` (it changes nothing), unlike
  1050. // the relation-management RPCs above which are admin-only.
  1051. message DescribeDeleteRequest {
  1052. string collection = 1;
  1053. string id = 2;
  1054. }
  1055. message RelationImpact {
  1056. string relation = 1;
  1057. string child_collection = 2;
  1058. string child_field = 3;
  1059. string on_delete = 4; // restrict | cascade | set_null | no_action
  1060. uint64 child_count = 5;
  1061. repeated string sample_child_ids = 6; // at most five
  1062. bool blocks = 7;
  1063. }
  1064. message DescribeDeleteResponse {
  1065. bool success = 1;
  1066. string error = 2;
  1067. bool would_be_blocked = 3;
  1068. repeated RelationImpact impacts = 4;
  1069. }
  1070. // v2.11.0 T7 — the bootstrap-scan gap. Declaring a relation with T3 alone
  1071. // only maintains the reverse index going FORWARD from writes made after
  1072. // declaration; rows already in the child collection were never indexed, so
  1073. // enforcement silently missed them. This RPC answers "is that still true
  1074. // right now, for this relation" without changing anything.
  1075. message CheckRelationRequest {
  1076. string name = 1; // relation name, project-qualified like the others
  1077. }
  1078. // One parent id referenced by the child collection's data, whose parent
  1079. // document does not exist.
  1080. message DanglingReference {
  1081. string parent_id = 1;
  1082. uint64 child_count = 2; // children of this parent, from the index
  1083. repeated string sample_child_ids = 3; // at most five
  1084. }
  1085. message CheckRelationResponse {
  1086. bool success = 1;
  1087. string error = 2;
  1088. // Every dangling parent id found, not just the reported sample - so a
  1089. // capped `dangling` list below does not silently understate the problem.
  1090. uint64 total_dangling = 3;
  1091. // Capped (see the server implementation) so a badly out-of-sync relation
  1092. // cannot blow up the response size; total_dangling is the true count.
  1093. repeated DanglingReference dangling = 4;
  1094. }
  1095. // ===== Collection Configuration =====
  1096. // Per-collection configuration for timestamp precision and other runtime knobs.
  1097. // Stored in the _collection_meta system collection.
  1098. message CollectionConfig {
  1099. // "ms" (default) — _created_at/_updated_at stamped in milliseconds since epoch
  1100. // "ns" — _created_at/_updated_at stamped in nanoseconds since epoch
  1101. // Empty on a ConfigureCollection request means "leave unchanged".
  1102. string timestamp_precision = 1;
  1103. // v2.4.5 — version history on/off for an EXISTING collection.
  1104. //
  1105. // `optional` (proto3 field presence) is load-bearing, not decoration: a
  1106. // plain bool defaults to false, so any ConfigureCollection call that meant
  1107. // to set only timestamp_precision would silently switch versioning OFF.
  1108. // With presence, an absent field means "leave unchanged" and the server
  1109. // overlays only what the caller actually set.
  1110. //
  1111. // Disabling stops NEW versions being recorded; history already on disk
  1112. // stays readable, so this is reversible. Distinct from max_versions=0,
  1113. // which means "unlimited", not "off".
  1114. optional bool versioning_enabled = 2;
  1115. // v2.11.0 T8 — whether declared relations are enforced for this collection.
  1116. //
  1117. // `optional` for the same reason versioning_enabled is: a plain bool
  1118. // defaults to false, so a ConfigureCollection call that meant to set
  1119. // only timestamp_precision would silently switch relation enforcement
  1120. // OFF. With presence, an absent field means "leave unchanged".
  1121. //
  1122. // Defaults to true (enforced) when never explicitly configured -
  1123. // declaring a relation names its child and parent explicitly, so the
  1124. // declaration IS the opt-in.
  1125. optional bool relations_enforced = 3;
  1126. // Room for future per-collection knobs
  1127. }
  1128. message ConfigureCollectionRequest {
  1129. string collection = 1;
  1130. CollectionConfig config = 2;
  1131. }
  1132. message ConfigureCollectionResponse {
  1133. bool success = 1;
  1134. string error = 2;
  1135. }
  1136. // v2.9.0 — secondary index management.
  1137. message CreateIndexRequest {
  1138. // Project-qualified collection name, as every other collection-carrying
  1139. // request takes.
  1140. string collection = 1;
  1141. // Field to index. Dotted paths address nested fields ("nest.deep"), and the
  1142. // document metadata fields (_id, _created_at, _updated_at, _version) are
  1143. // addressable too.
  1144. string field = 2;
  1145. // v2.11.0 T11 — when true, the field carries a UNIQUE constraint: two
  1146. // documents may never hold the same value for it. Declaring this over a
  1147. // collection that already contains duplicate values is REFUSED (see
  1148. // CreateIndexResponse.duplicate_examples) rather than accepted and left
  1149. // silently unenforced against the data that already violates it.
  1150. //
  1151. // v2.12.0 - NULL IS NOT A VALUE for uniqueness: any number of documents
  1152. // may hold null (or omit the field), as in SQL. Before 2.12.0 two nulls
  1153. // collided while two absent fields did not.
  1154. bool unique = 3;
  1155. // v2.12.0 - a COMPOSITE unique constraint over the TUPLE of these fields
  1156. // (two or more; `field` must then be empty and `unique` true). Two
  1157. // documents may not hold the same combination. A document in which any
  1158. // of the fields is absent, null, an object or an array is not constrained
  1159. // - the SQL rule for a NULL in a unique key. Composite constraints back no
  1160. // query (the planner never uses them) and are listed by ListIndexes with
  1161. // `fields` set. Declaring one over existing duplicates is refused with
  1162. // examples, as for a single field.
  1163. repeated string fields = 4;
  1164. }
  1165. message CreateIndexResponse {
  1166. bool success = 1;
  1167. string error = 2;
  1168. // Rows indexed by the backfill. Creating an index over an existing
  1169. // collection populates it, so it is usable immediately rather than only for
  1170. // rows written afterwards.
  1171. uint64 rows_indexed = 3;
  1172. // True when the index already existed - creation is idempotent.
  1173. bool already_existed = 4;
  1174. // v2.11.0 T11 — set when a `unique=true` request was refused because the
  1175. // field already holds duplicate values. `error` explains the refusal;
  1176. // these are up to five example document ids that collide, one entry per
  1177. // colliding value, so the caller can go fix the data rather than guess.
  1178. repeated string duplicate_examples = 5;
  1179. }
  1180. message DropIndexRequest {
  1181. string collection = 1;
  1182. string field = 2;
  1183. // v2.12.0 - drop the composite unique constraint over exactly these
  1184. // fields, in the order they were declared. `field` must then be empty.
  1185. repeated string fields = 3;
  1186. }
  1187. message DropIndexResponse {
  1188. bool success = 1;
  1189. string error = 2;
  1190. }
  1191. message ListIndexesRequest {
  1192. string collection = 1;
  1193. }
  1194. message IndexInfo {
  1195. string field = 1;
  1196. // Distinct values held, and total postings. Both come from the index itself
  1197. // rather than a scan, so an operator can judge selectivity - a field whose
  1198. // postings are concentrated in few values will not be used by the planner.
  1199. uint64 distinct_values = 2;
  1200. uint64 entries = 3;
  1201. // v2.11.0 T11 — true when this index also carries a UNIQUE constraint.
  1202. bool unique = 4;
  1203. // v2.12.0 - set for a COMPOSITE unique constraint: its fields, in order.
  1204. // `field` then holds a display form ("a+b") and `unique` is true.
  1205. repeated string fields = 5;
  1206. }
  1207. message ListIndexesResponse {
  1208. bool success = 1;
  1209. string error = 2;
  1210. repeated IndexInfo indexes = 3;
  1211. }
  1212. message GetIndexValuesRequest {
  1213. string collection = 1;
  1214. string field = 2;
  1215. // Bounded on purpose: reading the values costs one document read each.
  1216. uint32 limit = 3;
  1217. // false walks from the highest value down, so limit=1 gives the maximum.
  1218. bool ascending = 4;
  1219. }
  1220. message IndexValueCount {
  1221. // The value as stored, JSON-encoded.
  1222. string value = 1;
  1223. uint64 count = 2;
  1224. }
  1225. message GetIndexValuesResponse {
  1226. bool success = 1;
  1227. string error = 2;
  1228. repeated IndexValueCount values = 3;
  1229. }
  1230. message GetCollectionConfigRequest {
  1231. string collection = 1;
  1232. }
  1233. message GetCollectionConfigResponse {
  1234. CollectionConfig config = 1;
  1235. bool found = 2;
  1236. }
  1237. message MigrateCollectionTimestampsRequest {
  1238. string collection = 1;
  1239. string from_precision = 2; // "ms" or "ns"
  1240. string to_precision = 3; // "ms" or "ns"
  1241. }
  1242. message MigrateCollectionTimestampsResponse {
  1243. bool success = 1;
  1244. string error = 2;
  1245. uint64 rows_migrated = 3; // how many documents had their timestamps updated
  1246. uint64 rows_skipped = 4; // already in target range (idempotent resume)
  1247. }
  1248. // ===== Read-Only Control =====
  1249. message SetReadOnlyRequest {
  1250. bool read_only = 1;
  1251. }
  1252. message SetReadOnlyResponse {
  1253. bool success = 1;
  1254. bool was_read_only = 2; // previous state
  1255. string error = 3;
  1256. }
  1257. message GetReadOnlyStatusRequest {}
  1258. message GetReadOnlyStatusResponse {
  1259. bool read_only = 1;
  1260. string reason = 2;
  1261. // Recovery outcome details (populated when auto-readonly triggered)
  1262. string recovery_outcome = 3; // "trivial_success" | "fresh_install" | "snapshot_fell_back" | "wal_only_replay" | "forced_empty" | "failed"
  1263. string expected_snapshot = 4;
  1264. string snapshot_used = 5;
  1265. string failure_reason = 6;
  1266. uint64 wal_entries_replayed = 7;
  1267. uint32 snapshots_attempted = 8;
  1268. }
  1269. // ===== Memory Stats =====
  1270. // Added in v1.7.0. Wire-compatible — server-side implementation lands in T9.
  1271. message GetMemoryStatsRequest {}
  1272. message CollectionMemoryStats {
  1273. string collection = 1;
  1274. uint64 document_count = 2;
  1275. uint64 estimated_bytes = 3;
  1276. uint64 evicted_stub_count = 4;
  1277. MemoryPriority priority = 5;
  1278. }
  1279. message GetMemoryStatsResponse {
  1280. uint64 total_memory_bytes = 1;
  1281. uint64 max_memory_bytes = 2;
  1282. uint32 pressure_percent = 3; // current usage / max × 100
  1283. string pressure_level = 4; // "normal" | "soft" | "hard" | "emergency"
  1284. repeated CollectionMemoryStats collections = 5;
  1285. // Most recent eviction
  1286. uint64 last_eviction_timestamp = 6; // ms since epoch, 0 if none
  1287. uint64 last_eviction_docs = 7;
  1288. uint64 last_eviction_bytes_freed = 8;
  1289. // v2.11.0 — TTL expiries a relation is refusing.
  1290. //
  1291. // ttl_blocked_documents is a GAUGE: how many documents are past their TTL
  1292. // right now and NOT being expired because deleting them would break a
  1293. // `restrict` relation (or because the cascade path is refusing while the
  1294. // LMDB mirror is unhealthy/drifted). It falls when a block lifts. Nonzero
  1295. // means those documents are outliving their retention policy - the
  1296. // deliberate alternative to silently orphaning their children.
  1297. //
  1298. // ttl_expiry_blocked_events is a monotonic COUNTER of refusal events, so it
  1299. // keeps rising while a block persists (each blocked document is retried on a
  1300. // slow cadence). Use the gauge for "how bad is it now", the counter for
  1301. // "is this still happening".
  1302. //
  1303. // Both are zero on any install that declares no relations.
  1304. uint64 ttl_blocked_documents = 9;
  1305. uint64 ttl_expiry_blocked_events = 10;
  1306. }