| 232 | } |
| 233 | |
| 234 | func TestDB_Snapshot(t *testing.T) { |
| 235 | tempDir := t.TempDir() |
| 236 | cfg := &config.Config{ |
| 237 | LevelDB: config.LevelDBConfig{ |
| 238 | CacheSize: 64 * 1024 * 1024, |
| 239 | BlockSize: 4 * 1024, |
| 240 | WriteBufferSize: 4 * 1024 * 1024, |
| 241 | MaxOpenFiles: 1000, |
| 242 | Compression: true, |
| 243 | }, |
| 244 | } |
| 245 | |
| 246 | db, err := Store{}.Open(tempDir, cfg) |
| 247 | require.NoError(t, err) |
| 248 | defer db.Close() |
| 249 | |
| 250 | key := []byte("snapshot-key") |
| 251 | value := []byte("snapshot-value") |
| 252 | |
| 253 | // Put initial value |
| 254 | err = db.Put(key, value) |
| 255 | require.NoError(t, err) |
| 256 | |
| 257 | // Create snapshot |
| 258 | snapshot, err := db.NewSnapshot() |
| 259 | require.NoError(t, err) |
| 260 | defer snapshot.Close() |
| 261 | |
| 262 | // Modify data after snapshot |
| 263 | newValue := []byte("new-value") |
| 264 | err = db.Put(key, newValue) |
| 265 | require.NoError(t, err) |
| 266 | |
| 267 | // Snapshot should still see old value |
| 268 | snapshotValue, err := snapshot.Get(key) |
| 269 | require.NoError(t, err) |
| 270 | assert.Equal(t, value, snapshotValue) |
| 271 | |
| 272 | // Current DB should see new value |
| 273 | // We need to ensure cache is invalidated, so we'll get from cold storage |
| 274 | currentValue, err := db.Get(key) |
| 275 | require.NoError(t, err) |
| 276 | assert.Equal(t, newValue, currentValue, "Current DB should see modified value") |
| 277 | } |
| 278 | |
| 279 | func TestDB_Compact(t *testing.T) { |
| 280 | tempDir := t.TempDir() |