Ticket #1329: RasdamanQuery.java

File RasdamanQuery.java, 10.6 KB (added by Dimitar Misev, 8 years ago)

test program

Line 
1
2import rasj.*;
3//import niftijio.NiftiVolume;
4
5//import rasj.odmg.*;
6import org.odmg.*;
7
8import java.io.IOException;
9import java.util.*;
10
11
12/**
13 * DFZ: This is to retrieve the data from Rasdaman
14 */
15public class RasdamanQuery {
16
17 /*
18 * DFZ: unit test
19 */
20 public static void main(String[] args) throws RasResultIsNoIntervalException, IOException, ODMGException {
21 //System.out.println("denoise collection exists? " + isCollExist("denoise"));
22 //System.out.println("notpossible collection exists? " + isCollExist("notpossible"));
23 query();
24 }
25
26 /*
27 * DFZ 5/23/16: added
28 */
29 public static int isCollExist (String coll) throws ODMGException {
30 int res = 0; //default is non existent
31
32 String server = "localhost";
33 String base = "RASBASE";
34 String port = "7001";
35 String user = "rasadmin";
36 String passwd = "rasadmin";
37
38 DBag resultBag = null;
39 RasMArrayDouble result = null;
40 Transaction myTa = null;
41 Database myDb = null;
42 OQLQuery myQu = null;
43
44 RasImplementation myApp =
45 new RasImplementation("http://" + server + ":" + port);
46 myApp.setUserIdentification(user, passwd);
47 myDb = myApp.newDatabase();
48 myDb.open(base, Database.OPEN_READ_WRITE);
49
50 //DFZ 5/23/16: attempt to create the collection
51 try {
52 myTa = myApp.newTransaction();
53 myTa.begin();
54 myQu = myApp.newOQLQuery();
55 myQu.create("create collection " + coll +" DoubleSet3");
56 myQu.execute();
57 myTa.commit();
58
59 myTa = myApp.newTransaction();
60 myTa.begin();
61 myQu = myApp.newOQLQuery();
62 myQu.create("drop collection " + coll);
63 myQu.execute();
64 myTa.commit();
65 } catch (Exception e) {
66 System.out.println(e.getMessage());
67 System.out.println("Please ignore the above error, it's benign.\n");
68 res = 1;
69 }
70
71 myDb.close();
72 return res;
73 }
74
75 /*
76 * DFZ 5/23/16: added
77 */
78 public static void store (String coll, double[][][][] rawData) throws ODMGException, RasResultIsNoIntervalException {
79 String server = "localhost";
80 String base = "RASBASE";
81 String port = "7001";
82 String user = "rasadmin";
83 String passwd = "rasadmin";
84
85// DBag resultBag = null;
86// RasMArrayDouble result = null;
87 Transaction myTa = null;
88 Database myDb = null;
89 OQLQuery myQu = null;
90
91 System.out.println("\nOpening database ...");
92 RasImplementation myApp =
93 new RasImplementation("http://" + server + ":" + port);
94 myApp.setUserIdentification(user, passwd);
95 myDb = myApp.newDatabase();
96 myDb.open(base, Database.OPEN_READ_WRITE);
97
98 //purge the old data, if needed
99 try {
100 myTa = myApp.newTransaction();
101 myTa.begin();
102 myQu = myApp.newOQLQuery();
103 myQu.create("drop collection " + coll);
104 myQu.execute();
105 myTa.commit();
106 } catch (Exception e) {
107 System.out.println(e.getMessage());
108 System.out.println("Please ignore the above error, it's benign.");
109 System.out.println("No need to drop " + coll +"; it doesn't exist.\n");
110 }
111
112 System.out.println("\nStarting transaction ...");
113 myTa = myApp.newTransaction();
114 myTa.begin();
115
116 //create a new collection
117 myQu = myApp.newOQLQuery();
118 myQu.create("create collection " + coll +" DoubleSet3");
119 myQu.execute();
120
121 //save MDDs to Rasdaman
122 double[] myArray = new double[145*174*145];
123 RasMArrayDouble myMDD = new RasMArrayDouble(
124 new RasMInterval("[1:145,1:174,1:145]"));
125 int t;
126 for (t = 0; t < 288; t++) { //DFZ TODO: 9 lifted to 288
127 System.out.println("Saving MDD #" + t);
128 for (int x = 0; x < 145; x++) { //start from 66 for non zero
129 for (int y = 0; y < 174; y++) { //start from 55 for non zero
130 for (int z = 0; z < 145; z++){ //start from 4 for non zero
131 myArray[x*145*174 + y*145 + z] = rawData[x][y][z][t]; //start from 58501
132 }
133 }
134 }
135
136 myMDD.setArray(myArray);
137 myMDD.setObjectTypeName("DoubleCube"); //is the argument arbitrary? no
138
139 myQu = myApp.newOQLQuery();
140 myQu.create("insert into " + coll + " values $1");
141 myQu.bind(myMDD);
142 myQu.execute();
143 }
144 System.out.println("\n" + t +" MDDs saved to Rasdaman.");
145
146 System.out.println("Committing transaction ...");
147 myTa.commit();
148 System.out.println("Closing database ...");
149 myDb.close();
150 }
151
152 /*
153 * DFZ 5/23/16: added
154 */
155 public static double[] query()
156 throws RasResultIsNoIntervalException, IOException {
157 return query("mri");
158 }
159
160 /*
161 * DFZ 5/23/16: add an argument to query to be used in multiple collections
162 */
163 public static double[] query(String coll)
164 throws IOException, RasResultIsNoIntervalException {
165 /*
166 * DFZ: start dealing with Rasdaman
167 */
168 final int VOL_SIZE = 145*174*145;
169 double[] valArray = new double[VOL_SIZE];
170 double[] res = new double[VOL_SIZE*288];
171
172 String server = "localhost";
173 String base = "RASBASE";
174 String port = "7001";
175 String user = "rasadmin";
176 String passwd = "rasadmin";
177
178 DBag resultBag = null;
179 RasMArrayDouble result = null;
180 Transaction myTa = null;
181 Database myDb = null;
182 OQLQuery myQu = null;
183
184 try {
185 RasImplementation myApp =
186 new RasImplementation("http://" + server + ":" + port);
187 myApp.setUserIdentification(user, passwd);
188
189 myDb = myApp.newDatabase();
190
191 System.out.println("\nOpening database ...");
192 myDb.open(base, Database.OPEN_READ_ONLY);
193 myTa = myApp.newTransaction();
194 myTa.begin();
195 myQu = myApp.newOQLQuery();
196 myQu.create("select c from " + coll + " as c");
197 System.out.println("Before query");
198 resultBag = (DBag) myQu.execute();
199 System.out.println("resultBag completed");
200
201 if (resultBag != null) {
202 int volIndex = 0;
203 Iterator iter = resultBag.iterator();
204 while (iter.hasNext()) {
205 result = (RasMArrayDouble) iter.next();
206 valArray = result.getDoubleArray();
207 System.arraycopy(valArray, 0, res, volIndex*VOL_SIZE, VOL_SIZE);
208 long sz = result.getArraySize();
209 System.out.println("Retrieved Volume #" + volIndex++ + " form Rasdaman/" + coll);
210 //DFZ: print the first 100 values
211// int cnt = 0;
212// for (int i = 0; i < sz/8 && cnt < 16; ++i) {
213// if (valArray[i] > 0.0005) {
214// cnt++;
215// System.out.print(i + ": " + valArray[i]);
216// if (0 == cnt%4) {
217// System.out.println();
218// }
219// else {
220// System.out.print(", ");
221// }
222// }
223// }
224 }
225 System.out.println("\nAll data loaded successfully.");
226 }
227 System.out.println("Committing transaction ...");
228 myTa.commit();
229 System.out.println("Closing database ...");
230 myDb.close();
231
232 } catch (org.odmg.ODMGException e) {
233 System.out.println("An exception has occurred: " + e.getMessage());
234 System.out.println("Try to abort the transaction ...");
235 if (myTa != null)
236 myTa.abort();
237
238 try {
239 System.out.println("Try to close the database ...");
240 if (myDb != null)
241 myDb.close();
242 } catch (org.odmg.ODMGException exp) {
243 System.err.println("Could not close the database: "
244 + exp.getMessage());
245 }
246 }
247 System.out.println("Done.\n");
248 return res;
249 }
250
251}