|
| 1 | +package structure |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/ByteStorage/FlyDB/config" |
| 5 | + "github.com/ByteStorage/FlyDB/db/engine" |
| 6 | + _const "github.com/ByteStorage/FlyDB/lib/const" |
| 7 | +) |
| 8 | + |
| 9 | +// DataStructureManager provides unified access to all data structures |
| 10 | +// Supports all data structure types through a single DB instance |
| 11 | +type DataStructureManager struct { |
| 12 | + db *engine.DB |
| 13 | + stringStruct *StringStructureInternal |
| 14 | + hashStruct *HashStructureInternal |
| 15 | + listStruct *ListStructureInternal |
| 16 | + setStruct *SetStructureInternal |
| 17 | + zsetStruct *ZSetStructureInternal |
| 18 | + bitmapStruct *BitmapStructureInternal |
| 19 | + streamStruct *StreamStructureInternal |
| 20 | + expiringStruct *ExpiringKeyInternal |
| 21 | +} |
| 22 | + |
| 23 | +// NewDataStructureManager creates a new data structure manager |
| 24 | +// It initializes all supported data structures sharing a single DB instance |
| 25 | +func NewDataStructureManager(options config.Options) (*DataStructureManager, error) { |
| 26 | + // Create a shared DB instance |
| 27 | + db, err := engine.NewDB(options) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + // Create manager instance |
| 33 | + manager := &DataStructureManager{ |
| 34 | + db: db, |
| 35 | + } |
| 36 | + |
| 37 | + // Initialize each data structure, sharing the same DB instance |
| 38 | + manager.stringStruct = newStringStructureInternal(db) |
| 39 | + manager.hashStruct = newHashStructureInternal(db) |
| 40 | + manager.listStruct = newListStructureInternal(db) |
| 41 | + manager.setStruct = newSetStructureInternal(db) |
| 42 | + manager.zsetStruct = newZSetStructureInternal(db) |
| 43 | + manager.bitmapStruct = newBitmapStructureInternal(db) |
| 44 | + manager.streamStruct = newStreamStructureInternal(db) |
| 45 | + manager.expiringStruct = newExpiringKeyInternal(db) |
| 46 | + |
| 47 | + return manager, nil |
| 48 | +} |
| 49 | + |
| 50 | +// String returns the string data structure operation interface |
| 51 | +func (m *DataStructureManager) String() *StringStructureInternal { |
| 52 | + return m.stringStruct |
| 53 | +} |
| 54 | + |
| 55 | +// Hash returns the hash data structure operation interface |
| 56 | +func (m *DataStructureManager) Hash() *HashStructureInternal { |
| 57 | + return m.hashStruct |
| 58 | +} |
| 59 | + |
| 60 | +// List returns the list data structure operation interface |
| 61 | +func (m *DataStructureManager) List() *ListStructureInternal { |
| 62 | + return m.listStruct |
| 63 | +} |
| 64 | + |
| 65 | +// Set returns the set data structure operation interface |
| 66 | +func (m *DataStructureManager) Set() *SetStructureInternal { |
| 67 | + return m.setStruct |
| 68 | +} |
| 69 | + |
| 70 | +// ZSet returns the sorted set data structure operation interface |
| 71 | +func (m *DataStructureManager) ZSet() *ZSetStructureInternal { |
| 72 | + return m.zsetStruct |
| 73 | +} |
| 74 | + |
| 75 | +// Bitmap returns the bitmap data structure operation interface |
| 76 | +func (m *DataStructureManager) Bitmap() *BitmapStructureInternal { |
| 77 | + return m.bitmapStruct |
| 78 | +} |
| 79 | + |
| 80 | +// Stream returns the stream data structure operation interface |
| 81 | +func (m *DataStructureManager) Stream() *StreamStructureInternal { |
| 82 | + return m.streamStruct |
| 83 | +} |
| 84 | + |
| 85 | +// Keys returns all keys matching the given pattern |
| 86 | +func (m *DataStructureManager) Keys(pattern string) ([]string, error) { |
| 87 | + // Need to implement a method to find keys from all data structures |
| 88 | + // Temporarily simplified implementation using StringStructure's Keys method |
| 89 | + return m.stringStruct.Keys(pattern) |
| 90 | +} |
| 91 | + |
| 92 | +// Type returns the data structure type of the key |
| 93 | +func (m *DataStructureManager) Type(key string) (string, error) { |
| 94 | + // Check string type |
| 95 | + if exists, _ := m.stringStruct.Exists(key); exists { |
| 96 | + return "string", nil |
| 97 | + } |
| 98 | + |
| 99 | + // Check hash type |
| 100 | + // Need to implement corresponding check method |
| 101 | + |
| 102 | + // Check list type |
| 103 | + // Need to implement corresponding check method |
| 104 | + |
| 105 | + // Check set type |
| 106 | + // Need to implement corresponding check method |
| 107 | + |
| 108 | + // If none exists, return "none" |
| 109 | + return "none", nil |
| 110 | +} |
| 111 | + |
| 112 | +// Del deletes a key |
| 113 | +func (m *DataStructureManager) Del(key string) (bool, error) { |
| 114 | + keyType, err := m.Type(key) |
| 115 | + if err != nil { |
| 116 | + return false, err |
| 117 | + } |
| 118 | + |
| 119 | + switch keyType { |
| 120 | + case "string": |
| 121 | + err := m.stringStruct.Del(key) |
| 122 | + return err == nil, err |
| 123 | + case "hash": |
| 124 | + return m.hashStruct.HDelAll(key) |
| 125 | + // Other type deletion methods |
| 126 | + // ... |
| 127 | + case "none": |
| 128 | + return false, nil |
| 129 | + default: |
| 130 | + return false, _const.ErrKeyNotFound |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// Expire sets key's expiration time |
| 135 | +func (m *DataStructureManager) Expire(key string, ttl int64) (bool, error) { |
| 136 | + keyType, err := m.Type(key) |
| 137 | + if err != nil { |
| 138 | + return false, err |
| 139 | + } |
| 140 | + |
| 141 | + switch keyType { |
| 142 | + case "string": |
| 143 | + err := m.stringStruct.Expire(key, ttl) |
| 144 | + return err == nil, err |
| 145 | + case "hash": |
| 146 | + return m.hashStruct.HExpire(key, ttl) |
| 147 | + // Other type expiration settings |
| 148 | + // ... |
| 149 | + case "none": |
| 150 | + return false, nil |
| 151 | + default: |
| 152 | + return false, _const.ErrKeyNotFound |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// TTL gets key's remaining time to live |
| 157 | +func (m *DataStructureManager) TTL(key string) (int64, error) { |
| 158 | + keyType, err := m.Type(key) |
| 159 | + if err != nil { |
| 160 | + return -2, err |
| 161 | + } |
| 162 | + |
| 163 | + switch keyType { |
| 164 | + case "string": |
| 165 | + return m.stringStruct.TTL(key) |
| 166 | + case "hash": |
| 167 | + return m.hashStruct.TTL(key) |
| 168 | + // Other type TTL retrieval |
| 169 | + // ... |
| 170 | + case "none": |
| 171 | + return -2, nil // Key doesn't exist |
| 172 | + default: |
| 173 | + return -2, _const.ErrKeyNotFound |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +// Sync syncs data to disk |
| 178 | +func (m *DataStructureManager) Sync() error { |
| 179 | + return m.db.Sync() |
| 180 | +} |
| 181 | + |
| 182 | +// Close closes the database |
| 183 | +func (m *DataStructureManager) Close() error { |
| 184 | + return m.db.Close() |
| 185 | +} |
| 186 | + |
| 187 | +// Clean empties the database |
| 188 | +func (m *DataStructureManager) Clean() { |
| 189 | + m.db.Clean() |
| 190 | +} |
0 commit comments