memory_database.go 3.04 KB
Newer Older
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 6 7 8
// 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.
//
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 13 14
// GNU Lesser General Public License for more details.
//
// 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

obscuren's avatar
obscuren committed
17 18 19
package ethdb

import (
20
	"errors"
obscuren's avatar
obscuren committed
21
	"fmt"
22
	"sync"
23

obscuren's avatar
obscuren committed
24
	"github.com/ethereum/go-ethereum/common"
obscuren's avatar
obscuren committed
25 26 27 28 29 30
)

/*
 * This is a test memory database. Do not use for any production it does not get persisted
 */
type MemDatabase struct {
31 32
	db   map[string][]byte
	lock sync.RWMutex
obscuren's avatar
obscuren committed
33 34 35
}

func NewMemDatabase() (*MemDatabase, error) {
36 37 38
	return &MemDatabase{
		db: make(map[string][]byte),
	}, nil
obscuren's avatar
obscuren committed
39 40
}

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

45
	db.db[string(key)] = common.CopyBytes(value)
46
	return nil
obscuren's avatar
obscuren committed
47 48
}

49
func (db *MemDatabase) Set(key []byte, value []byte) {
50 51 52
	db.lock.Lock()
	defer db.lock.Unlock()

53 54 55
	db.Put(key, value)
}

obscuren's avatar
obscuren committed
56
func (db *MemDatabase) Get(key []byte) ([]byte, error) {
57 58 59
	db.lock.RLock()
	defer db.lock.RUnlock()

60 61 62 63
	if entry, ok := db.db[string(key)]; ok {
		return entry, nil
	}
	return nil, errors.New("not found")
obscuren's avatar
obscuren committed
64 65
}

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

70 71 72 73 74 75 76
	keys := [][]byte{}
	for key, _ := range db.db {
		keys = append(keys, []byte(key))
	}
	return keys
}

77
/*
obscuren's avatar
obscuren committed
78
func (db *MemDatabase) GetKeys() []*common.Key {
79 80
	data, _ := db.Get([]byte("KeyRing"))

obscuren's avatar
obscuren committed
81
	return []*common.Key{common.NewKeyFromBytes(data)}
82
}
83
*/
84

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

89
	delete(db.db, string(key))
90 91 92
	return nil
}

obscuren's avatar
obscuren committed
93
func (db *MemDatabase) Print() {
94 95 96
	db.lock.RLock()
	defer db.lock.RUnlock()

obscuren's avatar
obscuren committed
97 98
	for key, val := range db.db {
		fmt.Printf("%x(%d): ", key, len(key))
obscuren's avatar
obscuren committed
99
		node := common.NewValueFromBytes(val)
100
		fmt.Printf("%q\n", node.Val)
obscuren's avatar
obscuren committed
101 102 103 104 105 106 107 108 109 110 111 112 113
	}
}

func (db *MemDatabase) Close() {
}

func (db *MemDatabase) LastKnownTD() []byte {
	data, _ := db.Get([]byte("LastKnownTotalDifficulty"))
	if len(data) == 0 || data == nil {
		data = []byte{0x0}
	}
	return data
}
114

Felix Lange's avatar
Felix Lange committed
115 116 117 118 119 120 121 122 123
func (db *MemDatabase) NewBatch() Batch {
	return &memBatch{db: db}
}

type kv struct{ k, v []byte }

type memBatch struct {
	db     *MemDatabase
	writes []kv
124
	lock   sync.RWMutex
Felix Lange's avatar
Felix Lange committed
125 126
}

127 128 129 130 131
func (b *memBatch) Put(key, value []byte) error {
	b.lock.Lock()
	defer b.lock.Unlock()

	b.writes = append(b.writes, kv{key, common.CopyBytes(value)})
Felix Lange's avatar
Felix Lange committed
132 133 134
	return nil
}

135 136 137 138
func (b *memBatch) Write() error {
	b.lock.RLock()
	defer b.lock.RUnlock()

139 140
	b.db.lock.Lock()
	defer b.db.lock.Unlock()
141 142 143

	for _, kv := range b.writes {
		b.db.db[string(kv.k)] = kv.v
Felix Lange's avatar
Felix Lange committed
144 145 146
	}
	return nil
}