Skip to content

Commit

Permalink
Add ability to retrieve multi primes
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Dougal committed Mar 8, 2016
1 parent 2f3c961 commit 2e9937a
Show file tree
Hide file tree
Showing 17 changed files with 321 additions and 62 deletions.
Expand Up @@ -168,7 +168,23 @@ public enum MatchType {

public static abstract class VariableMatch {
private MatchType type = type();

protected abstract MatchType type();

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

VariableMatch that = (VariableMatch) o;

return type == that.type;
}

@Override
public int hashCode() {
return type != null ? type.hashCode() : 0;
}
}

public static class AnyMatch extends VariableMatch {
Expand All @@ -185,6 +201,15 @@ public ExactMatch(Object matcher) {
this.matcher = matcher;
}

@Override
public MatchType type() {
return MatchType.exact;
}

public Object matcher() {
return matcher;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -201,11 +226,6 @@ public int hashCode() {
return matcher != null ? matcher.hashCode() : 0;
}

@Override
public MatchType type() {
return MatchType.exact;
}

public static class Builder {
private Object matcher;

Expand Down Expand Up @@ -246,7 +266,6 @@ public int hashCode() {
return variable_matcher != null ? variable_matcher.hashCode() : 0;
}


public static class Builder {
private List<VariableMatch> variable_matcher;

Expand Down Expand Up @@ -335,11 +354,30 @@ public Outcome(Criteria criteria, Action action) {
this.criteria = criteria;
this.action = action;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Outcome outcome = (Outcome) o;

if (criteria != null ? !criteria.equals(outcome.criteria) : outcome.criteria != null) return false;
return !(action != null ? !action.equals(outcome.action) : outcome.action != null);

}

@Override
public int hashCode() {
int result = criteria != null ? criteria.hashCode() : 0;
result = 31 * result + (action != null ? action.hashCode() : 0);
return result;
}
}

public final static class When {
private final String query;
private final List<Consistency> consistency;
private final List<Consistency> consistency;

private When(String query, List<Consistency> consistency) {
this.query = query;
Expand Down
Expand Up @@ -27,10 +27,7 @@
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.scassandra.cql.CqlType;
import org.scassandra.http.client.types.GsonCqlTypeDeserialiser;
import org.scassandra.http.client.types.GsonCqlTypeSerialiser;
import org.scassandra.http.client.types.GsonDateSerialiser;
import org.scassandra.http.client.types.GsonInetAddressSerialiser;
import org.scassandra.http.client.types.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -44,11 +41,13 @@ public class PrimingClient {

public static final String DELETING_OF_PRIMES_FAILED = "Deleting of primes failed";
public static final String PRIMING_FAILED = "Priming failed";

public static class PrimingClientBuilder {

private String host = "localhost";

private int port = 8043;

private PrimingClientBuilder() {
}

Expand All @@ -67,6 +66,7 @@ public PrimingClient build() {
}

}

private static final Logger LOGGER = LoggerFactory.getLogger(PrimingClient.class);

public static PrimingClientBuilder builder() {
Expand All @@ -78,6 +78,8 @@ public static PrimingClientBuilder builder() {
.registerTypeAdapter(CqlType.class, new GsonCqlTypeDeserialiser())
.registerTypeAdapter(InetAddress.class, new GsonInetAddressSerialiser())
.registerTypeAdapter(Date.class, new GsonDateSerialiser())
.registerTypeAdapter(MultiPrimeRequest.VariableMatch.class, new GsonVariableMatchDeserialiser())
.registerTypeAdapter(MultiPrimeRequest.ExactMatch.class, new GsonExactMatchSerialiser())
.enableComplexMapKeySerialization()
.create();

Expand Down Expand Up @@ -121,7 +123,6 @@ public void multiPrime(MultiPrimeRequest primeRequest) throws PrimeFailedExcepti
prime(primeRequest, primePreparedMultiUrl);
}


/**
* @param primeRequest The Prime
* @deprecated Use prime() instead.
Expand All @@ -147,11 +148,15 @@ public void primePreparedStatement(PrimingRequest primeRequest) throws PrimeFail
}

public List<PrimingRequest> retrievePreparedPrimes() {
return httpGetPrimingRequests(primePreparedUrl);
return httpGetSinglePrimingRequests(primePreparedUrl);
}

public List<MultiPrimeRequest> retrievePreparedMultiPrimes() {
return httpGetMultiPrimingRequests(primePreparedMultiUrl);
}

public List<PrimingRequest> retrieveQueryPrimes() {
return httpGetPrimingRequests(primeQueryUrl);
return httpGetSinglePrimingRequests(primeQueryUrl);
}

public void clearAllPrimes() {
Expand All @@ -171,7 +176,19 @@ public void clearPreparedMultiPrimes() {
httpDelete(primePreparedMultiUrl);
}

private List<PrimingRequest> httpGetPrimingRequests(String url) {
private List<PrimingRequest> httpGetSinglePrimingRequests(String url) {
String responseAsString = httpGet(url);
PrimingRequest[] primes = (PrimingRequest[]) gson.fromJson(responseAsString, (Class) PrimingRequest[].class);
return Arrays.asList(primes);
}

private List<MultiPrimeRequest> httpGetMultiPrimingRequests(String url) {
String responseAsString = httpGet(url);
MultiPrimeRequest[] primes = (MultiPrimeRequest[]) gson.fromJson(responseAsString, (Class) MultiPrimeRequest[].class);
return Arrays.asList(primes);
}

private String httpGet(String url) {
HttpGet get = new HttpGet(url);
try {
CloseableHttpResponse httpResponse = httpClient.execute(get);
Expand All @@ -183,8 +200,7 @@ private List<PrimingRequest> httpGetPrimingRequests(String url) {
throw new PrimeFailedException(errorMessage);
}
LOGGER.debug("Received response from scassandra {}", responseAsString);
PrimingRequest[] primes = (PrimingRequest[]) gson.fromJson(responseAsString, (Class) PrimingRequest[].class);
return Arrays.asList(primes);
return responseAsString;
} catch (IOException e) {
LOGGER.info("Retrieving failed", e);
throw new PrimeFailedException("Retrieving of primes failed.", e);
Expand Down
@@ -0,0 +1,16 @@
package org.scassandra.http.client.types;

import com.google.gson.*;
import org.scassandra.http.client.MultiPrimeRequest;

import java.lang.reflect.Type;

public class GsonExactMatchSerialiser implements JsonSerializer<MultiPrimeRequest.ExactMatch> {
@Override
public JsonElement serialize(MultiPrimeRequest.ExactMatch src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jsonObject = new JsonObject();
jsonObject.add("matcher", context.serialize(src.matcher()));
jsonObject.addProperty("type", src.type().toString());
return jsonObject;
}
}
@@ -0,0 +1,24 @@
package org.scassandra.http.client.types;

import com.google.gson.*;
import org.scassandra.http.client.MultiPrimeRequest;

import java.lang.reflect.Type;

public class GsonVariableMatchDeserialiser implements JsonDeserializer<MultiPrimeRequest.VariableMatch> {
@Override
public MultiPrimeRequest.VariableMatch deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = (JsonObject) json;

String type = jsonObject.get("type").getAsString();

if (type.equalsIgnoreCase("any")) {
return new MultiPrimeRequest.AnyMatch();
} else if (type.equalsIgnoreCase("exact")) {
String matcher = jsonObject.get("matcher").getAsString();
return new MultiPrimeRequest.ExactMatch(matcher);
} else {
throw new JsonParseException("Unexpected variable matcher type received: " + type);
}
}
}
Expand Up @@ -12,6 +12,6 @@ public void testEqualsContract() {
EqualsVerifier.forClass(MultiPrimeRequest.Criteria.class).allFieldsShouldBeUsed().verify();
EqualsVerifier.forClass(MultiPrimeRequest.When.class).allFieldsShouldBeUsed().verify();
EqualsVerifier.forClass(MultiPrimeRequest.Then.class).allFieldsShouldBeUsed().verify();
EqualsVerifier.forClass(MultiPrimeRequest.ExactMatch.class).allFieldsShouldBeUsedExcept("type").verify();
EqualsVerifier.forClass(MultiPrimeRequest.ExactMatch.class).allFieldsShouldBeUsedExcept("type").withRedefinedSuperclass().verify();
}
}

0 comments on commit 2e9937a

Please sign in to comment.