database.proto 36 KB

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