mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-01 00:31:21 +00:00
126 lines
3.2 KiB
Go
126 lines
3.2 KiB
Go
/*
|
|
Copyright 2021 Erigon contributors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package mdbx_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
|
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
|
|
"github.com/ledgerwatch/erigon-lib/kv/memdb"
|
|
"github.com/ledgerwatch/log/v3"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBucketCRUD(t *testing.T) {
|
|
require := require.New(t)
|
|
db := memdb.New()
|
|
defer db.Close()
|
|
|
|
ctx := context.Background()
|
|
tx, err := db.BeginRw(ctx)
|
|
require.NoError(err)
|
|
defer tx.Rollback()
|
|
|
|
normalBucket := kv.ChaindataTables[15]
|
|
deprecatedBucket := kv.ChaindataDeprecatedTables[0]
|
|
migrator, ok := tx.(kv.BucketMigrator)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
// check thad buckets have unique DBI's
|
|
uniquness := map[kv.DBI]bool{}
|
|
castedKv, ok := db.(*mdbx.MdbxKV)
|
|
if !ok {
|
|
t.Skip()
|
|
}
|
|
for _, dbi := range castedKv.AllDBI() {
|
|
if dbi == mdbx.NonExistingDBI {
|
|
continue
|
|
}
|
|
_, ok := uniquness[dbi]
|
|
require.False(ok)
|
|
uniquness[dbi] = true
|
|
}
|
|
|
|
require.True(migrator.ExistsBucket(normalBucket))
|
|
require.True(errors.Is(migrator.DropBucket(normalBucket), kv.ErrAttemptToDeleteNonDeprecatedBucket))
|
|
|
|
require.False(migrator.ExistsBucket(deprecatedBucket))
|
|
require.NoError(migrator.CreateBucket(deprecatedBucket))
|
|
require.True(migrator.ExistsBucket(deprecatedBucket))
|
|
|
|
require.NoError(migrator.DropBucket(deprecatedBucket))
|
|
require.False(migrator.ExistsBucket(deprecatedBucket))
|
|
|
|
require.NoError(migrator.CreateBucket(deprecatedBucket))
|
|
require.True(migrator.ExistsBucket(deprecatedBucket))
|
|
|
|
c, err := tx.RwCursor(deprecatedBucket)
|
|
require.NoError(err)
|
|
err = c.Put([]byte{1}, []byte{1})
|
|
require.NoError(err)
|
|
v, err := tx.GetOne(deprecatedBucket, []byte{1})
|
|
require.NoError(err)
|
|
require.Equal([]byte{1}, v)
|
|
|
|
buckets, err := migrator.ListBuckets()
|
|
require.NoError(err)
|
|
require.True(len(buckets) > 10)
|
|
|
|
// check thad buckets have unique DBI's
|
|
uniquness = map[kv.DBI]bool{}
|
|
for _, dbi := range castedKv.AllDBI() {
|
|
if dbi == mdbx.NonExistingDBI {
|
|
continue
|
|
}
|
|
_, ok := uniquness[dbi]
|
|
require.False(ok)
|
|
uniquness[dbi] = true
|
|
}
|
|
}
|
|
|
|
func TestReadOnlyMode(t *testing.T) {
|
|
path := t.TempDir()
|
|
logger := log.New()
|
|
db1 := mdbx.NewMDBX(logger).Path(path).WithTableCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg {
|
|
return kv.TableCfg{
|
|
kv.Headers: kv.TableCfgItem{},
|
|
}
|
|
}).MustOpen()
|
|
db1.Close()
|
|
|
|
db2 := mdbx.NewMDBX(logger).Readonly().Path(path).WithTableCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg {
|
|
return kv.TableCfg{
|
|
kv.Headers: kv.TableCfgItem{},
|
|
}
|
|
}).MustOpen()
|
|
defer db2.Close()
|
|
|
|
tx, err := db2.BeginRo(context.Background())
|
|
require.NoError(t, err)
|
|
defer tx.Rollback()
|
|
|
|
c, err := tx.Cursor(kv.Headers)
|
|
require.NoError(t, err)
|
|
_, _, err = c.Seek([]byte("some prefix"))
|
|
require.NoError(t, err)
|
|
}
|