database.proto 31 KB

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