database.proto 17 KB

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