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:
parent
3b40dced22
commit
0c14601b7d
|
@ -2,15 +2,14 @@ package org.gcube.contentmanager.storageserver.store;
|
|||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.BasicDBObjectBuilder;
|
||||
import com.mongodb.DB;
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBCursor;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.Mongo;
|
||||
import com.mongodb.MongoOptions;
|
||||
import com.mongodb.gridfs.GridFS;
|
||||
|
||||
public class MongoDB {
|
||||
|
||||
|
@ -87,6 +86,8 @@ public class MongoDB {
|
|||
boolean auth = db.authenticate(user, pwd.toCharArray());
|
||||
if(auth) logger.info("mongo is in authenticate mode");
|
||||
else logger.info("mongo is not in authenticate mode");
|
||||
ssCollection = db.getCollection(collection);
|
||||
ssCollection.findOne();
|
||||
// GridFS gfs = new GridFS(db);
|
||||
String firstServer = server[0];
|
||||
server[0] = srv;
|
||||
|
@ -103,11 +104,9 @@ public class MongoDB {
|
|||
}
|
||||
|
||||
public void put(String consumer, long volume, int count){
|
||||
// StorageStatusRecord ssr=new StorageStatusRecord(consumer, volume, count);
|
||||
BasicDBObject doc = new BasicDBObject("consumer", consumer)
|
||||
.append("volume", volume)
|
||||
.append("count", count);
|
||||
// .append("info", new BasicDBObject("x", 203).append("y", 102));
|
||||
ssCollection.insert(doc);
|
||||
}
|
||||
|
||||
|
@ -119,7 +118,12 @@ public class MongoDB {
|
|||
ssr.setCount(currentCount);
|
||||
long currentVolume=ssr.getVolume();
|
||||
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{
|
||||
put(consumer, volume, count);
|
||||
}
|
||||
|
@ -130,36 +134,29 @@ public class MongoDB {
|
|||
public StorageStatusRecord get(String consumer){
|
||||
BasicDBObject query = new BasicDBObject("consumer", consumer);
|
||||
DBCursor cursor=ssCollection.find(query);
|
||||
// if(cursor.length()>1){
|
||||
// logger.error("found more than one mongodb objects with consumer: "+consumer);
|
||||
// return null;
|
||||
// }else if(cursor== null){
|
||||
// logger.info("record with consumer: "+consumer+" not found");
|
||||
// return null;
|
||||
// }else{
|
||||
DBObject obj=null;
|
||||
try{
|
||||
if(cursor.hasNext()){
|
||||
obj=cursor.next();
|
||||
|
||||
}
|
||||
}finally{
|
||||
cursor.close();
|
||||
DBObject obj=null;
|
||||
try{
|
||||
if(cursor.hasNext()){
|
||||
obj=cursor.next();
|
||||
|
||||
}
|
||||
if(obj!=null){
|
||||
String cons=null;
|
||||
if(obj.containsField("consumer")) cons=(String) obj.get("consumer");
|
||||
else logger.error("incomplete record found. consumer field is missing");
|
||||
long vol =0;
|
||||
if(obj.containsField("volume")) vol=(long) obj.get("volume");
|
||||
else logger.error("incomplete record found. volume field is missing");
|
||||
int count=0;
|
||||
if(obj.containsField("count")) count=(int) obj.get("count");
|
||||
else logger.error("incomplete record found. count field is missing");
|
||||
String id=(String)obj.get("id");
|
||||
return new StorageStatusRecord(id, cons, vol, count);
|
||||
}else return null;
|
||||
// }
|
||||
}finally{
|
||||
cursor.close();
|
||||
}
|
||||
if(obj!=null){
|
||||
String cons=null;
|
||||
if(obj.containsField("consumer")) cons=(String) obj.get("consumer");
|
||||
else logger.error("incomplete record found. consumer field is missing");
|
||||
long vol =0;
|
||||
if(obj.containsField("volume")) vol=(long) obj.get("volume");
|
||||
else logger.error("incomplete record found. volume field is missing");
|
||||
int count=0;
|
||||
if(obj.containsField("count")) count=(int) obj.get("count");
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package org.gcube.contentmanager.storageserver.store;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
public class StorageStatusRecord {
|
||||
|
||||
private String consumer;
|
||||
|
@ -10,11 +12,14 @@ public class StorageStatusRecord {
|
|||
|
||||
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.consumer=consumer;
|
||||
this.volume=volume;
|
||||
this.count=count;
|
||||
this.dbo=obj;
|
||||
}
|
||||
|
||||
public String getConsumer() {
|
||||
|
@ -49,5 +54,13 @@ public class StorageStatusRecord {
|
|||
this.id = id;
|
||||
}
|
||||
|
||||
public DBObject getDbo() {
|
||||
return dbo;
|
||||
}
|
||||
|
||||
public void setDbo(DBObject dbo) {
|
||||
this.dbo = dbo;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -7,16 +7,16 @@ import org.junit.Test;
|
|||
|
||||
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;
|
||||
|
||||
// @BeforeClass
|
||||
@BeforeClass
|
||||
public static void init(){
|
||||
mongo=new MongoDB(server, null, null);
|
||||
}
|
||||
|
||||
|
||||
// @Test
|
||||
@Test
|
||||
public void putAndRetrieve(){
|
||||
mongo.update("test.consumer", 100, -1);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue