database.proto 31 KB

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