// Deletes a record at a given recno // Becomes more inefficient as you the record set increases and you delete records // early in the record queue. EDB_Status EDB::deleteRec(unsigned long recno) { if (recno < 0 || recno > EDB_head.n_recs) return EDB_OUT_OF_RANGE; EDB_Rec rec = (byte*)malloc(EDB_head.rec_size); for (unsigned long i = recno + 1; i <= EDB_head.n_recs; i++) { readRec(i, rec); writeRec(i - 1, rec); } free(rec); EDB_head.n_recs--; writeHead(); return EDB_OK; }
// Inserts a record at a given recno, increasing all following records' recno by 1. // This function becomes increasingly inefficient as it's currently implemented and // is the slowest way to add a record. EDB_Status EDB::insertRec(unsigned long recno, EDB_Rec rec) { if (count() == limit()) return EDB_TABLE_FULL; if (count() > 0 && (recno < 0 || recno > EDB_head.n_recs)) return EDB_OUT_OF_RANGE; if (count() == 0 && recno == 1) return appendRec(rec); EDB_Rec buf = (byte*)malloc(EDB_head.rec_size); for (unsigned long i = EDB_head.n_recs; i >= recno; i--) { readRec(i, buf); writeRec(i + 1, buf); } free(buf); writeRec(recno, rec); EDB_head.n_recs++; writeHead(); return EDB_OK; }
/*---- Function ------------------------------------------------------------- Does: Scan through the database from start and send every record matching the given reference. Graciously handle the situations where a record from database is cut at the end of the buffer's capacity. Wants: Reference record data. Handler to a function to use to send the replies. Gives: True on success. ----------------------------------------------------------------------------*/ bool BintxtSinkImpl::queryRec(Record const &reference, Sink::SendRecord_f const &send) const { char buffer[1500]; fseek(file_, 0, SEEK_SET); Record rec; int bytes = 0; while (!feof(file_)) { bytes += fread(buffer + bytes, 1, sizeof(buffer) - bytes, file_); int bufPos = 0; while (1) { int const ret = readRec(rec, buffer + bufPos, bytes - bufPos); if (0 == ret) { bytes -= bufPos; memmove(buffer, buffer + bufPos, bytes); break; } rec.action = REC_ACT_REPLY; if (rec.match(reference) && send(rec, reference.priv) < 0) { return false; } bufPos += ret; if (bufPos >= bytes) { break; } } } return true; }
PUBLIC bool updateRecFromParams(cchar *table) { return updateRec(setFields(readRec(table, param("id")), params())); }
/* Get a resource */ static void getPak() { EdiRec *rec; rec = readRec("pak", param("id")); ediFilterRecFields(rec, "password", 0); sendRec(rec); }
/* Get a resource */ static void getPost() { readRec("post", param("id")); renderView("post/post-edit"); }
/* Prepare a template for editing a resource */ static void editPost() { readRec("post", param("id")); }
int main(int argc,char *argv[]){ if(argc<2) usage(0); char *sourceURL=argv[1]; clock_t begin, end; int i=0; char *handleURL; int pos; UPU *tmp; URL_FILE *buf; char prefix[10],host[1024],path[4096],filename[4096]; for(i=0;i<HashSize;i++){ URLPool[i]=NULL; finishPool[i]=NULL; failedPool[i]=NULL; } memset(prefix,0,10); memset(host,0,1024); memset(path,0,4096); memset(filename,0,4096); sepURL(sourceURL,prefix,host,path,filename); //check/create Folder folderInit(host); readRec(sourceURL); //init if(!logInit(host)){ printf("log file error\n"); exit(1); } pos=hashfn(sourceURL); myAdd(&URLPool[pos],sourceURL,strlen(sourceURL)); //start loop begin=clock(); i=0; while(getPoolSize(URLPool)!=0){ //random get one URL Structure Pointer from URLPool handleURL=randGet(URLPool); pos=hashfn(handleURL); printf("run [%d] %s\n",pos,handleURL); memset(prefix,0,10); memset(host,0,1024); memset(path,0,4096); memset(filename,0,4096); sepURL(handleURL,prefix,host,path,filename); //put the url in finishPool tmp=finishPool[pos]; if(tmp==NULL){ myAdd(&finishPool[pos],handleURL,strlen(handleURL)); }else{ while(tmp->next!=NULL){ tmp=tmp->next; } myAdd(&(tmp->next),handleURL,strlen(handleURL)); } //start run the CURL if((buf=runCURL(handleURL))!=NULL){ //write to File myWrite(host,filename,buf->buffer,buf->buffer_len,i++); getURL(buf->buffer,buf->buffer_len,handleURL,prefix,host,path,filename); printf("%d\n",getPoolSize(URLPool)); memset(buf->buffer,0,buf->buffer_len); free(buf->buffer); fflush(logfp); } else{ printf("no content\n"); } free(buf); } logClose(); end=clock(); printf("finally finish: %d 's URL, cost %lf sec\n",getPoolSize(finishPool),(double)( end - begin ) / CLOCKS_PER_SEC); myDump(host); return 0; }