database.proto 26 KB

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