memory.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package dbmanager
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. // MemoryDbManager implements DbManager using in-memory storage
  7. type MemoryDbManager[T Entity] struct {
  8. // In-memory database storage
  9. collections map[string]map[string]T
  10. }
  11. // NewMemoryDbManager creates a new memory-based database manager
  12. func NewMemoryDbManager[T Entity]() *MemoryDbManager[T] {
  13. return &MemoryDbManager[T]{
  14. collections: make(map[string]map[string]T),
  15. }
  16. }
  17. // Connect initializes the memory database
  18. func (db *MemoryDbManager[T]) Connect() error {
  19. // Nothing to do for memory implementation
  20. return nil
  21. }
  22. // Disconnect cleans up memory database
  23. func (db *MemoryDbManager[T]) Disconnect() error {
  24. // Clear collections to free memory
  25. db.collections = make(map[string]map[string]T)
  26. return nil
  27. }
  28. // getCollection returns the collection for the given entity type
  29. func (db *MemoryDbManager[T]) getCollection(entityType string) map[string]T {
  30. if _, exists := db.collections[entityType]; !exists {
  31. db.collections[entityType] = make(map[string]T)
  32. }
  33. return db.collections[entityType]
  34. }
  35. // Create creates a new entity in memory
  36. func (db *MemoryDbManager[T]) Create(entityType string, entity T) error {
  37. collection := db.getCollection(entityType)
  38. id := entity.GetID()
  39. if _, exists := collection[id]; exists {
  40. return fmt.Errorf("entity with ID %s already exists in %s", id, entityType)
  41. }
  42. collection[id] = entity
  43. return nil
  44. }
  45. // GetByID retrieves an entity by ID from memory
  46. func (db *MemoryDbManager[T]) GetByID(entityType string, id string) (T, error) {
  47. collection := db.getCollection(entityType)
  48. if entity, exists := collection[id]; exists {
  49. return entity, nil
  50. }
  51. var zero T
  52. return zero, fmt.Errorf("entity with ID %s not found in %s", id, entityType)
  53. }
  54. // Update updates an existing entity in memory
  55. func (db *MemoryDbManager[T]) Update(entityType string, entity T) error {
  56. collection := db.getCollection(entityType)
  57. id := entity.GetID()
  58. if _, exists := collection[id]; !exists {
  59. return fmt.Errorf("entity with ID %s does not exist in %s", id, entityType)
  60. }
  61. collection[id] = entity
  62. return nil
  63. }
  64. // Delete deletes an entity from memory
  65. func (db *MemoryDbManager[T]) Delete(entityType string, id string) error {
  66. collection := db.getCollection(entityType)
  67. if _, exists := collection[id]; !exists {
  68. return fmt.Errorf("entity with ID %s does not exist in %s", id, entityType)
  69. }
  70. delete(collection, id)
  71. return nil
  72. }
  73. // List retrieves entities based on a filter from memory
  74. func (db *MemoryDbManager[T]) List(entityType string, filter map[string]interface{}) ([]T, error) {
  75. collection := db.getCollection(entityType)
  76. result := make([]T, 0)
  77. // If no filter is provided, return all entities
  78. if len(filter) == 0 {
  79. for _, entity := range collection {
  80. result = append(result, entity)
  81. }
  82. return result, nil
  83. }
  84. // Filter entities based on field values
  85. // Note: This is a simple implementation that relies on reflection
  86. // and might not be efficient for large datasets
  87. for _, entity := range collection {
  88. matches := true
  89. entityValue := reflect.ValueOf(entity)
  90. // For structs, we need to get the underlying value
  91. if entityValue.Kind() == reflect.Ptr {
  92. entityValue = entityValue.Elem()
  93. }
  94. for field, value := range filter {
  95. // Find the field in the struct
  96. structField := entityValue.FieldByName(field)
  97. if !structField.IsValid() {
  98. matches = false
  99. break
  100. }
  101. // Compare values
  102. fieldValue := structField.Interface()
  103. if fieldValue != value {
  104. matches = false
  105. break
  106. }
  107. }
  108. if matches {
  109. result = append(result, entity)
  110. }
  111. }
  112. return result, nil
  113. }