int ResortDB(char * db_file,struct URLDB * links,unsigned int loaded_links) { if ( !isURLDBSorted() ) { AmmServer_Warning("URLDB is not sorted Sorting it now..!\n"); qsort(links, loaded_links , sizeof(struct URLDB), struct_cmp_urldb_items); if ( !isURLDBSorted() ) { AmmServer_Warning("Could not sort URLDB ..! :( , exiting \n"); return 0; } else { AmmServer_Success("Sorted URLDB \n"); if ( ReWriteMyURLDBFile(db_file,links,loaded_links) ) { AmmServer_Success("Saved db file \n"); } } } else { AmmServer_Success("Presorted URLDB \n"); } return 1; }
//Binary Search inline unsigned int Find_longURL(char * shortURL,int * found) { #if USE_BINARY_SEARCH *found = 0; if (shortURL==0) { return 0; } if (loaded_links==0) { return 0; } if (sorted_links!=0) { unsigned long our_hash = hashURL(shortURL); unsigned int binarySearchLastElement = sorted_links; unsigned int beg=0,mid=0,fin=binarySearchLastElement-1; while ( beg <= fin ) { mid=(unsigned int) beg + ( (fin-beg)/2 ); if (mid >= binarySearchLastElement) { AmmServer_Error("Binary Search overflowed ( beg %u mid %u fin %u ) , binarySearchLastElement %u \n",beg,mid,fin,binarySearchLastElement); break; } else if (our_hash<links[mid].shortURLHash) { fin=mid-1; } else if (our_hash>links[mid].shortURLHash) { beg=mid+1; } else { *found = 1; AmmServer_Success("Found %s using binary search\n",shortURL); return mid; } } } //TODO : Remove this message in the future ------------- AmmServer_Warning("Binary Search couldn't find result , extending search to unsorted list\n"); return Find_longURLSerial(shortURL,found); //---------------------------------------- #else // USE_BINARY_SEARCH return Find_longURLSerial(shortURL,found); #endif return 0; }
struct videoCollection * loadVideoDatabase(const char * directoryPath,const char * databasePath) { struct videoCollection * newDB=(struct videoCollection * ) malloc(sizeof(struct videoCollection)); if (newDB==0) { fprintf(stderr,"Could not allocate a video collection \n"); return 0; } newDB->MAX_numberOfVideos = 9000; newDB->numberOfLoadedVideos=0; newDB->video = (struct videoItem *) malloc( newDB->MAX_numberOfVideos * sizeof(struct videoItem) ); if (newDB->video==0) { fprintf(stderr,"Could not allocate a video item\n"); free(newDB); return 0;} struct stat st; struct dirent *dp= {0}; // enter existing path to directory below DIR *dir = opendir(directoryPath); if (dir==0) { free(newDB->video); free(newDB); return 0; } while ((dp=readdir(dir)) != 0) { //TODO: remove // from requests.. of dp->d_name is like /filename.ext //char *tmp = path_cat(client_path, dp->d_name); if (dp->d_name==0) { fprintf(stderr,"Got garbage out of readdir(%s)\n",dir); } else if ( (strcmp(dp->d_name,".")!=0) && (strcmp(dp->d_name,"..")!=0) ) { if ( (strlen(dp->d_name)>0) && (dp->d_name[0]=='.') ) { fprintf(stderr,"Hidding hidden file %s from directory list\n" ,dp->d_name); } else { if (newDB->numberOfLoadedVideos+1<newDB->MAX_numberOfVideos) { //fprintf(stderr,"do 0 %u (%s) ",newDB->numberOfLoadedVideos,dp->d_name); ++newDB->numberOfLoadedVideos; snprintf(newDB->video[newDB->numberOfLoadedVideos].filename,MAX_STR,"%s",dp->d_name); snprintf(newDB->video[newDB->numberOfLoadedVideos].title,MAX_STR,"%s",dp->d_name); //fprintf(stderr,"do 1 %u ",newDB->numberOfLoadedVideos); loadVideoStats(newDB , databasePath , newDB->numberOfLoadedVideos); //fprintf(stderr,"do 2 %u ",newDB->numberOfLoadedVideos); clearExtensionFAST(newDB->video[newDB->numberOfLoadedVideos].title); if (strcmp(newDB->video[newDB->numberOfLoadedVideos].title,DEFAULT_TEST_TRANSMISSION_VIDEO_TITLE)==0) { AmmServer_Success("Found our default transmission video"); videoDefaultTestTranmission=newDB->numberOfLoadedVideos; } //Now lets try to get filesize and modification date using stat.h //fprintf(stderr,"do 3 %u ",newDB->numberOfLoadedVideos); char * fullpath = path_cat2(directoryPath,dp->d_name); if (fullpath!=0 ) { clear_line(); fprintf(stdout,"%u (%0.2f%%) - %s ", newDB->numberOfLoadedVideos, (float) (100*newDB->numberOfLoadedVideos)/ newDB->MAX_numberOfVideos, dp->d_name); if (AmmServer_FileIsVideo(fullpath)) { fprintf(stdout," is video "); } else { fprintf(stdout," is nothing "); } fprintf(stdout,"\n"); if ( stat(fullpath, &st) == 0 ) { char sizeStr[512]= {0}; snprintf(sizeStr,512,"%li",st.st_size); //Append FileSize information /* strncat(memory,tag_pre_date,mem_remaining); mem_remaining-=tag_pre_date_size; strftime(sizeStr, 512, "%Y-%m-%d %H:%M:%S", localtime(&st.st_mtime ) ); strncat(memory,sizeStr,mem_remaining); mem_remaining-=strlen(sizeStr); strncat(memory,tag_after_date,mem_remaining); mem_remaining-=tag_after_date_size;*/ } else { fprintf(stderr,"Error stating file %s -> %s\n",fullpath,strerror(errno)); } free(fullpath); fullpath=0; } //--------------------------------- }// <- we still have space } } } closedir(dir); return newDB; }