Esempio n. 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
}
Esempio n. 2
0
func (cdbm *ArcCdbm) evict(f CacheCleanFunc) {
	var cdb base.CacheDirectoryBlock
	if cdbm.t1.size >= max(1, cdbm.target_t1) {
		cdb = cdbm.t1.RemoveLRU()
		acdb := cdb.(*ArcCdb)
		acdb.where = in_b1
		acdb.element = cdbm.b1.PushBack(acdb)
	} else {
		cdb = cdbm.t2.RemoveLRU()
		acdb := cdb.(*ArcCdb)
		acdb.where = in_b2
		acdb.element = cdbm.b2.PushBack(acdb)
	}
	object := cdb.GetObject()
	cdbm.Size -= object.Size()
	if f != nil {
		f(object)
	}
	cdb.SetObject(nil)
	return
}
Esempio n. 3
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
}