Go-ethereum 源码解析之 go-ethereum/ethdb/interface.go - Go语言中文社区

Go-ethereum 源码解析之 go-ethereum/ethdb/interface.go


Go-ethereum 源码解析之 go-ethereum/ethdb/interface.go


Source code

// 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()
}


Appendix A. 总体批注

此文件描述了包 ethdb 提供的基本接口。


Appendix B. 详细批注

1. const IdealBatchSize = 100 * 1024

使用批处理的代码应该尝试将这么多数据添加到批处理中。

该值是根据经验确定的。

2. type Putter interface

接口 Putter 包装了批处理和常规数据库支持的数据库写入操作。

2.1 Put(key []byte, value []byte) error

方法 Put() 存储给定的 key 和 value 到数据库。

参数:

  • key []byte: key
  • value []byte: value

3. type Deleter interface

接口 Deleter 包装批处理和常规数据库支持的数据库删除操作。

3.1 Delete(key []byte) error

方法 Delete() 从数据库中删除给定的 key。

4. type Database interface

接口 Database 包装所有数据库操作。所有方法都能够安全地支持并发。

4.1 Putter

继承了接口 Putter。提供了方法 Put()。

4.2 Deleter

继承了接口 Deleter。提供了方法 Delete()。

4.3 Get(key []byte) ([]byte, error)

方法 Get() 从数据库中获取 key 对应的 value。

参数:

  • key []byte: key

返回值

  • []byte: value
  • error: 错误消息或 nil

4.4 Has(key []byte) (bool, error)

方法 Has() 查询给定 key 是否存在于数据库中。

参数:

  • key []byte: key

返回值

  • bool: 存在为 true,不存在则为 false
  • error: 错误消息或 nil

5. type Batch interface

接口 Batch 是一个只写数据库,在调用方法 Write() 时会将更改提交到其主机数据库。批处理不支付并发。

5.1 Putter

继承了接口 Putter。提供了方法 Put()。

5.2 Deleter

继承了接口 Deleter。提供了方法 Delete()。

5.3 ValueSize() int

方法 ValueSize() 返回批处理中的数据量。

5.4 Write() error

方法 Write() 将更改提交到其主机数据库。

5.5 Reset()

方法 Reset() 重置批处理以便重复使用。


Reference

  1. https://github.com/ethereum/go-ethereum/blob/master/ethdb/interface.go

Contributor

  1. Windstamp, https://github.com/windstamp
版权声明:本文来源简书,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.jianshu.com/p/4a22ff754788
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-01-09 21:52:38
  • 阅读 ( 1578 )
  • 分类:数据库

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢