memory.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package dbmanager
  2. import (
  3. "fmt"
  4. "git.linuxforward.com/byop/byop-engine/models"
  5. )
  6. // MemoryDbManager implements DbManager using in-memory storage
  7. type MemoryDbManager struct {
  8. // In-memory database storage
  9. // User storage
  10. users map[int64]*models.User
  11. // Client storage
  12. clients map[int64]*models.Client
  13. // Deployment storage
  14. deployments map[int64]*models.Deployment
  15. // Other entity storage
  16. // ...
  17. }
  18. // NewMemoryDbManager creates a new MemoryDbManager
  19. func NewMemoryDbManager() *MemoryDbManager {
  20. return &MemoryDbManager{
  21. users: make(map[int64]*models.User),
  22. clients: make(map[int64]*models.Client),
  23. deployments: make(map[int64]*models.Deployment),
  24. }
  25. }
  26. // Connect establishes a connection to the in-memory database
  27. func (m *MemoryDbManager) Connect() error {
  28. // No action needed for in-memory storage
  29. return nil
  30. }
  31. // Disconnect closes the connection to the in-memory database
  32. func (m *MemoryDbManager) Disconnect() error {
  33. // No action needed for in-memory storage
  34. return nil
  35. }
  36. // Exec executes a query against the in-memory database
  37. func (m *MemoryDbManager) Exec(query string, args ...interface{}) (interface{}, error) {
  38. // In-memory storage does not support complex queries
  39. // Implement simple query execution if needed
  40. return nil, fmt.Errorf("exec not supported in in-memory storage")
  41. }
  42. // Create creates a new entity in the in-memory database
  43. func (m *MemoryDbManager) Create(entityType string, entity interface{}) error {
  44. switch entityType {
  45. case "users":
  46. user, ok := entity.(*models.User)
  47. if !ok {
  48. return fmt.Errorf("invalid user type")
  49. }
  50. m.users[user.ID] = user
  51. case "clients":
  52. client, ok := entity.(*models.Client)
  53. if !ok {
  54. return fmt.Errorf("invalid client type")
  55. }
  56. m.clients[client.ID] = client
  57. case "deployments":
  58. deployment, ok := entity.(*models.Deployment)
  59. if !ok {
  60. return fmt.Errorf("invalid deployment type")
  61. }
  62. m.deployments[deployment.ID] = deployment
  63. default:
  64. return fmt.Errorf("unsupported entity type: %s", entityType)
  65. }
  66. return nil
  67. }
  68. // GetByID retrieves an entity by ID from the in-memory database
  69. func (m *MemoryDbManager) GetByID(entityType string, id string) (interface{}, error) {
  70. // Convert string ID to int64 for the new ID format
  71. var intID int64
  72. if _, err := fmt.Sscanf(id, "%d", &intID); err != nil {
  73. return nil, fmt.Errorf("invalid ID format: %s", id)
  74. }
  75. switch entityType {
  76. case "users":
  77. user, exists := m.users[intID]
  78. if !exists {
  79. return nil, fmt.Errorf("user not found")
  80. }
  81. return user, nil
  82. case "clients":
  83. client, exists := m.clients[intID]
  84. if !exists {
  85. return nil, fmt.Errorf("client not found")
  86. }
  87. return client, nil
  88. case "deployments":
  89. deployment, exists := m.deployments[intID]
  90. if !exists {
  91. return nil, fmt.Errorf("deployment not found")
  92. }
  93. return deployment, nil
  94. default:
  95. return nil, fmt.Errorf("unsupported entity type: %s", entityType)
  96. }
  97. }
  98. // Update updates an existing entity in the in-memory database
  99. func (m *MemoryDbManager) Update(entityType string, entity interface{}) error {
  100. switch entityType {
  101. case "users":
  102. user, ok := entity.(*models.User)
  103. if !ok {
  104. return fmt.Errorf("invalid user type")
  105. }
  106. m.users[user.ID] = user
  107. case "clients":
  108. client, ok := entity.(*models.Client)
  109. if !ok {
  110. return fmt.Errorf("invalid client type")
  111. }
  112. m.clients[client.ID] = client
  113. case "deployments":
  114. deployment, ok := entity.(*models.Deployment)
  115. if !ok {
  116. return fmt.Errorf("invalid deployment type")
  117. }
  118. m.deployments[deployment.ID] = deployment
  119. default:
  120. return fmt.Errorf("unsupported entity type: %s", entityType)
  121. }
  122. return nil
  123. }
  124. // Delete deletes an entity by ID from the in-memory database
  125. func (m *MemoryDbManager) Delete(entityType string, id string) error {
  126. // Convert string ID to int64 for the new ID format
  127. var intID int64
  128. if _, err := fmt.Sscanf(id, "%d", &intID); err != nil {
  129. return fmt.Errorf("invalid ID format: %s", id)
  130. }
  131. switch entityType {
  132. case "users":
  133. delete(m.users, intID)
  134. case "clients":
  135. delete(m.clients, intID)
  136. case "deployments":
  137. delete(m.deployments, intID)
  138. default:
  139. return fmt.Errorf("unsupported entity type: %s", entityType)
  140. }
  141. return nil
  142. }
  143. // ListByFilter retrieves entities based on a filter from the in-memory database
  144. func (m *MemoryDbManager) List(entityType string, filter map[string]interface{}) ([]interface{}, error) {
  145. switch entityType {
  146. case "users":
  147. users := make([]interface{}, 0, len(m.users))
  148. for _, user := range m.users {
  149. if matchesFilter(user, filter) {
  150. users = append(users, user)
  151. }
  152. }
  153. return users, nil
  154. case "clients":
  155. clients := make([]interface{}, 0, len(m.clients))
  156. for _, client := range m.clients {
  157. if matchesFilter(client, filter) {
  158. clients = append(clients, client)
  159. }
  160. }
  161. return clients, nil
  162. case "deployments":
  163. deployments := make([]interface{}, 0, len(m.deployments))
  164. for _, deployment := range m.deployments {
  165. if matchesFilter(deployment, filter) {
  166. deployments = append(deployments, deployment)
  167. }
  168. }
  169. return deployments, nil
  170. default:
  171. return nil, fmt.Errorf("unsupported entity type: %s", entityType)
  172. }
  173. }
  174. // matchesFilter checks if an entity matches the given filter
  175. func matchesFilter(entity interface{}, filter map[string]interface{}) bool {
  176. // Implement your filtering logic here
  177. // For example, check if the entity's fields match the filter criteria
  178. return true // Placeholder, implement actual filtering logic
  179. }