eu.clarussecure.dataoperations.metadataStorage.MetadataStorage Maven / Gradle / Ivy
The newest version!
package eu.clarussecure.dataoperations.metadataStorage;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions;
import eu.clarussecure.dataoperations.splitting.SplitPoint;
import org.bson.Document;
import java.io.*;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import static com.mongodb.client.model.Filters.eq;
public class MetadataStorage {
private static MetadataStorage metaStorage = null;
private PropertiesManager props;
private final MongoClient mongoClient;
private final MongoDatabase db;
private final MongoCollection dbCollection;
private MongoCredential creds;
private int instancesNumber;
private MetadataStorage() {
props = new PropertiesManager();
if (props.getUsesAuth()) {
creds = MongoCredential.createCredential(props.getDbuser(), props.getDatabase(),
props.getDbpassword().toCharArray());
mongoClient = new MongoClient(new ServerAddress(props.getServer(), props.getPort()), Arrays.asList(creds));
} else {
mongoClient = new MongoClient(props.getServer(), props.getPort());
}
db = mongoClient.getDatabase(props.getDatabase());
dbCollection = db.getCollection(props.getCollectionName());
instancesNumber++;
}
public static MetadataStorage getInstance() {
if (MetadataStorage.metaStorage == null) {
MetadataStorage.metaStorage = new MetadataStorage();
}
return MetadataStorage.metaStorage;
}
public void deleteInstance() {
this.instancesNumber--;
if (this.instancesNumber <= 0) {
this.mongoClient.close();
MetadataStorage.metaStorage = null;
}
}
public void storeMetadata(String dataID, List