Пример #1
0
func (cdbm *LruCdbm) MakeSpace(objectSize, sizeLimit int, f CacheCleanFunc) (base.CacheDirectoryBlock, error) {
	if sizeLimit < objectSize {
		return nil, ObjectTooBig
	}

	//there is nothing
	if len(cdbm.Hash) == 0 {
		return nil, nil
	}

	var repl base.CacheDirectoryBlock

	for avail := sizeLimit - cdbm.Size; objectSize > avail; avail = sizeLimit - cdbm.Size {
		lru := cdbm.cdbs.Front()
		if lru == nil {
			panic("cdbs is empty with nonzero size")
		}
		repl = cdbm.cdbs.Remove(lru).(base.CacheDirectoryBlock)
		evObject := repl.GetObject()
		cdbm.Size -= evObject.Size()
		if f != nil && evObject != nil {
			f(evObject)
		}
		delete(cdbm.Hash, repl.GetKey())
	}
	return repl, nil
}
Пример #2
0
func (cdbm *ArcCdbm) MakeSpace(objectSize, sizeLimit int, f CacheCleanFunc) (base.CacheDirectoryBlock, error) {
	if sizeLimit < objectSize {
		return nil, ObjectTooBig
	}

	var cdb base.CacheDirectoryBlock
	for avail := sizeLimit - cdbm.Size; objectSize > avail; avail = sizeLimit - cdbm.Size {
		if cdbm.t1.size+cdbm.b1.size+objectSize >= sizeLimit {
			if cdbm.b1.size > 0 {
				cdb = cdbm.b1.RemoveLRU()
				cdbm.evict(f)
			} else {
				cdb = cdbm.t1.RemoveLRU()
				object := cdb.GetObject()
				cdbm.Size -= object.Size()
				if f != nil {
					f(object)
				}
			}
			delete(cdbm.Hash, cdb.GetKey())
		} else if cdbm.t1.size+cdbm.t2.size+cdbm.b1.size+cdbm.b2.size+objectSize >= sizeLimit {
			if cdbm.t1.size+cdbm.t2.size+cdbm.b1.size+cdbm.b2.size+objectSize >= sizeLimit*2 {
				cdb = cdbm.b2.RemoveLRU()
				delete(cdbm.Hash, cdb.GetKey())
			} else {
				cdb = newCacheDirectorBlock()
			}
			cdbm.evict(f)
		} else {
			cdb = newCacheDirectorBlock()
		}
	}
	if cdb == nil {
		cdb = newCacheDirectorBlock()
	}
	return cdb, nil
}