database.proto 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. // ===== Main Database Service =====
  17. service DatabaseService {
  18. // Document operations
  19. rpc Insert(InsertRequest) returns (InsertResponse);
  20. rpc Get(GetRequest) returns (GetResponse);
  21. rpc Update(UpdateRequest) returns (UpdateResponse);
  22. rpc Upsert(UpsertRequest) returns (UpsertResponse);
  23. rpc Delete(DeleteRequest) returns (DeleteResponse);
  24. rpc Exists(ExistsRequest) returns (ExistsResponse);
  25. // Batch operations
  26. rpc BatchInsert(BatchInsertRequest) returns (BatchInsertResponse);
  27. rpc BatchGet(BatchGetRequest) returns (BatchGetResponse);
  28. rpc BatchDelete(BatchDeleteRequest) returns (BatchDeleteResponse);
  29. // Query operations
  30. rpc Find(FindRequest) returns (FindResponse);
  31. rpc Count(CountRequest) returns (CountResponse);
  32. // Set operations (Redis compatibility)
  33. rpc SetAdd(SetAddRequest) returns (SetAddResponse);
  34. rpc SetRemove(SetRemoveRequest) returns (SetRemoveResponse);
  35. rpc SetMembers(SetMembersRequest) returns (SetMembersResponse);
  36. rpc SetIsMember(SetIsMemberRequest) returns (SetIsMemberResponse);
  37. // Collection management
  38. rpc CreateCollection(CreateCollectionRequest) returns (CreateCollectionResponse);
  39. rpc DropCollection(DropCollectionRequest) returns (DropCollectionResponse);
  40. rpc ListCollections(ListCollectionsRequest) returns (ListCollectionsResponse);
  41. rpc GetCollectionInfo(GetCollectionInfoRequest) returns (GetCollectionInfoResponse);
  42. // File operations
  43. rpc UploadFile(stream FileChunk) returns (UploadFileResponse);
  44. rpc DownloadFile(DownloadFileRequest) returns (stream FileChunk);
  45. rpc DeleteFile(DeleteFileRequest) returns (DeleteFileResponse);
  46. rpc GetFileInfo(GetFileInfoRequest) returns (FileInfo);
  47. rpc ListFiles(ListFilesRequest) returns (ListFilesResponse);
  48. // Event subscription
  49. rpc Subscribe(SubscribeRequest) returns (stream DatabaseEvent);
  50. // Health and stats
  51. rpc HealthCheck(HealthCheckRequest) returns (HealthCheckResponse);
  52. rpc GetStats(GetStatsRequest) returns (GetStatsResponse);
  53. }
  54. // ===== Replication Service =====
  55. service DatabaseReplication {
  56. // Bidirectional streaming for real-time sync
  57. rpc SyncStream(stream ReplicationMessage) returns (stream ReplicationMessage);
  58. // Pull missed entries (for recovery)
  59. rpc GetEntriesSince(GetEntriesRequest) returns (stream ReplicationEntry);
  60. // Get current node state
  61. rpc GetNodeState(GetNodeStateRequest) returns (NodeState);
  62. }
  63. // ===== Document Types =====
  64. message Document {
  65. string id = 1;
  66. string collection = 2;
  67. bytes data = 3; // JSON-encoded document data
  68. uint64 version = 4;
  69. uint64 created_at = 5; // Timestamp in milliseconds
  70. uint64 updated_at = 6;
  71. uint64 expires_at = 7; // 0 = no expiration
  72. string node_id = 8; // Origin node for replication
  73. bool encrypted = 9;
  74. repeated string encrypted_fields = 10;
  75. string created_by = 11; // User ID who created the document
  76. string updated_by = 12; // User ID who last updated the document
  77. }
  78. // ===== Document Operations =====
  79. message InsertRequest {
  80. string collection = 1;
  81. bytes data = 2; // JSON document
  82. string id = 3; // Optional, auto-generated if empty
  83. uint32 ttl_seconds = 4; // 0 = use collection default
  84. string actor = 5; // User ID performing the operation (for audit)
  85. }
  86. message InsertResponse {
  87. string id = 1;
  88. uint64 version = 2;
  89. }
  90. message GetRequest {
  91. string collection = 1;
  92. string id = 2;
  93. }
  94. message GetResponse {
  95. Document document = 1;
  96. bool found = 2;
  97. }
  98. message UpdateRequest {
  99. string collection = 1;
  100. string id = 2;
  101. bytes data = 3;
  102. uint64 expected_version = 4; // 0 = no version check (force update)
  103. string actor = 5; // User ID performing the operation (for audit)
  104. }
  105. message UpdateResponse {
  106. uint64 new_version = 1;
  107. bool success = 2;
  108. string error = 3;
  109. }
  110. message UpsertRequest {
  111. string collection = 1;
  112. bytes data = 2;
  113. string id = 3;
  114. uint32 ttl_seconds = 4;
  115. string actor = 5; // User ID performing the operation (for audit)
  116. }
  117. message UpsertResponse {
  118. string id = 1;
  119. uint64 version = 2;
  120. bool inserted = 3; // true if inserted, false if updated
  121. }
  122. message DeleteRequest {
  123. string collection = 1;
  124. string id = 2;
  125. }
  126. message DeleteResponse {
  127. bool deleted = 1;
  128. }
  129. message ExistsRequest {
  130. string collection = 1;
  131. string id = 2;
  132. }
  133. message ExistsResponse {
  134. bool exists = 1;
  135. }
  136. // ===== Batch Operations =====
  137. message BatchInsertRequest {
  138. string collection = 1;
  139. repeated BatchInsertItem items = 2;
  140. string actor = 3; // User ID performing the operation (for audit)
  141. }
  142. message BatchInsertItem {
  143. bytes data = 1;
  144. string id = 2;
  145. uint32 ttl_seconds = 3;
  146. }
  147. message BatchInsertResponse {
  148. repeated string ids = 1;
  149. uint32 success_count = 2;
  150. uint32 error_count = 3;
  151. }
  152. message BatchGetRequest {
  153. string collection = 1;
  154. repeated string ids = 2;
  155. }
  156. message BatchGetResponse {
  157. repeated Document documents = 1;
  158. }
  159. message BatchDeleteRequest {
  160. string collection = 1;
  161. repeated string ids = 2;
  162. }
  163. message BatchDeleteResponse {
  164. uint64 deleted_count = 1;
  165. }
  166. // ===== Query Operations =====
  167. message FindRequest {
  168. string collection = 1;
  169. repeated Filter filters = 2;
  170. Sort sort = 3;
  171. uint32 limit = 4; // Default 100
  172. uint32 offset = 5;
  173. repeated string projection = 6; // Fields to include (empty = all)
  174. }
  175. message Filter {
  176. string field = 1;
  177. FilterOp op = 2;
  178. bytes value = 3; // JSON-encoded value
  179. }
  180. enum FilterOp {
  181. FILTER_OP_UNSPECIFIED = 0;
  182. FILTER_OP_EQ = 1;
  183. FILTER_OP_NE = 2;
  184. FILTER_OP_GT = 3;
  185. FILTER_OP_GTE = 4;
  186. FILTER_OP_LT = 5;
  187. FILTER_OP_LTE = 6;
  188. FILTER_OP_IN = 7;
  189. FILTER_OP_CONTAINS = 8;
  190. FILTER_OP_EXISTS = 9;
  191. FILTER_OP_REGEX = 10;
  192. FILTER_OP_SEARCH = 11; // Full-text search across ID and string fields
  193. }
  194. message Sort {
  195. string field = 1;
  196. bool descending = 2;
  197. }
  198. message FindResponse {
  199. repeated Document documents = 1;
  200. uint64 total_count = 2;
  201. bool has_more = 3;
  202. }
  203. message CountRequest {
  204. string collection = 1;
  205. repeated Filter filters = 2;
  206. }
  207. message CountResponse {
  208. uint64 count = 1;
  209. }
  210. // ===== Set Operations =====
  211. message SetAddRequest {
  212. string collection = 1;
  213. string set_id = 2;
  214. string member = 3;
  215. }
  216. message SetAddResponse {
  217. bool added = 1; // false if already exists
  218. }
  219. message SetRemoveRequest {
  220. string collection = 1;
  221. string set_id = 2;
  222. string member = 3;
  223. }
  224. message SetRemoveResponse {
  225. bool removed = 1;
  226. }
  227. message SetMembersRequest {
  228. string collection = 1;
  229. string set_id = 2;
  230. }
  231. message SetMembersResponse {
  232. repeated string members = 1;
  233. }
  234. message SetIsMemberRequest {
  235. string collection = 1;
  236. string set_id = 2;
  237. string member = 3;
  238. }
  239. message SetIsMemberResponse {
  240. bool is_member = 1;
  241. }
  242. // ===== Collection Management =====
  243. message CreateCollectionRequest {
  244. string name = 1;
  245. CollectionOptions options = 2;
  246. }
  247. message CollectionOptions {
  248. bool auto_create_id = 1;
  249. uint32 default_ttl_seconds = 2;
  250. bool encrypted = 3;
  251. repeated string sensitive_fields = 4;
  252. }
  253. message CreateCollectionResponse {
  254. bool created = 1;
  255. }
  256. message DropCollectionRequest {
  257. string name = 1;
  258. }
  259. message DropCollectionResponse {
  260. bool dropped = 1;
  261. }
  262. message ListCollectionsRequest {
  263. }
  264. message ListCollectionsResponse {
  265. repeated string names = 1;
  266. }
  267. message GetCollectionInfoRequest {
  268. string name = 1;
  269. }
  270. message GetCollectionInfoResponse {
  271. CollectionInfo info = 1;
  272. bool found = 2;
  273. }
  274. message CollectionInfo {
  275. string name = 1;
  276. uint64 document_count = 2;
  277. uint64 size_bytes = 3;
  278. CollectionOptions options = 4;
  279. uint64 created_at = 5;
  280. uint64 updated_at = 6;
  281. }
  282. // ===== File Operations =====
  283. message FileChunk {
  284. oneof content {
  285. FileMetadata metadata = 1; // First chunk includes metadata
  286. bytes data = 2; // Subsequent chunks are data
  287. }
  288. }
  289. message FileMetadata {
  290. string id = 1; // Set by server on upload response
  291. string name = 2;
  292. string mime_type = 3;
  293. uint64 size = 4;
  294. string file_type = 5; // audio/recording, pcap, upload
  295. string related_id = 6; // e.g., call_id for recordings
  296. map<string, string> metadata = 7; // Custom metadata
  297. }
  298. message UploadFileResponse {
  299. string id = 1;
  300. uint64 size = 2;
  301. string checksum = 3; // SHA-256
  302. }
  303. message DownloadFileRequest {
  304. string id = 1;
  305. }
  306. message DeleteFileRequest {
  307. string id = 1;
  308. }
  309. message DeleteFileResponse {
  310. bool deleted = 1;
  311. }
  312. message GetFileInfoRequest {
  313. string id = 1;
  314. }
  315. message FileInfo {
  316. string id = 1;
  317. string name = 2;
  318. string mime_type = 3;
  319. uint64 size = 4;
  320. string file_type = 5;
  321. string related_id = 6;
  322. string checksum = 7;
  323. uint64 created_at = 8;
  324. map<string, string> metadata = 9;
  325. }
  326. message ListFilesRequest {
  327. string file_type = 1; // Filter by type (optional)
  328. string related_id = 2; // Filter by related ID (optional)
  329. uint32 limit = 3;
  330. uint32 offset = 4;
  331. }
  332. message ListFilesResponse {
  333. repeated FileInfo files = 1;
  334. uint64 total_count = 2;
  335. bool has_more = 3;
  336. }
  337. // ===== Event Subscription =====
  338. message SubscribeRequest {
  339. repeated string collections = 1; // Empty = all collections
  340. repeated string patterns = 2; // Glob patterns (e.g., "assistants:*")
  341. bool include_data = 3; // Include document data in events
  342. }
  343. message DatabaseEvent {
  344. EventType type = 1;
  345. string collection = 2;
  346. string document_id = 3;
  347. uint64 timestamp = 4;
  348. string node_id = 5;
  349. bytes data = 6; // JSON document (if include_data)
  350. }
  351. enum EventType {
  352. EVENT_UNKNOWN = 0;
  353. EVENT_INSERT = 1;
  354. EVENT_UPDATE = 2;
  355. EVENT_DELETE = 3;
  356. EVENT_EXPIRE = 4;
  357. EVENT_INVALIDATE = 5;
  358. }
  359. // ===== Replication =====
  360. message ReplicationEntry {
  361. uint64 sequence = 1;
  362. uint64 global_timestamp = 2;
  363. string node_id = 3;
  364. OperationType op = 4;
  365. string collection = 5;
  366. string document_id = 6;
  367. uint64 document_version = 7;
  368. bytes data = 8;
  369. }
  370. enum OperationType {
  371. OP_UNKNOWN = 0;
  372. OP_INSERT = 1;
  373. OP_UPDATE = 2;
  374. OP_DELETE = 3;
  375. OP_UPSERT = 4;
  376. OP_CREATE_COLLECTION = 5;
  377. OP_DROP_COLLECTION = 6;
  378. }
  379. message ReplicationMessage {
  380. oneof content {
  381. ReplicationEntry entry = 1;
  382. Heartbeat heartbeat = 2;
  383. WatermarkUpdate watermark = 3;
  384. SyncRequest sync_request = 4;
  385. }
  386. }
  387. message Heartbeat {
  388. string node_id = 1;
  389. uint64 timestamp = 2;
  390. uint64 sequence = 3;
  391. }
  392. message WatermarkUpdate {
  393. string node_id = 1;
  394. uint64 sequence = 2;
  395. }
  396. message SyncRequest {
  397. uint64 from_sequence = 1;
  398. }
  399. message GetEntriesRequest {
  400. uint64 from_sequence = 1;
  401. uint32 limit = 2;
  402. }
  403. message GetNodeStateRequest {
  404. }
  405. message NodeState {
  406. string node_id = 1;
  407. uint64 current_sequence = 2;
  408. map<string, uint64> peer_watermarks = 3;
  409. uint64 document_count = 4;
  410. repeated string collections = 5;
  411. bool healthy = 6;
  412. // Per-collection sequence tracking for collection discovery
  413. map<string, uint64> collection_sequences = 7;
  414. }
  415. // ===== Health and Stats =====
  416. message HealthCheckRequest {
  417. }
  418. message HealthCheckResponse {
  419. bool healthy = 1;
  420. uint64 uptime_seconds = 2;
  421. uint64 document_count = 3;
  422. uint64 memory_used_bytes = 4;
  423. uint64 wal_size_bytes = 5;
  424. repeated PeerHealth peers = 6;
  425. }
  426. message PeerHealth {
  427. string node_id = 1;
  428. bool connected = 2;
  429. uint64 latency_ms = 3;
  430. uint64 lag_entries = 4;
  431. }
  432. message GetStatsRequest {
  433. }
  434. message GetStatsResponse {
  435. uint64 total_documents = 1;
  436. uint64 total_collections = 2;
  437. uint64 memory_used_bytes = 3;
  438. uint64 wal_sequence = 4;
  439. uint64 wal_size_bytes = 5;
  440. uint64 snapshot_count = 6;
  441. uint64 last_snapshot_sequence = 7;
  442. uint64 insert_count = 8;
  443. uint64 update_count = 9;
  444. uint64 delete_count = 10;
  445. uint64 query_count = 11;
  446. }