11package cl .alma .onedocument ;
22
33import java .net .UnknownHostException ;
4+ import java .util .Arrays ;
45import java .util .Calendar ;
56import java .util .Date ;
67import java .util .GregorianCalendar ;
78import java .util .HashMap ;
89import java .util .Map ;
910import java .util .concurrent .LinkedBlockingQueue ;
1011
12+ import org .apache .log4j .Logger ;
13+
1114import com .mongodb .BasicDBObject ;
1215import com .mongodb .DB ;
1316import com .mongodb .DBCollection ;
17+ import com .mongodb .DBCursor ;
1418import com .mongodb .DBObject ;
1519import com .mongodb .Mongo ;
1620
1721public class MongoManager implements Runnable {
22+ private static final Logger log = Logger .getLogger (MongoManager .class );
23+
1824 private Mongo mongo ;
1925 private DB database ;
20- private HashMap <String , DBCollection > mongoCollections ;
26+ //private HashMap<String, DBCollection> mongoCollections;
27+ private HashMap <Integer , DBCollection > mongoCollections ;
28+ private DBCollection multipleCollection ;
29+
2130 private DBCollection collection ;
2231 private LinkedBlockingQueue <DBObject > queue ;
2332
@@ -26,25 +35,44 @@ public MongoManager(String host, String dbname, String coll)
2635
2736 mongo = new Mongo (host );
2837 database = mongo .getDB (dbname );
29- mongoCollections = new HashMap <String , DBCollection >(70 );
38+ //mongoCollections = new HashMap<String, DBCollection>(70);
39+ mongoCollections = new HashMap <Integer , DBCollection >(70 );
3040 //collection = database.getCollection("monitorData");
3141 collection = database .getCollection (coll );
3242 }
3343
3444 public void setQueue (LinkedBlockingQueue <DBObject > queue ) {
3545 this .queue = queue ;
3646 }
37-
47+
3848 public DBCollection getCollection (DocumentID id ) {
3949 //String key = id.getAntenna() + DocumentID.SEPARATOR + id.getComponent();
40- String key = id .toString ();
50+ //String key = id.toString();
51+
52+ // Using the month as the key
53+ Integer key = id .getMonth ();
4154
4255 if (mongoCollections .containsKey (key ))
4356 return mongoCollections .get (key );
4457
58+ BasicDBObject index = null ;
59+ if (!database .collectionExists ("monitorData_" +key )) {
60+ System .out .println ("la coleccion no existe" );
61+ index = new BasicDBObject ("metadata.date" , 1 );
62+ index .append ("metadata.monitorPoint" , 1 );
63+ index .append ("metadata.antenna" , 1 );
64+ index .append ("metadata.component" , 1 );
65+ }
66+
4567 // if the collection does not exist, it will be created automatically
46- DBCollection c = database .getCollection ("monData_ " +key );
68+ DBCollection c = database .getCollection ("monitorData_ " +key );
4769 mongoCollections .put (key , c );
70+
71+ if (index !=null ) {
72+ //c.createIndex(index);
73+ c .ensureIndex (index );
74+ }
75+
4876 return c ;
4977 }
5078
@@ -79,7 +107,11 @@ public void upsert(Metadata metadata, int hour, int minute,
79107 //System.out.println("Document: "+document);
80108 //System.out.println("Update Document: "+updateDocument);
81109
82- collection .update (document , updateDocument , true , false );
110+ //collection.update(document, updateDocument, true, false);
111+
112+ // Codigo para usar colecciones mensuales.
113+ multipleCollection = getCollection (metadata .getDocumentID ());
114+ multipleCollection .update (document , updateDocument , true , false );
83115 }
84116
85117 /*public void update(DocumentID id, int hour, int minute, int second,
@@ -135,9 +167,10 @@ public static void main(String[] args) {
135167 mongoManager .upsert (metadata , 14 , 4 , 6 , "12345" );
136168 }
137169
170+ /*
138171 @Override
139172 public void run() {
140- int cont =0 ;
173+ int cont=0, error=0 ;
141174 try {
142175 while (true) {
143176 DBObject object = queue.take();
@@ -147,14 +180,18 @@ public void run() {
147180 //System.out.println("Set: "+mySet);
148181
149182
150- /* if (myMap.get("date") instanceof Date) {
183+ // if (myMap.get("date") instanceof Date) {
151184 //System.out.println("Es instacia de DATE!!");
152- } else {
185+ // } else {
153186 //System.out.println("NO lo es!");
154- }*/
187+ //}
155188
156189 Calendar calendar = Calendar.getInstance();
157- calendar .setTime ((Date )myMap .get ("date" ));
190+ try {
191+ calendar.setTime((Date)myMap.get("date"));
192+ } catch (NullPointerException e) {
193+ System.err.println("Object: "+myMap);
194+ }
158195
159196 // ************************************************ //
160197 // Se añaden las tres horas de diferencia //
@@ -213,9 +250,127 @@ public void run() {
213250 }
214251 } catch (InterruptedException e) {
215252
253+ } catch (Throwable e) {
254+ error++;
255+ //System.err.println("Exception caught: "+e.getMessage());
256+ log.error("Exception caught: "+e.getMessage());
216257 } finally {
217258 close();
218259 System.out.println("Registros insertados: "+cont);
260+ System.out.println("Errores: "+error);
261+ }
262+ }*/
263+
264+ @ Override
265+ public void run () {
266+ SynchronousQuery synQuery = null ;
267+ try {
268+ synQuery = new SynchronousQuery ("mongo-r1.osf.alma.cl" , "MONDB" ,
269+ "monitorPoints" );
270+ } catch (UnknownHostException e1 ) {
271+ System .exit (-1 );
272+ }
273+
274+ DBCursor cursor = synQuery .exportData ();
275+
276+ int cont =0 , error =0 ;
277+ while (cursor .hasNext ()) {
278+ try {
279+ //DBObject object = queue.take();
280+ DBObject object = cursor .next ();
281+
282+ //Set<String> mySet = object.keySet();
283+ Map <String , Object > myMap = object .toMap ();
284+ //System.out.println("Set: "+mySet);
285+
286+
287+ /*if (myMap.get("date") instanceof Date) {
288+ //System.out.println("Es instacia de DATE!!");
289+ } else {
290+ //System.out.println("NO lo es!");
291+ }*/
292+
293+ Calendar calendar = Calendar .getInstance ();
294+ try {
295+ calendar .setTime ((Date )myMap .get ("date" ));
296+ } catch (NullPointerException e ) {
297+ log .error ("NullPointerException: " +myMap );
298+ error ++;
299+ }
300+
301+ // ************************************************ //
302+ // Se añaden las tres horas de diferencia //
303+ // con el servidor de mongo. //
304+ // ************************************************ //
305+ calendar .add (Calendar .HOUR , 3 );
306+
307+ int year = calendar .get (Calendar .YEAR );
308+ int month = calendar .get (Calendar .MONTH )+1 ;
309+ int day = calendar .get (Calendar .DAY_OF_MONTH );
310+
311+ int hour = calendar .get (Calendar .HOUR_OF_DAY );
312+ int minute = calendar .get (Calendar .MINUTE );
313+ int second = calendar .get (Calendar .SECOND );
314+
315+ //System.out.println("Map: "+myMap+",\n year: "+year+", month: "+
316+ // month+", day: "+day+", hour: "+hour+", minute: "+minute+
317+ //", second: "+second);
318+
319+ // We need to split the componentName that comes from the old schema.
320+ // The format is "CONTROL/DV10/FrontEnd/Cryostat".
321+ // We extract the antenna, component and subcomponent
322+ // from it.
323+ String [] oldComponentName = ((String )myMap .get ("componentName" )).split ("/" );
324+ String antenna = oldComponentName [1 ];
325+ // Component and subcomponent are put together
326+ String component = null ;
327+
328+ // If the old ComponentName length is three it means there is
329+ // just a component, however, if the length is four
330+ // there is a component and subcomponent
331+ if (oldComponentName .length ==3 ) {
332+ component = oldComponentName [2 ];
333+ } else if (oldComponentName .length ==4 ) {
334+ component = oldComponentName [2 ]+"/" +oldComponentName [3 ];
335+ } else {
336+ log .error ("Error detected in component name: " +myMap .get ("componentName" ));
337+ }
338+
339+ //if (oldComponentName.length>3) {
340+ //component = oldComponentName[2]+"/"+oldComponentName[3];
341+ //} else {
342+ //component = oldComponentName[2];
343+ //}
344+
345+ String property = (String )myMap .get ("propertyName" );
346+ String monitorPoint = (String )myMap .get ("monitorPointName" );
347+ String location = (String )myMap .get ("location" );
348+ String serialNumber = (String )myMap .get ("serialNumber" );
349+ String monitorValue = (String )myMap .get ("monitorValue" );
350+ int index = Integer .parseInt (myMap .get ("index" ).toString ());
351+ int sampleTime = 0 ;
352+
353+ DocumentID documentID = new DocumentID (year , month , day ,
354+ antenna , component , monitorPoint );
355+ //Metadata metadata = new Metadata(documentID, "ASDF Property", "TFING",
356+ // "as76d6fh", 5, 2);
357+ Metadata metadata = new Metadata (documentID , property ,
358+ location , serialNumber , sampleTime , index );
359+
360+ //mongoManager.upsert(metadata, 14, 4, 6, "12345");
361+ upsert (metadata , hour , minute , second , monitorValue );
362+ cont ++;
363+ //insert(record);
364+ } catch (Throwable e ) {
365+ error ++;
366+ log .error ("Exception caught: " +e .getMessage ());
367+ log .error (Arrays .toString (e .getStackTrace ()));
368+ }
219369 }
370+
371+ synQuery .closeCursor ();
372+ close ();
373+ log .info ("Registros insertados: " +cont );
374+ log .info ("Errores: " +error );
220375 }
221376}
0 commit comments