backend

package
v0.0.0-...-dc80162 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2019 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package backend defines a standard interface for etcd's backend MVCC storage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDefaultTmpBackend

func NewDefaultTmpBackend() (*backend, string)

func NewTmpBackend

func NewTmpBackend(batchInterval time.Duration, batchLimit int) (*backend, string)

NewTmpBackend creates a backend implementation for testing.

Types

type Backend

type Backend interface {
	//创建一个只读事务,这里的ReadTx接口是V3存储对只读事务的抽象
	ReadTx() ReadTx
	//创建一个批量事务,这里的BatchTx接口是对批量读写事务的抽象
	BatchTx() BatchTx

	Snapshot() Snapshot //创建快照
	Hash(ignores map[IgnoreKey]struct{}) (uint32, error)
	// Size returns the current size of the backend.
	Size() int64 //获取当前已存储的总字节数
	// SizeInUse returns the current size of the backend logically in use.
	// Since the backend can manage free space in a non-byte unit such as
	// number of pages, the returned value can be not exactly accurate in bytes.
	SizeInUse() int64
	Defrag() error //碎片整理
	ForceCommit()  //提交批量读写事务
	Close() error
}

Backend接口的主要功能是将底层存储与上层进行解耦,其中定义底层存储需要对上层提供的接口。

func New

func New(bcfg BackendConfig) Backend

func NewDefaultBackend

func NewDefaultBackend(path string) Backend

type BackendConfig

type BackendConfig struct {
	// Path is the file path to the backend file.						BoltDB数据库文件的路径
	Path string
	// BatchInterval is the maximum time before flushing the BatchTx.	提交两次批量事务的最大时间差,用来初始化backend实例中的batchInterval字段,默认值是100ms
	BatchInterval time.Duration
	// BatchLimit is the maximum puts before flushing the BatchTx.		指定每个批量读写事务能包含的最多的操作个数,当超过这个阈值之后,当前批量读写事务自动提交
	BatchLimit int
	// MmapSize is the number of bytes to mmap for the backend.
	// BoltDB使用mmap技术对数据库文件建映射,该字段用来设置mmap中使用的内存大小,该字段会在创建BoltDB实例时使用
	MmapSize uint64
}

func DefaultBackendConfig

func DefaultBackendConfig() BackendConfig

type BatchTx

type BatchTx interface {
	ReadTx                                                    //内嵌ReadTx接口
	UnsafeCreateBucket(name []byte)                           //创建Bucket
	UnsafePut(bucketName []byte, key []byte, value []byte)    //向指定Bucket中添加键值对
	UnsafeSeqPut(bucketName []byte, key []byte, value []byte) //向指定Bucket中添加键值对
	UnsafeDelete(bucketName []byte, key []byte)               //在指定Bucket中删除指定的键值对
	// Commit commits a previous tx and begins a new writable one.
	Commit() //提交当前的读写事务,之后立即打开一个新的读写事务
	// CommitAndStop commits the previous tx and does not create a new one.
	CommitAndStop() //提交当前的读写事务,之后并不会再打开新的读写事务
}

批量读写事务的抽象

type IgnoreKey

type IgnoreKey struct {
	Bucket string
	Key    string
}

type ReadTx

type ReadTx interface {
	Lock()
	Unlock()

	UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vals [][]byte) //在指定的Bucket中进行范围查找
	UnsafeForEach(bucketName []byte, visitor func(k, v []byte) error) error                        //遍历指定Bucket中的全部键值对
}

只读事务的抽象

type Snapshot

type Snapshot interface {
	// Size gets the size of the snapshot.
	Size() int64
	// WriteTo writes the snapshot into the given writer.
	WriteTo(w io.Writer) (n int64, err error)
	// Close closes the snapshot.
	Close() error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL