database.proto 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. // Smartbotic Database Service
  2. // Document-oriented storage with persistence and replication
  3. syntax = "proto3";
  4. package smartbotic.databasepb;
  5. // Error code range: 6000-6999 for Database errors
  6. // 6000: Generic database error
  7. // 6001: Collection not found
  8. // 6002: Document not found
  9. // 6003: Document already exists
  10. // 6004: Version conflict
  11. // 6005: Invalid query
  12. // 6006: File not found
  13. // 6007: File too large
  14. // 6008: Encryption error
  15. // 6009: Replication error
  16. // 6010: Version not found
  17. // ===== Main Database Service =====
  18. service DatabaseService {
  19. // Document operations
  20. rpc Insert(InsertRequest) returns (InsertResponse);
  21. rpc Get(GetRequest) returns (GetResponse);
  22. rpc Update(UpdateRequest) returns (UpdateResponse);
  23. rpc Upsert(UpsertRequest) returns (UpsertResponse);
  24. rpc Delete(DeleteRequest) returns (DeleteResponse);
  25. rpc PatchDocument(PatchDocumentRequest) returns (PatchDocumentResponse);
  26. rpc Exists(ExistsRequest) returns (ExistsResponse);
  27. // Version history operations
  28. rpc GetVersionHistory(GetVersionHistoryRequest) returns (GetVersionHistoryResponse);
  29. rpc GetDocumentVersion(GetDocumentVersionRequest) returns (GetDocumentVersionResponse);
  30. rpc RestoreVersion(RestoreVersionRequest) returns (RestoreVersionResponse);
  31. rpc RestoreToDate(RestoreToDateRequest) returns (RestoreToDateResponse);
  32. // Batch operations
  33. rpc BatchInsert(BatchInsertRequest) returns (BatchInsertResponse);
  34. rpc BatchGet(BatchGetRequest) returns (BatchGetResponse);
  35. rpc BatchDelete(BatchDeleteRequest) returns (BatchDeleteResponse);
  36. // Query operations
  37. rpc Find(FindRequest) returns (FindResponse);
  38. rpc Count(CountRequest) returns (CountResponse);
  39. rpc SimilaritySearch(SimilaritySearchRequest) returns (SimilaritySearchResponse);
  40. // Set operations (Redis compatibility)
  41. rpc SetAdd(SetAddRequest) returns (SetAddResponse);
  42. rpc SetRemove(SetRemoveRequest) returns (SetRemoveResponse);
  43. rpc SetMembers(SetMembersRequest) returns (SetMembersResponse);
  44. rpc SetIsMember(SetIsMemberRequest) returns (SetIsMemberResponse);
  45. // Collection management
  46. rpc CreateCollection(CreateCollectionRequest) returns (CreateCollectionResponse);
  47. rpc DropCollection(DropCollectionRequest) returns (DropCollectionResponse);
  48. rpc ListCollections(ListCollectionsRequest) returns (ListCollectionsResponse);
  49. rpc GetCollectionInfo(GetCollectionInfoRequest) returns (GetCollectionInfoResponse);
  50. // View management
  51. rpc CreateView(CreateViewRequest) returns (CreateViewResponse);
  52. rpc DropView(DropViewRequest) returns (DropViewResponse);
  53. rpc ListViews(ListViewsRequest) returns (ListViewsResponse);
  54. rpc GetViewInfo(GetViewInfoRequest) returns (GetViewInfoResponse);
  55. // File operations
  56. rpc UploadFile(stream FileChunk) returns (UploadFileResponse);
  57. rpc DownloadFile(DownloadFileRequest) returns (stream FileChunk);
  58. rpc DeleteFile(DeleteFileRequest) returns (DeleteFileResponse);
  59. rpc GetFileInfo(GetFileInfoRequest) returns (FileInfo);
  60. rpc ListFiles(ListFilesRequest) returns (ListFilesResponse);
  61. // Event subscription
  62. rpc Subscribe(SubscribeRequest) returns (stream DatabaseEvent);
  63. // Health and stats
  64. rpc HealthCheck(HealthCheckRequest) returns (HealthCheckResponse);
  65. rpc GetStats(GetStatsRequest) returns (GetStatsResponse);
  66. }
  67. // ===== Replication Service =====
  68. service DatabaseReplication {
  69. // Bidirectional streaming for real-time sync
  70. rpc SyncStream(stream ReplicationMessage) returns (stream ReplicationMessage);
  71. // Pull missed entries (for recovery)
  72. rpc GetEntriesSince(GetEntriesRequest) returns (stream ReplicationEntry);
  73. // Get current node state
  74. rpc GetNodeState(GetNodeStateRequest) returns (NodeState);
  75. }
  76. // ===== Document Types =====
  77. message Document {
  78. string id = 1;
  79. string collection = 2;
  80. bytes data = 3; // JSON-encoded document data
  81. uint64 version = 4;
  82. uint64 created_at = 5; // Timestamp in milliseconds
  83. uint64 updated_at = 6;
  84. uint64 expires_at = 7; // 0 = no expiration
  85. string node_id = 8; // Origin node for replication
  86. bool encrypted = 9;
  87. repeated string encrypted_fields = 10;
  88. string created_by = 11; // User ID who created the document
  89. string updated_by = 12; // User ID who last updated the document
  90. }
  91. // ===== Document Operations =====
  92. message InsertRequest {
  93. string collection = 1;
  94. bytes data = 2; // JSON document
  95. string id = 3; // Optional, auto-generated if empty
  96. uint32 ttl_seconds = 4; // 0 = use collection default
  97. string actor = 5; // User ID performing the operation (for audit)
  98. }
  99. message InsertResponse {
  100. string id = 1;
  101. uint64 version = 2;
  102. }
  103. message GetRequest {
  104. string collection = 1;
  105. string id = 2;
  106. }
  107. message GetResponse {
  108. Document document = 1;
  109. bool found = 2;
  110. }
  111. message UpdateRequest {
  112. string collection = 1;
  113. string id = 2;
  114. bytes data = 3;
  115. uint64 expected_version = 4; // 0 = no version check (force update)
  116. string actor = 5; // User ID performing the operation (for audit)
  117. }
  118. message UpdateResponse {
  119. uint64 new_version = 1;
  120. bool success = 2;
  121. string error = 3;
  122. }
  123. message UpsertRequest {
  124. string collection = 1;
  125. bytes data = 2;
  126. string id = 3;
  127. uint32 ttl_seconds = 4;
  128. string actor = 5; // User ID performing the operation (for audit)
  129. }
  130. message UpsertResponse {
  131. string id = 1;
  132. uint64 version = 2;
  133. bool inserted = 3; // true if inserted, false if updated
  134. }
  135. message DeleteRequest {
  136. string collection = 1;
  137. string id = 2;
  138. }
  139. message DeleteResponse {
  140. bool deleted = 1;
  141. }
  142. message PatchDocumentRequest {
  143. string collection = 1;
  144. string id = 2;
  145. bytes patch_json = 3; // JSON fields to merge into existing document
  146. string actor = 4; // User ID performing the operation (for audit)
  147. }
  148. message PatchDocumentResponse {
  149. uint64 new_version = 1;
  150. bool success = 2;
  151. string error = 3;
  152. }
  153. message ExistsRequest {
  154. string collection = 1;
  155. string id = 2;
  156. }
  157. message ExistsResponse {
  158. bool exists = 1;
  159. }
  160. // ===== Version History Operations =====
  161. message DocumentVersionEntry {
  162. uint64 version = 1;
  163. bytes data = 2; // JSON-encoded document data at this version
  164. uint64 timestamp = 3; // When this version was created (updatedAt)
  165. string updated_by = 4;
  166. bool encrypted = 5;
  167. repeated string encrypted_fields = 6;
  168. }
  169. message GetVersionHistoryRequest {
  170. string collection = 1;
  171. string id = 2;
  172. uint32 limit = 3; // 0 = all versions
  173. uint32 offset = 4;
  174. }
  175. message GetVersionHistoryResponse {
  176. repeated DocumentVersionEntry versions = 1;
  177. uint64 current_version = 2; // 0 if document is deleted
  178. uint64 total_count = 3;
  179. bool document_deleted = 4;
  180. }
  181. message GetDocumentVersionRequest {
  182. string collection = 1;
  183. string id = 2;
  184. uint64 version = 3;
  185. }
  186. message GetDocumentVersionResponse {
  187. DocumentVersionEntry version_entry = 1;
  188. bool found = 2;
  189. }
  190. message RestoreVersionRequest {
  191. string collection = 1;
  192. string id = 2;
  193. uint64 version = 3; // Version number to restore
  194. string actor = 4; // User performing the restore
  195. }
  196. message RestoreVersionResponse {
  197. uint64 new_version = 1; // The newly created version number
  198. bool success = 2;
  199. string error = 3;
  200. }
  201. message RestoreToDateRequest {
  202. string collection = 1;
  203. string id = 2;
  204. uint64 timestamp = 3; // Find version active at this time (ms since epoch)
  205. string actor = 4;
  206. }
  207. message RestoreToDateResponse {
  208. uint64 restored_version = 1; // Which old version was restored from
  209. uint64 new_version = 2; // The newly created version number
  210. bool success = 3;
  211. string error = 4;
  212. }
  213. // ===== Batch Operations =====
  214. message BatchInsertRequest {
  215. string collection = 1;
  216. repeated BatchInsertItem items = 2;
  217. string actor = 3; // User ID performing the operation (for audit)
  218. }
  219. message BatchInsertItem {
  220. bytes data = 1;
  221. string id = 2;
  222. uint32 ttl_seconds = 3;
  223. }
  224. message BatchInsertResponse {
  225. repeated string ids = 1;
  226. uint32 success_count = 2;
  227. uint32 error_count = 3;
  228. }
  229. message BatchGetRequest {
  230. string collection = 1;
  231. repeated string ids = 2;
  232. }
  233. message BatchGetResponse {
  234. repeated Document documents = 1;
  235. }
  236. message BatchDeleteRequest {
  237. string collection = 1;
  238. repeated string ids = 2;
  239. }
  240. message BatchDeleteResponse {
  241. uint64 deleted_count = 1;
  242. }
  243. // ===== Query Operations =====
  244. message FindRequest {
  245. string collection = 1;
  246. repeated Filter filters = 2;
  247. Sort sort = 3;
  248. uint32 limit = 4; // Default 100
  249. uint32 offset = 5;
  250. repeated string projection = 6; // Fields to include (empty = all)
  251. }
  252. message Filter {
  253. string field = 1;
  254. FilterOp op = 2;
  255. bytes value = 3; // JSON-encoded value
  256. }
  257. enum FilterOp {
  258. FILTER_OP_UNSPECIFIED = 0;
  259. FILTER_OP_EQ = 1;
  260. FILTER_OP_NE = 2;
  261. FILTER_OP_GT = 3;
  262. FILTER_OP_GTE = 4;
  263. FILTER_OP_LT = 5;
  264. FILTER_OP_LTE = 6;
  265. FILTER_OP_IN = 7;
  266. FILTER_OP_CONTAINS = 8;
  267. FILTER_OP_EXISTS = 9;
  268. FILTER_OP_REGEX = 10;
  269. FILTER_OP_SEARCH = 11; // Full-text search across ID and string fields
  270. }
  271. message Sort {
  272. string field = 1;
  273. bool descending = 2;
  274. }
  275. message FindResponse {
  276. repeated Document documents = 1;
  277. uint64 total_count = 2;
  278. bool has_more = 3;
  279. // WAL fallback metrics (for queries that include evicted documents)
  280. bool used_wal_fallback = 4; // True if WAL was scanned for evicted docs
  281. uint32 memory_match_count = 5; // Documents matched from memory
  282. uint32 wal_match_count = 6; // Documents matched from WAL
  283. uint64 memory_search_micros = 7; // Time spent searching memory (µs)
  284. uint64 wal_search_micros = 8; // Time spent loading/searching WAL (µs)
  285. }
  286. message CountRequest {
  287. string collection = 1;
  288. repeated Filter filters = 2;
  289. }
  290. message CountResponse {
  291. uint64 count = 1;
  292. }
  293. message SimilaritySearchRequest {
  294. string collection = 1;
  295. repeated float query_vector = 2;
  296. uint32 top_k = 3;
  297. float min_score = 4;
  298. }
  299. message SimilaritySearchResponse {
  300. repeated SimilarityResult results = 1;
  301. }
  302. message SimilarityResult {
  303. string id = 1;
  304. float score = 2;
  305. bytes data = 3;
  306. }
  307. // ===== Set Operations =====
  308. message SetAddRequest {
  309. string collection = 1;
  310. string set_id = 2;
  311. string member = 3;
  312. }
  313. message SetAddResponse {
  314. bool added = 1; // false if already exists
  315. }
  316. message SetRemoveRequest {
  317. string collection = 1;
  318. string set_id = 2;
  319. string member = 3;
  320. }
  321. message SetRemoveResponse {
  322. bool removed = 1;
  323. }
  324. message SetMembersRequest {
  325. string collection = 1;
  326. string set_id = 2;
  327. }
  328. message SetMembersResponse {
  329. repeated string members = 1;
  330. }
  331. message SetIsMemberRequest {
  332. string collection = 1;
  333. string set_id = 2;
  334. string member = 3;
  335. }
  336. message SetIsMemberResponse {
  337. bool is_member = 1;
  338. }
  339. // ===== Collection Management =====
  340. message CreateCollectionRequest {
  341. string name = 1;
  342. CollectionOptions options = 2;
  343. }
  344. message CollectionOptions {
  345. bool auto_create_id = 1;
  346. uint32 default_ttl_seconds = 2;
  347. bool encrypted = 3;
  348. repeated string sensitive_fields = 4;
  349. uint32 max_versions = 5; // Max version history per document (0 = unlimited)
  350. uint32 vector_dimension = 6; // Dimension of vectors stored in this collection (0 = not a vector collection)
  351. }
  352. message CreateCollectionResponse {
  353. bool created = 1;
  354. }
  355. message DropCollectionRequest {
  356. string name = 1;
  357. }
  358. message DropCollectionResponse {
  359. bool dropped = 1;
  360. }
  361. message ListCollectionsRequest {
  362. }
  363. message ListCollectionsResponse {
  364. repeated string names = 1;
  365. }
  366. message GetCollectionInfoRequest {
  367. string name = 1;
  368. }
  369. message GetCollectionInfoResponse {
  370. CollectionInfo info = 1;
  371. bool found = 2;
  372. }
  373. message CollectionInfo {
  374. string name = 1;
  375. uint64 document_count = 2;
  376. uint64 size_bytes = 3;
  377. CollectionOptions options = 4;
  378. uint64 created_at = 5;
  379. uint64 updated_at = 6;
  380. uint32 vector_dimension = 7; // Dimension of vectors stored in this collection (0 = not a vector collection)
  381. }
  382. // ===== File Operations =====
  383. message FileChunk {
  384. oneof content {
  385. FileMetadata metadata = 1; // First chunk includes metadata
  386. bytes data = 2; // Subsequent chunks are data
  387. }
  388. }
  389. message FileMetadata {
  390. string id = 1; // Set by server on upload response
  391. string name = 2;
  392. string mime_type = 3;
  393. uint64 size = 4;
  394. string file_type = 5; // "plugin", "document", "generated"
  395. string related_id = 6; // plugin_id, conversation_id, etc.
  396. map<string, string> metadata = 7; // Custom metadata
  397. bool is_public = 8; // Visibility flag
  398. }
  399. message UploadFileResponse {
  400. string id = 1;
  401. uint64 size = 2;
  402. string checksum = 3; // SHA-256
  403. bool deduplicated = 4; // True if blob already existed
  404. }
  405. message DownloadFileRequest {
  406. string id = 1;
  407. }
  408. message DeleteFileRequest {
  409. string id = 1;
  410. }
  411. message DeleteFileResponse {
  412. bool deleted = 1;
  413. }
  414. message GetFileInfoRequest {
  415. string id = 1;
  416. }
  417. message FileInfo {
  418. string id = 1;
  419. string name = 2;
  420. string mime_type = 3;
  421. uint64 size = 4;
  422. string file_type = 5;
  423. string related_id = 6;
  424. string checksum = 7;
  425. uint64 created_at = 8;
  426. map<string, string> metadata = 9;
  427. bool is_public = 10; // Visibility flag
  428. uint32 ref_count = 11; // Blob reference count (informational)
  429. }
  430. message ListFilesRequest {
  431. string file_type = 1; // Filter by type (optional)
  432. string related_id = 2; // Filter by related ID (optional)
  433. uint32 limit = 3;
  434. uint32 offset = 4;
  435. string checksum = 5; // Filter by checksum (for dedup queries)
  436. string name = 6; // Filter by exact filename (optional)
  437. }
  438. message ListFilesResponse {
  439. repeated FileInfo files = 1;
  440. uint64 total_count = 2;
  441. bool has_more = 3;
  442. }
  443. // ===== Event Subscription =====
  444. message SubscribeRequest {
  445. repeated string collections = 1; // Empty = all collections
  446. repeated string patterns = 2; // Glob patterns (e.g., "assistants:*")
  447. bool include_data = 3; // Include document data in events
  448. }
  449. message DatabaseEvent {
  450. EventType type = 1;
  451. string collection = 2;
  452. string document_id = 3;
  453. uint64 timestamp = 4;
  454. string node_id = 5;
  455. bytes data = 6; // JSON document (if include_data)
  456. }
  457. enum EventType {
  458. EVENT_UNKNOWN = 0;
  459. EVENT_INSERT = 1;
  460. EVENT_UPDATE = 2;
  461. EVENT_DELETE = 3;
  462. EVENT_EXPIRE = 4;
  463. EVENT_INVALIDATE = 5;
  464. }
  465. // ===== Replication =====
  466. message ReplicationEntry {
  467. uint64 sequence = 1;
  468. uint64 global_timestamp = 2;
  469. string node_id = 3;
  470. OperationType op = 4;
  471. string collection = 5;
  472. string document_id = 6;
  473. uint64 document_version = 7;
  474. bytes data = 8;
  475. }
  476. enum OperationType {
  477. OP_UNKNOWN = 0;
  478. OP_INSERT = 1;
  479. OP_UPDATE = 2;
  480. OP_DELETE = 3;
  481. OP_UPSERT = 4;
  482. OP_CREATE_COLLECTION = 5;
  483. OP_DROP_COLLECTION = 6;
  484. }
  485. message ReplicationMessage {
  486. oneof content {
  487. ReplicationEntry entry = 1;
  488. Heartbeat heartbeat = 2;
  489. WatermarkUpdate watermark = 3;
  490. SyncRequest sync_request = 4;
  491. }
  492. }
  493. message Heartbeat {
  494. string node_id = 1;
  495. uint64 timestamp = 2;
  496. uint64 sequence = 3;
  497. }
  498. message WatermarkUpdate {
  499. string node_id = 1;
  500. uint64 sequence = 2;
  501. }
  502. message SyncRequest {
  503. uint64 from_sequence = 1;
  504. }
  505. message GetEntriesRequest {
  506. uint64 from_sequence = 1;
  507. uint32 limit = 2;
  508. }
  509. message GetNodeStateRequest {
  510. }
  511. message NodeState {
  512. string node_id = 1;
  513. uint64 current_sequence = 2;
  514. map<string, uint64> peer_watermarks = 3;
  515. uint64 document_count = 4;
  516. repeated string collections = 5;
  517. bool healthy = 6;
  518. // Per-collection sequence tracking for collection discovery
  519. map<string, uint64> collection_sequences = 7;
  520. }
  521. // ===== Health and Stats =====
  522. message HealthCheckRequest {
  523. }
  524. message HealthCheckResponse {
  525. bool healthy = 1;
  526. uint64 uptime_seconds = 2;
  527. uint64 document_count = 3;
  528. uint64 memory_used_bytes = 4;
  529. uint64 wal_size_bytes = 5;
  530. repeated PeerHealth peers = 6;
  531. }
  532. message PeerHealth {
  533. string node_id = 1;
  534. bool connected = 2;
  535. uint64 latency_ms = 3;
  536. uint64 lag_entries = 4;
  537. }
  538. message GetStatsRequest {
  539. }
  540. message GetStatsResponse {
  541. uint64 total_documents = 1;
  542. uint64 total_collections = 2;
  543. uint64 memory_used_bytes = 3;
  544. uint64 wal_sequence = 4;
  545. uint64 wal_size_bytes = 5;
  546. uint64 snapshot_count = 6;
  547. uint64 last_snapshot_sequence = 7;
  548. uint64 insert_count = 8;
  549. uint64 update_count = 9;
  550. uint64 delete_count = 10;
  551. uint64 query_count = 11;
  552. // Memory eviction stats
  553. uint64 evicted_documents = 12; // Documents currently evicted to disk
  554. uint64 total_evictions = 13; // Total eviction operations performed
  555. uint64 recovery_count = 14; // Documents recovered from eviction
  556. // Memory configuration
  557. uint64 max_memory_bytes = 15; // Configured max memory limit
  558. uint32 eviction_threshold_percent = 16; // Start evicting at this % of max
  559. uint32 eviction_target_percent = 17; // Evict down to this % of max
  560. // Operation timing (microseconds) - for performance monitoring
  561. uint64 get_count = 18; // Number of get operations
  562. uint64 get_total_micros = 19; // Total time spent in get operations
  563. uint64 get_max_micros = 20; // Max single get operation time
  564. uint64 insert_total_micros = 21;
  565. uint64 insert_max_micros = 22;
  566. uint64 update_total_micros = 23;
  567. uint64 update_max_micros = 24;
  568. uint64 query_total_micros = 25;
  569. uint64 query_max_micros = 26;
  570. }
  571. // ===== View Operations =====
  572. message ViewDefinition {
  573. string name = 1; // view name (globally unique, cannot start with _)
  574. string collection = 2; // target real collection (must NOT be another view)
  575. repeated string include = 3; // field paths to include (dot-notation for nested)
  576. repeated string exclude = 4; // field paths to exclude (ignored if include is non-empty)
  577. repeated Filter where = 5; // baked-in filters (AND-merged with caller filters)
  578. Sort default_sort = 6; // baked-in default sort (used when caller doesn't specify)
  579. uint64 created_at = 7;
  580. uint64 updated_at = 8;
  581. }
  582. message CreateViewRequest {
  583. string name = 1;
  584. string collection = 2;
  585. repeated string include = 3;
  586. repeated string exclude = 4;
  587. repeated Filter where = 5;
  588. Sort default_sort = 6;
  589. }
  590. message CreateViewResponse {
  591. bool success = 1;
  592. string error = 2;
  593. }
  594. message DropViewRequest {
  595. string name = 1;
  596. }
  597. message DropViewResponse {
  598. bool success = 1;
  599. string error = 2;
  600. }
  601. message ListViewsRequest {}
  602. message ListViewsResponse {
  603. repeated ViewDefinition views = 1;
  604. }
  605. message GetViewInfoRequest {
  606. string name = 1;
  607. }
  608. message GetViewInfoResponse {
  609. ViewDefinition view = 1;
  610. bool found = 2;
  611. }