Merge pull request #470 from addo37/enhance-db

Enhance DBContext with a single variable getter, pushes forward #425
This commit is contained in:
Ruben Bermudez 2018-06-03 14:55:36 +02:00 committed by GitHub
commit c4e13e25b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 0 deletions

View File

@ -39,6 +39,13 @@ public interface DBContext extends Closeable {
*/ */
<T> Set<T> getSet(String name); <T> Set<T> getSet(String name);
/**
* @param name the unique name of the {@link Var}
* @param <T> the type that the variable holds
* @return the variable with the specified name
*/
<T> Var<T> getVar(String name);
/** /**
* @return a high-level summary of the database structures (Sets, Lists, Maps, ...) present. * @return a high-level summary of the database structures (Sets, Lists, Maps, ...) present.
*/ */

View File

@ -3,6 +3,7 @@ package org.telegram.abilitybots.api.db;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.mapdb.Atomic;
import org.mapdb.DB; import org.mapdb.DB;
import org.mapdb.DBMaker; import org.mapdb.DBMaker;
import org.mapdb.Serializer; import org.mapdb.Serializer;
@ -93,6 +94,11 @@ public class MapDBContext implements DBContext {
return (Set<T>) db.<T>hashSet(name, JAVA).createOrOpen(); return (Set<T>) db.<T>hashSet(name, JAVA).createOrOpen();
} }
@Override
public <T> Var<T> getVar(String name) {
return new MapDBVar<>((Atomic.Var<T>) db.atomicVar(name).createOrOpen());
}
@Override @Override
public String summary() { public String summary() {
return stream(db.getAllNames().spliterator(), false) return stream(db.getAllNames().spliterator(), false)

View File

@ -0,0 +1,49 @@
package org.telegram.abilitybots.api.db;
import com.google.common.base.MoreObjects;
import org.mapdb.Atomic;
import java.util.Objects;
/**
* The MapDB variant for {@link DBContext#getVar(String)}.
*
* @param <T> the type of the inner variable
*/
public final class MapDBVar<T> implements Var<T> {
private Atomic.Var<T> var;
public MapDBVar(Atomic.Var<T> var) {
this.var = var;
}
@Override
public T get() {
return var.get();
}
@Override
public void set(T var) {
this.var.set(var);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MapDBVar<?> mapDBVar = (MapDBVar<?>) o;
return Objects.equals(var, mapDBVar.var);
}
@Override
public int hashCode() {
return Objects.hash(var);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("var", var)
.toString();
}
}

View File

@ -0,0 +1,19 @@
package org.telegram.abilitybots.api.db;
/**
* The interface governing a variable for abstract getters and setters.
* @param <T> the type of the variable
*
* @author Abbas Abou Daya
*/
public interface Var<T> {
/**
* @return the variable contained
*/
T get();
/**
* @param var the new variable value
*/
void set(T var);
}

View File

@ -100,6 +100,23 @@ public class MapDBContextTest {
db.info(TEST); db.info(TEST);
} }
@Test
public void canGetAndSetVariables() {
String varName = "somevar";
Var<User> var = db.getVar(varName);
var.set(CREATOR);
db.commit();
var = db.getVar(varName);
assertEquals(var.get(), CREATOR);
var.set(USER);
db.commit();
Var<User> changedVar = db.getVar(varName);
assertEquals(changedVar.get(), USER);
}
@After @After
public void tearDown() throws IOException { public void tearDown() throws IOException {
db.clear(); db.clear();