bool saveCacheIndex(bool fast) { //clean all DEAD obj cache.syncDisk(); #ifdef ENABLE_DB_DISK_INDEX if (dci) { dci->start(ci_close,NULL); while (!dci->allWorkedDone()) { my_msleep(100); } return false; } #endif HttpObjectIndexHeader indexHeader; memset(&indexHeader,0,sizeof(HttpObjectIndexHeader)); indexHeader.head_size = sizeof(HttpObjectIndexHeader); indexHeader.block_size = sizeof(HttpObjectIndex); indexHeader.cache_dir_mask1 = CACHE_DIR_MASK1; indexHeader.cache_dir_mask2 = CACHE_DIR_MASK2; char *file_name = getCacheIndexFile(); KFile fp; bool result = true; if(!fp.open(file_name,fileWrite)){ klog(KLOG_ERR,"cann't open cache index file for write [%s]\n",file_name); xfree(file_name); return false; } fp.write((char *)&indexHeader,sizeof(HttpObjectIndexHeader)); int save_count = cache.save_disk_index(&fp); if(result){ //set the index file state is clean indexHeader.state = INDEX_STATE_CLEAN; indexHeader.object_count = save_count; fp.seek(0,seekBegin); fp.write((char *)&indexHeader,sizeof(HttpObjectIndexHeader)); klog(KLOG_ERR,"total save %d object\n",save_count); } if(file_name){ xfree(file_name); } return result; }
bool loadCacheIndex() { load_count = 0; bool result = false; char *file_name = getCacheIndexFile(); #if 0 rebuild_cache_hash = true; #endif KFile fp; if (!fp.open(file_name,fileReadWrite)) { xfree(file_name); #ifdef ENABLE_DB_DISK_INDEX if (dci) { return true; } #endif recreate_index(time(NULL)); return false; } HttpObjectIndexHeader indexHeader; if(fp.read((char *)&indexHeader,sizeof(HttpObjectIndexHeader))!=sizeof(HttpObjectIndexHeader)){ klog(KLOG_ERR,"cann't read cache index header\n"); recreate_index(time(NULL)); goto done; } #if 0 if (indexHeader.cache_dir_mask1==CACHE_DIR_MASK1 && indexHeader.cache_dir_mask2==CACHE_DIR_MASK2) { rebuild_cache_hash = false; } #endif if(indexHeader.state != INDEX_STATE_CLEAN #if 0 || rebuild_cache_hash #endif ){ klog(KLOG_ERR,"cache index file not clean.\n"); recreate_index(time(NULL)); goto done; } for(;;){ KHttpObject *obj = new KHttpObject; if (fp.read((char *)&obj->index,sizeof(HttpObjectIndex))!=sizeof(HttpObjectIndex)) { obj->release(); break; } if (create_http_object(&fp,obj)==cor_success) { load_count++; #ifdef ENABLE_DB_DISK_INDEX if (dci) { dci->start(ci_add,obj); } #endif } else { SET(obj->index.flags,FLAG_DEAD|FLAG_IN_DISK); } obj->release(); } result = true; if(load_count!=indexHeader.object_count){ klog(KLOG_ERR,"Warning not all obj have loaded,total obj count=%d,loaded=%d\n",indexHeader.object_count,load_count); debug("total object count=%d\n",cache.getCount()); } //设置index为notclean //fseeko(fp,0,SEEK_SET); fp.seek(0,seekBegin); indexHeader.state = INDEX_STATE_UNCLEAN; fp.write((char *)&indexHeader,sizeof(HttpObjectIndexHeader)); done: if(file_name){ xfree(file_name); } klog(KLOG_ERR,"total load disk obj count=%d\n",load_count); return result; }