database.proto 29 KB

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