database.proto 18 KB

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