memStore consistently returns the same Chunk object.

This commit is contained in:
Daniel A. Nagy 2015-02-13 16:01:53 +01:00
parent e787f4c885
commit b0d7fa2c7a
3 changed files with 16 additions and 13 deletions

View file

@ -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]) // dpaLogger.Debugf("chunk data received for %x", chunk.Key[:4])
} }
if len(chunk.Data) == 0 { if len(chunk.Data) == 0 {
// dpaLogger.Debugf("No payload.") dpaLogger.Debugf("No payload in %x.", chunk.Key)
return 0, notFound return 0, notFound
} }
self.size = chunk.Size self.size = chunk.Size

View file

@ -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) { func (s *memStore) Put(entry *Chunk) {
if s.capacity == 0 { if s.capacity == 0 {
@ -193,13 +194,15 @@ func (s *memStore) Put(entry *Chunk) {
if node.entry.Key.isEqual(entry.Key) { if node.entry.Key.isEqual(entry.Key) {
node.updateAccess(s.accessCnt) node.updateAccess(s.accessCnt)
if node.entry.Data == nil { if entry.Data == nil {
node.entry.Size = entry.Size entry.Size = node.entry.Size
node.entry.Data = entry.Data entry.Data = node.entry.Data
} }
if node.entry.req == nil { if entry.req == nil {
node.entry.req = entry.req entry.req = node.entry.req
} }
entry.C = node.entry.C
node.entry = entry
return return
} }
@ -253,11 +256,7 @@ func (s *memStore) Get(hash Key) (chunk *Chunk, err error) {
if node.entry.Key.isEqual(hash) { if node.entry.Key.isEqual(hash) {
s.accessCnt++ s.accessCnt++
node.updateAccess(s.accessCnt) node.updateAccess(s.accessCnt)
chunk = &Chunk{ chunk = node.entry
Key: hash,
Data: node.entry.Data,
Size: node.entry.Size,
}
if s.dbAccessCnt-node.lastDBaccess > dbForceUpdateAccessCnt { if s.dbAccessCnt-node.lastDBaccess > dbForceUpdateAccessCnt {
s.dbAccessCnt++ s.dbAccessCnt++
node.lastDBaccess = s.dbAccessCnt node.lastDBaccess = s.dbAccessCnt

View file

@ -68,7 +68,7 @@ func (self *NetStore) Put(entry *Chunk) {
func (self *NetStore) put(entry *Chunk) { func (self *NetStore) put(entry *Chunk) {
self.localStore.Put(entry) 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) go self.store(entry)
// only send responses once // only send responses once
dpaLogger.Debugf("NetStore.put: req: %#v", entry.req) dpaLogger.Debugf("NetStore.put: req: %#v", entry.req)
@ -82,7 +82,9 @@ func (self *NetStore) put(entry *Chunk) {
func (self *NetStore) addStoreRequest(req *storeRequestMsgData) { func (self *NetStore) addStoreRequest(req *storeRequestMsgData) {
self.lock.Lock() self.lock.Lock()
defer self.lock.Unlock() defer self.lock.Unlock()
dpaLogger.Debugf("NetStore.addStoreRequest: req = %#v", req)
chunk, err := self.localStore.Get(req.Key) 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 // we assume that a returned chunk is the one stored in the memory cache
if err != nil { if err != nil {
chunk = &Chunk{ chunk = &Chunk{
@ -91,6 +93,7 @@ func (self *NetStore) addStoreRequest(req *storeRequestMsgData) {
Size: int64(req.Size), Size: int64(req.Size),
} }
} else if chunk.Data == nil { } else if chunk.Data == nil {
// response to a search request
chunk.Data = req.Data chunk.Data = req.Data
chunk.Size = int64(req.Size) chunk.Size = int64(req.Size)
} else { } else {
@ -116,7 +119,7 @@ func (self *NetStore) Get(key Key) (chunk *Chunk, err error) {
dpaLogger.Debugf("NetStore.Get: %064x request time out ", key) dpaLogger.Debugf("NetStore.Get: %064x request time out ", key)
err = notFound err = notFound
case <-chunk.req.C: 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 return
@ -137,6 +140,7 @@ func (self *NetStore) get(key Key) (chunk *Chunk) {
if chunk.req == nil { if chunk.req == nil {
chunk.req = new(requestStatus) chunk.req = new(requestStatus)
chunk.req.C = make(chan bool)
} }
return return
} }