示例#1
0
void test(LRUCache<int>& lruCache){
    lruCache.insert("one",1);
    lruCache.insert("two",2);
    lruCache.insert("three",3);
    lruCache.insert("four",4);
    lruCache.insert("five",5);
    lruCache.insert("six",6);
    lruCache.insert("seven",7);
    lruCache.insert("eight",8);
    lruCache.insert("nine",9);
    lruCache.insert("ten",10);
    std::cout << "check " << 1 << ":" << lruCache.get("one") << std::endl;
    std::cout << "check " << 11 << ":" << lruCache.get("eleven")<< std::endl;
    lruCache.insert("eleven",11);
    std::cout << "check " << 2 << ":" << lruCache.get("two")<< std::endl;
    std::cout << "check " << 11 << ":" << lruCache.get("eleven")<< std::endl;
    lruCache.insert("twelve",12);
    std::cout << "check " << 12 << ":" << lruCache.get("twelve")<< std::endl;
    std::cout << "check " << 3 << ":" << lruCache.get("three")<< std::endl;
    std::cout << "check " << 13 << ":" << lruCache.get("thirteen")<< std::endl;
    return;
}
示例#2
0
//コメントアウト部をonにすればキャッシュにない画像にアクセスすることになるためエラーを吐くようになっている
void imageTest(LRUCache<cv::Mat>& lruCache){
    std::string filepath1 = "Sample Pictures\\angkor-wat.jpg";
    std::string filepath2 = "Sample Pictures\\castle.jpg";
    std::string filepath3 = "Sample Pictures\\Chrysanthemum.jpg";
    std::string filepath4 = "Sample Pictures\\Desert.jpg";
    std::string filepath5 = "Sample Pictures\\Hydrangeas.jpg";
    std::string filepath6 = "Sample Pictures\\Jellyfish.jpg";
    std::string filepath7 = "Sample Pictures\\Koala.jpg";
    std::string filepath8 = "Sample Pictures\\Lake.jpg";
    std::string filepath9 = "Sample Pictures\\Lighthouse.jpg";
    std::string filepath10 = "Sample Pictures\\Penguins.jpg";
    std::string filepath11 = "Sample Pictures\\Statue.jpg";
    std::string filepath12 = "Sample Pictures\\Tulips.jpg";
    
    cv::Mat img1 = cv::imread(filepath1,1);
    cv::Mat img2 = cv::imread(filepath2,1);
    cv::Mat img3 = cv::imread(filepath3,1);
    cv::Mat img4 = cv::imread(filepath4,1);
    cv::Mat img5 = cv::imread(filepath5,1);
    cv::Mat img6 = cv::imread(filepath6,1);
    cv::Mat img7 = cv::imread(filepath7,1);
    cv::Mat img8 = cv::imread(filepath8,1);
    cv::Mat img9 = cv::imread(filepath9,1);
    cv::Mat img10 = cv::imread(filepath10,1);
    cv::Mat img11 = cv::imread(filepath11,1);
    cv::Mat img12 = cv::imread(filepath12,1);
    
    lruCache.insert(filepath1,img1);
    lruCache.insert(filepath2,img2);
    lruCache.insert(filepath3,img3);
    lruCache.insert(filepath4,img4);
    lruCache.insert(filepath5,img5);
    lruCache.insert(filepath6,img6);
    lruCache.insert(filepath7,img7);
    lruCache.insert(filepath8,img8);
    lruCache.insert(filepath9,img9);
    lruCache.insert(filepath10,img10);

    cv::namedWindow("image1", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
    cv::imshow("image1",lruCache.get(filepath1));
    cv::waitKey(0);
    
    //cv::namedWindow("image2", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
    //cv::imshow("image2",lruCache.get(filepath11));
    //cv::waitKey(0);
    
    lruCache.insert(filepath11,img11);
    cv::namedWindow("image3", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
    cv::imshow("image3",lruCache.get(filepath11));
    cv::waitKey(0);
    
    lruCache.insert(filepath12,img12);
    //cv::namedWindow("image4", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
 //   cv::imshow("image4",lruCache.get(filepath2));
    //cv::waitKey(0);
    
    cv::namedWindow("image5", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
    cv::imshow("image5",lruCache.get(filepath12));
    cv::waitKey(0);
    
    return;
}
示例#3
0
uint32 Storage::WriteData(HANDLE rDataFileHandle, LRUCache &rLRUCache, uint64 x, uint64 y, const uint8 *pRecord, uint16 recordSize)
{
    //ret value
    uint32 status;
    
    //write accesor
	RecordIndexMap::accessor rWriteAccesor;
    
    //update LRU
    rLRUCache.put(x);
    
    //try insert - returns false if record is existing
    if(m_dataIndexes.insert(rWriteAccesor, x))
    {
        //allocate blockmanager
        //create block manager + add new block for write + update num of blocks
        rWriteAccesor->second.m_pBlockManager = blman_create(NULL, 0);
        rWriteAccesor->second.m_blockCount = rWriteAccesor->second.m_pBlockManager->blockCount;
        
        //init index block
        rWriteAccesor->second.m_IB_blockNumber = 0;
        rWriteAccesor->second.m_IB_recordOffset = -1;
        //set flags to eRIF_RealocateBlocks
        rWriteAccesor->second.m_flags = eRIF_RelocateBlocks | eRIF_Dirty;
        //write data to block
        status = blman_write_record(rWriteAccesor->second.m_pBlockManager, y, pRecord, recordSize);
        
        //set record start to -1 (data are not written on disk)
        rWriteAccesor->second.m_recordStart = -1;
            
        //update memory usage
        uint64 blockManMemoryUsage = blman_get_memory_usage(rWriteAccesor->second.m_pBlockManager);
        m_memoryUsed += blockManMemoryUsage;
               
        //queue write to disk
        m_pDiskWriter->Queue(rWriteAccesor);
    }
	else
	{
        //now we have locked write access, just update data
        //check manager - load from disk if not in memory
        CheckBlockManager(rDataFileHandle, x, rWriteAccesor);
        
        //save memory usage before write
        uint64 memoryUsageBeforeWrite = blman_get_memory_usage(rWriteAccesor->second.m_pBlockManager);
        
        //try to write record
        status = blman_write_record(rWriteAccesor->second.m_pBlockManager, y, pRecord, recordSize);
        if(status == eBMS_FailRecordTooBig)
        {
            Log.Warning(__FUNCTION__, "Record [x:" I64FMTD ", y:" I64FMTD "] size: %u is too big, max record size is: %u.", x, y, recordSize, MAX_RECORD_SIZE);
        }
        else if(status & eBMS_OldRecord)
        {
            //update memory usage
            uint64 memoryUsageAfterWrite = blman_get_memory_usage(rWriteAccesor->second.m_pBlockManager);
            m_memoryUsed += (memoryUsageAfterWrite - memoryUsageBeforeWrite);
            
            //log warning
            Log.Warning(__FUNCTION__, "Record [x:" I64FMTD ", y:" I64FMTD "] size: %u attempt to save old y.", x, y, recordSize);
        }
        else if(status & eBMS_NeedDefrag)
        {
            //if data needs defrag
            //this function takes care about freespace, memory usage, disk writer, sets correct flags
            DefragmentDataInternal(rWriteAccesor);
        }
        else if(status & eBMS_ReallocatedBlocks)
        {
            if(!(rWriteAccesor->second.m_flags & eRIF_RelocateBlocks))
            {
                //set flag reallocate blocks so diskwrite must find new place for data
                rWriteAccesor->second.m_flags |= eRIF_RelocateBlocks;
            }
            
            //set dirty
            rWriteAccesor->second.m_flags |= eRIF_Dirty;
            
            //update memory usage
            uint64 memoryUsageAfterWrite = blman_get_memory_usage(rWriteAccesor->second.m_pBlockManager);
            m_memoryUsed += (memoryUsageAfterWrite - memoryUsageBeforeWrite);
            
            //queue write to disk
            m_pDiskWriter->Queue(rWriteAccesor);
        }
        else
        {
            //update memory usage
            uint64 memoryUsageAfterWrite = blman_get_memory_usage(rWriteAccesor->second.m_pBlockManager);
            m_memoryUsed += (memoryUsageAfterWrite - memoryUsageBeforeWrite);
            
            //queue write to disk
            m_pDiskWriter->Queue(rWriteAccesor);
        }
	}
    
    //log
    Log.Debug(__FUNCTION__, "Written data [x:" I64FMTD ", y:" I64FMTD "] size: %u with status: %u", x, y, recordSize, status);

    return status;
}
示例#4
0
void test2(LRUCache<int>& lruCache){
    lruCache.insert("1one",101);
    lruCache.insert("1two",102);
    lruCache.insert("1three",103);
    lruCache.insert("1four",104);
    lruCache.insert("1five",105);
    lruCache.insert("1six",106);
    lruCache.insert("1seven",107);
    lruCache.insert("1eight",108);
    lruCache.insert("1nine",109);
    lruCache.insert("1ten",110);
    std::cout << "check " << 101 << ":" << lruCache.get("1one") << std::endl;
    std::cout << "check " << 111 << ":" << lruCache.get("1eleven")<< std::endl;
    lruCache.insert("1eleven",111);
    std::cout << "check " << 102 << ":" << lruCache.get("1two")<< std::endl;
    std::cout << "check " << 111 << ":" << lruCache.get("1eleven")<< std::endl;
    lruCache.insert("1twelve",112);
    std::cout << "check " << 112 << ":" << lruCache.get("1twelve")<< std::endl;
    std::cout << "check " << 103 << ":" << lruCache.get("1three")<< std::endl;
    std::cout << "check " << 113 << ":" << lruCache.get("1thirteen")<< std::endl;
    return;
}
void testLRUCache()
{
	int i;
	LRUCache c;

	int t[100];
	for (i = 0; i < 100; ++i) t[i] = 1000+i;
	char* k[100];
	for (i = 0; i < 100; ++i) {
		k[i] = new char[5];
		sprintf(k[i], "k%03d", i);
	}

	bool r;
	void* p;
	const char* k2;

	r = c.Lookup("k050", p);
	assert(!r);

	c.SetAt("k050", &t[50]);
	r = c.Lookup("k050", p);
	assert(r);
	assert(p == &t[50]);

	for (i = 0; i < 100; ++i)
		c.SetAt(k[i], &t[i]);

	r = c.getLRU(0, k2, p);
	assert(r);
	assert(strcmp(k2, "k000") == 0);
	assert(p == &t[0]);

	c.Touch("k000");
	r = c.getLRU(0, k2, p);
	assert(r);
	assert(strcmp(k2, "k001") == 0);
	assert(p == &t[1]);

	r = c.getLRU(1, k2, p);
	assert(r);
	assert(strcmp(k2, "k002") == 0);
	assert(p == &t[2]);

	c.Remove("k001");

	r = c.getLRU(0, k2, p);
	assert(r);
	assert(strcmp(k2, "k002") == 0);
	assert(p == &t[2]);

	for (i = 0; i < 98; ++i) {
		r = c.getLRU(0, k2, p);
		assert(r);
		assert(strcmp(k2, k[2+i]) == 0);
		assert(p == &t[2+i]);
		c.Remove(k2);
	}

	assert(c.GetCount() == 1);

	r = c.getLRU(0, k2, p);
	assert(r);
	assert(strcmp(k2, "k000") == 0);
	assert(p == &t[0]);

	assert(!c.getLRU(1, k2, p));
}