Load a chunk from ram or creates it
(id RequestID)
| 333 | |
| 334 | // Load a chunk from ram or creates it |
| 335 | func (s *Storage) Load(id RequestID) []byte { |
| 336 | s.lock.RLock() |
| 337 | chunk := s.fetch(id) |
| 338 | if nil == chunk { |
| 339 | Log.Tracef("Load chunk %v (missing)", id) |
| 340 | s.lock.RUnlock() |
| 341 | return nil |
| 342 | } |
| 343 | if chunk.clean { |
| 344 | Log.Tracef("Load chunk %v (clean)", id) |
| 345 | defer s.lock.RUnlock() |
| 346 | return chunk.bytes |
| 347 | } |
| 348 | s.lock.RUnlock() |
| 349 | // Switch to write lock to avoid races on crc verification |
| 350 | s.lock.Lock() |
| 351 | defer s.lock.Unlock() |
| 352 | if chunk.valid(id) { |
| 353 | Log.Debugf("Load chunk %v (verified)", id) |
| 354 | return chunk.bytes |
| 355 | } |
| 356 | Log.Warningf("Load chunk %v (bad checksum: %08x <> %08x)", id, chunk.checksum, chunk.calculateChecksum()) |
| 357 | s.stack.Purge(chunk.item) |
| 358 | return nil |
| 359 | } |
| 360 | |
| 361 | // Store stores a chunk in the RAM and adds it to the disk storage queue |
| 362 | func (s *Storage) Store(id RequestID, bytes []byte) (err error) { |
no test coverage detected