github.com/m3shine/gochain@v2.2.26+incompatible/common/db.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package common 18 19 import ( 20 "errors" 21 "io" 22 ) 23 24 var ErrNotFound = errors.New("not found") 25 26 // Database wraps all database operations. All methods are safe for concurrent use. 27 type Database interface { 28 io.Closer 29 GlobalTable() Table 30 BodyTable() Table 31 HeaderTable() Table 32 ReceiptTable() Table 33 } 34 35 // Putter wraps the write operation supported by both batches and regular tables. 36 type Putter interface { 37 Put(key []byte, value []byte) error 38 } 39 40 // Deleter wraps the database delete operation supported by both batches and regular databases. 41 type Deleter interface { 42 Delete(key []byte) error 43 } 44 45 // Table wraps all mutation & accessor operations. All methods are safe for concurrent use. 46 type Table interface { 47 Putter 48 Get(key []byte) ([]byte, error) 49 Has(key []byte) (bool, error) 50 Delete(key []byte) error 51 NewBatch() Batch 52 } 53 54 // Batch is a write-only database that commits changes to its host database 55 // when Write is called. Batch cannot be used concurrently. 56 type Batch interface { 57 Putter 58 Deleter 59 ValueSize() int // amount of data in the batch 60 Write() error 61 // Reset resets the batch for reuse 62 Reset() 63 } 64 65 // TablePrefixer represents an wrapper for Database that prefixes all operations with a key prefix. 66 type TablePrefixer struct { 67 table Table 68 prefix string 69 } 70 71 // NewTablePrefixer returns a new instance of TablePrefixer. 72 func NewTablePrefixer(t Table, prefix string) *TablePrefixer { 73 return &TablePrefixer{table: t, prefix: prefix} 74 } 75 76 func (p *TablePrefixer) Put(key []byte, value []byte) error { 77 return p.table.Put(append([]byte(p.prefix), key...), value) 78 } 79 80 func (p *TablePrefixer) Has(key []byte) (bool, error) { 81 return p.table.Has(append([]byte(p.prefix), key...)) 82 } 83 84 func (p *TablePrefixer) Get(key []byte) ([]byte, error) { 85 return p.table.Get(append([]byte(p.prefix), key...)) 86 } 87 88 func (p *TablePrefixer) Delete(key []byte) error { 89 return p.table.Delete(append([]byte(p.prefix), key...)) 90 } 91 92 func (p *TablePrefixer) Close() error { return nil } 93 94 func (p *TablePrefixer) NewBatch() Batch { 95 return &TablePrefixerBatch{p.table.NewBatch(), p.prefix} 96 } 97 98 type TablePrefixerBatch struct { 99 batch Batch 100 prefix string 101 } 102 103 func (b *TablePrefixerBatch) Put(key, value []byte) error { 104 return b.batch.Put(append([]byte(b.prefix), key...), value) 105 } 106 107 func (b *TablePrefixerBatch) Delete(key []byte) error { 108 return b.batch.Delete(append([]byte(b.prefix), key...)) 109 } 110 111 func (b *TablePrefixerBatch) Write() error { 112 return b.batch.Write() 113 } 114 115 func (b *TablePrefixerBatch) ValueSize() int { 116 return b.batch.ValueSize() 117 } 118 119 func (b *TablePrefixerBatch) Reset() { 120 b.batch.Reset() 121 }