summaryrefslogtreecommitdiffstats
path: root/relindexif/test/exportindex.cc
blob: 1e4e45b3310f428be5c8a8c3532f59246b52c57e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/*
get mddoid, iterate domain, dataformat

select mdd
get domain
	{
	begin ta
	iterate over domain
	retrieve tiles
	alter tiles
	commit ta
	}
*/

#ifdef EARLY_TEMPLATE
#define __EXECUTABLE__
#include "server/template_inst.hh"
#endif

#include "reladminif/adminif.hh"
#include "reladminif/databaseif.hh"
#include "reladminif/transactionif.hh"
#include "reladminif/oidif.hh"
#include "cmlparser.hh"
#include "relindexif/indexid.hh"

#define ALLDONE			0
#define ERRORPARSINGCOMMANDLINE	1
#define DOMAINMISSING		2
#define OIDMISSING		3
#define OIDINVALID		4
#define DOMAINMISMATCH		5
#define FAILED			6
#define DOMAININVALID		7	
#define UNKNOWNDATAFORMAT	8

char globalConnectId[256];

RMINITGLOBALS('S')

r_Minterval domain;
OId oid;

int
main(int argc, char** argv)
	{
        RMInit::logOut.rdbuf(cout.rdbuf());
        RMInit::dbgOut.rdbuf(cout.rdbuf());
	int retval = 0;
	bool import = false;
	const char* fileName = NULL;
	CommandLineParser    &cmlInter     	= CommandLineParser::getInstance();
	CommandLineParameter &clp_help		= cmlInter.addFlagParameter('h', "help", "show command line switches");
	CommandLineParameter &clp_import	= cmlInter.addStringParameter('i', "import", "import data");
	
	CommandLineParameter &clp_connect	= cmlInter.addStringParameter(CommandLineParser::noShortName, "connect", "database connect string", "/");
	CommandLineParameter &clp_oid		= cmlInter.addStringParameter(CommandLineParser::noShortName, "oid", "the oid of the mdd to operate on");
	try	{
		cmlInter.processCommandLine(argc, argv);
		}
	catch(CmlException& err)
		{
		cmlInter.printHelp();
		cout << "Error parsing command line:" << endl;
		cout << err.what() << endl;
		return ERRORPARSINGCOMMANDLINE;
		}
	if (cmlInter.isPresent('h'))
		{
		cmlInter.printHelp();
		return ALLDONE;
		}
	if (cmlInter.isPresent("oid"))
		{
		oid = OId(atol(cmlInter.getValueAsString("oid")));
		if (oid.getType() != OId::MDDHIERIXOID)
			{
			cout << "oid is not a mdd oid" << endl;
			return OIDINVALID;
			}
		}
	else	{
 		if (cmlInter.isPresent('i'))
			{
			import = true;
			fileName = cmlInter.getValueAsString('i');
			}
		else	{
			cout << "oid is missing" << endl;
			return OIDMISSING;
			}
		}
	strcpy((char*)globalConnectId, cmlInter.getValueAsString("connect"));
	cout << "connect " << globalConnectId << endl;
	cout << "set up done" << endl;
	AdminIf::instance();
	DatabaseIf d;
	TransactionIf t;
	if (import)
		{
		std::ifstream i;
		i.open(fileName);
		if (i.is_open())
			{
			i.seekg(0, std::ios::end);
			std::streampos end = i.tellg();
			size_t length = end;
			BinaryRepresentation brp;
			brp.binaryLength = length;
			brp.binaryName = new char[strlen(fileName) + 1];
			memcpy(brp.binaryName, fileName, strlen(fileName) + 1);
			brp.binaryData = new char[length];
			i.seekg(0, std::ios::beg);
			i.read(brp.binaryData, end);
			i.close();
			DBHierIndex* p = new DBHierIndex(0, false, false);
			p->setBinaryRepresentation(brp);
			delete [] brp.binaryData;
			brp.binaryData = NULL;
			delete [] brp.binaryName;
			brp.binaryName = NULL;
			p->printStatus();
			try	{
				d.open("RASBASE");
				t.begin(&d, false);
				ObjectBroker::registerDBObject(p);
				t.commit();
				d.close();
				}
			catch (const r_Error& e)
				{
				cout << "Caught exception " << e.get_errorno() << " " << e.what() << endl;
				try	{
					t.abort();
					}
				catch (const r_Error& ee)
					{
					cout << "Caugh exception while aborting " << ee.get_errorno() << " " << ee.what() << endl;
					try	{
						t.abort();
						}
					catch (const r_Error& eee)
						{
						cout << "Caugh exception while 2nd aborting " << eee.get_errorno() << " " << eee.what() << endl;
						}
					}
				try	{
					d.close();
					}
				catch (const r_Error& eeee)
					{
					cout << "Caugh exception while closing " << eeee.get_errorno() << " " << eeee.what() << endl;
					}
				return FAILED;
				}
			}
		else	{
			cout << "Unable to open file " << fileName << endl;
			}
		}
	else	{
		try	{
			d.open("RASBASE");
			t.begin(&d, true);
			DBHierIndexId index(oid);
			BinaryRepresentation bin = index->getBinaryRepresentation();
			index->printStatus();
			std::ofstream o;
			o.open(bin.binaryName);
			if (o.is_open())
				{
				o.write(bin.binaryData, bin.binaryLength);
				o.flush();
				o.close();
				}
			else	{
				cout << "Unable to open file " << bin.binaryName << endl;
				}
			delete [] bin.binaryName;
			delete [] bin.binaryData;
			t.commit();
			d.close();
			}
		catch (const r_Error& e)
			{
			cout << "Caught exception " << e.get_errorno() << " " << e.what() << endl;
			try	{
				t.abort();
				}
			catch (const r_Error& ee)
				{
				cout << "Caugh exception while aborting " << ee.get_errorno() << " " << ee.what() << endl;
				try	{
					t.abort();
					}
				catch (const r_Error& eee)
					{
					cout << "Caugh exception while 2nd aborting " << eee.get_errorno() << " " << eee.what() << endl;
					}
				}
			try	{
				d.close();
				}
			catch (const r_Error& eeee)
				{
				cout << "Caugh exception while closing " << eeee.get_errorno() << " " << eeee.what() << endl;
				}
			return FAILED;
			}
		}
	return retval;
	}