Changeset 39

Show
Ignore:
Timestamp:
11/09/07 20:06:06 (1 year ago)
Author:
nlawren2
Message:

Back where I was before svn konked out on me...

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • vowfsc/db.c

    r38 r39  
    130130 
    131131        // Make the request 
    132         char *pq_request[BUFFER_SIZE]; 
     132        char pq_request[BUFFER_SIZE]; 
    133133        sprintf(pq_request,"SELECT file_id" 
    134134                           "FROM file WHERE file_name = \'%s\' " 
     
    142142        int file_id=*(int*)PQgetvalue(res,0,0); 
    143143         
    144         // Free the result 
    145144        PQfreemem(res); 
    146145 
     
    152151        file_t stat; 
    153152 
    154         char *pq_request[BUFFER_SIZE]; 
     153        char pq_request[BUFFER_SIZE]; 
    155154        sprintf(pq_request,"SELECT * " 
    156155                           "FROM file " 
     
    182181#include "file.db" 
    183182         
    184         // Free the result 
    185183        PQfreemem(res); 
    186184 
     
    192190        file_t stat; 
    193191 
    194         char *pq_request[BUFFER_SIZE]; 
     192        char pq_request[BUFFER_SIZE]; 
    195193        sprintf(pq_request,"SELECT * " 
    196194                           "FROM file " 
     
    223221#include "file.db" 
    224222         
    225         // Free the result 
    226223        PQfreemem(res); 
    227224 
     
    229226} 
    230227 
    231 int db_get_file_max_revision( int file_id ){ 
     228int db_get_max_file_revision( int file_id ){ 
    232229        // Make the request 
    233         char *pq_request[BUFFER_SIZE]; 
     230        char pq_request[BUFFER_SIZE]; 
    234231        sprintf(pq_request,"SELECT MAX(revision_id) " 
    235232                           "FROM file " 
     
    243240        int revision=*(int*)PQgetvalue(res,0,0); 
    244241 
    245         // Free the result 
    246242        PQfreemem(res); 
    247243 
     
    251247 
    252248char *db_get_file_data( int file_id ){ 
    253         char *pq_request[BUFFER_SIZE]; 
     249        char pq_request[BUFFER_SIZE]; 
    254250        sprintf(pq_request,"SELECT diff" 
    255251                           "FROM file" 
     
    265261        memcpy(data,PQgetvalue(res,0,0),data_len); 
    266262 
    267         // Free the result 
    268263        PQfreemem(res); 
    269264 
     
    273268 
    274269char *get_file_data( int file_id, int revision ){ 
    275         int i = get_file_max_revision(file_id); 
     270        int i = get_max_file_revision(file_id); 
    276271        char *data, *diff, *temp; 
    277272 
    278         char *pq_request[BUFFER_SIZE]; 
     273        char pq_request[BUFFER_SIZE]; 
    279274        sprintf(pq_request,"SELECT diff " 
    280275                           "FROM file " 
     
    286281        if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 
    287282 
    288         int data_len=PQgetlength(res,0,0)
    289         char *data=malloc(data_len)
     283        int data_len=PQgetlength(res,0,0), diff_len, temp_len
     284        char *data=malloc(data_len), *temp, *diff
    290285        memcpy(data,PQgetvalue(res,0,0),data_len); 
    291286 
    292         // Free the result 
    293287        PQfreemem(res); 
    294288 
    295289        while( --i >= revision ){ 
    296                 char *pq_request[BUFFER_SIZE]; 
     290                char pq_request[BUFFER_SIZE]; 
    297291                sprintf(pq_request,"SELECT diff" 
    298292                                   "FROM file" 
     
    304298                if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 
    305299 
    306                 int data_len=PQgetlength(res,0,0); 
    307                 char *diff=malloc(data_len); 
    308                 memcpy(data,PQgetvalue(res,0,0),data_len); 
    309  
    310                 temp = xdelta_patch( data, patch ); 
     300                diff_len=PQgetlength(res,0,0); 
     301                diff=malloc(data_len); 
     302                 
     303                temp=malloc(XD3_ALLOCSIZE); 
     304 
     305                xd3_decode_memory(diff, diff_len, 
     306                                  data, data_len, 
     307                                  temp, &temp_len, XD3_ALLOCSIZE, 
     308                                  0); 
     309                 
     310                free(diff); 
    311311                free(data); 
    312312                data = temp; 
     313                data_len = temp_len; 
    313314        } 
    314315 
     
    321322 
    322323        // Make the request 
    323         char *pq_request[BUFFER_SIZE]; 
     324        char pq_request[BUFFER_SIZE]; 
    324325        sprintf(pq_request,"SELECT directory_id " 
    325326                           "FROM directory " 
     
    334335        int dir_id=*(int*)PQgetvalue(res,0,0); 
    335336         
    336         // Free the result 
    337337        PQfreemem(res); 
    338338 
     
    344344        dir_t stat; 
    345345 
    346         char *pq_request[BUFFER_SIZE]; 
     346        char pq_request[BUFFER_SIZE]; 
    347347        sprintf(pq_request,"SELECT * " 
    348348                           "FROM directory " 
     
    374374#include "dir.db" 
    375375         
    376         // Free the result 
    377376        PQfreemem(res); 
    378377 
     
    384383        dir_t stat; 
    385384 
    386         char *pq_request[BUFFER_SIZE]; 
     385        char pq_request[BUFFER_SIZE]; 
    387386        sprintf(pq_request,"SELECT * " 
    388387                           "FROM directory " 
     
    415414#include "dir.db" 
    416415         
    417         // Free the result 
    418416        PQfreemem(res); 
    419417 
     
    423421int db_get_dir_max_revision( int dir_id ){ 
    424422        // Make the request 
    425         char *pq_request[BUFFER_SIZE]; 
     423        char pq_request[BUFFER_SIZE]; 
    426424        sprintf(pq_request,"SELECT MAX(revision_id) " 
    427425                           "FROM directory " 
     
    435433        int revision=*(int*)PQgetvalue(res,0,0); 
    436434 
    437         // Free the result 
    438435        PQfreemem(res); 
    439436 
     
    443440 
    444441int db_init(char *host, int port, char *dbname, char *username, char *password){ 
    445         char *pq_request[BUFFER_SIZE]; 
     442        char pq_request[BUFFER_SIZE]; 
    446443        sprintf(pq_request,"host=%s port=%i dbname=%s user=%s password=%s", host, port, dbname, username, password); 
    447444        pq_conn=PQconnectdb(pq_request); 
     
    462459        stream_t stat; 
    463460 
    464         char *pq_request[BUFFER_SIZE]; 
     461        char pq_request[BUFFER_SIZE]; 
    465462        sprintf(pq_request,"SELECT * " 
    466                            "FROM streamectory
    467                            "WHERE streamectory_id = \'%i\'", 
     463                           "FROM stream
     464                           "WHERE stream_id = \'%i\'", 
    468465                           stream_id); 
    469466        PGresult *res=PQexec(pq_conn,pq_request); 
     
    488485 
    489486        // Fill stat 
    490 #define db(type, name)  stat.name = *(type*)PQgetvalue(pq_data, /*row*/, stream_format.name ); 
    491 #define db_string(name) stat.name = strdup(PQgetvalue(pq_data, /*row*/, stream_format.name )); 
     487#define db(type, name)  stat.name = *(type*)PQgetvalue(pq_data, 0, stream_format.name ); 
     488#define db_string(name) stat.name = strdup(PQgetvalue(pq_data, 0, stream_format.name )); 
    492489#include "stream.db" 
    493490         
    494         // Free the result 
    495491        PQfreemem(res); 
    496492 
    497493        return stat; 
    498494} 
    499  
    500495 
    501496stream_t db_get_stream( int stream_id, int revision ){ 
    502497        stream_t stat; 
    503498 
    504         char *pq_request[BUFFER_SIZE]; 
     499        char pq_request[BUFFER_SIZE]; 
    505500        sprintf(pq_request,"SELECT * " 
    506                            "FROM streamectory
    507                            "WHERE streamectory_id = \'%i\' " 
     501                           "FROM stream
     502                           "WHERE stream_id = \'%i\' " 
    508503                           "AND revision_id = \'%i\'", 
    509504                           stream_id, revision); 
     
    533528#include "stream.db" 
    534529         
    535         // Free the result 
    536530        PQfreemem(res); 
    537531 
     
    539533} 
    540534 
    541  
     535int db_get_max_stream_id(){ 
     536        PGresult *res=PQexec(pq_conn,"SELECT MAX(stream_id) FROM stream"); 
     537 
     538        if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 
     539 
     540        int stream_id=*(int*)PQgetvalue(res,0,0); 
     541 
     542        PQfreemem(res); 
     543 
     544        return stream_id; 
     545
     546 
     547 
     548int db_create_stream( char *stream_name, char *stream ){ 
     549        int stream_id; 
     550 
     551        char pq_request[BUFFER_SIZE]; 
     552        sprintf(pq_request, "SELECT stream_id " 
     553                            "FROM stream " 
     554                            "WHERE stream_name = \'%s\' " 
     555                            "AND stream = \'%s\'", 
     556                            stream_name, stream); 
     557        PGresult *res = PQexec(pq_conn,pq_request); 
     558 
     559        if( PQresultStatus == PGRES_TUPLES_OK ){ 
     560                stream_id = *(int*)PQgetvalue(res,0,0); 
     561                PQfreemem(res); 
     562                 
     563                sprintf(pq_request, "UPDATE stream " 
     564                                    "SET use_count = use_count + 1 " 
     565                                    "WHERE stream_id = \'%i\'", 
     566                                    stream_id); 
     567                PQfreemem(PQexec(pq_conn,pq_request)); 
     568        }else{ 
     569                PQfreemem(res); 
     570                stream_id = db_get_max_stream_id() + 1; 
     571                 
     572                sprintf(pq_request, "INSERT stream " 
     573                                    "(stream_id, use_count, stream_name, stream) " 
     574                                    "VALUES" 
     575                                    "%i, 0, \'%s\', \'%s\'", 
     576                                    stream_id, stream_name, stream); 
     577                PQfreemem(PQexec(pq_conn,pq_request)); 
     578        } 
     579        return stream_id; 
     580
     581 
     582 
     583int db_get_max_file_id(){ 
     584        PGresult *res=PQexec(pq_conn,"SELECT MAX(file_id) FROM file"); 
     585 
     586        if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 
     587 
     588        int file_id=*(int*)PQgetvalue(res,0,0); 
     589 
     590        PQfreemem(res); 
     591 
     592        return file_id; 
     593
     594 
     595int db_get_max_file_uid(){ 
     596        PGresult *res=PQexec(pq_conn,"SELECT MAX(file_uid) FROM file"); 
     597 
     598        if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 
     599 
     600        int file_uid=*(int*)PQgetvalue(res,0,0); 
     601 
     602        PQfreemem(res); 
     603 
     604        return file_uid; 
     605
     606 
     607int db_create_file(char *file_name, char *file_path, int stream_id ){ 
     608        int file_uid = db_get_max_file_uid() + 1; 
     609        int file_id = db_get_max_file_id() + 1; 
     610         
     611        char *dir_name = normalize_dir_name( file_path ); 
     612 
     613        char pq_request[BUFFER_SIZE]; 
     614        sprintf(pq_request, "INSERT file " 
     615                            "(file_uid, file_id, file_path, file_name, stream_id) " 
     616                            "VALUES " 
     617                            "%i, %i, \'%s\', \'%s\', %i", 
     618                            file_uid, file_id, dir_name, file_name, stream_id); 
     619        PQexec(pq_conn, pq_request); 
     620 
     621        return file_id; 
     622
     623 
     624 
     625 
  • vowfsc/db.h

    r38 r39  
    7777 * a new stream and returns the new stream's id. 
    7878 */ 
    79 int db_create_stream( char *stream_name, char *stream ); // TODO 
     79int db_create_stream( char *stream_name, char *stream ); 
    8080 
    81 /* Sets the delete flag in the file 
     81/* Copies the file to a new reversion, and sets the delete flag  
     82 * in the new file.  It then returns the revision number of the 
     83 * new file. 
    8284 */ 
    8385int db_remove_file(int file_id); // TODO 
     
    8890 * revisions it.   
    8991 */ 
    90 int db_create_file(char *file_name, char *file_path, stream_t stream); // TODO 
     92int db_create_file(char *file_name, char *file_path, int stream_id ); // TODO 
    9193 
    9294