summaryrefslogtreecommitdiffstats
path: root/java/rasj/test/MassLoadTest.java
blob: 6ec4fbe8e8c856802a09747bc8d0027a1c33ef33 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* 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>.
*/
package rasj.test;

import rasj.*;
import rasj.global.*;
import org.odmg.*;

import java.util.*;
import java.io.*;


public class MassLoadTest implements RasGlobalDefs
{

    public static void main(String[] args)
    {

	String hostname = null;
	String port = null;
	Database mydb = null;
	Transaction myta = null;
	RasGMArray myMDD = null;
	Implementation myApp = null;
	OQLQuery myQuery = null;
	String collectionName = "testCollection";
	int tileSize = 1000;
	int overallSize = 200000;
	int commitInterval = 50;


	/*******************************************
	 *                                         *
	 * Usage: MassLoadTest hostname [port]     *
	 *                                         *
	 *******************************************/

	// we need the hostname
	if(args.length>0 && args[0] == null)
	    {
		System.out.println("Please provide the hostname as a parameter when calling this test-program.");
		System.exit(-1);
	    }
        else
	    hostname = args[0];

	// is there a port specified?
	if(args.length>1 && args[1] != null)
	    port = args[1];         
        else
	    port = "8080";

	try {
	    myApp = new RasImplementation("http://" + hostname + ":" + port);
	    mydb = myApp.newDatabase();
	    System.err.println("Open Database ...");
	    mydb.open("RASBASE",Database.OPEN_READ_WRITE);
	    System.err.println("     ok.");
	    myta = myApp.newTransaction();
	    System.err.println("Begin transaction ...");
	    myta.begin();
	    System.err.println("     ok.");

	    //create test collection
	    System.err.println("Create a test collection ...");
	    myQuery = myApp.newOQLQuery();
	    myQuery.create("create collection $1 GreySet");
	    //myQuery.create("drop collection $1");

	    myQuery.bind(collectionName);
	    myQuery.execute();
	    System.err.println("     ok.");
	    myta.commit();

	    // init insertion tile
	    System.err.println("create test arrays ...");
	    myMDD = new RasGMArray(new RasMInterval("[0:" + (tileSize-1) + ",0:" + (tileSize-1) + "]"),1);
	    int cells = tileSize*tileSize;
	    byte[] mydata = new byte[cells];
	    for(int i = 0; i < cells; i++)
		mydata[i]=0;
            myMDD.setArray(mydata);
	    myMDD.setObjectTypeName("GreyImage");

	    // now insert tiles
	    boolean first = true;
	    myta.begin();
	    int dimX = 0;
	    int dimY = 0;
	    String domain = null;
	    int finished = 1;

	    int counter = (int)Math.abs(overallSize/tileSize);
	    for(int x = 0; x < counter; x++)
		{
		    dimX += tileSize;
		    dimY = 0;
		    for(int y = 0; y < counter; y++)
			{
			    dimY += tileSize;

			    // init mdd
			    RasGMArray myMDD2 = new RasGMArray(new RasMInterval("[" + String.valueOf(dimX-tileSize) + ":" + String.valueOf(dimX-1) + "," +
				String.valueOf(dimY-tileSize) + ":" + String.valueOf(dimY-1) + "]"),1);
			    cells = tileSize*tileSize;
			    byte[] mydata2 = new byte[cells];
			    for(int i = 0; i < cells; i++)
				mydata2[i]=0;
			    myMDD2.setArray(mydata2);
			    myMDD2.setObjectTypeName("GreyImage");

			    // prepare query
			    myQuery = myApp.newOQLQuery();
			    if(first)
				{
				    myQuery.create("insert into $1 values $2");
				    first = false;
				}
			    else
				myQuery.create("update $1 AS img SET img ASSIGN $2");
			    myQuery.bind(collectionName);
			    myQuery.bind(myMDD2);

			    // execute query
			    System.err.println("Inserting tile " + myMDD2.spatialDomain());
			    myQuery.execute();
			    finished += 1;

			    // commit?
			    if(finished == commitInterval)
				{
				    myta.commit();
				    System.err.println("... committed, begin new transaction ...");
				    myta = myApp.newTransaction();
				    myta.begin();
				    finished = 1;
				}

			}
		    // now write the rest of y if there is something left:
		    if(dimY < overallSize-1)
			{
			    RasGMArray myMDD3 = new RasGMArray(new RasMInterval("[" + String.valueOf(dimX-tileSize) + ":" + String.valueOf(dimX-1) + "," +
										String.valueOf(dimY) + ":" + String.valueOf(overallSize-1) + "]"),1);
			    cells = tileSize*(overallSize-dimY);
			    byte[] mydata3 = new byte[cells];
			    for(int i = 0; i < cells; i++)
				mydata3[i]=0;
			    myMDD3.setArray(mydata3);
			    myMDD3.setObjectTypeName("GreyImage");

			    // prepare query
			    myQuery = myApp.newOQLQuery();
			    if(first)
				{
				    myQuery.create("insert into $1 values $2");
				    first = false;
				}
			    else
				myQuery.create("update $1 AS img SET img ASSIGN $2");
			    myQuery.bind(collectionName);
			    myQuery.bind(myMDD3);

			    // execute query
			    System.err.println("Inserting tile " + myMDD3.spatialDomain());
			    myQuery.execute();
			    finished += 1;

			    // commit?
			    if(finished == commitInterval)
				{
				    myta.commit();
				    System.err.println("... committed, begin new transaction ...");
				    myta = myApp.newTransaction();
				    myta.begin();
				    finished = 1;
				}
			}
		}

	    // now write the rest of x if there is something left
	    dimY = 0;
	    if(dimX < overallSize-1)
		{
		    for(int y = 0; y < counter; y++)
			{
			    dimY += tileSize;

			    // init mdd
			    RasGMArray myMDD4 = new RasGMArray(new RasMInterval("[" + String.valueOf(dimX) + ":" + String.valueOf(overallSize-1) + "," +
										String.valueOf(dimY-tileSize) + ":" + String.valueOf(dimY-1) + "]"),1);
			    cells = (overallSize-dimX)*tileSize;
			    byte[] mydata4 = new byte[cells];
			    for(int i = 0; i < cells; i++)
				mydata4[i]=0;
			    myMDD4.setArray(mydata4);
			    myMDD4.setObjectTypeName("GreyImage");

			    // prepare query
			    myQuery = myApp.newOQLQuery();
			    if(first)
				{
				    myQuery.create("insert into $1 values $2");
				    first = false;
				}
			    else
				myQuery.create("update $1 AS img SET img ASSIGN $2");
			    myQuery.bind(collectionName);
			    myQuery.bind(myMDD4);

			    // execute query
			    System.err.println("Inserting tile " + myMDD4.spatialDomain());
			    myQuery.execute();
			    finished += 1;

			    // commit?
			    if(finished == commitInterval)
				{
				    myta.commit();
				    System.err.println("... committed, begin new transaction ...");
				    myta = myApp.newTransaction();
				    myta.begin();
				    finished = 1;
				}
			}

		    // now write the rest of y if available
		    if(dimY < overallSize-1)
			{
			    RasGMArray myMDD5 = new RasGMArray(new RasMInterval("[" + String.valueOf(dimX) + ":" + String.valueOf(overallSize-1) + "," +
										String.valueOf(dimY) + ":" + String.valueOf(overallSize-1) + "]"),1);
			    cells = (overallSize-dimX)*(overallSize-dimY);
			    byte[] mydata5 = new byte[cells];
			    for(int i = 0; i < cells; i++)
				mydata5[i]=0;
			    myMDD5.setArray(mydata5);
			    myMDD5.setObjectTypeName("GreyImage");

			    // prepare query
			    myQuery = myApp.newOQLQuery();
			    if(first)
				{
				    myQuery.create("insert into $1 values $2");
				    first = false;
				}
			    else
				myQuery.create("update $1 AS img SET img ASSIGN $2");
			    myQuery.bind(collectionName);
			    myQuery.bind(myMDD5);

			    // execute query
			    System.err.println("Inserting tile " + myMDD5.spatialDomain());
			    myQuery.execute();
			    finished += 1;
			}
		}

	    // READY!			
	    myta.commit();
	    System.err.println("FINISHED!");


	    // drop test collection
	    myta = myApp.newTransaction();
	    myta.begin();
	    System.err.println("Drop test collection ...");
	    myQuery = myApp.newOQLQuery();
	    myQuery.create("drop collection $1");
	    myQuery.bind(collectionName);
	    myQuery.execute();
	    System.err.println("     ok.");
	    myta.commit();
	    myta = myApp.newTransaction();
	    myta.commit();



	}

	catch ( Exception e ) {
	    System.out.println("ERROR: ");
	    System.out.println(e.getMessage());

	    if(myta.isOpen())
		{
		    System.err.println("Abort transaction ...");
		    myta.abort();
		}
	    System.err.println("Close database ...");
	    try {
		mydb.close();
		System.err.println("     ok.");
	    }
	    catch ( Exception e2 ) {
	    }
	    System.err.println("Exiting...");
	    System.exit(-1);
	}
    }

}