package io.vertx.ext.mongo.impl.config;
import com.mongodb.ConnectionString;
import com.mongodb.WriteConcern;
import io.vertx.core.json.JsonObject;
import java.util.concurrent.TimeUnit;
class WriteConcernParser {
private final WriteConcern writeConcern;
WriteConcernParser(ConnectionString connectionString, JsonObject config) {
WriteConcern connStringWriteConcern = null;
if (connectionString != null) {
connStringWriteConcern = connectionString.getWriteConcern();
}
if (connStringWriteConcern != null) {
writeConcern = connStringWriteConcern;
} else {
WriteConcern wc;
String wcs = config.getString("writeConcern");
if (wcs != null) {
wc = WriteConcern.valueOf(wcs);
if (wc == null) throw new IllegalArgumentException("Invalid WriteConcern " + wcs);
} else {
Boolean safe = config.getBoolean("safe");
Object w = config.getValue("w");
Integer wtimeout = config.getInteger("wtimeoutMS", null);
Boolean fsync = config.getBoolean("fsync", null);
Boolean j = config.getBoolean("j", null);
if (j == null) {
j = config.getBoolean("journal", null);
}
if (w != null || wtimeout != null || (fsync != null && fsync) || (j != null && j)) {
if (w == null) {
wc = new WriteConcern(1);
} else {
wc = getWriteConcern(w);
}
if (wtimeout != null) {
wc = wc.withWTimeout(wtimeout, TimeUnit.MILLISECONDS);
}
if (j != null) {
wc = wc.withJournal(j);
}
if (fsync != null) {
wc = wc.withFsync(fsync);
}
} else if (safe != null) {
wc = safe ? WriteConcern.ACKNOWLEDGED : WriteConcern.UNACKNOWLEDGED;
} else {
wc = null;
}
}
writeConcern = wc;
}
}
private WriteConcern getWriteConcern(Object w) {
WriteConcern wc;
if (w instanceof String) {
wc = new WriteConcern((String) w);
} else if (w instanceof Integer) {
wc = new WriteConcern((int) w);
} else {
throw new IllegalArgumentException("Invalid type " + w.getClass() + " for w of WriteConcern");
}
return wc;
}
WriteConcern writeConcern() {
return writeConcern;
}
}