社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package ethdb
// Code using batches should try to add this much data to the batch.
// The value was determined empirically.
const IdealBatchSize = 100 * 1024
// Putter wraps the database write operation supported by both batches and regular databases.
type Putter interface {
Put(key []byte, value []byte) error
}
// Deleter wraps the database delete operation supported by both batches and regular databases.
type Deleter interface {
Delete(key []byte) error
}
// Database wraps all database operations. All methods are safe for concurrent use.
type Database interface {
Putter
Deleter
Get(key []byte) ([]byte, error)
Has(key []byte) (bool, error)
Close()
NewBatch() Batch
}
// Batch is a write-only database that commits changes to its host database
// when Write is called. Batch cannot be used concurrently.
type Batch interface {
Putter
Deleter
ValueSize() int // amount of data in the batch
Write() error
// Reset resets the batch for reuse
Reset()
}
此文件描述了包 ethdb 提供的基本接口。
使用批处理的代码应该尝试将这么多数据添加到批处理中。
该值是根据经验确定的。
接口 Putter 包装了批处理和常规数据库支持的数据库写入操作。
方法 Put() 存储给定的 key 和 value 到数据库。
参数:
接口 Deleter 包装批处理和常规数据库支持的数据库删除操作。
方法 Delete() 从数据库中删除给定的 key。
接口 Database 包装所有数据库操作。所有方法都能够安全地支持并发。
继承了接口 Putter。提供了方法 Put()。
继承了接口 Deleter。提供了方法 Delete()。
方法 Get() 从数据库中获取 key 对应的 value。
参数:
返回值
方法 Has() 查询给定 key 是否存在于数据库中。
参数:
返回值
接口 Batch 是一个只写数据库,在调用方法 Write() 时会将更改提交到其主机数据库。批处理不支付并发。
继承了接口 Putter。提供了方法 Put()。
继承了接口 Deleter。提供了方法 Delete()。
方法 ValueSize() 返回批处理中的数据量。
方法 Write() 将更改提交到其主机数据库。
方法 Reset() 重置批处理以便重复使用。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!