Update in sync with enterprise version.
[csql.git] / src / sqllog / FileSend.cxx
blob52533c32f4354d219a9a064f87734b76dcebd423
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 #include <os.h>
21 #include <SqlLogConnection.h>
22 #include <CSql.h>
24 FileSend::FileSend()
26 char fileName[1024];
27 sprintf(fileName, "%s/csql.db.cur", Conf::config.getDbFile());
28 int durableMode = Conf::config.getDurableMode();
29 switch(durableMode) {
30 case 1:
31 case 2:
32 fdRedoLog = os::openFileForAppend(fileName, O_CREAT);
33 break;
34 case 3:
35 fdRedoLog = os::openFileForAppend(fileName, O_CREAT|O_SYNC);
36 break;
37 case 4:
38 #ifdef SOLARIS
39 fdRedoLog = os::openFileForAppend(fileName, O_CREAT|O_DSYNC);
40 #else
41 fdRedoLog = os::openFileForAppend(fileName, O_CREAT|O_DIRECT);
42 #endif
43 break;
44 default:
45 fdRedoLog = os::openFileForAppend(fileName, O_CREAT);
46 break;
50 FileSend::~FileSend() { if (fdRedoLog > 0) os::closeFile(fdRedoLog); fdRedoLog = -1; }
52 DbRetVal FileSend::prepare(int txnId, int stmtId, int len, char *stmt, char *tblName)
54 if (fdRedoLog < 0) return ErrBadArg;
55 //The following structure needs strlen after stmt id for traversal in
56 //redolog file unlike msg queue structure where string is the last element
57 //and is not a continuous piece of memory.
58 int datalen = os::align(5 * sizeof(int) + len); // for len + txnId + msg type + stmtId + tableName + stmtstrlen + stmtstring
59 char *buf = (char*) malloc(datalen);
60 char *msg = buf;
61 //Note:: msg type is taken as -ve as we need to differentiate between
62 //statement id and logtype during recovery.
63 *(int*) msg = -1;
64 if (strlen(stmt) > 6 && ( strncasecmp(stmt,"CREATE", 6) == 0 || strncasecmp(stmt,"DROP", 4) == 0 ))
65 *(int*)msg = -4; //means prepare and execute the stmt
66 msg = msg+sizeof(int);
67 *(int *)msg = datalen;
68 msg = msg+sizeof(int);
69 *(int *)msg = txnId;
70 msg = msg+sizeof(int);
71 *(int *)msg = stmtId;
72 msg = msg+ sizeof(int);
73 *(int *)msg = os::align(len);
74 msg = msg+ sizeof(int);
75 msg[len-1] = '\0';
76 strcpy(msg, stmt);
77 int ret =0;
78 if (Conf::config.getDurableMode() != 1) {
79 ret = os::lockFile(fdRedoLog);
80 if (-1 == ret) {
81 ::free(buf);
82 printError(ErrLockTimeOut,"Unable to get exclusive lock on redo log file");
83 return ErrLockTimeOut;
86 ret = os::write(fdRedoLog, buf, datalen);
87 if (Conf::config.getDurableMode() != 1) {
88 os::unlockFile(fdRedoLog);
90 ::free(buf);
91 //if (ret == datalen) { printf("log written successfully %d\n", ret); return OK; }
92 if (ret == datalen) { return OK; }
93 return ErrOS;
96 DbRetVal FileSend::commit(int len, void *data)
98 if (fdRedoLog < 0) return ErrBadArg;
99 char *dat=(char*)data - sizeof(int);
100 *(int*)dat = -2; //type 2->commit
101 if (Conf::config.getDurableMode() != 1) {
102 int ret = os::lockFile(fdRedoLog);
103 if (-1 == ret) {
104 printError(ErrLockTimeOut,"Unable to get exclusive lock on redo log file");
105 return ErrLockTimeOut;
108 int ret = os::write(fdRedoLog, dat, len+sizeof(int));
109 if (Conf::config.getDurableMode() != 1) {
110 os::unlockFile(fdRedoLog);
112 //if (ret == len+sizeof(int)) { printf("log written successfully %d\n", ret); return OK; }
113 if (ret == len+sizeof(int)) { return OK; }
114 return ErrOS;
116 DbRetVal FileSend::free(int txnId, int stmtId)
118 int buflen = 4 *sizeof(int);
119 char *msg = (char *) malloc(buflen);
120 char *ptr = msg;
121 *(int*)ptr = -3;
122 ptr += sizeof(int);
123 *(int *)ptr = 3 * sizeof(int); // for len + txnId + stmtId
124 ptr += sizeof(int);
125 *(int *)ptr = txnId;
126 ptr += sizeof(int);
127 *(int *)ptr = stmtId;
128 printDebug(DM_SqlLog, "stmtID sent = %d\n", *(int *)ptr);
129 if (Conf::config.getDurableMode() != 1) {
130 int ret = os::lockFile(fdRedoLog);
131 if (-1 == ret) {
132 ::free(msg);
133 printError(ErrLockTimeOut,"Unable to get exclusive lock on redo log file");
134 return ErrLockTimeOut;
137 int ret = os::write(fdRedoLog, msg, buflen);
138 if (Conf::config.getDurableMode() != 1) {
139 os::unlockFile(fdRedoLog);
141 //if (ret == buflen) { printf("log written successfully %d\n", ret); return OK; }
142 ::free(msg);
143 if (ret == buflen) { return OK; }
144 return ErrOS;