database.proto 52 KB

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