servlib/bundling/BPQCache.cc
changeset 74 a86a8cfe49c8
parent 73 46ccb2af4459
child 75 d647d52028bc
--- a/servlib/bundling/BPQCache.cc	Tue Jan 10 17:03:46 2012 +0000
+++ b/servlib/bundling/BPQCache.cc	Tue Jan 10 17:50:20 2012 +0000
@@ -43,8 +43,7 @@
 	if ( iter == bpq_table_.end() ) {
 		log_debug("no response found in cache, create new cache entry");
 
-		create_cache_entry(bundle, block, key);
-		return true;
+		return create_cache_entry(bundle, block, key);
 
 	} else {
 		log_debug("response found in cache");
@@ -59,8 +58,7 @@
 				log_debug("received bundle is newer than cached one: "
 						  "replace cache entry");
 
-				replace_cache_entry(entry, bundle, block, key);
-				return true;
+				return replace_cache_entry(entry, bundle, block, key);
 
 			} else {
 				log_debug("cached bundle is newer than received one: "
@@ -187,9 +185,16 @@
 
 
 //----------------------------------------------------------------------
-void
+bool
 BPQCache::create_cache_entry(Bundle* bundle, BPQBlock* block, std::string key)
 {
+	if (cache_size_ < bundle->payload().length()) {
+		log_warn("bundle too large to add to cache {cache size: %u, bundle size: %u}",
+				cache_size_, bundle->payload().length());
+
+		return false;
+	}
+
 	if ( bundle->is_fragment() ) {
 		log_debug("creating new cache entry for bundle fragment "
 				  "{key: %s, offset: %u, length: %u}",
@@ -214,22 +219,22 @@
 	bpq_table_[key] = entry;
 	cache_size_ += entry->entry_size();
 	update_lru_keys(key);
+
+	return true;
 }
 
 //----------------------------------------------------------------------
-void
+bool
 BPQCache::replace_cache_entry(BPQCacheEntry* entry, Bundle* bundle,
 							  BPQBlock* block, std::string key)
 {
 	ASSERT ( ! bundle->is_fragment() );
 	log_debug("Remove existing cache entry");
 
-
 	remove_cache_entry(entry, key);
 
-
 	log_debug("Create new cache entry");
-	create_cache_entry(bundle, block, key);
+	return create_cache_entry(bundle, block, key);
 }
 
 //----------------------------------------------------------------------
@@ -259,6 +264,13 @@
 {
 	ASSERT( bundle->is_fragment() );
 
+	if (cache_size_ < bundle->payload().length()) {
+		log_warn("bundle too large to add to cache {cache size: %u, bundle size: %u}",
+				cache_size_, bundle->payload().length());
+
+		return false;
+	}
+
 	log_debug("appending received bundle fragment to cache {offset: %u, length: %u}",
 			  bundle->frag_offset(), bundle->payload().length());
 
@@ -375,6 +387,10 @@
 	while (cache_size_ > BPQCache::max_cache_size_) {
 		std::string lru = lru_keys_.back();
 
+		// don't remove the key that's being updated
+		if (lru == key)
+			break;
+
 		Cache::iterator cache_iter = bpq_table_.find(lru);
 
 		if ( cache_iter != bpq_table_.end() ) {