From b0d7fa2c7a5523f37b824f73fee7b440460b7792 Mon Sep 17 00:00:00 2001 From: "Daniel A. Nagy" Date: Fri, 13 Feb 2015 16:01:53 +0100 Subject: [PATCH] memStore consistently returns the same Chunk object. --- bzz/chunker.go | 2 +- bzz/memstore.go | 19 +++++++++---------- bzz/netstore.go | 8 ++++++-- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/bzz/chunker.go b/bzz/chunker.go index 1838242d94..5d543c68bc 100644 --- a/bzz/chunker.go +++ b/bzz/chunker.go @@ -318,7 +318,7 @@ func (self *LazyChunkReader) ReadAt(b []byte, off int64) (read int, err error) { // dpaLogger.Debugf("chunk data received for %x", chunk.Key[:4]) } if len(chunk.Data) == 0 { - // dpaLogger.Debugf("No payload.") + dpaLogger.Debugf("No payload in %x.", chunk.Key) return 0, notFound } self.size = chunk.Size diff --git a/bzz/memstore.go b/bzz/memstore.go index ce012222d4..0fab5462e1 100644 --- a/bzz/memstore.go +++ b/bzz/memstore.go @@ -159,6 +159,7 @@ func (s *memStore) getEntryCnt() uint { } +// entry (not its copy) is going to be in memStore func (s *memStore) Put(entry *Chunk) { if s.capacity == 0 { @@ -193,13 +194,15 @@ func (s *memStore) Put(entry *Chunk) { if node.entry.Key.isEqual(entry.Key) { node.updateAccess(s.accessCnt) - if node.entry.Data == nil { - node.entry.Size = entry.Size - node.entry.Data = entry.Data + if entry.Data == nil { + entry.Size = node.entry.Size + entry.Data = node.entry.Data } - if node.entry.req == nil { - node.entry.req = entry.req + if entry.req == nil { + entry.req = node.entry.req } + entry.C = node.entry.C + node.entry = entry return } @@ -253,11 +256,7 @@ func (s *memStore) Get(hash Key) (chunk *Chunk, err error) { if node.entry.Key.isEqual(hash) { s.accessCnt++ node.updateAccess(s.accessCnt) - chunk = &Chunk{ - Key: hash, - Data: node.entry.Data, - Size: node.entry.Size, - } + chunk = node.entry if s.dbAccessCnt-node.lastDBaccess > dbForceUpdateAccessCnt { s.dbAccessCnt++ node.lastDBaccess = s.dbAccessCnt diff --git a/bzz/netstore.go b/bzz/netstore.go index d4c3e6fe37..65381b091c 100644 --- a/bzz/netstore.go +++ b/bzz/netstore.go @@ -68,7 +68,7 @@ func (self *NetStore) Put(entry *Chunk) { func (self *NetStore) put(entry *Chunk) { self.localStore.Put(entry) - dpaLogger.Debugf("NetStore.put: localStore.Put of %064x completed.", entry.Key) + dpaLogger.Debugf("NetStore.put: localStore.Put of %064x completed, %d bytes (%p).", entry.Key, len(entry.Data), entry) go self.store(entry) // only send responses once dpaLogger.Debugf("NetStore.put: req: %#v", entry.req) @@ -82,7 +82,9 @@ func (self *NetStore) put(entry *Chunk) { func (self *NetStore) addStoreRequest(req *storeRequestMsgData) { self.lock.Lock() defer self.lock.Unlock() + dpaLogger.Debugf("NetStore.addStoreRequest: req = %#v", req) chunk, err := self.localStore.Get(req.Key) + dpaLogger.Debugf("NetStore.addStoreRequest: chunk reference %p", chunk) // we assume that a returned chunk is the one stored in the memory cache if err != nil { chunk = &Chunk{ @@ -91,6 +93,7 @@ func (self *NetStore) addStoreRequest(req *storeRequestMsgData) { Size: int64(req.Size), } } else if chunk.Data == nil { + // response to a search request chunk.Data = req.Data chunk.Size = int64(req.Size) } else { @@ -116,7 +119,7 @@ func (self *NetStore) Get(key Key) (chunk *Chunk, err error) { dpaLogger.Debugf("NetStore.Get: %064x request time out ", key) err = notFound case <-chunk.req.C: - dpaLogger.Debugf("NetStore.get: %064x retrieved", key) + dpaLogger.Debugf("NetStore.Get: %064x retrieved, %d bytes (%p)", key, len(chunk.Data), chunk) } return @@ -137,6 +140,7 @@ func (self *NetStore) get(key Key) (chunk *Chunk) { if chunk.req == nil { chunk.req = new(requestStatus) + chunk.req.C = make(chan bool) } return }