database.proto 16 KB

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