swarm/storage: fix ldbstore import to not include key twice

This commit is contained in:
Anton Evangelatov 2018-03-29 17:30:13 +02:00
parent 6d182b8bfc
commit 5abb02c218
3 changed files with 19 additions and 14 deletions

View file

@ -78,7 +78,10 @@ func Bytes2Hex(d []byte) string {
} }
func Hex2Bytes(str string) []byte { func Hex2Bytes(str string) []byte {
h, _ := hex.DecodeString(str) h, err := hex.DecodeString(str)
if err != nil {
panic(err)
}
return h return h
} }

View file

@ -59,6 +59,7 @@ const (
) )
var ( var (
keyIndex = byte(0)
keyOldData = byte(1) keyOldData = byte(1)
keyAccessCnt = []byte{2} keyAccessCnt = []byte{2}
keyEntryCnt = []byte{3} keyEntryCnt = []byte{3}
@ -207,7 +208,7 @@ func (s *LDBStore) updateIndexAccess(index *dpaDBIndex) {
func getIndexKey(hash Key) []byte { func getIndexKey(hash Key) []byte {
hashSize := len(hash) hashSize := len(hash)
key := make([]byte, hashSize+1) key := make([]byte, hashSize+1)
key[0] = 0 key[0] = keyIndex
copy(key[1:], hash[:]) copy(key[1:], hash[:])
return key return key
} }
@ -364,11 +365,13 @@ func (s *LDBStore) Export(out io.Writer) (int64, error) {
} }
var index dpaDBIndex var index dpaDBIndex
decodeIndex(it.Value(), &index)
hash := key[1:] hash := key[1:]
decodeIndex(it.Value(), &index)
data, err := s.db.Get(getDataKey(index.Idx, s.po(hash))) po := s.po(hash)
datakey := getDataKey(index.Idx, po)
log.Trace("store.export", "dkey", fmt.Sprintf("%x", datakey), "dataidx", index.Idx, "po", po)
data, err := s.db.Get(datakey)
if err != nil { if err != nil {
log.Warn(fmt.Sprintf("Chunk %x found but could not be accessed: %v", key[:], err)) log.Warn(fmt.Sprintf("Chunk %x found but could not be accessed: %v", key[:], err))
continue continue
@ -421,7 +424,7 @@ func (s *LDBStore) Import(in io.Reader) (int64, error) {
return count, err return count, err
} }
chunk := NewChunk(key, nil) chunk := NewChunk(key, nil)
chunk.SData = data chunk.SData = data[32:]
s.Put(chunk) s.Put(chunk)
wg.Add(1) wg.Add(1)
go func() { go func() {
@ -559,10 +562,10 @@ func (s *LDBStore) Put(chunk *Chunk) {
s.lock.Lock() s.lock.Lock()
defer s.lock.Unlock() defer s.lock.Unlock()
log.Trace("ldbstore.put: s.db.Get", "key", chunk.Key) log.Trace("ldbstore.put: s.db.Get", "key", chunk.Key, "ikey", fmt.Sprintf("%x", ikey))
idata, err := s.db.Get(ikey) idata, err := s.db.Get(ikey)
if err != nil { if err != nil {
s.doPut(chunk, ikey, &index, po) s.doPut(chunk, &index, po)
batchC := s.batchC batchC := s.batchC
go func() { go func() {
<-batchC <-batchC
@ -584,10 +587,10 @@ func (s *LDBStore) Put(chunk *Chunk) {
} }
// force putting into db, does not check access index // force putting into db, does not check access index
func (s *LDBStore) doPut(chunk *Chunk, ikey []byte, index *dpaDBIndex, po uint8) { func (s *LDBStore) doPut(chunk *Chunk, index *dpaDBIndex, po uint8) {
log.Trace("ldbstore.doPut", "key", chunk.Key)
data := s.encodeDataFunc(chunk) data := s.encodeDataFunc(chunk)
s.batch.Put(getDataKey(s.dataIdx, po), data) dkey := getDataKey(s.dataIdx, po)
s.batch.Put(dkey, data)
index.Idx = s.dataIdx index.Idx = s.dataIdx
s.bucketCnt[po] = s.dataIdx s.bucketCnt[po] = s.dataIdx
s.entryCnt++ s.entryCnt++
@ -597,7 +600,6 @@ func (s *LDBStore) doPut(chunk *Chunk, ikey []byte, index *dpaDBIndex, po uint8)
cntKey[0] = keyDistanceCnt cntKey[0] = keyDistanceCnt
cntKey[1] = po cntKey[1] = po
s.batch.Put(cntKey, U64ToBytes(s.bucketCnt[po])) s.batch.Put(cntKey, U64ToBytes(s.bucketCnt[po]))
} }
func (s *LDBStore) writeBatches() { func (s *LDBStore) writeBatches() {
@ -690,7 +692,7 @@ func (s *LDBStore) get(key Key) (chunk *Chunk, err error) {
proximity := s.po(key) proximity := s.po(key)
datakey := getDataKey(indx.Idx, proximity) datakey := getDataKey(indx.Idx, proximity)
data, err = s.db.Get(datakey) data, err = s.db.Get(datakey)
log.Trace("ldbstore.get retrieve", "key", key, "indexkey", indx.Idx, "datakey", datakey, "proximity", proximity) log.Trace("ldbstore.get retrieve", "key", key, "indexkey", indx.Idx, "datakey", fmt.Sprintf("%x", datakey), "proximity", proximity)
if err != nil { if err != nil {
log.Trace("ldbstore.get chunk found but could not be accessed", "key", key, "err", err) log.Trace("ldbstore.get chunk found but could not be accessed", "key", key, "err", err)
s.delete(indx.Idx, getIndexKey(key), s.po(key)) s.delete(indx.Idx, getIndexKey(key), s.po(key))

View file

@ -118,7 +118,7 @@ func NewSwarm(ctx *node.ServiceContext, backend chequebook.Backend, config *api.
log.Debug(fmt.Sprintf("Setting up Swarm service components")) log.Debug(fmt.Sprintf("Setting up Swarm service components"))
hash := storage.MakeHashFunc(config.ChunkerParams.Hash) hash := storage.MakeHashFunc(config.ChunkerParams.Hash)
self.lstore, err = storage.NewLocalStore(hash, config.StoreParams, common.Hex2Bytes(config.BzzKey), mockStore) self.lstore, err = storage.NewLocalStore(hash, config.StoreParams, common.FromHex(config.BzzKey), mockStore)
if err != nil { if err != nil {
return return
} }