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

Go-ethereum 源码解析之 go-ethereum/ethdb/memory_database.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 .

package ethdb

import (
    "errors"
    "sync"

    "github.com/ethereum/go-ethereum/common"
)

/*
 * This is a test memory database. Do not use for any production it does not get persisted
 */
type MemDatabase struct {
    db   map[string][]byte
    lock sync.RWMutex
}

func NewMemDatabase() *MemDatabase {
    return &MemDatabase{
        db: make(map[string][]byte),
    }
}

func NewMemDatabaseWithCap(size int) *MemDatabase {
    return &MemDatabase{
        db: make(map[string][]byte, size),
    }
}

func (db *MemDatabase) Put(key []byte, value []byte) error {
    db.lock.Lock()
    defer db.lock.Unlock()

    db.db[string(key)] = common.CopyBytes(value)
    return nil
}

func (db *MemDatabase) Has(key []byte) (bool, error) {
    db.lock.RLock()
    defer db.lock.RUnlock()

    _, ok := db.db[string(key)]
    return ok, nil
}

func (db *MemDatabase) Get(key []byte) ([]byte, error) {
    db.lock.RLock()
    defer db.lock.RUnlock()

    if entry, ok := db.db[string(key)]; ok {
        return common.CopyBytes(entry), nil
    }
    return nil, errors.New("not found")
}

func (db *MemDatabase) Keys() [][]byte {
    db.lock.RLock()
    defer db.lock.RUnlock()

    keys := [][]byte{}
    for key := range db.db {
        keys = append(keys, []byte(key))
    }
    return keys
}

func (db *MemDatabase) Delete(key []byte) error {
    db.lock.Lock()
    defer db.lock.Unlock()

    delete(db.db, string(key))
    return nil
}

func (db *MemDatabase) Close() {}

func (db *MemDatabase) NewBatch() Batch {
    return &memBatch{db: db}
}

func (db *MemDatabase) Len() int { return len(db.db) }

type kv struct {
    k, v []byte
    del  bool
}

type memBatch struct {
    db     *MemDatabase
    writes []kv
    size   int
}

func (b *memBatch) Put(key, value []byte) error {
    b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value), false})
    b.size += len(value)
    return nil
}

func (b *memBatch) Delete(key []byte) error {
    b.writes = append(b.writes, kv{common.CopyBytes(key), nil, true})
    b.size += 1
    return nil
}

func (b *memBatch) Write() error {
    b.db.lock.Lock()
    defer b.db.lock.Unlock()

    for _, kv := range b.writes {
        if kv.del {
            delete(b.db.db, string(kv.k))
            continue
        }
        b.db.db[string(kv.k)] = kv.v
    }
    return nil
}

func (b *memBatch) ValueSize() int {
    return b.size
}

func (b *memBatch) Reset() {
    b.writes = b.writes[:0]
    b.size = 0
}


Appendix A. 总体批注

实现了一个内存数据库 MemDatabase 用于测试环境,但不能将其用于生产环境。

ethdb.MemDatabase 实现了接口 ethdb.Database。

ethdb.memBatch 在 ethdb.MemDatabase 的基础上提供了批处理能力。

这里将基于接口编程的思想展现的淋漓尽致。


Appendix B. 详细批注

1. type MemDatabase struct

数据结构 MemDatabase 是一个测试内存数据库。不要将其用于任何生产环境,因为它不会被持久化。

  • db map[string][]byte: key-value 对?
  • lock sync.RWMutex: 锁

1.1 func NewMemDatabase() *MemDatabase

构造函数 NewMemDatabase() 创建对象 MemDatabase,并使用默认值初始化。

1.2 func NewMemDatabaseWithCap(size int) *MemDatabase

构造函数 NewMemDatabaseWithCap() 创建对象 MemDatabase,并设定 db 的大小。

1.3 func (db *MemDatabase) Put(key []byte, value []byte) error

方法 Put() 实现了接口 ethdb.Putter 和接口 ethdb.Database。

参数:

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

返回值:

  • 出错返回错误消息 error,否则返回 nil

主要实现:

  • 加锁。代码为: db.lock.Lock()
  • defer 解锁。代码为:defer db.lock.Unlock()
  • 将 (key, value) 对存储数据库 db。db.db[string(key)] = common.CopyBytes(value)

1.4 func (db *MemDatabase) Has(key []byte) (bool, error)

方法 Has() 实现了接口 ethdb.Database。

参数:

  • key []byte: key

返回值:

  • 存在返回 true,否则返回 false
  • 出错返回错误消息 error,否则返回 nil

主要实现:

  • 加锁。代码为: db.lock.RLock()
  • defer 解锁。代码为:defer db.lock.RUnlock()
  • 是否存在。_, ok := db.db[string(key)]

1.5 func (db *MemDatabase) Get(key []byte) ([]byte, error)

方法 Get() 实现了接口 ethdb.Database。

参数:

  • key []byte: key

返回值:

  • 存在返回 key 对应的 value
  • 出错返回错误消息 error,否则返回 nil

主要实现:

  • 加锁。代码为:db.lock.RLock()
  • defer 解锁。代码为:defer db.lock.RUnlock()
  • 获取 key 对应的值 entry。代码为:entry, ok := db.db[string(key)]
  • 将 entry 的副本返回。代码为:return common.CopyBytes(entry)

1.6 func (db *MemDatabase) Keys() [][]byte

方法 Keys() 返回数据库中的所有 key。

返回值:

  • 所有的 key 构成的列表

主要实现:

  • 加锁。代码为:db.lock.RLock()
  • defer 解锁。代码为:defer db.lock.RUnlock()
  • 定义所有 key 的列表 keys
  • 遍历数据库 db.db 中的所有 key
    • 将 key 添加到 keys

1.7 func (db *MemDatabase) Delete(key []byte) error

方法 Put() 实现了接口 ethdb.Deleter 和接口 ethdb.Database。

参数:

  • key []byte: key

返回值:

  • 出错返回错误消息 error,否则返回 nil

主要实现:

  • 加锁。代码为:db.lock.Lock()
  • defer 解锁。代码为:defer db.lock.Unlock()
  • 通过 Go 内置函数 delete() 从数据库 db.db 中删除对应的 key。代码为:delete(db.db, string(key))

1.8 func (db *MemDatabase) Close() {}

方法 Close() 实现了接口 ethdb.Database。

主要实现:

  • 空实现。

1.9 func (db *MemDatabase) NewBatch() Batch

方法 NewBatch() 实现了接口 ethdb.Database。

主要实现:

  • return &memBatch{db: db}

1.10 func (db *MemDatabase) Len() int

方法 Len() 返回数据库包含的数据量。

返回值:

  • 数据量

主要实现:

  • return len(db.db)

2. type kv struct

数据结构 kv 用于描述批处理的值 k, v 和操作类型是 add 还是 del。

  • k, v []byte: Key & Value
  • del bool: 操作类型是插入还是删除

3. type memBatch struct

数据结构 memBatch 是具有批处理能力的内存数据库。

  • db *MemDatabase: 内存数据库
  • writes []kv: 批处理数据
  • size int: 批处理的字节数

3.1 func (b *memBatch) Put(key, value []byte) error

方法 Put() 实现了接口 ethdb.Putter,用于将给定的 key & value 插入数据库。

参数:

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

返回值:

  • 出错返回错误消息 error,否则返回 nil

主要实现:

  • 将 key & value & false 构建的 kv 插入批处理数据 writes
    • b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value), false})
  • 增加批处理字节数 size
    • b.size += len(value)

3.2 func (b *memBatch) Delete(key []byte) error

方法 Delete() 实现了接口 ethdb.Deleter,用于从数据库中删除给定的 key。

参数:

  • key []byte: key

返回值:

  • 出错返回错误消息 error,否则返回 nil

主要实现:

  • 将 key & nil & true 构建的 kv 插入批处理数据 writes
    • b.writes = append(b.writes, kv{common.CopyBytes(key), nil, true})
  • 更新批处理字节数 size
    • b.size += 1

3.3 func (b *memBatch) Write() error

方法 Write() 一次性将批处理数据更新到数据库。

返回值:

  • 出错返回错误消息 error,否则返回 nil

主要实现:

  • 加锁。代码为:db.lock.Lock()
  • defer 解锁。代码为:defer db.lock.Unlock()
  • 遍历批处理数据 b.writes 的每个 kv
    • 如果 kv.del
      • 从数据库中删除 kv.k
        • delete(b.db.db, string(kv.k))
      • 退出本轮迭代
    • 否则,将 kv.k & kv.v 插入数据库
      • b.db.db[string(kv.k)] = kv.v

3.4 func (b *memBatch) ValueSize() int

方法 ValueSize() 返回批处理字节数。

返回值:

  • 批处理字节数。

主要实现:

  • return b.size

3.5 func (b *memBatch) Reset()

方法 Reset() 重置批处理操作。

主要实现:

  • 清空批处理操作
    • b.writes = b.writes[:0]
    • b.size = 0

Reference

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

Contributor

  1. Windstamp, https://github.com/windstamp

你可能感兴趣的:(Go-ethereum 源码解析之 go-ethereum/ethdb/memory_database.go)