changes for open source mmdb build
[csql.git] / include / SqlLogConnection.h
blobf0e8f015d29a39d20ecd9a5cd113c806b5a42c9c
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifndef SQLLOGCONNECTION_H
21 #define SQLLOGCONNECTION_H
22 #include<CSql.h>
23 #include<SqlFactory.h>
24 #include<Util.h>
25 #include<Network.h>
27 /**
28 * @class SqlLogConnection
32 typedef struct my_msgbuffer {
33 long mtype;
34 char data[1];
35 } Message;
37 class AbsSqlLogSend
39 public:
40 virtual DbRetVal prepare(int tId, int sId, int len, char *st,
41 char *tn)=0;
42 virtual DbRetVal commit(int len, void *data)=0;
43 virtual DbRetVal free(int txnId, int stmtId)=0;
46 class MsgQueueSend : public AbsSqlLogSend
48 int msgQId;
49 public:
50 MsgQueueSend() { msgQId = os::msgget(Conf::config.getMsgKey(), 0666); }
51 DbRetVal prepare(int tId, int sId, int len, char *stmt, char *tn);
52 DbRetVal commit(int len, void *data);
53 DbRetVal free(int txnId, int stmtId);
56 class FileSend : public AbsSqlLogSend
58 int fdRedoLog;
59 public:
60 FileSend();
61 ~FileSend();
62 DbRetVal prepare(int txnId, int stmtId, int len, char *stmt, char*tn);
63 DbRetVal commit(int len, void *data);
64 DbRetVal free(int txnId, int stmtId);
67 enum ExecType
69 EXECONLY = 0,
70 SETPARAM
73 class ExecLogInfo
75 public:
76 ExecLogInfo() : pos(0), len(0) {}
77 int stmtId;
78 ExecType type;
79 int pos;
80 DataType dataType;
81 int len;
82 int value; //Extendible value as per parameter type size
85 class SqlLogConnection : public AbsSqlConnection
87 Connection dummyConn;
89 //stores all the sql log packets to be shipped to peers
90 List logStore;
92 List execLogStore;
93 int execLogStoreSize;
95 //stores all the prepare log packets to be shipped to peers
96 //as soon as connection is reestablished to cache server
97 List prepareStore;
99 //stores all the prepare log packets to be shipped between two
100 //consecutive commits. Commit() call sends first all the stmts
101 //prepared during the course and then sends the exec pkts
102 List curPrepareStore;
104 //sync mode of the current transaction
105 TransSyncMode syncMode;
107 //stores client objects in it for peer
108 NetworkTable nwTable;
109 AbsSqlLogSend *msgQSend;
110 AbsSqlLogSend *fileSend;
112 GlobalUniqueID txnUID;
113 static List cacheList;
114 int txnID;
115 DbRetVal populateCachedTableList();
116 public:
117 SqlLogConnection() {
118 innerConn = NULL; syncMode = ASYNC;
119 if (Conf::config.useCache() &&
120 Conf::config.getCacheMode()==ASYNC_MODE)
121 msgQSend = new MsgQueueSend();
122 else msgQSend = NULL;
123 if (Conf::config.useDurability()) { fileSend = new FileSend(); }
124 else fileSend = NULL;
125 txnUID.open();
126 execLogStoreSize =0;
127 noMsgLog = false;
129 ~SqlLogConnection();
130 bool isTableCached(char *name);
131 bool noMsgLog;
132 //Note::forced to implement this as it is pure virtual in base class
133 Connection& getConnObject(){ return dummyConn; }
135 DbRetVal connect (char *user, char * pass);
137 DbRetVal disconnect();
139 DbRetVal commit();
141 DbRetVal rollback();
143 DbRetVal beginTrans (IsolationLevel isoLevel, TransSyncMode mode);
145 DbRetVal msgPrepare(int tId, int sId, int len, char *stmt, char *tname)
147 return msgQSend->prepare(tId, sId, len, stmt, tname);
149 DbRetVal fileLogPrepare(int tId, int sId, int len, char *stmt, char *tname)
151 return fileSend->prepare(tId, sId, len, stmt, tname);
153 DbRetVal commitLogs(int logSize, void *data)
155 int txnId = getTxnID();
156 if (((Conf::config.useCache() &&
157 Conf::config.getCacheMode() == ASYNC_MODE)) && !noMsgLog)
158 msgQSend->commit(logSize, data);
159 if (Conf::config.useDurability()) fileSend->commit(logSize, data);
160 return OK;
162 DbRetVal freeLogs(int stmtId)
164 int txnId = getTxnID();
165 if ( ((Conf::config.useCache() &&
166 Conf::config.getCacheMode() == ASYNC_MODE)) && !noMsgLog)
167 msgQSend->free(txnId, stmtId);
168 if (Conf::config.useDurability()) fileSend->free(txnId, stmtId);
169 return OK;
171 void addExecLog(ExecLogInfo *info) { execLogStore.append(info); }
172 void addToExecLogSize(int size){ execLogStoreSize += size; }
173 int getExecLogStoreSize() { return execLogStoreSize; }
174 List getExecLogList() { return execLogStore; }
175 DbRetVal addPacket(BasePacket *pkt);
176 DbRetVal addPreparePacket(PacketPrepare *pkt);
177 DbRetVal removePreparePacket(int stmtid);
179 DbRetVal setSyncMode(TransSyncMode mode);
180 void setNoMsgLog(bool nmlog) { noMsgLog = nmlog; }
181 TransSyncMode getSyncMode() { return syncMode; }
182 int getTxnID() { return txnID; }
183 DbRetVal connectIfNotConnected() { return nwTable.connectIfNotConnected(); }
184 DbRetVal sendAndReceive(NetworkPacketType type, char *packet, int length);
185 friend class SqlFactory;
188 #endif