mirror of
https://github.com/luxfi/database.git
synced 2026-10-30 15:01:18 +00:00
- Add Syncer interface to database.Database with Sync() method - Implement Sync() for all database backends: - badgerdb: calls db.Sync() to flush WAL - pebbledb: calls LogData with pebble.Sync - leveldb: writes empty batch with Sync option - memdb: no-op for in-memory database - versiondb: delegates to underlying database - prefixdb: delegates to underlying database - meterdb: delegates with metrics tracking - metricdb: delegates with metrics tracking - hookdb: new wrapper with sync notification hooks - corruptabledb: delegates to underlying database - encdb: delegates to underlying database This ensures data persists across restarts when using async writes (e.g., badgerdb with SyncWrites=false). Critical for admin.importChain to persist imported blocks.
46 lines
959 B
Go
46 lines
959 B
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
//go:build pebbledb
|
|
|
|
package pebbledb
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// Note: TestInterface tests other batch functionality.
|
|
func TestBatch(t *testing.T) {
|
|
require := require.New(t)
|
|
dirName := t.TempDir()
|
|
|
|
db, err := New(dirName, 512*1024*1024, 256, "test", false)
|
|
require.NoError(err)
|
|
|
|
batchIntf := db.NewBatch()
|
|
batch, ok := batchIntf.(*batch)
|
|
require.True(ok)
|
|
|
|
require.False(batch.written)
|
|
|
|
key1, value1 := []byte("key1"), []byte("value1")
|
|
require.NoError(batch.Put(key1, value1))
|
|
require.Equal(len(key1)+len(value1)+pebbleByteOverHead, batch.Size())
|
|
|
|
require.NoError(batch.Write())
|
|
|
|
require.True(batch.written)
|
|
|
|
got, err := db.Get(key1)
|
|
require.NoError(err)
|
|
require.Equal(value1, got)
|
|
|
|
batch.Reset()
|
|
require.False(batch.written)
|
|
require.Zero(batch.Size())
|
|
|
|
require.NoError(db.Close())
|
|
}
|