129 lines
4.5 KiB
Java
129 lines
4.5 KiB
Java
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.DBCollection;
|
|
import com.mongodb.DBCursor;
|
|
import com.mongodb.DBObject;
|
|
|
|
public class FolderStatusOperationManager {
|
|
|
|
Logger logger=LoggerFactory.getLogger(FolderStatusOperationManager.class);
|
|
|
|
DBCollection fsCollection;
|
|
|
|
public FolderStatusOperationManager(DBCollection fsCollection){
|
|
setFsCollection(fsCollection);
|
|
}
|
|
|
|
public void putFSRecord(String folder, long volume, int count, String lastUpdate){
|
|
BasicDBObject doc = new BasicDBObject("folder", folder)
|
|
.append("volume", volume)
|
|
.append("count", count)
|
|
.append("lastupdate", lastUpdate);
|
|
getFsCollection().insert(doc);
|
|
}
|
|
|
|
public FolderStatusRecord updateFolder(FolderStatusRecord fsRecord, String lastOperation){
|
|
FolderStatusRecord oldFsr=getFSRecord(fsRecord.getFolder());
|
|
if(oldFsr != null){
|
|
|
|
int partialCount=oldFsr.getCount();
|
|
int count = countCalculation(fsRecord.getCount(), partialCount, lastOperation);
|
|
fsRecord.setCount(count);
|
|
|
|
long partialVolume=oldFsr.getVolume();
|
|
long volume = volumeCalculation(fsRecord.getVolume(), partialVolume, lastOperation);
|
|
fsRecord.setVolume(volume);
|
|
|
|
final BasicDBObject query = new BasicDBObject("folder", fsRecord.getFolder());
|
|
// Creating BasicDBObjectBuilder object without arguments
|
|
DBObject documentBuilder = BasicDBObjectBuilder.start()
|
|
.add("volume", volume).add("count", count).add("lastUpdate", fsRecord.getLastUpdate()).get();
|
|
|
|
// get the dbobject from builder and Inserting document
|
|
getFsCollection().update(query,new BasicDBObject("$set", documentBuilder), true, false);
|
|
// // if is a move operation then will be update also the source folder
|
|
// if(lastOperation.equalsIgnoreCase("MOVE")){
|
|
// FolderStatusRecord originalFolderRecord = getFSRecord(folder)
|
|
// }
|
|
|
|
}else{
|
|
putFSRecord(fsRecord.getFolder(), fsRecord.getVolume(), fsRecord.getCount(), fsRecord.getLastUpdate());
|
|
}
|
|
return fsRecord;
|
|
}
|
|
|
|
|
|
|
|
public FolderStatusRecord getFSRecord(String folder){
|
|
BasicDBObject query = new BasicDBObject("folder", folder);
|
|
DBCursor cursor=getFsCollection().find(query);
|
|
DBObject obj=null;
|
|
try{
|
|
if(cursor.hasNext()){
|
|
obj=cursor.next();
|
|
|
|
}
|
|
}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 lastUpdate=null;
|
|
if(obj.containsField("lastUpdate")) lastUpdate=(String) obj.get("lastUpdate");
|
|
else logger.error("incomplete record found. lastUpdate field is missing");
|
|
String originalFolder=null;
|
|
if(obj.containsField("from")) originalFolder=(String) obj.get("from");
|
|
else logger.info(" originalFolder field is missing");
|
|
String id=(String)obj.get("id");
|
|
return new FolderStatusRecord(id, cons, vol, count, lastUpdate, originalFolder, obj);
|
|
}else{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private long volumeCalculation(long currentVolume, long partialVolume, String operation) {
|
|
logger.info("accounting: operation "+operation+" total Volume "+partialVolume+" current volume "+currentVolume);
|
|
if(operation.equalsIgnoreCase("UPLOAD") || operation.equalsIgnoreCase("COPY")){
|
|
partialVolume=partialVolume+currentVolume;
|
|
}else if(operation.equalsIgnoreCase("DELETE")){
|
|
partialVolume=partialVolume-currentVolume;
|
|
}
|
|
logger.info("new volume "+partialVolume);
|
|
return partialVolume;
|
|
}
|
|
|
|
private int countCalculation(int currentCount, int partialCount, String operation) {
|
|
logger.info("accounting: operation "+operation+" total count "+partialCount+" current count"+currentCount);
|
|
if(operation.equalsIgnoreCase("UPLOAD")|| operation.equalsIgnoreCase("COPY"))
|
|
partialCount=partialCount+currentCount;
|
|
else if(operation.equalsIgnoreCase("DELETE"))
|
|
partialCount=partialCount-currentCount;
|
|
logger.info("new count: "+partialCount);
|
|
return partialCount;
|
|
}
|
|
|
|
public DBCollection getFsCollection() {
|
|
return fsCollection;
|
|
}
|
|
|
|
public void setFsCollection(DBCollection fsCollection) {
|
|
this.fsCollection = fsCollection;
|
|
}
|
|
|
|
|
|
|
|
}
|