database.proto 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. }
  262. message CountRequest {
  263. string collection = 1;
  264. repeated Filter filters = 2;
  265. }
  266. message CountResponse {
  267. uint64 count = 1;
  268. }
  269. // ===== Set Operations =====
  270. message SetAddRequest {
  271. string collection = 1;
  272. string set_id = 2;
  273. string member = 3;
  274. }
  275. message SetAddResponse {
  276. bool added = 1; // false if already exists
  277. }
  278. message SetRemoveRequest {
  279. string collection = 1;
  280. string set_id = 2;
  281. string member = 3;
  282. }
  283. message SetRemoveResponse {
  284. bool removed = 1;
  285. }
  286. message SetMembersRequest {
  287. string collection = 1;
  288. string set_id = 2;
  289. }
  290. message SetMembersResponse {
  291. repeated string members = 1;
  292. }
  293. message SetIsMemberRequest {
  294. string collection = 1;
  295. string set_id = 2;
  296. string member = 3;
  297. }
  298. message SetIsMemberResponse {
  299. bool is_member = 1;
  300. }
  301. // ===== Collection Management =====
  302. message CreateCollectionRequest {
  303. string name = 1;
  304. CollectionOptions options = 2;
  305. }
  306. message CollectionOptions {
  307. bool auto_create_id = 1;
  308. uint32 default_ttl_seconds = 2;
  309. bool encrypted = 3;
  310. repeated string sensitive_fields = 4;
  311. uint32 max_versions = 5; // Max version history per document (0 = unlimited)
  312. }
  313. message CreateCollectionResponse {
  314. bool created = 1;
  315. }
  316. message DropCollectionRequest {
  317. string name = 1;
  318. }
  319. message DropCollectionResponse {
  320. bool dropped = 1;
  321. }
  322. message ListCollectionsRequest {
  323. }
  324. message ListCollectionsResponse {
  325. repeated string names = 1;
  326. }
  327. message GetCollectionInfoRequest {
  328. string name = 1;
  329. }
  330. message GetCollectionInfoResponse {
  331. CollectionInfo info = 1;
  332. bool found = 2;
  333. }
  334. message CollectionInfo {
  335. string name = 1;
  336. uint64 document_count = 2;
  337. uint64 size_bytes = 3;
  338. CollectionOptions options = 4;
  339. uint64 created_at = 5;
  340. uint64 updated_at = 6;
  341. }
  342. // ===== File Operations =====
  343. message FileChunk {
  344. oneof content {
  345. FileMetadata metadata = 1; // First chunk includes metadata
  346. bytes data = 2; // Subsequent chunks are data
  347. }
  348. }
  349. message FileMetadata {
  350. string id = 1; // Set by server on upload response
  351. string name = 2;
  352. string mime_type = 3;
  353. uint64 size = 4;
  354. string file_type = 5; // audio/recording, pcap, upload
  355. string related_id = 6; // e.g., call_id for recordings
  356. map<string, string> metadata = 7; // Custom metadata
  357. }
  358. message UploadFileResponse {
  359. string id = 1;
  360. uint64 size = 2;
  361. string checksum = 3; // SHA-256
  362. }
  363. message DownloadFileRequest {
  364. string id = 1;
  365. }
  366. message DeleteFileRequest {
  367. string id = 1;
  368. }
  369. message DeleteFileResponse {
  370. bool deleted = 1;
  371. }
  372. message GetFileInfoRequest {
  373. string id = 1;
  374. }
  375. message FileInfo {
  376. string id = 1;
  377. string name = 2;
  378. string mime_type = 3;
  379. uint64 size = 4;
  380. string file_type = 5;
  381. string related_id = 6;
  382. string checksum = 7;
  383. uint64 created_at = 8;
  384. map<string, string> metadata = 9;
  385. }
  386. message ListFilesRequest {
  387. string file_type = 1; // Filter by type (optional)
  388. string related_id = 2; // Filter by related ID (optional)
  389. uint32 limit = 3;
  390. uint32 offset = 4;
  391. }
  392. message ListFilesResponse {
  393. repeated FileInfo files = 1;
  394. uint64 total_count = 2;
  395. bool has_more = 3;
  396. }
  397. // ===== Event Subscription =====
  398. message SubscribeRequest {
  399. repeated string collections = 1; // Empty = all collections
  400. repeated string patterns = 2; // Glob patterns (e.g., "assistants:*")
  401. bool include_data = 3; // Include document data in events
  402. }
  403. message DatabaseEvent {
  404. EventType type = 1;
  405. string collection = 2;
  406. string document_id = 3;
  407. uint64 timestamp = 4;
  408. string node_id = 5;
  409. bytes data = 6; // JSON document (if include_data)
  410. }
  411. enum EventType {
  412. EVENT_UNKNOWN = 0;
  413. EVENT_INSERT = 1;
  414. EVENT_UPDATE = 2;
  415. EVENT_DELETE = 3;
  416. EVENT_EXPIRE = 4;
  417. EVENT_INVALIDATE = 5;
  418. }
  419. // ===== Replication =====
  420. message ReplicationEntry {
  421. uint64 sequence = 1;
  422. uint64 global_timestamp = 2;
  423. string node_id = 3;
  424. OperationType op = 4;
  425. string collection = 5;
  426. string document_id = 6;
  427. uint64 document_version = 7;
  428. bytes data = 8;
  429. }
  430. enum OperationType {
  431. OP_UNKNOWN = 0;
  432. OP_INSERT = 1;
  433. OP_UPDATE = 2;
  434. OP_DELETE = 3;
  435. OP_UPSERT = 4;
  436. OP_CREATE_COLLECTION = 5;
  437. OP_DROP_COLLECTION = 6;
  438. }
  439. message ReplicationMessage {
  440. oneof content {
  441. ReplicationEntry entry = 1;
  442. Heartbeat heartbeat = 2;
  443. WatermarkUpdate watermark = 3;
  444. SyncRequest sync_request = 4;
  445. }
  446. }
  447. message Heartbeat {
  448. string node_id = 1;
  449. uint64 timestamp = 2;
  450. uint64 sequence = 3;
  451. }
  452. message WatermarkUpdate {
  453. string node_id = 1;
  454. uint64 sequence = 2;
  455. }
  456. message SyncRequest {
  457. uint64 from_sequence = 1;
  458. }
  459. message GetEntriesRequest {
  460. uint64 from_sequence = 1;
  461. uint32 limit = 2;
  462. }
  463. message GetNodeStateRequest {
  464. }
  465. message NodeState {
  466. string node_id = 1;
  467. uint64 current_sequence = 2;
  468. map<string, uint64> peer_watermarks = 3;
  469. uint64 document_count = 4;
  470. repeated string collections = 5;
  471. bool healthy = 6;
  472. // Per-collection sequence tracking for collection discovery
  473. map<string, uint64> collection_sequences = 7;
  474. }
  475. // ===== Health and Stats =====
  476. message HealthCheckRequest {
  477. }
  478. message HealthCheckResponse {
  479. bool healthy = 1;
  480. uint64 uptime_seconds = 2;
  481. uint64 document_count = 3;
  482. uint64 memory_used_bytes = 4;
  483. uint64 wal_size_bytes = 5;
  484. repeated PeerHealth peers = 6;
  485. }
  486. message PeerHealth {
  487. string node_id = 1;
  488. bool connected = 2;
  489. uint64 latency_ms = 3;
  490. uint64 lag_entries = 4;
  491. }
  492. message GetStatsRequest {
  493. }
  494. message GetStatsResponse {
  495. uint64 total_documents = 1;
  496. uint64 total_collections = 2;
  497. uint64 memory_used_bytes = 3;
  498. uint64 wal_sequence = 4;
  499. uint64 wal_size_bytes = 5;
  500. uint64 snapshot_count = 6;
  501. uint64 last_snapshot_sequence = 7;
  502. uint64 insert_count = 8;
  503. uint64 update_count = 9;
  504. uint64 delete_count = 10;
  505. uint64 query_count = 11;
  506. // Memory eviction stats
  507. uint64 evicted_documents = 12; // Documents currently evicted to disk
  508. uint64 total_evictions = 13; // Total eviction operations performed
  509. uint64 recovery_count = 14; // Documents recovered from eviction
  510. // Memory configuration
  511. uint64 max_memory_bytes = 15; // Configured max memory limit
  512. uint32 eviction_threshold_percent = 16; // Start evicting at this % of max
  513. uint32 eviction_target_percent = 17; // Evict down to this % of max
  514. }