github.com/klaytn/klaytn@v1.12.1/storage/database/database_test_util.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package database
    18  
    19  import (
    20  	"fmt"
    21  	"path/filepath"
    22  
    23  	"github.com/syndtr/goleveldb/leveldb/opt"
    24  )
    25  
    26  // NewLevelDBManagerForTest returns a DBManager, consisted of only LevelDB.
    27  // It also accepts LevelDB option, opt.Options.
    28  func NewLevelDBManagerForTest(dbc *DBConfig, levelDBOption *opt.Options) (DBManager, error) {
    29  	dbm := newDatabaseManager(dbc)
    30  
    31  	checkDBEntryConfigRatio()
    32  
    33  	var ldb *levelDB
    34  	var err error
    35  	for i := 0; i < int(databaseEntryTypeSize); i++ {
    36  		if dbm.config.SingleDB {
    37  			if i == 0 {
    38  				ldb, err = NewLevelDBWithOption(dbc.Dir, levelDBOption)
    39  			}
    40  		} else {
    41  			dir := filepath.Join(dbc.Dir, dbBaseDirs[i])
    42  			LDBOption := getLevelDBOptionByDBType(levelDBOption, DBEntryType(i))
    43  			LDBOption.Compression = getCompressionType(dbc.LevelDBCompression, DBEntryType(i))
    44  
    45  			ldb, err = NewLevelDBWithOption(dir, LDBOption)
    46  
    47  			fmt.Printf("Database: %-15s Compression: %-15s DisableBufferPool: %v \n", dbBaseDirs[i], LDBOption.Compression, levelDBOption.DisableBufferPool)
    48  		}
    49  
    50  		if err != nil {
    51  			return nil, fmt.Errorf("failed to create new LevelDB with options. err: %v", err)
    52  		}
    53  
    54  		dbm.dbs[i] = ldb
    55  	}
    56  
    57  	return dbm, nil
    58  }
    59  
    60  // getLevelDBOptionByDBType returns scaled LevelDB option from the given LevelDB option.
    61  // Some settings are not changed since they are not globally shared resources.
    62  // e.g., NoSync or CompactionTableSizeMultiplier
    63  func getLevelDBOptionByDBType(levelDBOption *opt.Options, i DBEntryType) *opt.Options {
    64  	copiedLevelDBOption := *levelDBOption
    65  	ratio := dbConfigRatio[i]
    66  	copiedLevelDBOption.WriteBuffer = levelDBOption.WriteBuffer * ratio / 100
    67  
    68  	return &copiedLevelDBOption
    69  }