1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.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 ***************************************************************************/
18 #include <DatabaseManagerImpl.h>
20 #include <TableImpl.h>
21 #include <CacheTableLoader.h>
22 #include <TableConfig.h>
26 printf("Usage: catalog [-u username] [-p passwd] [-l] [-i] [-d] [-T table] [-I index] [-D <lock|trans|proc|chunk>]\n");
27 printf(" l -> list all table with field information\n");
28 printf(" list all Index information\n");
29 printf(" i -> reinitialize catalog tables. Drops all tables.\n");
30 printf(" d -> print db usage statistics\n");
31 printf(" T -> list table information\n");
32 printf(" I -> list index information\n");
33 printf(" D -> print debug information for system tables\n");
34 printf("Note: If multiple options are specified, last one will be considered.\n");
39 int main(int argc
, char **argv
)
41 char username
[IDENTIFIER_LENGTH
];
43 char password
[IDENTIFIER_LENGTH
];
46 char name
[IDENTIFIER_LENGTH
];
47 while ((c
= getopt(argc
, argv
, "u:p:T:I:D:licdsS?")) != EOF
)
51 case 'u' : { strcpy(username
, argv
[optind
- 1]); opt
=1; break; }
52 case 'p' : { strcpy(password
, argv
[optind
- 1]); opt
=1; break; }
53 case 'T' : { strcpy(name
, argv
[optind
- 1]); opt
= 5; break; }
54 case 'I' : { strcpy(name
, argv
[optind
- 1]); opt
= 6; break; }
55 case 'D' : { strcpy(name
, argv
[optind
- 1]); opt
= 7; break; }
56 case 'l' : { opt
= 2; break; } //list all the table with field info
57 case 'i' : { opt
= 3; break; }//reinitialize the catalog table
58 case 'd' : { opt
= 4; break; }//print db usage statistics
59 case 's' : { if(opt
== 6) opt
=11;
60 else printf("Use -I IndexName -s\n");
61 break; }//print db usage statistics
62 case 'S' : { if(opt
== 6) opt
=12;
63 else printf("Use -I IndexName -S\n");
64 break; }//print db usage statistics
65 case '?' : { opt
= 10; break; } //print help
66 default: opt
=1; //list all the tables
75 //printf("%s %s \n", username, password);
76 if (username
[0] == '\0' )
78 strcpy(username
, I_USER
);
79 strcpy(password
, I_PASS
);
80 opt
=1;//if username is not specified, just list all table names
84 DbRetVal rv
= conn
.open(username
, password
);
85 if (rv
!= OK
) return 1;
86 DatabaseManagerImpl
*dbMgr
= (DatabaseManagerImpl
*) conn
.getDatabaseManager();
87 if (dbMgr
== NULL
) { printf("Auth failed\n"); return 2;}
88 List tableList
= dbMgr
->getAllTableNames();
89 ListIterator iter
= tableList
.getIterator();
90 Identifier
*elem
= NULL
;
93 printf("<TableNames>\n");
95 while (iter
.hasElement())
97 elem
= (Identifier
*) iter
.nextElement();
99 printf(" <TableName> %s </TableName>\n", elem
->name
);
101 if (count
==0) printf(" <No tables exist></No tables exist>\n");
102 printf("</TableNames>\n");
106 printf("<Table Information of all tables>\n");
108 while (iter
.hasElement())
110 elem
= (Identifier
*) iter
.nextElement();
111 printf(" <TableInfo> \n");
112 printf(" <TableName> %s </TableName>\n", elem
->name
);
113 Table
*table
= dbMgr
->openTable(elem
->name
);
114 FieldInfo
*info
= new FieldInfo();
115 List fNameList
= table
->getFieldNameList();
116 ListIterator fNameIter
= fNameList
.getIterator();
118 char fieldName
[IDENTIFIER_LENGTH
];
119 while (fNameIter
.hasElement()) {
120 elem
= (Identifier
*) fNameIter
.nextElement();
121 Table::getFieldNameAlone(elem
->name
, fieldName
);
122 table
->getFieldInfo((const char*)elem
->name
, info
);
123 printf(" <FieldInfo>\n");
124 printf(" <FieldName> %s </FieldName>\n", fieldName
);
125 printf(" <Type> %d </Type>\n", info
->type
);
126 printf(" <Length> %d </Length>\n", info
->length
);
127 printf(" <Primary> %d </Primary>\n", info
->isPrimary
);
128 printf(" <Null> %d </Null>\n", info
->isNull
);
129 printf(" <Default> %d </Default>\n", info
->isDefault
);
130 printf(" <DefaultValue> %s </DefaultValue>\n", info
->defaultValueBuf
);
131 printf(" </FieldInfo>\n");
135 dbMgr
->closeTable(table
);
136 printf(" </TableInfo> \n");
139 if (count
== 0) printf(" <No tables exist></No tables exist>\n");
140 printf("</Table Information of all tables>\n");
141 printf("<Index Information of all Indexs>\n");
142 CatalogTableINDEXFIELD
cIndexField(dbMgr
->sysDb());
143 cIndexField
.printAllIndex();
144 printf("</Index Information of all Indexs>\n");
148 if (!dbMgr
->isAnyOneRegistered()) {
149 printf("<DropTable>\n");
151 while (iter
.hasElement())
153 elem
= (Identifier
*) iter
.nextElement();
155 rv
=TableConf::config
.isTableCached(elem
->name
);
157 printf(" <TableName> %s </TableName>\n", elem
->name
);
158 dbMgr
->dropTable(elem
->name
);
161 printf(" <TableName> %s </TableName>\n", elem
->name
);
162 dbMgr
->dropTable(elem
->name
);
166 //TODO::If durability is on, remove chkpt and redo log file
167 if (count
==0) printf(" <No tables exist></No tables exist>\n");
168 printf("</DropTable>\n");
171 printf("Catalog not initialized. Found registered process\n");
176 printf("<Database Usage Statistics>\n");
177 Database
*db
= dbMgr
->sysDb();
178 db
->printStatistics();
179 dbMgr
->printUsageStatistics();
181 db
->printStatistics();
182 printf("</Database Usage Statistics>\n");
186 printf("<Table Info> \n");
188 TableImpl
*table
= (TableImpl
*) dbMgr
->openTable(name
);
191 dbMgr
->closeTable(table
);
193 printf(" <Table Not Found> %s </Table Not Found>\n", name
);
196 printf("</Table Info> \n");
200 printf("<Index Info> \n");
201 rv
= dbMgr
->printIndexInfo(name
);//TODO::handle no index case to return 1
204 printf(" <Index Not Found> %s </Index Not Found>\n", name
);
207 printf("<Index Info> \n");
210 if (strcmp(name
, "lock") == 0)
212 dbMgr
->printDebugLockInfo();
214 else if (strcmp(name
, "trans") == 0)
216 dbMgr
->printDebugTransInfo();
218 else if (strcmp(name
, "proc") == 0)
220 dbMgr
->printDebugProcInfo();
222 else if(strcmp(name
,"chunk") ==0)
224 Database
*db
= dbMgr
->sysDb();
227 printf("<Chunk information>\n");
228 printf(" <System Chunk >\n");
229 chunk
=db
->getSystemDatabaseChunk(UserChunkTableId
);
233 chunk
=db
->getSystemDatabaseChunk(id
);
234 if((chunk
->getChunkID())!=0){
239 printf(" </System Chunk >\n");
240 printf(" <User Chunk >\n");
241 chunk
=db
->getSystemDatabaseChunk(UserChunkTableId
);
242 size_t size
=chunk
->getSize();
243 int noOfDataNodes
=os::floor((PAGE_SIZE
- sizeof(PageInfo
))/size
);
244 Page
* page
=chunk
->getFirstPage();
249 char *data
= ((char*)page
) + sizeof(PageInfo
);
250 for (i
= 0; i
< noOfDataNodes
; i
++)
252 if (*((int*)data
) == 1)
254 chk
=(Chunk
*)((int*)data
+1);
259 page
= (PageInfo
*)(((PageInfo
*)page
)->nextPage_
) ;
263 printf(" </User Chunk >\n");
264 printf("</Chunk information>\n");
267 printf("Wrong argument passed\n");
271 } else if (opt
== 11){
272 dbMgr
->printTreeIndexNodeInfo(name
, true);
273 } else if (opt
== 12){
274 dbMgr
->printTreeIndexNodeInfo(name
,false);
277 while (iter
.hasElement()) delete iter
.nextElement();