StorageStatus: updateOperation DONE

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/content-management/storage-manager-trigger@96737 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
roberto.cirillo 2014-06-05 10:19:20 +00:00
parent 3b40dced22
commit 0c14601b7d
3 changed files with 48 additions and 38 deletions

View File

@ -2,15 +2,14 @@ package org.gcube.contentmanager.storageserver.store;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB; import com.mongodb.DB;
import com.mongodb.DBCollection; import com.mongodb.DBCollection;
import com.mongodb.DBCursor; import com.mongodb.DBCursor;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import com.mongodb.Mongo; import com.mongodb.Mongo;
import com.mongodb.MongoOptions; import com.mongodb.MongoOptions;
import com.mongodb.gridfs.GridFS;
public class MongoDB { public class MongoDB {
@ -87,6 +86,8 @@ public class MongoDB {
boolean auth = db.authenticate(user, pwd.toCharArray()); boolean auth = db.authenticate(user, pwd.toCharArray());
if(auth) logger.info("mongo is in authenticate mode"); if(auth) logger.info("mongo is in authenticate mode");
else logger.info("mongo is not in authenticate mode"); else logger.info("mongo is not in authenticate mode");
ssCollection = db.getCollection(collection);
ssCollection.findOne();
// GridFS gfs = new GridFS(db); // GridFS gfs = new GridFS(db);
String firstServer = server[0]; String firstServer = server[0];
server[0] = srv; server[0] = srv;
@ -103,11 +104,9 @@ public class MongoDB {
} }
public void put(String consumer, long volume, int count){ public void put(String consumer, long volume, int count){
// StorageStatusRecord ssr=new StorageStatusRecord(consumer, volume, count);
BasicDBObject doc = new BasicDBObject("consumer", consumer) BasicDBObject doc = new BasicDBObject("consumer", consumer)
.append("volume", volume) .append("volume", volume)
.append("count", count); .append("count", count);
// .append("info", new BasicDBObject("x", 203).append("y", 102));
ssCollection.insert(doc); ssCollection.insert(doc);
} }
@ -119,7 +118,12 @@ public class MongoDB {
ssr.setCount(currentCount); ssr.setCount(currentCount);
long currentVolume=ssr.getVolume(); long currentVolume=ssr.getVolume();
currentVolume=currentVolume+volume; currentVolume=currentVolume+volume;
String id=ssr.getId(); final BasicDBObject query = new BasicDBObject("consumer", consumer);
// Creating BasicDBObjectBuilder object without arguments
DBObject documentBuilder = BasicDBObjectBuilder.start()
.add("volume", currentVolume).add("count", currentCount).get();
// get the dbobject from builder and Inserting document
ssCollection.update(query,new BasicDBObject("$set", documentBuilder), true, false);
}else{ }else{
put(consumer, volume, count); put(consumer, volume, count);
} }
@ -130,36 +134,29 @@ public class MongoDB {
public StorageStatusRecord get(String consumer){ public StorageStatusRecord get(String consumer){
BasicDBObject query = new BasicDBObject("consumer", consumer); BasicDBObject query = new BasicDBObject("consumer", consumer);
DBCursor cursor=ssCollection.find(query); DBCursor cursor=ssCollection.find(query);
// if(cursor.length()>1){ DBObject obj=null;
// logger.error("found more than one mongodb objects with consumer: "+consumer); try{
// return null; if(cursor.hasNext()){
// }else if(cursor== null){ obj=cursor.next();
// logger.info("record with consumer: "+consumer+" not found");
// return null;
// }else{
DBObject obj=null;
try{
if(cursor.hasNext()){
obj=cursor.next();
}
}finally{
cursor.close();
} }
if(obj!=null){ }finally{
String cons=null; cursor.close();
if(obj.containsField("consumer")) cons=(String) obj.get("consumer"); }
else logger.error("incomplete record found. consumer field is missing"); if(obj!=null){
long vol =0; String cons=null;
if(obj.containsField("volume")) vol=(long) obj.get("volume"); if(obj.containsField("consumer")) cons=(String) obj.get("consumer");
else logger.error("incomplete record found. volume field is missing"); else logger.error("incomplete record found. consumer field is missing");
int count=0; long vol =0;
if(obj.containsField("count")) count=(int) obj.get("count"); if(obj.containsField("volume")) vol=(long) obj.get("volume");
else logger.error("incomplete record found. count field is missing"); else logger.error("incomplete record found. volume field is missing");
String id=(String)obj.get("id"); int count=0;
return new StorageStatusRecord(id, cons, vol, count); if(obj.containsField("count")) count=(int) obj.get("count");
}else return null; else logger.error("incomplete record found. count field is missing");
// } String id=(String)obj.get("id");
return new StorageStatusRecord(id, cons, vol, count, obj);
}else
return null;
} }
} }

View File

@ -1,5 +1,7 @@
package org.gcube.contentmanager.storageserver.store; package org.gcube.contentmanager.storageserver.store;
import com.mongodb.DBObject;
public class StorageStatusRecord { public class StorageStatusRecord {
private String consumer; private String consumer;
@ -10,11 +12,14 @@ public class StorageStatusRecord {
private String id; private String id;
public StorageStatusRecord(String id, String consumer, long volume, int count){ private DBObject dbo;
public StorageStatusRecord(String id, String consumer, long volume, int count, DBObject obj){
this.id=id; this.id=id;
this.consumer=consumer; this.consumer=consumer;
this.volume=volume; this.volume=volume;
this.count=count; this.count=count;
this.dbo=obj;
} }
public String getConsumer() { public String getConsumer() {
@ -49,5 +54,13 @@ public class StorageStatusRecord {
this.id = id; this.id = id;
} }
public DBObject getDbo() {
return dbo;
}
public void setDbo(DBObject dbo) {
this.dbo = dbo;
}
} }

View File

@ -7,16 +7,16 @@ import org.junit.Test;
public class MongoDBTest { public class MongoDBTest {
private static String[] server={"146.48.123.72","146.48.123.71"}; private static String[] server={"146.48.123.71","146.48.123.72"};
private static MongoDB mongo; private static MongoDB mongo;
// @BeforeClass @BeforeClass
public static void init(){ public static void init(){
mongo=new MongoDB(server, null, null); mongo=new MongoDB(server, null, null);
} }
// @Test @Test
public void putAndRetrieve(){ public void putAndRetrieve(){
mongo.update("test.consumer", 100, -1); mongo.update("test.consumer", 100, -1);
} }