1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
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. *
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. *
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 ***************************************************************************/
21 #include <SqlLogConnection.h>
27 sprintf(fileName
, "%s/csql.db.cur", Conf::config
.getDbFile());
28 int durableMode
= Conf::config
.getDurableMode();
32 fdRedoLog
= os::openFileForAppend(fileName
, O_CREAT
);
35 fdRedoLog
= os::openFileForAppend(fileName
, O_CREAT
|O_SYNC
);
39 fdRedoLog
= os::openFileForAppend(fileName
, O_CREAT
|O_DSYNC
);
41 fdRedoLog
= os::openFileForAppend(fileName
, O_CREAT
|O_DIRECT
);
45 fdRedoLog
= os::openFileForAppend(fileName
, O_CREAT
);
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
);
61 //Note:: msg type is taken as -ve as we need to differentiate between
62 //statement id and logtype during recovery.
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);
70 msg
= msg
+sizeof(int);
72 msg
= msg
+ sizeof(int);
73 *(int *)msg
= os::align(len
);
74 msg
= msg
+ sizeof(int);
78 if (Conf::config
.getDurableMode() != 1) {
79 ret
= os::lockFile(fdRedoLog
);
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
);
91 //if (ret == datalen) { printf("log written successfully %d\n", ret); return OK; }
92 if (ret
== datalen
) { return OK
; }
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
);
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
; }
116 DbRetVal
FileSend::free(int txnId
, int stmtId
)
118 int buflen
= 4 *sizeof(int);
119 char *msg
= (char *) malloc(buflen
);
123 *(int *)ptr
= 3 * sizeof(int); // for len + txnId + stmtId
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
);
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; }
143 if (ret
== buflen
) { return OK
; }