database.proto 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. service DatabaseService {
  19. // Document operations
  20. rpc Insert(InsertRequest) returns (InsertResponse);
  21. rpc Get(GetRequest) returns (GetResponse);
  22. rpc Update(UpdateRequest) returns (UpdateResponse);
  23. rpc Upsert(UpsertRequest) returns (UpsertResponse);
  24. rpc Delete(DeleteRequest) returns (DeleteResponse);
  25. rpc PatchDocument(PatchDocumentRequest) returns (PatchDocumentResponse);
  26. rpc Exists(ExistsRequest) returns (ExistsResponse);
  27. // Version history operations
  28. rpc GetVersionHistory(GetVersionHistoryRequest) returns (GetVersionHistoryResponse);
  29. rpc GetDocumentVersion(GetDocumentVersionRequest) returns (GetDocumentVersionResponse);
  30. rpc RestoreVersion(RestoreVersionRequest) returns (RestoreVersionResponse);
  31. rpc RestoreToDate(RestoreToDateRequest) returns (RestoreToDateResponse);
  32. // Batch operations
  33. rpc BatchInsert(BatchInsertRequest) returns (BatchInsertResponse);
  34. rpc BatchGet(BatchGetRequest) returns (BatchGetResponse);
  35. rpc BatchDelete(BatchDeleteRequest) returns (BatchDeleteResponse);
  36. // Query operations
  37. rpc Find(FindRequest) returns (FindResponse);
  38. rpc Count(CountRequest) returns (CountResponse);
  39. rpc SimilaritySearch(SimilaritySearchRequest) returns (SimilaritySearchResponse);
  40. // Set operations (Redis compatibility)
  41. rpc SetAdd(SetAddRequest) returns (SetAddResponse);
  42. rpc SetRemove(SetRemoveRequest) returns (SetRemoveResponse);
  43. rpc SetMembers(SetMembersRequest) returns (SetMembersResponse);
  44. rpc SetIsMember(SetIsMemberRequest) returns (SetIsMemberResponse);
  45. // Collection management
  46. rpc CreateCollection(CreateCollectionRequest) returns (CreateCollectionResponse);
  47. rpc DropCollection(DropCollectionRequest) returns (DropCollectionResponse);
  48. rpc ListCollections(ListCollectionsRequest) returns (ListCollectionsResponse);
  49. rpc GetCollectionInfo(GetCollectionInfoRequest) returns (GetCollectionInfoResponse);
  50. // v2.3 — Project management. Projects are top-level namespaces over
  51. // collections. The "default" project is implicit and always exists.
  52. rpc ListProjects(ListProjectsRequest) returns (ListProjectsResponse);
  53. rpc CreateProject(CreateProjectRequest) returns (CreateProjectResponse);
  54. rpc DropProject(DropProjectRequest) returns (DropProjectResponse);
  55. // View management
  56. rpc CreateView(CreateViewRequest) returns (CreateViewResponse);
  57. rpc DropView(DropViewRequest) returns (DropViewResponse);
  58. rpc ListViews(ListViewsRequest) returns (ListViewsResponse);
  59. rpc GetViewInfo(GetViewInfoRequest) returns (GetViewInfoResponse);
  60. // v2.11.0 T6b — relation (referential integrity) management. Admin-only:
  61. // `_relations` is a system collection and a relation names another
  62. // collection's schema, so declaring one is not ordinary per-collection
  63. // write access.
  64. rpc CreateRelation(CreateRelationRequest) returns (CreateRelationResponse);
  65. rpc DropRelation(DropRelationRequest) returns (DropRelationResponse);
  66. rpc ListRelations(ListRelationsRequest) returns (ListRelationsResponse);
  67. rpc GetRelationInfo(GetRelationInfoRequest) returns (GetRelationInfoResponse);
  68. // Collection configuration
  69. rpc ConfigureCollection(ConfigureCollectionRequest) returns (ConfigureCollectionResponse);
  70. // v2.9.0 — secondary indexes. Declaration is explicit per collection: an
  71. // index costs write throughput, and indexing every field would build useless
  72. // ones (a low-cardinality field like a status enum is slower through an
  73. // index than a scan).
  74. rpc CreateIndex(CreateIndexRequest) returns (CreateIndexResponse);
  75. rpc DropIndex(DropIndexRequest) returns (DropIndexResponse);
  76. rpc ListIndexes(ListIndexesRequest) returns (ListIndexesResponse);
  77. // v2.10.0 — the distinct values an indexed field holds, with row counts.
  78. // Answered from the index, one step per distinct value rather than per row.
  79. rpc GetIndexValues(GetIndexValuesRequest) returns (GetIndexValuesResponse);
  80. rpc GetCollectionConfig(GetCollectionConfigRequest) returns (GetCollectionConfigResponse);
  81. rpc MigrateCollectionTimestamps(MigrateCollectionTimestampsRequest) returns (MigrateCollectionTimestampsResponse);
  82. // File operations
  83. rpc UploadFile(stream FileChunk) returns (UploadFileResponse);
  84. rpc DownloadFile(DownloadFileRequest) returns (stream FileChunk);
  85. rpc DeleteFile(DeleteFileRequest) returns (DeleteFileResponse);
  86. rpc GetFileInfo(GetFileInfoRequest) returns (FileInfo);
  87. rpc ListFiles(ListFilesRequest) returns (ListFilesResponse);
  88. rpc SetFileTtl(SetFileTtlRequest) returns (SetFileTtlResponse);
  89. // Event subscription
  90. rpc Subscribe(SubscribeRequest) returns (stream DatabaseEvent);
  91. // Health and stats
  92. rpc HealthCheck(HealthCheckRequest) returns (HealthCheckResponse);
  93. rpc GetStats(GetStatsRequest) returns (GetStatsResponse);
  94. rpc GetMemoryStats(GetMemoryStatsRequest) returns (GetMemoryStatsResponse);
  95. // Read-only runtime state control
  96. rpc SetReadOnly(SetReadOnlyRequest) returns (SetReadOnlyResponse);
  97. rpc GetReadOnlyStatus(GetReadOnlyStatusRequest) returns (GetReadOnlyStatusResponse);
  98. }
  99. // ===== Replication Service =====
  100. service DatabaseReplication {
  101. // Bidirectional streaming for real-time sync
  102. rpc SyncStream(stream ReplicationMessage) returns (stream ReplicationMessage);
  103. // Pull missed entries (for recovery)
  104. rpc GetEntriesSince(GetEntriesRequest) returns (stream ReplicationEntry);
  105. // Get current node state
  106. rpc GetNodeState(GetNodeStateRequest) returns (NodeState);
  107. }
  108. // ===== Document Types =====
  109. message Document {
  110. string id = 1;
  111. string collection = 2;
  112. bytes data = 3; // JSON-encoded document data
  113. uint64 version = 4;
  114. uint64 created_at = 5; // Timestamp in milliseconds
  115. uint64 updated_at = 6;
  116. uint64 expires_at = 7; // 0 = no expiration
  117. string node_id = 8; // Origin node for replication
  118. bool encrypted = 9;
  119. repeated string encrypted_fields = 10;
  120. string created_by = 11; // User ID who created the document
  121. string updated_by = 12; // User ID who last updated the document
  122. }
  123. // ===== Document Operations =====
  124. message InsertRequest {
  125. string collection = 1;
  126. bytes data = 2; // JSON document
  127. string id = 3; // Optional, auto-generated if empty
  128. uint32 ttl_seconds = 4; // 0 = use collection default
  129. string actor = 5; // User ID performing the operation (for audit)
  130. }
  131. message InsertResponse {
  132. string id = 1;
  133. uint64 version = 2;
  134. }
  135. message GetRequest {
  136. string collection = 1;
  137. string id = 2;
  138. }
  139. message GetResponse {
  140. Document document = 1;
  141. bool found = 2;
  142. }
  143. message UpdateRequest {
  144. string collection = 1;
  145. string id = 2;
  146. bytes data = 3;
  147. uint64 expected_version = 4; // 0 = no version check (force update)
  148. string actor = 5; // User ID performing the operation (for audit)
  149. // v2.8.0 — change the document's expiry as part of the update.
  150. //
  151. // `optional` gives three distinct meanings, which a plain uint32 cannot:
  152. // absent -> leave the existing expiry alone (the default, and what an
  153. // update did implicitly once it stopped WIPING the expiry)
  154. // 0 -> clear the expiry: the document becomes permanent
  155. // N -> expire N seconds from now
  156. //
  157. // Before this, ttl_seconds existed only on Insert/Upsert/BatchInsert, so
  158. // changing a TTL meant rewriting the whole document via upsert.
  159. optional uint32 ttl_seconds = 6;
  160. }
  161. message UpdateResponse {
  162. uint64 new_version = 1;
  163. bool success = 2;
  164. string error = 3;
  165. }
  166. message UpsertRequest {
  167. string collection = 1;
  168. bytes data = 2;
  169. string id = 3;
  170. uint32 ttl_seconds = 4;
  171. string actor = 5; // User ID performing the operation (for audit)
  172. }
  173. message UpsertResponse {
  174. string id = 1;
  175. uint64 version = 2;
  176. bool inserted = 3; // true if inserted, false if updated
  177. }
  178. message DeleteRequest {
  179. string collection = 1;
  180. string id = 2;
  181. }
  182. message DeleteResponse {
  183. bool deleted = 1;
  184. }
  185. message PatchDocumentRequest {
  186. string collection = 1;
  187. string id = 2;
  188. bytes patch_json = 3; // JSON fields to merge into existing document
  189. string actor = 4; // User ID performing the operation (for audit)
  190. // v2.8.0 — same three-way semantics as UpdateRequest.ttl_seconds: absent
  191. // leaves the expiry alone, 0 clears it, N sets N seconds from now.
  192. optional uint32 ttl_seconds = 5;
  193. }
  194. message PatchDocumentResponse {
  195. uint64 new_version = 1;
  196. bool success = 2;
  197. string error = 3;
  198. }
  199. message ExistsRequest {
  200. string collection = 1;
  201. string id = 2;
  202. }
  203. message ExistsResponse {
  204. bool exists = 1;
  205. }
  206. // ===== Version History Operations =====
  207. message DocumentVersionEntry {
  208. uint64 version = 1;
  209. bytes data = 2; // JSON-encoded document data at this version
  210. uint64 timestamp = 3; // When this version was created (updatedAt)
  211. string updated_by = 4;
  212. bool encrypted = 5;
  213. repeated string encrypted_fields = 6;
  214. }
  215. message GetVersionHistoryRequest {
  216. string collection = 1;
  217. string id = 2;
  218. uint32 limit = 3; // 0 = all versions
  219. uint32 offset = 4;
  220. }
  221. message GetVersionHistoryResponse {
  222. repeated DocumentVersionEntry versions = 1;
  223. uint64 current_version = 2; // 0 if document is deleted
  224. uint64 total_count = 3;
  225. bool document_deleted = 4;
  226. }
  227. message GetDocumentVersionRequest {
  228. string collection = 1;
  229. string id = 2;
  230. uint64 version = 3;
  231. }
  232. message GetDocumentVersionResponse {
  233. DocumentVersionEntry version_entry = 1;
  234. bool found = 2;
  235. }
  236. message RestoreVersionRequest {
  237. string collection = 1;
  238. string id = 2;
  239. uint64 version = 3; // Version number to restore
  240. string actor = 4; // User performing the restore
  241. }
  242. message RestoreVersionResponse {
  243. uint64 new_version = 1; // The newly created version number
  244. bool success = 2;
  245. string error = 3;
  246. }
  247. message RestoreToDateRequest {
  248. string collection = 1;
  249. string id = 2;
  250. uint64 timestamp = 3; // Find version active at this time (ms since epoch)
  251. string actor = 4;
  252. }
  253. message RestoreToDateResponse {
  254. uint64 restored_version = 1; // Which old version was restored from
  255. uint64 new_version = 2; // The newly created version number
  256. bool success = 3;
  257. string error = 4;
  258. }
  259. // ===== Batch Operations =====
  260. message BatchInsertRequest {
  261. string collection = 1;
  262. repeated BatchInsertItem items = 2;
  263. string actor = 3; // User ID performing the operation (for audit)
  264. }
  265. message BatchInsertItem {
  266. bytes data = 1;
  267. string id = 2;
  268. uint32 ttl_seconds = 3;
  269. }
  270. message BatchInsertResponse {
  271. repeated string ids = 1;
  272. uint32 success_count = 2;
  273. uint32 error_count = 3;
  274. }
  275. message BatchGetRequest {
  276. string collection = 1;
  277. repeated string ids = 2;
  278. }
  279. message BatchGetResponse {
  280. repeated Document documents = 1;
  281. }
  282. message BatchDeleteRequest {
  283. string collection = 1;
  284. repeated string ids = 2;
  285. }
  286. message BatchDeleteResponse {
  287. uint64 deleted_count = 1;
  288. }
  289. // ===== Query Operations =====
  290. message FindRequest {
  291. string collection = 1;
  292. repeated Filter filters = 2;
  293. Sort sort = 3;
  294. uint32 limit = 4; // Default 100
  295. uint32 offset = 5;
  296. repeated string projection = 6; // Fields to include (empty = all)
  297. }
  298. message Filter {
  299. string field = 1;
  300. FilterOp op = 2;
  301. bytes value = 3; // JSON-encoded value
  302. }
  303. enum FilterOp {
  304. FILTER_OP_UNSPECIFIED = 0;
  305. FILTER_OP_EQ = 1;
  306. FILTER_OP_NE = 2;
  307. FILTER_OP_GT = 3;
  308. FILTER_OP_GTE = 4;
  309. FILTER_OP_LT = 5;
  310. FILTER_OP_LTE = 6;
  311. FILTER_OP_IN = 7;
  312. FILTER_OP_CONTAINS = 8;
  313. FILTER_OP_EXISTS = 9;
  314. FILTER_OP_REGEX = 10;
  315. FILTER_OP_SEARCH = 11; // Full-text search across ID and string fields
  316. }
  317. message Sort {
  318. string field = 1;
  319. bool descending = 2;
  320. }
  321. message FindResponse {
  322. repeated Document documents = 1;
  323. uint64 total_count = 2;
  324. bool has_more = 3;
  325. // WAL fallback metrics (for queries that include evicted documents)
  326. bool used_wal_fallback = 4; // True if WAL was scanned for evicted docs
  327. uint32 memory_match_count = 5; // Documents matched from memory
  328. uint32 wal_match_count = 6; // Documents matched from WAL
  329. uint64 memory_search_micros = 7; // Time spent searching memory (µs)
  330. uint64 wal_search_micros = 8; // Time spent loading/searching WAL (µs)
  331. }
  332. message CountRequest {
  333. string collection = 1;
  334. repeated Filter filters = 2;
  335. }
  336. message CountResponse {
  337. uint64 count = 1;
  338. }
  339. message SimilaritySearchRequest {
  340. string collection = 1;
  341. repeated float query_vector = 2;
  342. uint32 top_k = 3;
  343. float min_score = 4;
  344. }
  345. message SimilaritySearchResponse {
  346. repeated SimilarityResult results = 1;
  347. }
  348. message SimilarityResult {
  349. string id = 1;
  350. float score = 2;
  351. bytes data = 3;
  352. }
  353. // ===== Set Operations =====
  354. message SetAddRequest {
  355. string collection = 1;
  356. string set_id = 2;
  357. string member = 3;
  358. }
  359. message SetAddResponse {
  360. bool added = 1; // false if already exists
  361. }
  362. message SetRemoveRequest {
  363. string collection = 1;
  364. string set_id = 2;
  365. string member = 3;
  366. }
  367. message SetRemoveResponse {
  368. bool removed = 1;
  369. }
  370. message SetMembersRequest {
  371. string collection = 1;
  372. string set_id = 2;
  373. }
  374. message SetMembersResponse {
  375. repeated string members = 1;
  376. }
  377. message SetIsMemberRequest {
  378. string collection = 1;
  379. string set_id = 2;
  380. string member = 3;
  381. }
  382. message SetIsMemberResponse {
  383. bool is_member = 1;
  384. }
  385. // ===== Collection Management =====
  386. message CreateCollectionRequest {
  387. string name = 1;
  388. CollectionOptions options = 2;
  389. }
  390. message CollectionOptions {
  391. bool auto_create_id = 1;
  392. uint32 default_ttl_seconds = 2;
  393. bool encrypted = 3;
  394. repeated string sensitive_fields = 4;
  395. uint32 max_versions = 5; // Max version history per document (0 = unlimited)
  396. uint32 vector_dimension = 6; // Dimension of vectors stored in this collection (0 = not a vector collection)
  397. // NEW in v1.7.0 — eviction priority for the collection.
  398. // UNSPECIFIED is treated as NORMAL for backward compatibility.
  399. MemoryPriority memory_priority = 7;
  400. }
  401. enum MemoryPriority {
  402. MEMORY_PRIORITY_UNSPECIFIED = 0; // treated as NORMAL
  403. MEMORY_PRIORITY_LOW = 1;
  404. MEMORY_PRIORITY_NORMAL = 2;
  405. MEMORY_PRIORITY_HIGH = 3;
  406. }
  407. message CreateCollectionResponse {
  408. bool created = 1;
  409. }
  410. message DropCollectionRequest {
  411. string name = 1;
  412. }
  413. message DropCollectionResponse {
  414. bool dropped = 1;
  415. }
  416. message ListCollectionsRequest {
  417. // v2.8.0 — restrict the listing to one project namespace, mirroring
  418. // ListViewsRequest. Empty lists every project (operator/CLI use); clients
  419. // always set it, so a workspace never sees another workspace's collection
  420. // names. A collection name is information about the shape of someone else's
  421. // data even when its contents are unreachable.
  422. string project = 1;
  423. }
  424. message ListCollectionsResponse {
  425. repeated string names = 1;
  426. }
  427. message GetCollectionInfoRequest {
  428. string name = 1;
  429. }
  430. message GetCollectionInfoResponse {
  431. CollectionInfo info = 1;
  432. bool found = 2;
  433. }
  434. message CollectionInfo {
  435. string name = 1;
  436. uint64 document_count = 2;
  437. uint64 size_bytes = 3;
  438. CollectionOptions options = 4;
  439. uint64 created_at = 5;
  440. uint64 updated_at = 6;
  441. uint32 vector_dimension = 7; // Dimension of vectors stored in this collection (0 = not a vector collection)
  442. }
  443. // ===== Project Management (v2.3) =====
  444. //
  445. // Projects are top-level namespaces over collections. Collections live
  446. // inside a project; wire-form "my_app:users" addresses collection "users"
  447. // in project "my_app", bare "users" addresses ("default", "users"). The
  448. // "default" project is implicit and always present.
  449. //
  450. // Stage F ships the RPC surface backed by a filesystem-only placeholder
  451. // (`<dataDir>/projects/<name>/env/`). Stage B replaces the placeholder
  452. // with the real ProjectStore-backed implementation.
  453. message ListProjectsRequest {}
  454. message ListProjectsResponse {
  455. // Alphabetical. Always includes "default".
  456. repeated string projects = 1;
  457. }
  458. message CreateProjectRequest {
  459. string name = 1;
  460. }
  461. message CreateProjectResponse {
  462. // true = newly created, false = already existed (idempotent) OR validation error.
  463. bool created = 1;
  464. // Populated on validation failure (e.g. invalid name). Empty on success
  465. // and on the idempotent "already existed" path.
  466. string error = 2;
  467. }
  468. message DropProjectRequest {
  469. string name = 1;
  470. }
  471. message DropProjectResponse {
  472. bool dropped = 1;
  473. // Populated when the drop is refused — e.g. name == "default",
  474. // unknown project, or filesystem error.
  475. string error = 2;
  476. }
  477. // ===== File Operations =====
  478. message FileChunk {
  479. oneof content {
  480. FileMetadata metadata = 1; // First chunk includes metadata
  481. bytes data = 2; // Subsequent chunks are data
  482. }
  483. }
  484. message FileMetadata {
  485. string id = 1; // Set by server on upload response
  486. string name = 2;
  487. string mime_type = 3;
  488. uint64 size = 4;
  489. string file_type = 5; // "plugin", "document", "generated"
  490. string related_id = 6; // plugin_id, conversation_id, etc.
  491. map<string, string> metadata = 7; // Custom metadata
  492. bool is_public = 8; // Visibility flag
  493. // v2.6.0 — owning project. Empty means "default", matching the rule for
  494. // bare collection names. Files are namespaced so that per-project access
  495. // policy has something to attach to.
  496. string project = 9;
  497. // v2.8.0 — relative time-to-live in seconds, applied at upload. Converted
  498. // once to an absolute expiry server-side, so a file's lifetime does not
  499. // restart when it is read or on restart.
  500. //
  501. // `optional` is load-bearing: ABSENT means "inherit the configured default
  502. // for this file type", while an explicitly SET 0 means "never expire" and
  503. // overrides that default. A plain uint32 cannot express the difference,
  504. // which would make it impossible to opt one file out of a retention policy.
  505. optional uint32 ttl_seconds = 10;
  506. }
  507. message UploadFileResponse {
  508. string id = 1;
  509. uint64 size = 2;
  510. string checksum = 3; // SHA-256
  511. bool deduplicated = 4; // True if blob already existed
  512. }
  513. message DownloadFileRequest {
  514. string id = 1;
  515. string project = 2; // v2.6.0 — empty means "default"
  516. }
  517. message DeleteFileRequest {
  518. string id = 1;
  519. string project = 2; // v2.6.0 — empty means "default"
  520. }
  521. message DeleteFileResponse {
  522. bool deleted = 1;
  523. }
  524. message GetFileInfoRequest {
  525. string id = 1;
  526. string project = 2; // v2.6.0 — empty means "default"
  527. }
  528. message FileInfo {
  529. string id = 1;
  530. string name = 2;
  531. string mime_type = 3;
  532. uint64 size = 4;
  533. string file_type = 5;
  534. string related_id = 6;
  535. string checksum = 7;
  536. uint64 created_at = 8;
  537. map<string, string> metadata = 9;
  538. bool is_public = 10; // Visibility flag
  539. uint32 ref_count = 11; // Blob reference count (informational)
  540. string project = 12; // v2.6.0 — owning project
  541. uint64 expires_at = 13; // v2.8.0 — absolute expiry ms, 0 = never
  542. }
  543. message ListFilesRequest {
  544. string file_type = 1; // Filter by type (optional)
  545. string related_id = 2; // Filter by related ID (optional)
  546. uint32 limit = 3;
  547. uint32 offset = 4;
  548. string checksum = 5; // Filter by checksum (for dedup queries)
  549. string name = 6; // Filter by exact filename (optional)
  550. string project = 7; // v2.6.0 — empty means "default"
  551. }
  552. // v2.8.0 — change the expiry of a file that is already stored. A file's TTL was
  553. // otherwise fixed at upload.
  554. message SetFileTtlRequest {
  555. string id = 1;
  556. string project = 2; // empty means "default"
  557. // Absent is invalid here - the call exists to change the expiry, so it must
  558. // say what to. 0 clears the expiry (keep indefinitely); N expires the file N
  559. // seconds from now.
  560. uint32 ttl_seconds = 3;
  561. }
  562. message SetFileTtlResponse {
  563. bool updated = 1; // false if no such file in that project
  564. uint64 expires_at = 2; // the resulting absolute expiry, 0 = never
  565. }
  566. message ListFilesResponse {
  567. repeated FileInfo files = 1;
  568. uint64 total_count = 2;
  569. bool has_more = 3;
  570. }
  571. // ===== Event Subscription =====
  572. message SubscribeRequest {
  573. repeated string collections = 1; // Empty = all collections
  574. repeated string patterns = 2; // Glob patterns (e.g., "assistants:*")
  575. bool include_data = 3; // Include document data in events
  576. }
  577. message DatabaseEvent {
  578. EventType type = 1;
  579. string collection = 2;
  580. string document_id = 3;
  581. uint64 timestamp = 4;
  582. string node_id = 5;
  583. bytes data = 6; // JSON document (if include_data)
  584. }
  585. enum EventType {
  586. EVENT_UNKNOWN = 0;
  587. EVENT_INSERT = 1;
  588. EVENT_UPDATE = 2;
  589. EVENT_DELETE = 3;
  590. EVENT_EXPIRE = 4;
  591. EVENT_INVALIDATE = 5;
  592. // NEW in v1.7.0 — memory-pressure observability events.
  593. EVENT_MEMORY_PRESSURE_HIGH = 6; // Emitted when pressure reaches hard threshold
  594. EVENT_MEMORY_EVICTION_BURST = 7; // Emitted when eviction runs a large burst
  595. }
  596. // ===== Replication =====
  597. message ReplicationEntry {
  598. uint64 sequence = 1;
  599. uint64 global_timestamp = 2;
  600. string node_id = 3;
  601. OperationType op = 4;
  602. string collection = 5;
  603. string document_id = 6;
  604. uint64 document_version = 7;
  605. bytes data = 8;
  606. }
  607. enum OperationType {
  608. OP_UNKNOWN = 0;
  609. OP_INSERT = 1;
  610. OP_UPDATE = 2;
  611. OP_DELETE = 3;
  612. OP_UPSERT = 4;
  613. OP_CREATE_COLLECTION = 5;
  614. OP_DROP_COLLECTION = 6;
  615. }
  616. message ReplicationMessage {
  617. oneof content {
  618. ReplicationEntry entry = 1;
  619. Heartbeat heartbeat = 2;
  620. WatermarkUpdate watermark = 3;
  621. SyncRequest sync_request = 4;
  622. }
  623. }
  624. message Heartbeat {
  625. string node_id = 1;
  626. uint64 timestamp = 2;
  627. uint64 sequence = 3;
  628. }
  629. message WatermarkUpdate {
  630. string node_id = 1;
  631. uint64 sequence = 2;
  632. }
  633. message SyncRequest {
  634. uint64 from_sequence = 1;
  635. }
  636. message GetEntriesRequest {
  637. uint64 from_sequence = 1;
  638. uint32 limit = 2;
  639. }
  640. message GetNodeStateRequest {
  641. }
  642. message NodeState {
  643. string node_id = 1;
  644. uint64 current_sequence = 2;
  645. map<string, uint64> peer_watermarks = 3;
  646. uint64 document_count = 4;
  647. repeated string collections = 5;
  648. bool healthy = 6;
  649. // Per-collection sequence tracking for collection discovery
  650. map<string, uint64> collection_sequences = 7;
  651. }
  652. // ===== Health and Stats =====
  653. message HealthCheckRequest {
  654. }
  655. message HealthCheckResponse {
  656. bool healthy = 1;
  657. uint64 uptime_seconds = 2;
  658. uint64 document_count = 3;
  659. uint64 memory_used_bytes = 4;
  660. uint64 wal_size_bytes = 5;
  661. repeated PeerHealth peers = 6;
  662. }
  663. message PeerHealth {
  664. string node_id = 1;
  665. bool connected = 2;
  666. uint64 latency_ms = 3;
  667. uint64 lag_entries = 4;
  668. }
  669. message GetStatsRequest {
  670. }
  671. message GetStatsResponse {
  672. uint64 total_documents = 1;
  673. uint64 total_collections = 2;
  674. uint64 memory_used_bytes = 3;
  675. uint64 wal_sequence = 4;
  676. uint64 wal_size_bytes = 5;
  677. uint64 snapshot_count = 6;
  678. uint64 last_snapshot_sequence = 7;
  679. uint64 insert_count = 8;
  680. uint64 update_count = 9;
  681. uint64 delete_count = 10;
  682. uint64 query_count = 11;
  683. // Memory eviction stats
  684. uint64 evicted_documents = 12; // Documents currently evicted to disk
  685. uint64 total_evictions = 13; // Total eviction operations performed
  686. uint64 recovery_count = 14; // Documents recovered from eviction
  687. // Memory configuration
  688. uint64 max_memory_bytes = 15; // Configured max memory limit
  689. uint32 eviction_threshold_percent = 16; // Start evicting at this % of max
  690. uint32 eviction_target_percent = 17; // Evict down to this % of max
  691. // Operation timing (microseconds) - for performance monitoring
  692. uint64 get_count = 18; // Number of get operations
  693. uint64 get_total_micros = 19; // Total time spent in get operations
  694. uint64 get_max_micros = 20; // Max single get operation time
  695. uint64 insert_total_micros = 21;
  696. uint64 insert_max_micros = 22;
  697. uint64 update_total_micros = 23;
  698. uint64 update_max_micros = 24;
  699. uint64 query_total_micros = 25;
  700. uint64 query_max_micros = 26;
  701. }
  702. // ===== View Operations =====
  703. message ViewDefinition {
  704. string name = 1; // view name (globally unique, cannot start with _)
  705. string collection = 2; // target real collection (must NOT be another view)
  706. repeated string include = 3; // field paths to include (dot-notation for nested)
  707. repeated string exclude = 4; // field paths to exclude (ignored if include is non-empty)
  708. repeated Filter where = 5; // baked-in filters (AND-merged with caller filters)
  709. Sort default_sort = 6; // baked-in default sort (used when caller doesn't specify)
  710. uint64 created_at = 7;
  711. uint64 updated_at = 8;
  712. }
  713. message CreateViewRequest {
  714. string name = 1;
  715. string collection = 2;
  716. repeated string include = 3;
  717. repeated string exclude = 4;
  718. repeated Filter where = 5;
  719. Sort default_sort = 6;
  720. }
  721. message CreateViewResponse {
  722. bool success = 1;
  723. string error = 2;
  724. }
  725. message DropViewRequest {
  726. string name = 1;
  727. }
  728. message DropViewResponse {
  729. bool success = 1;
  730. string error = 2;
  731. }
  732. message ListViewsRequest {
  733. // Restrict the listing to one project namespace. Empty lists every
  734. // project (operator/CLI use). Clients always set it so a workspace
  735. // never sees another workspace's views.
  736. string project = 1;
  737. }
  738. message ListViewsResponse {
  739. repeated ViewDefinition views = 1;
  740. }
  741. message GetViewInfoRequest {
  742. string name = 1;
  743. }
  744. message GetViewInfoResponse {
  745. ViewDefinition view = 1;
  746. bool found = 2;
  747. }
  748. // ===== Relation (Referential Integrity) Operations — v2.11.0 T6b =====
  749. //
  750. // A relation says: documents in `child` reference documents in `parent`
  751. // through `child_field` (a dot-path that may resolve to a single id or an
  752. // array of ids). Declarations are persisted in the global `_relations`
  753. // system collection (mirrors `_views`) and are project-qualified like any
  754. // other collection-carrying name. Management is admin-only.
  755. message RelationDefinition {
  756. string name = 1; // relation name, project-qualified
  757. string child = 2; // collection holding the reference, project-qualified
  758. string child_field = 3; // dot-path on the child; may resolve to an array of ids
  759. string parent = 4; // collection being referenced, project-qualified
  760. // "restrict" (default) | "cascade" | "set_null" | "no_action".
  761. // Only restrict/no_action are enforced today (v2.11.0 T4/T6a); cascade
  762. // and set_null are accepted and persisted but currently behave as
  763. // permit (Task 12 makes them destructive).
  764. string on_delete = 5;
  765. // Not yet enforced (Task 13). Accepted and persisted.
  766. bool validate_on_write = 6;
  767. uint64 created_at = 7;
  768. uint64 updated_at = 8;
  769. }
  770. message CreateRelationRequest {
  771. string name = 1;
  772. string child = 2;
  773. string child_field = 3;
  774. string parent = 4;
  775. string on_delete = 5; // empty defaults to "restrict"
  776. bool validate_on_write = 6;
  777. }
  778. message CreateRelationResponse {
  779. bool success = 1;
  780. string error = 2;
  781. }
  782. message DropRelationRequest {
  783. string name = 1;
  784. }
  785. message DropRelationResponse {
  786. bool success = 1;
  787. string error = 2;
  788. }
  789. message ListRelationsRequest {
  790. // Restrict the listing to one project namespace. Empty lists every
  791. // project (operator/CLI use). Clients always send their own project so
  792. // one workspace never enumerates another's relations.
  793. string project = 1;
  794. }
  795. message ListRelationsResponse {
  796. repeated RelationDefinition relations = 1;
  797. }
  798. message GetRelationInfoRequest {
  799. string name = 1;
  800. }
  801. message GetRelationInfoResponse {
  802. RelationDefinition relation = 1;
  803. bool found = 2;
  804. }
  805. // ===== Collection Configuration =====
  806. // Per-collection configuration for timestamp precision and other runtime knobs.
  807. // Stored in the _collection_meta system collection.
  808. message CollectionConfig {
  809. // "ms" (default) — _created_at/_updated_at stamped in milliseconds since epoch
  810. // "ns" — _created_at/_updated_at stamped in nanoseconds since epoch
  811. // Empty on a ConfigureCollection request means "leave unchanged".
  812. string timestamp_precision = 1;
  813. // v2.4.5 — version history on/off for an EXISTING collection.
  814. //
  815. // `optional` (proto3 field presence) is load-bearing, not decoration: a
  816. // plain bool defaults to false, so any ConfigureCollection call that meant
  817. // to set only timestamp_precision would silently switch versioning OFF.
  818. // With presence, an absent field means "leave unchanged" and the server
  819. // overlays only what the caller actually set.
  820. //
  821. // Disabling stops NEW versions being recorded; history already on disk
  822. // stays readable, so this is reversible. Distinct from max_versions=0,
  823. // which means "unlimited", not "off".
  824. optional bool versioning_enabled = 2;
  825. // v2.11.0 T8 — whether declared relations are enforced for this collection.
  826. //
  827. // `optional` for the same reason versioning_enabled is: a plain bool
  828. // defaults to false, so a ConfigureCollection call that meant to set
  829. // only timestamp_precision would silently switch relation enforcement
  830. // OFF. With presence, an absent field means "leave unchanged".
  831. //
  832. // Defaults to true (enforced) when never explicitly configured -
  833. // declaring a relation names its child and parent explicitly, so the
  834. // declaration IS the opt-in.
  835. optional bool relations_enforced = 3;
  836. // Room for future per-collection knobs
  837. }
  838. message ConfigureCollectionRequest {
  839. string collection = 1;
  840. CollectionConfig config = 2;
  841. }
  842. message ConfigureCollectionResponse {
  843. bool success = 1;
  844. string error = 2;
  845. }
  846. // v2.9.0 — secondary index management.
  847. message CreateIndexRequest {
  848. // Project-qualified collection name, as every other collection-carrying
  849. // request takes.
  850. string collection = 1;
  851. // Field to index. Dotted paths address nested fields ("nest.deep"), and the
  852. // document metadata fields (_id, _created_at, _updated_at, _version) are
  853. // addressable too.
  854. string field = 2;
  855. }
  856. message CreateIndexResponse {
  857. bool success = 1;
  858. string error = 2;
  859. // Rows indexed by the backfill. Creating an index over an existing
  860. // collection populates it, so it is usable immediately rather than only for
  861. // rows written afterwards.
  862. uint64 rows_indexed = 3;
  863. // True when the index already existed - creation is idempotent.
  864. bool already_existed = 4;
  865. }
  866. message DropIndexRequest {
  867. string collection = 1;
  868. string field = 2;
  869. }
  870. message DropIndexResponse {
  871. bool success = 1;
  872. string error = 2;
  873. }
  874. message ListIndexesRequest {
  875. string collection = 1;
  876. }
  877. message IndexInfo {
  878. string field = 1;
  879. // Distinct values held, and total postings. Both come from the index itself
  880. // rather than a scan, so an operator can judge selectivity - a field whose
  881. // postings are concentrated in few values will not be used by the planner.
  882. uint64 distinct_values = 2;
  883. uint64 entries = 3;
  884. }
  885. message ListIndexesResponse {
  886. bool success = 1;
  887. string error = 2;
  888. repeated IndexInfo indexes = 3;
  889. }
  890. message GetIndexValuesRequest {
  891. string collection = 1;
  892. string field = 2;
  893. // Bounded on purpose: reading the values costs one document read each.
  894. uint32 limit = 3;
  895. // false walks from the highest value down, so limit=1 gives the maximum.
  896. bool ascending = 4;
  897. }
  898. message IndexValueCount {
  899. // The value as stored, JSON-encoded.
  900. string value = 1;
  901. uint64 count = 2;
  902. }
  903. message GetIndexValuesResponse {
  904. bool success = 1;
  905. string error = 2;
  906. repeated IndexValueCount values = 3;
  907. }
  908. message GetCollectionConfigRequest {
  909. string collection = 1;
  910. }
  911. message GetCollectionConfigResponse {
  912. CollectionConfig config = 1;
  913. bool found = 2;
  914. }
  915. message MigrateCollectionTimestampsRequest {
  916. string collection = 1;
  917. string from_precision = 2; // "ms" or "ns"
  918. string to_precision = 3; // "ms" or "ns"
  919. }
  920. message MigrateCollectionTimestampsResponse {
  921. bool success = 1;
  922. string error = 2;
  923. uint64 rows_migrated = 3; // how many documents had their timestamps updated
  924. uint64 rows_skipped = 4; // already in target range (idempotent resume)
  925. }
  926. // ===== Read-Only Control =====
  927. message SetReadOnlyRequest {
  928. bool read_only = 1;
  929. }
  930. message SetReadOnlyResponse {
  931. bool success = 1;
  932. bool was_read_only = 2; // previous state
  933. string error = 3;
  934. }
  935. message GetReadOnlyStatusRequest {}
  936. message GetReadOnlyStatusResponse {
  937. bool read_only = 1;
  938. string reason = 2;
  939. // Recovery outcome details (populated when auto-readonly triggered)
  940. string recovery_outcome = 3; // "trivial_success" | "fresh_install" | "snapshot_fell_back" | "wal_only_replay" | "forced_empty" | "failed"
  941. string expected_snapshot = 4;
  942. string snapshot_used = 5;
  943. string failure_reason = 6;
  944. uint64 wal_entries_replayed = 7;
  945. uint32 snapshots_attempted = 8;
  946. }
  947. // ===== Memory Stats =====
  948. // Added in v1.7.0. Wire-compatible — server-side implementation lands in T9.
  949. message GetMemoryStatsRequest {}
  950. message CollectionMemoryStats {
  951. string collection = 1;
  952. uint64 document_count = 2;
  953. uint64 estimated_bytes = 3;
  954. uint64 evicted_stub_count = 4;
  955. MemoryPriority priority = 5;
  956. }
  957. message GetMemoryStatsResponse {
  958. uint64 total_memory_bytes = 1;
  959. uint64 max_memory_bytes = 2;
  960. uint32 pressure_percent = 3; // current usage / max × 100
  961. string pressure_level = 4; // "normal" | "soft" | "hard" | "emergency"
  962. repeated CollectionMemoryStats collections = 5;
  963. // Most recent eviction
  964. uint64 last_eviction_timestamp = 6; // ms since epoch, 0 if none
  965. uint64 last_eviction_docs = 7;
  966. uint64 last_eviction_bytes_freed = 8;
  967. }