diff options
author | Janos Guljas <janos@resenje.org> | 2018-02-23 01:50:47 +0800 |
---|---|---|
committer | Janos Guljas <janos@resenje.org> | 2018-02-23 01:51:34 +0800 |
commit | 6a9730edaa3c398ef1e9fe084f9b16de4d3ef78e (patch) | |
tree | 442ae3e5d75fa5418b362474754d319a7fdfa8f0 /light/nodeset.go | |
parent | a3a07350dcef0ba39829a20d8ddba4bd3463d293 (diff) | |
parent | 221486a29109803286c1448426d6180ef5024cf0 (diff) | |
download | dexon-6a9730edaa3c398ef1e9fe084f9b16de4d3ef78e.tar dexon-6a9730edaa3c398ef1e9fe084f9b16de4d3ef78e.tar.gz dexon-6a9730edaa3c398ef1e9fe084f9b16de4d3ef78e.tar.bz2 dexon-6a9730edaa3c398ef1e9fe084f9b16de4d3ef78e.tar.lz dexon-6a9730edaa3c398ef1e9fe084f9b16de4d3ef78e.tar.xz dexon-6a9730edaa3c398ef1e9fe084f9b16de4d3ef78e.tar.zst dexon-6a9730edaa3c398ef1e9fe084f9b16de4d3ef78e.zip |
swarm, cmd/swarm: Merge branch 'master' into multiple-ens-endpoints
Diffstat (limited to 'light/nodeset.go')
-rw-r--r-- | light/nodeset.go | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/light/nodeset.go b/light/nodeset.go index ffdb71bb7..6f25219c1 100644 --- a/light/nodeset.go +++ b/light/nodeset.go @@ -1,4 +1,4 @@ -// Copyright 2014 The go-ethereum Authors +// 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 @@ -29,7 +29,9 @@ import ( // NodeSet stores a set of trie nodes. It implements trie.Database and can also // act as a cache for another trie.Database. type NodeSet struct { - db map[string][]byte + nodes map[string][]byte + order []string + dataSize int lock sync.RWMutex } @@ -37,7 +39,7 @@ type NodeSet struct { // NewNodeSet creates an empty node set func NewNodeSet() *NodeSet { return &NodeSet{ - db: make(map[string][]byte), + nodes: make(map[string][]byte), } } @@ -46,10 +48,15 @@ func (db *NodeSet) Put(key []byte, value []byte) error { db.lock.Lock() defer db.lock.Unlock() - if _, ok := db.db[string(key)]; !ok { - db.db[string(key)] = common.CopyBytes(value) - db.dataSize += len(value) + if _, ok := db.nodes[string(key)]; ok { + return nil } + keystr := string(key) + + db.nodes[keystr] = common.CopyBytes(value) + db.order = append(db.order, keystr) + db.dataSize += len(value) + return nil } @@ -58,7 +65,7 @@ func (db *NodeSet) Get(key []byte) ([]byte, error) { db.lock.RLock() defer db.lock.RUnlock() - if entry, ok := db.db[string(key)]; ok { + if entry, ok := db.nodes[string(key)]; ok { return entry, nil } return nil, errors.New("not found") @@ -75,7 +82,7 @@ func (db *NodeSet) KeyCount() int { db.lock.RLock() defer db.lock.RUnlock() - return len(db.db) + return len(db.nodes) } // DataSize returns the aggregated data size of nodes in the set @@ -92,8 +99,8 @@ func (db *NodeSet) NodeList() NodeList { defer db.lock.RUnlock() var values NodeList - for _, value := range db.db { - values = append(values, value) + for _, key := range db.order { + values = append(values, db.nodes[key]) } return values } @@ -103,7 +110,7 @@ func (db *NodeSet) Store(target ethdb.Putter) { db.lock.RLock() defer db.lock.RUnlock() - for key, value := range db.db { + for key, value := range db.nodes { target.Put([]byte(key), value) } } |