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

server.h

00001 //-< SERVER.CPP >----------------------------------------------------*--------*
00002 // FastDB                    Version 1.0         (c) 1999  GARRET    *     ?  *
00003 // (Main Memory Database Management System)                          *   /\|  *
00004 //                                                                   *  /  \  *
00005 //                          Created:     13-Jan-2000  K.A. Knizhnik  * / [] \ *
00006 //                          Last update: 13-Jan-2000  K.A. Knizhnik  * GARRET *
00007 //-------------------------------------------------------------------*--------*
00008 // CLI multithreaded server class
00009 //-------------------------------------------------------------------*--------*
00010 
00011 #ifndef __SERVER_H__
00012 #define __SERVER_H__
00013 
00014 #include "sockio.h"
00015 
00016 class dbColumnBinding { 
00017   public:
00018     dbColumnBinding*   next;
00019     dbFieldDescriptor* fd;
00020     int                cliType;
00021     int                len;
00022     char*              ptr;
00023 
00024     int  unpackArray(char* dst, size_t offs);
00025     void unpackScalar(char* dst);
00026 
00027     dbColumnBinding(dbFieldDescriptor* field, int type) { 
00028         fd = field;
00029         cliType = type;
00030         next = NULL;
00031     }
00032 };
00033 
00034 struct dbParameterBinding { 
00035     union { 
00036         int1       i1;
00037         int2       i2;
00038         int4       i4;
00039         db_int8    i8;
00040         real4      r4;
00041         real8      r8;
00042         oid_t      oid;
00043         bool       b;
00044         char*      str;
00045     } u;
00046     int type;
00047 };
00048 
00049 const int dbQueryMaxIdLength = 256;
00050 
00051 class dbQueryScanner { 
00052   public:
00053     char*    p;
00054     db_int8     ival;
00055     real8    fval;
00056     char     buf[dbQueryMaxIdLength];
00057     char*    ident;
00058 
00059     int  get();
00060 
00061     void reset(char* stmt) { 
00062         p = stmt;
00063     }
00064 };
00065     
00066 class dbStatement { 
00067   public:
00068     int                 id;
00069     bool                firstFetch;
00070     dbStatement*        next;
00071     dbAnyCursor*        cursor; 
00072     dbQuery             query;
00073     dbColumnBinding*    columns;
00074     char*               buf;
00075     int                 buf_size;
00076     int                 n_params;
00077     int                 n_columns;
00078     dbParameterBinding* params;
00079     dbTableDescriptor*  table;
00080     
00081     void reset();
00082 
00083     dbStatement(int stmt_id) { 
00084         id = stmt_id;
00085         columns = NULL;
00086         params = NULL;
00087         buf = NULL;
00088         buf_size = 0;
00089         table = NULL;
00090         cursor = NULL;
00091     }
00092     ~dbStatement() { 
00093         reset(); 
00094         delete[] buf;
00095     }
00096 };
00097 
00098 class dbSession { 
00099   public:
00100     dbSession*         next;  
00101     dbStatement*       stmts;
00102     dbQueryScanner     scanner;
00103     socket_t*          sock;
00104     bool               in_transaction;
00105     dbTableDescriptor* dropped_tables;
00106     dbTableDescriptor* existed_tables;
00107 };
00108 
00109 class dbServer {     
00110   protected:
00111     static dbServer* chain;
00112     dbServer*        next;
00113     char*            URL;
00114     dbSession*       freeList;
00115     dbSession*       waitList;
00116     dbSession*       activeList;
00117     int              optimalNumberOfThreads;
00118     int              nActiveThreads;
00119     int              nIdleThreads;
00120     int              waitListLength;
00121     bool             cancelWait;
00122     bool             cancelAccept;
00123     bool             cancelSession;
00124     dbMutex          mutex;
00125     dbLocalSemaphore go;
00126     dbLocalSemaphore done;
00127     socket_t*        globalAcceptSock;
00128     socket_t*        localAcceptSock;
00129     dbThread         localAcceptThread;
00130     dbThread         globalAcceptThread;
00131     dbDatabase*      db;
00132 
00133     static void thread_proc serverThread(void* arg);
00134     static void thread_proc acceptLocalThread(void* arg);
00135     static void thread_proc acceptGlobalThread(void* arg);
00136 
00137     void serveClient();
00138     void acceptConnection(socket_t* sock);
00139     
00140     
00141     bool freeze(dbSession* session, int stmt_id);
00142     bool unfreeze(dbSession* session, int stmt_id);
00143     bool get_first(dbSession* session, int stmt_id);
00144     bool get_last(dbSession* session, int stmt_id);
00145     bool get_next(dbSession* session, int stmt_id);
00146     bool get_prev(dbSession* session, int stmt_id);
00147     bool seek(dbSession* session, int stmt_id, char* buf);
00148     bool skip(dbSession* session, int stmt_id, char* buf);
00149     bool fetch(dbSession* session, dbStatement* stmt, oid_t result);
00150     bool fetch(dbSession* session, dbStatement* stmt) { 
00151         return fetch(session, stmt, stmt->cursor->currId);
00152     }
00153     bool remove(dbSession* session, int stmt_id);
00154     bool update(dbSession* session, int stmt_id, char* new_data);
00155     bool insert(dbSession* session, int stmt_id, char* data, bool prepare);
00156     bool select(dbSession* session, int stmt_id, char* data, bool prepare);
00157     bool show_tables(dbSession* session); 
00158     bool describe_table(dbSession* session, char const* table);
00159     bool create_table(dbSession* session, char* data, bool create);
00160     bool drop_table(dbSession* session, char* data);
00161     bool alter_index(dbSession* session, char* data);
00162 
00163     char* checkColumns(dbStatement* stmt, int n_columns, 
00164                        dbTableDescriptor* desc, char* data, 
00165                        int4& reponse);
00166       
00167     dbStatement* findStatement(dbSession* stmt, int stmt_id);
00168 
00169   public:
00170     static dbServer* find(char const* serverURL);
00171     static void      cleanup();
00172 
00173     void stop();
00174     void start();
00175 
00176     dbServer(dbDatabase* db,
00177              char const* serverURL, 
00178              int optimalNumberOfThreads = 8,  
00179              int connectionQueueLen = 64);
00180     ~dbServer();
00181 };
00182 
00183 #endif

Generated on Thu Feb 12 13:04:48 2004 for FastDB by doxygen 1.3.5