database.proto 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. // View management
  51. rpc CreateView(CreateViewRequest) returns (CreateViewResponse);
  52. rpc DropView(DropViewRequest) returns (DropViewResponse);
  53. rpc ListViews(ListViewsRequest) returns (ListViewsResponse);
  54. rpc GetViewInfo(GetViewInfoRequest) returns (GetViewInfoResponse);
  55. // Collection configuration
  56. rpc ConfigureCollection(ConfigureCollectionRequest) returns (ConfigureCollectionResponse);
  57. rpc GetCollectionConfig(GetCollectionConfigRequest) returns (GetCollectionConfigResponse);
  58. rpc MigrateCollectionTimestamps(MigrateCollectionTimestampsRequest) returns (MigrateCollectionTimestampsResponse);
  59. // File operations
  60. rpc UploadFile(stream FileChunk) returns (UploadFileResponse);
  61. rpc DownloadFile(DownloadFileRequest) returns (stream FileChunk);
  62. rpc DeleteFile(DeleteFileRequest) returns (DeleteFileResponse);
  63. rpc GetFileInfo(GetFileInfoRequest) returns (FileInfo);
  64. rpc ListFiles(ListFilesRequest) returns (ListFilesResponse);
  65. // Event subscription
  66. rpc Subscribe(SubscribeRequest) returns (stream DatabaseEvent);
  67. // Health and stats
  68. rpc HealthCheck(HealthCheckRequest) returns (HealthCheckResponse);
  69. rpc GetStats(GetStatsRequest) returns (GetStatsResponse);
  70. rpc GetMemoryStats(GetMemoryStatsRequest) returns (GetMemoryStatsResponse);
  71. // Read-only runtime state control
  72. rpc SetReadOnly(SetReadOnlyRequest) returns (SetReadOnlyResponse);
  73. rpc GetReadOnlyStatus(GetReadOnlyStatusRequest) returns (GetReadOnlyStatusResponse);
  74. }
  75. // ===== Replication Service =====
  76. service DatabaseReplication {
  77. // Bidirectional streaming for real-time sync
  78. rpc SyncStream(stream ReplicationMessage) returns (stream ReplicationMessage);
  79. // Pull missed entries (for recovery)
  80. rpc GetEntriesSince(GetEntriesRequest) returns (stream ReplicationEntry);
  81. // Get current node state
  82. rpc GetNodeState(GetNodeStateRequest) returns (NodeState);
  83. }
  84. // ===== Document Types =====
  85. message Document {
  86. string id = 1;
  87. string collection = 2;
  88. bytes data = 3; // JSON-encoded document data
  89. uint64 version = 4;
  90. uint64 created_at = 5; // Timestamp in milliseconds
  91. uint64 updated_at = 6;
  92. uint64 expires_at = 7; // 0 = no expiration
  93. string node_id = 8; // Origin node for replication
  94. bool encrypted = 9;
  95. repeated string encrypted_fields = 10;
  96. string created_by = 11; // User ID who created the document
  97. string updated_by = 12; // User ID who last updated the document
  98. }
  99. // ===== Document Operations =====
  100. message InsertRequest {
  101. string collection = 1;
  102. bytes data = 2; // JSON document
  103. string id = 3; // Optional, auto-generated if empty
  104. uint32 ttl_seconds = 4; // 0 = use collection default
  105. string actor = 5; // User ID performing the operation (for audit)
  106. }
  107. message InsertResponse {
  108. string id = 1;
  109. uint64 version = 2;
  110. }
  111. message GetRequest {
  112. string collection = 1;
  113. string id = 2;
  114. }
  115. message GetResponse {
  116. Document document = 1;
  117. bool found = 2;
  118. }
  119. message UpdateRequest {
  120. string collection = 1;
  121. string id = 2;
  122. bytes data = 3;
  123. uint64 expected_version = 4; // 0 = no version check (force update)
  124. string actor = 5; // User ID performing the operation (for audit)
  125. }
  126. message UpdateResponse {
  127. uint64 new_version = 1;
  128. bool success = 2;
  129. string error = 3;
  130. }
  131. message UpsertRequest {
  132. string collection = 1;
  133. bytes data = 2;
  134. string id = 3;
  135. uint32 ttl_seconds = 4;
  136. string actor = 5; // User ID performing the operation (for audit)
  137. }
  138. message UpsertResponse {
  139. string id = 1;
  140. uint64 version = 2;
  141. bool inserted = 3; // true if inserted, false if updated
  142. }
  143. message DeleteRequest {
  144. string collection = 1;
  145. string id = 2;
  146. }
  147. message DeleteResponse {
  148. bool deleted = 1;
  149. }
  150. message PatchDocumentRequest {
  151. string collection = 1;
  152. string id = 2;
  153. bytes patch_json = 3; // JSON fields to merge into existing document
  154. string actor = 4; // User ID performing the operation (for audit)
  155. }
  156. message PatchDocumentResponse {
  157. uint64 new_version = 1;
  158. bool success = 2;
  159. string error = 3;
  160. }
  161. message ExistsRequest {
  162. string collection = 1;
  163. string id = 2;
  164. }
  165. message ExistsResponse {
  166. bool exists = 1;
  167. }
  168. // ===== Version History Operations =====
  169. message DocumentVersionEntry {
  170. uint64 version = 1;
  171. bytes data = 2; // JSON-encoded document data at this version
  172. uint64 timestamp = 3; // When this version was created (updatedAt)
  173. string updated_by = 4;
  174. bool encrypted = 5;
  175. repeated string encrypted_fields = 6;
  176. }
  177. message GetVersionHistoryRequest {
  178. string collection = 1;
  179. string id = 2;
  180. uint32 limit = 3; // 0 = all versions
  181. uint32 offset = 4;
  182. }
  183. message GetVersionHistoryResponse {
  184. repeated DocumentVersionEntry versions = 1;
  185. uint64 current_version = 2; // 0 if document is deleted
  186. uint64 total_count = 3;
  187. bool document_deleted = 4;
  188. }
  189. message GetDocumentVersionRequest {
  190. string collection = 1;
  191. string id = 2;
  192. uint64 version = 3;
  193. }
  194. message GetDocumentVersionResponse {
  195. DocumentVersionEntry version_entry = 1;
  196. bool found = 2;
  197. }
  198. message RestoreVersionRequest {
  199. string collection = 1;
  200. string id = 2;
  201. uint64 version = 3; // Version number to restore
  202. string actor = 4; // User performing the restore
  203. }
  204. message RestoreVersionResponse {
  205. uint64 new_version = 1; // The newly created version number
  206. bool success = 2;
  207. string error = 3;
  208. }
  209. message RestoreToDateRequest {
  210. string collection = 1;
  211. string id = 2;
  212. uint64 timestamp = 3; // Find version active at this time (ms since epoch)
  213. string actor = 4;
  214. }
  215. message RestoreToDateResponse {
  216. uint64 restored_version = 1; // Which old version was restored from
  217. uint64 new_version = 2; // The newly created version number
  218. bool success = 3;
  219. string error = 4;
  220. }
  221. // ===== Batch Operations =====
  222. message BatchInsertRequest {
  223. string collection = 1;
  224. repeated BatchInsertItem items = 2;
  225. string actor = 3; // User ID performing the operation (for audit)
  226. }
  227. message BatchInsertItem {
  228. bytes data = 1;
  229. string id = 2;
  230. uint32 ttl_seconds = 3;
  231. }
  232. message BatchInsertResponse {
  233. repeated string ids = 1;
  234. uint32 success_count = 2;
  235. uint32 error_count = 3;
  236. }
  237. message BatchGetRequest {
  238. string collection = 1;
  239. repeated string ids = 2;
  240. }
  241. message BatchGetResponse {
  242. repeated Document documents = 1;
  243. }
  244. message BatchDeleteRequest {
  245. string collection = 1;
  246. repeated string ids = 2;
  247. }
  248. message BatchDeleteResponse {
  249. uint64 deleted_count = 1;
  250. }
  251. // ===== Query Operations =====
  252. message FindRequest {
  253. string collection = 1;
  254. repeated Filter filters = 2;
  255. Sort sort = 3;
  256. uint32 limit = 4; // Default 100
  257. uint32 offset = 5;
  258. repeated string projection = 6; // Fields to include (empty = all)
  259. }
  260. message Filter {
  261. string field = 1;
  262. FilterOp op = 2;
  263. bytes value = 3; // JSON-encoded value
  264. }
  265. enum FilterOp {
  266. FILTER_OP_UNSPECIFIED = 0;
  267. FILTER_OP_EQ = 1;
  268. FILTER_OP_NE = 2;
  269. FILTER_OP_GT = 3;
  270. FILTER_OP_GTE = 4;
  271. FILTER_OP_LT = 5;
  272. FILTER_OP_LTE = 6;
  273. FILTER_OP_IN = 7;
  274. FILTER_OP_CONTAINS = 8;
  275. FILTER_OP_EXISTS = 9;
  276. FILTER_OP_REGEX = 10;
  277. FILTER_OP_SEARCH = 11; // Full-text search across ID and string fields
  278. }
  279. message Sort {
  280. string field = 1;
  281. bool descending = 2;
  282. }
  283. message FindResponse {
  284. repeated Document documents = 1;
  285. uint64 total_count = 2;
  286. bool has_more = 3;
  287. // WAL fallback metrics (for queries that include evicted documents)
  288. bool used_wal_fallback = 4; // True if WAL was scanned for evicted docs
  289. uint32 memory_match_count = 5; // Documents matched from memory
  290. uint32 wal_match_count = 6; // Documents matched from WAL
  291. uint64 memory_search_micros = 7; // Time spent searching memory (µs)
  292. uint64 wal_search_micros = 8; // Time spent loading/searching WAL (µs)
  293. }
  294. message CountRequest {
  295. string collection = 1;
  296. repeated Filter filters = 2;
  297. }
  298. message CountResponse {
  299. uint64 count = 1;
  300. }
  301. message SimilaritySearchRequest {
  302. string collection = 1;
  303. repeated float query_vector = 2;
  304. uint32 top_k = 3;
  305. float min_score = 4;
  306. }
  307. message SimilaritySearchResponse {
  308. repeated SimilarityResult results = 1;
  309. }
  310. message SimilarityResult {
  311. string id = 1;
  312. float score = 2;
  313. bytes data = 3;
  314. }
  315. // ===== Set Operations =====
  316. message SetAddRequest {
  317. string collection = 1;
  318. string set_id = 2;
  319. string member = 3;
  320. }
  321. message SetAddResponse {
  322. bool added = 1; // false if already exists
  323. }
  324. message SetRemoveRequest {
  325. string collection = 1;
  326. string set_id = 2;
  327. string member = 3;
  328. }
  329. message SetRemoveResponse {
  330. bool removed = 1;
  331. }
  332. message SetMembersRequest {
  333. string collection = 1;
  334. string set_id = 2;
  335. }
  336. message SetMembersResponse {
  337. repeated string members = 1;
  338. }
  339. message SetIsMemberRequest {
  340. string collection = 1;
  341. string set_id = 2;
  342. string member = 3;
  343. }
  344. message SetIsMemberResponse {
  345. bool is_member = 1;
  346. }
  347. // ===== Collection Management =====
  348. message CreateCollectionRequest {
  349. string name = 1;
  350. CollectionOptions options = 2;
  351. }
  352. message CollectionOptions {
  353. bool auto_create_id = 1;
  354. uint32 default_ttl_seconds = 2;
  355. bool encrypted = 3;
  356. repeated string sensitive_fields = 4;
  357. uint32 max_versions = 5; // Max version history per document (0 = unlimited)
  358. uint32 vector_dimension = 6; // Dimension of vectors stored in this collection (0 = not a vector collection)
  359. // NEW in v1.7.0 — eviction priority for the collection.
  360. // UNSPECIFIED is treated as NORMAL for backward compatibility.
  361. MemoryPriority memory_priority = 7;
  362. }
  363. enum MemoryPriority {
  364. MEMORY_PRIORITY_UNSPECIFIED = 0; // treated as NORMAL
  365. MEMORY_PRIORITY_LOW = 1;
  366. MEMORY_PRIORITY_NORMAL = 2;
  367. MEMORY_PRIORITY_HIGH = 3;
  368. }
  369. message CreateCollectionResponse {
  370. bool created = 1;
  371. }
  372. message DropCollectionRequest {
  373. string name = 1;
  374. }
  375. message DropCollectionResponse {
  376. bool dropped = 1;
  377. }
  378. message ListCollectionsRequest {
  379. }
  380. message ListCollectionsResponse {
  381. repeated string names = 1;
  382. }
  383. message GetCollectionInfoRequest {
  384. string name = 1;
  385. }
  386. message GetCollectionInfoResponse {
  387. CollectionInfo info = 1;
  388. bool found = 2;
  389. }
  390. message CollectionInfo {
  391. string name = 1;
  392. uint64 document_count = 2;
  393. uint64 size_bytes = 3;
  394. CollectionOptions options = 4;
  395. uint64 created_at = 5;
  396. uint64 updated_at = 6;
  397. uint32 vector_dimension = 7; // Dimension of vectors stored in this collection (0 = not a vector collection)
  398. }
  399. // ===== File Operations =====
  400. message FileChunk {
  401. oneof content {
  402. FileMetadata metadata = 1; // First chunk includes metadata
  403. bytes data = 2; // Subsequent chunks are data
  404. }
  405. }
  406. message FileMetadata {
  407. string id = 1; // Set by server on upload response
  408. string name = 2;
  409. string mime_type = 3;
  410. uint64 size = 4;
  411. string file_type = 5; // "plugin", "document", "generated"
  412. string related_id = 6; // plugin_id, conversation_id, etc.
  413. map<string, string> metadata = 7; // Custom metadata
  414. bool is_public = 8; // Visibility flag
  415. }
  416. message UploadFileResponse {
  417. string id = 1;
  418. uint64 size = 2;
  419. string checksum = 3; // SHA-256
  420. bool deduplicated = 4; // True if blob already existed
  421. }
  422. message DownloadFileRequest {
  423. string id = 1;
  424. }
  425. message DeleteFileRequest {
  426. string id = 1;
  427. }
  428. message DeleteFileResponse {
  429. bool deleted = 1;
  430. }
  431. message GetFileInfoRequest {
  432. string id = 1;
  433. }
  434. message FileInfo {
  435. string id = 1;
  436. string name = 2;
  437. string mime_type = 3;
  438. uint64 size = 4;
  439. string file_type = 5;
  440. string related_id = 6;
  441. string checksum = 7;
  442. uint64 created_at = 8;
  443. map<string, string> metadata = 9;
  444. bool is_public = 10; // Visibility flag
  445. uint32 ref_count = 11; // Blob reference count (informational)
  446. }
  447. message ListFilesRequest {
  448. string file_type = 1; // Filter by type (optional)
  449. string related_id = 2; // Filter by related ID (optional)
  450. uint32 limit = 3;
  451. uint32 offset = 4;
  452. string checksum = 5; // Filter by checksum (for dedup queries)
  453. string name = 6; // Filter by exact filename (optional)
  454. }
  455. message ListFilesResponse {
  456. repeated FileInfo files = 1;
  457. uint64 total_count = 2;
  458. bool has_more = 3;
  459. }
  460. // ===== Event Subscription =====
  461. message SubscribeRequest {
  462. repeated string collections = 1; // Empty = all collections
  463. repeated string patterns = 2; // Glob patterns (e.g., "assistants:*")
  464. bool include_data = 3; // Include document data in events
  465. }
  466. message DatabaseEvent {
  467. EventType type = 1;
  468. string collection = 2;
  469. string document_id = 3;
  470. uint64 timestamp = 4;
  471. string node_id = 5;
  472. bytes data = 6; // JSON document (if include_data)
  473. }
  474. enum EventType {
  475. EVENT_UNKNOWN = 0;
  476. EVENT_INSERT = 1;
  477. EVENT_UPDATE = 2;
  478. EVENT_DELETE = 3;
  479. EVENT_EXPIRE = 4;
  480. EVENT_INVALIDATE = 5;
  481. // NEW in v1.7.0 — memory-pressure observability events.
  482. EVENT_MEMORY_PRESSURE_HIGH = 6; // Emitted when pressure reaches hard threshold
  483. EVENT_MEMORY_EVICTION_BURST = 7; // Emitted when eviction runs a large burst
  484. }
  485. // ===== Replication =====
  486. message ReplicationEntry {
  487. uint64 sequence = 1;
  488. uint64 global_timestamp = 2;
  489. string node_id = 3;
  490. OperationType op = 4;
  491. string collection = 5;
  492. string document_id = 6;
  493. uint64 document_version = 7;
  494. bytes data = 8;
  495. }
  496. enum OperationType {
  497. OP_UNKNOWN = 0;
  498. OP_INSERT = 1;
  499. OP_UPDATE = 2;
  500. OP_DELETE = 3;
  501. OP_UPSERT = 4;
  502. OP_CREATE_COLLECTION = 5;
  503. OP_DROP_COLLECTION = 6;
  504. }
  505. message ReplicationMessage {
  506. oneof content {
  507. ReplicationEntry entry = 1;
  508. Heartbeat heartbeat = 2;
  509. WatermarkUpdate watermark = 3;
  510. SyncRequest sync_request = 4;
  511. }
  512. }
  513. message Heartbeat {
  514. string node_id = 1;
  515. uint64 timestamp = 2;
  516. uint64 sequence = 3;
  517. }
  518. message WatermarkUpdate {
  519. string node_id = 1;
  520. uint64 sequence = 2;
  521. }
  522. message SyncRequest {
  523. uint64 from_sequence = 1;
  524. }
  525. message GetEntriesRequest {
  526. uint64 from_sequence = 1;
  527. uint32 limit = 2;
  528. }
  529. message GetNodeStateRequest {
  530. }
  531. message NodeState {
  532. string node_id = 1;
  533. uint64 current_sequence = 2;
  534. map<string, uint64> peer_watermarks = 3;
  535. uint64 document_count = 4;
  536. repeated string collections = 5;
  537. bool healthy = 6;
  538. // Per-collection sequence tracking for collection discovery
  539. map<string, uint64> collection_sequences = 7;
  540. }
  541. // ===== Health and Stats =====
  542. message HealthCheckRequest {
  543. }
  544. message HealthCheckResponse {
  545. bool healthy = 1;
  546. uint64 uptime_seconds = 2;
  547. uint64 document_count = 3;
  548. uint64 memory_used_bytes = 4;
  549. uint64 wal_size_bytes = 5;
  550. repeated PeerHealth peers = 6;
  551. }
  552. message PeerHealth {
  553. string node_id = 1;
  554. bool connected = 2;
  555. uint64 latency_ms = 3;
  556. uint64 lag_entries = 4;
  557. }
  558. message GetStatsRequest {
  559. }
  560. message GetStatsResponse {
  561. uint64 total_documents = 1;
  562. uint64 total_collections = 2;
  563. uint64 memory_used_bytes = 3;
  564. uint64 wal_sequence = 4;
  565. uint64 wal_size_bytes = 5;
  566. uint64 snapshot_count = 6;
  567. uint64 last_snapshot_sequence = 7;
  568. uint64 insert_count = 8;
  569. uint64 update_count = 9;
  570. uint64 delete_count = 10;
  571. uint64 query_count = 11;
  572. // Memory eviction stats
  573. uint64 evicted_documents = 12; // Documents currently evicted to disk
  574. uint64 total_evictions = 13; // Total eviction operations performed
  575. uint64 recovery_count = 14; // Documents recovered from eviction
  576. // Memory configuration
  577. uint64 max_memory_bytes = 15; // Configured max memory limit
  578. uint32 eviction_threshold_percent = 16; // Start evicting at this % of max
  579. uint32 eviction_target_percent = 17; // Evict down to this % of max
  580. // Operation timing (microseconds) - for performance monitoring
  581. uint64 get_count = 18; // Number of get operations
  582. uint64 get_total_micros = 19; // Total time spent in get operations
  583. uint64 get_max_micros = 20; // Max single get operation time
  584. uint64 insert_total_micros = 21;
  585. uint64 insert_max_micros = 22;
  586. uint64 update_total_micros = 23;
  587. uint64 update_max_micros = 24;
  588. uint64 query_total_micros = 25;
  589. uint64 query_max_micros = 26;
  590. }
  591. // ===== View Operations =====
  592. message ViewDefinition {
  593. string name = 1; // view name (globally unique, cannot start with _)
  594. string collection = 2; // target real collection (must NOT be another view)
  595. repeated string include = 3; // field paths to include (dot-notation for nested)
  596. repeated string exclude = 4; // field paths to exclude (ignored if include is non-empty)
  597. repeated Filter where = 5; // baked-in filters (AND-merged with caller filters)
  598. Sort default_sort = 6; // baked-in default sort (used when caller doesn't specify)
  599. uint64 created_at = 7;
  600. uint64 updated_at = 8;
  601. }
  602. message CreateViewRequest {
  603. string name = 1;
  604. string collection = 2;
  605. repeated string include = 3;
  606. repeated string exclude = 4;
  607. repeated Filter where = 5;
  608. Sort default_sort = 6;
  609. }
  610. message CreateViewResponse {
  611. bool success = 1;
  612. string error = 2;
  613. }
  614. message DropViewRequest {
  615. string name = 1;
  616. }
  617. message DropViewResponse {
  618. bool success = 1;
  619. string error = 2;
  620. }
  621. message ListViewsRequest {}
  622. message ListViewsResponse {
  623. repeated ViewDefinition views = 1;
  624. }
  625. message GetViewInfoRequest {
  626. string name = 1;
  627. }
  628. message GetViewInfoResponse {
  629. ViewDefinition view = 1;
  630. bool found = 2;
  631. }
  632. // ===== Collection Configuration =====
  633. // Per-collection configuration for timestamp precision and other runtime knobs.
  634. // Stored in the _collection_meta system collection.
  635. message CollectionConfig {
  636. // "ms" (default) — _created_at/_updated_at stamped in milliseconds since epoch
  637. // "ns" — _created_at/_updated_at stamped in nanoseconds since epoch
  638. string timestamp_precision = 1;
  639. // Room for future per-collection knobs
  640. }
  641. message ConfigureCollectionRequest {
  642. string collection = 1;
  643. CollectionConfig config = 2;
  644. }
  645. message ConfigureCollectionResponse {
  646. bool success = 1;
  647. string error = 2;
  648. }
  649. message GetCollectionConfigRequest {
  650. string collection = 1;
  651. }
  652. message GetCollectionConfigResponse {
  653. CollectionConfig config = 1;
  654. bool found = 2;
  655. }
  656. message MigrateCollectionTimestampsRequest {
  657. string collection = 1;
  658. string from_precision = 2; // "ms" or "ns"
  659. string to_precision = 3; // "ms" or "ns"
  660. }
  661. message MigrateCollectionTimestampsResponse {
  662. bool success = 1;
  663. string error = 2;
  664. uint64 rows_migrated = 3; // how many documents had their timestamps updated
  665. uint64 rows_skipped = 4; // already in target range (idempotent resume)
  666. }
  667. // ===== Read-Only Control =====
  668. message SetReadOnlyRequest {
  669. bool read_only = 1;
  670. }
  671. message SetReadOnlyResponse {
  672. bool success = 1;
  673. bool was_read_only = 2; // previous state
  674. string error = 3;
  675. }
  676. message GetReadOnlyStatusRequest {}
  677. message GetReadOnlyStatusResponse {
  678. bool read_only = 1;
  679. string reason = 2;
  680. // Recovery outcome details (populated when auto-readonly triggered)
  681. string recovery_outcome = 3; // "trivial_success" | "fresh_install" | "snapshot_fell_back" | "wal_only_replay" | "forced_empty" | "failed"
  682. string expected_snapshot = 4;
  683. string snapshot_used = 5;
  684. string failure_reason = 6;
  685. uint64 wal_entries_replayed = 7;
  686. uint32 snapshots_attempted = 8;
  687. }
  688. // ===== Memory Stats =====
  689. // Added in v1.7.0. Wire-compatible — server-side implementation lands in T9.
  690. message GetMemoryStatsRequest {}
  691. message CollectionMemoryStats {
  692. string collection = 1;
  693. uint64 document_count = 2;
  694. uint64 estimated_bytes = 3;
  695. uint64 evicted_stub_count = 4;
  696. MemoryPriority priority = 5;
  697. }
  698. message GetMemoryStatsResponse {
  699. uint64 total_memory_bytes = 1;
  700. uint64 max_memory_bytes = 2;
  701. uint32 pressure_percent = 3; // current usage / max × 100
  702. string pressure_level = 4; // "normal" | "soft" | "hard" | "emergency"
  703. repeated CollectionMemoryStats collections = 5;
  704. // Most recent eviction
  705. uint64 last_eviction_timestamp = 6; // ms since epoch, 0 if none
  706. uint64 last_eviction_docs = 7;
  707. uint64 last_eviction_bytes_freed = 8;
  708. }