Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

convert_partdata.cpp

Go to the documentation of this file.
00001 /*
00002   nur base64: 485520k
00003   mit compress: 418896k
00004 */
00005 
00006 
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <libpq-fe.h>
00010 #include <qsqlquery.h>
00011 #include <qcstring.h>
00012 #include <qapplication.h>
00013 #include <kmdcodec.h>
00014 #include <qfile.h>
00015 #include "../global.h"
00016 #include "libpq/libpq-fs.h"
00017 
00018 // void writebuf(QByteArray &buf);
00019 void exit_nicely(PGconn *conn);
00020 
00021 void writebuf(PGconn *conn, QByteArray b, int chunkno, char* fileno) {
00022   PGresult *ins;
00023   size_t len;
00024 
00025   QCString b64=KCodecs::base64Encode(qCompress(b));
00026   
00027   QFile f("/tmp/writebuf"+QString().setNum(chunkno));
00028   f.open(IO_WriteOnly);
00029   f.writeBlock(b);
00030   f.close();
00031   
00032 
00033   ins=PQexec(conn, "insert into file (chunk, partdata_no, chunk_no) values ('"+QCString(b64)+"', "+QCString(fileno)+", "+QCString().setNum(chunkno)+")");
00034   if (!ins || PQresultStatus(ins) != PGRES_COMMAND_OK) {
00035     fprintf(stderr, "res: command failed\n");
00036     PQclear(ins);
00037     exit_nicely(conn);
00038   }
00039   PQclear(ins);
00040 }
00041 
00042 void
00043 exit_nicely(PGconn *conn)
00044 {
00045   PQfinish(conn);
00046   exit(1);
00047 }
00048 
00049 main()
00050 {
00051   char *pghost,
00052     *pgport,
00053     *pgoptions,
00054     *pgtty;
00055   char       *dbName;
00056   int         nFields;
00057   int         i,
00058     j;
00059   
00060   PGconn     *conn;
00061   PGresult   *recs, *res;
00062   
00063   
00064   pghost = NULL;              /* host name of the backend server */
00065   pgport = NULL;              /* port of the backend server */
00066   pgoptions = NULL;           /* special options to start up the backend
00067                                * server */
00068   pgtty = NULL;               /* debugging tty for the backend server */
00069   dbName = "kaspadev";
00070   
00071   /* make a connection to the database */
00072   conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
00073   
00074   /*
00075    * check to see that the backend connection was successfully made
00076    */
00077   if (PQstatus(conn) == CONNECTION_BAD)
00078     {
00079       fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
00080       fprintf(stderr, "%s", PQerrorMessage(conn));
00081       exit_nicely(conn);
00082     }
00083   
00084   /* debug = fopen("/tmp/trace.out","w"); */
00085   /* PQtrace(conn, debug);  */
00086   
00087   /* start a transaction block */
00088   recs = PQexec(conn, "select no, filename from partdata");
00089   if (!recs || PQresultStatus(recs) != PGRES_TUPLES_OK)
00090     {
00091       fprintf(stderr, "recs: command failed\n");
00092       PQclear(recs);
00093       exit_nicely(conn);
00094     }
00095 
00096   for (i=0; i<PQntuples(recs); i++)
00097     {
00098       res=PQexec(conn, QCString("select file from partdata where no=")+PQgetvalue(recs, i, 0));
00099       if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
00100         {
00101           fprintf(stderr, "res: command failed\n");
00102           PQclear(res);
00103           exit_nicely(conn);
00104         }
00105       fprintf(stderr, "Processing file %i\n", i);
00106       QByteArray buf;
00107       KCodecs::base64Decode(QCString(PQgetvalue(res, 0, 0)), buf);
00108       PQclear(res);
00109 
00110       
00111       QFile f("/tmp/orig");
00112       f.open(IO_WriteOnly);
00113       f.writeBlock(buf);
00114       f.close();
00115       
00116   
00117       #define bufsize 1000000
00118       QByteArray chunk(bufsize);
00119       int j=0;
00120       while((j+1)*bufsize<buf.size()) {
00121         memcpy(chunk.data(), buf.data()+j*bufsize, bufsize);
00122         j++;
00123         fprintf(stderr, "Writing chunk %i\n", j);
00124         writebuf(conn, chunk, j, PQgetvalue(recs, i, 0));
00125       }
00126       chunk.resize(buf.size()-j*bufsize);
00127       memcpy(chunk.data(), buf.data()+j*bufsize, buf.size()-j*bufsize);
00128       writebuf(conn, chunk, j+1, PQgetvalue(recs, i, 0));
00129     }
00130   /* close the connection to the database and cleanup */
00131   PQfinish(conn);
00132   
00133   return 0;
00134 }
00135 
00136 /*
00137 
00138 int main(int argc, char **argv) {
00139   QApplication a(argc, argv);   
00140   createConnection("qt_sql_default_connection");
00141   
00142   QSqlQuery p("select no, filename from partdata where no=15317");
00143   while(p.next()) {
00144     debug("Processing "+p.value(1).toString());
00145     QSqlQuery q("select file from partdata where no="+p.value(0).toString());
00146     q.first();
00147     QByteArray buf;
00148     KCodecs::base64Decode(q.value(0).toCString(), buf);
00149     q.~QSqlQuery();
00150 
00151     QSqlQuery r;
00152     //    r.prepare("update file set file=? where no="+p.value(0).toString());
00153     //    r.bindValue(0, qCompress(buf));
00154 
00155     r.prepare("update file set file=encode(?, 'base64') where no="+p.value(0).toString());
00156     r.bindValue(0, qCompress(buf));
00157 
00158     debug("Buffer:"+QString().setNum(buf.count()));
00159     debug("Buffer:"+QString().setNum(qCompress(buf).count()));
00160     
00161     // r.bindValue(0, qCompress(KCodecs::base64Encode(q.value(0).toByteArray(), false)));;
00162     // r.bindValue(0, qCompress(q.value(0).toByteArray()));
00163     r.exec();
00164   }
00165 }
00166 
00167 */

Generated on Sat Mar 27 19:20:38 2004 for Literature by doxygen 1.3.6-20040222