Permalink
Browse files

many clients

  • Loading branch information...
1 parent 6094a4d commit 1f3b17243f4804eb8405fed6e76b57600cd6a0f8 @otaviojava committed Jul 31, 2012
View
@@ -5,7 +5,7 @@
<groupId>org.easycassandra</groupId>
<artifactId>EasyCassandra</artifactId>
- <version>1.1.0-ALFA</version>
+ <version>1.1.0-RC2</version>
<packaging>jar</packaging>
<name>EasyCassandra</name>
@@ -69,7 +69,6 @@
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
-
</license>
</licenses>
View
@@ -4,7 +4,8 @@ Easily programming with this framework, the Easy Cassandra
The EasyCassandra uses the Thrift implementation and has like the main objective be one simple ORM( Object relational manager). It need the jdk 7 for run, because some parts in your code was replaced reflection for invoke dynamic. So will have a behavior faster than other framework.
Version: 1.1.0
-
+* Client for many Client in sequencial mode
+* Client for many Client in sequencial random
* DownGrade to java 6
* create keyspace automatically
* do single client
@@ -311,7 +311,7 @@ public void setReferenciaSuperColunas(
/**
* @param keyStore the keyStore to set
*/
- public void setKeyStore(String keyStore) {
+ public void setKeySpace(String keyStore) {
this.keyStore = keyStore;
}
}
@@ -15,8 +15,7 @@
package org.easycassandra.persistence;
//import java.util.Objects;
-import org.apache.cassandra.thrift.Cassandra;
-import org.apache.cassandra.thrift.Cassandra.Client;
+import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TTransport;
/**
@@ -33,29 +32,18 @@
*/
private String host;
/**
- * The keyspace Name
- */
- private String keyspace;
- /**
* The number's port
*/
private Integer port;
/**
* @see TTransport
*/
private TTransport transport;
+
/**
- * @see Cassandra#Client
+ * @see TProtocol
*/
- private Cassandra.Client client;
-
- public Client getClient() {
- return client;
- }
-
- public void setClient(Client client) {
- this.client = client;
- }
+ private TProtocol protocol;
public String getHost() {
return host;
@@ -65,13 +53,6 @@ public void setHost(String host) {
this.host = host;
}
- public String getKeyspace() {
- return keyspace;
- }
-
- public void setKeyspace(String keyspace) {
- this.keyspace = keyspace;
- }
public Integer getPort() {
return port;
@@ -89,9 +70,19 @@ public void setTransport(TTransport transport) {
this.transport = transport;
}
- @Override
+
+
+ public TProtocol getProtocol() {
+ return protocol;
+ }
+
+ public void setProtocol(TProtocol protocol) {
+ this.protocol = protocol;
+ }
+
+ @Override
public String toString() {
- return host + keyspace + port;
+ return host + port;
}
@Override
@@ -115,17 +106,15 @@ public boolean equals(Object obj) {
public int hashCode() {
int hash = HASH_VALUE;
hash = HASH_CODE * hash + host.hashCode();
- hash = HASH_CODE * hash + keyspace.hashCode();
hash = HASH_CODE * hash + (this.port);
return hash;
}
public EasyCassandraClient() {
}
- public EasyCassandraClient(String host, String keyspace, Integer port) {
+ public EasyCassandraClient(String host, Integer port) {
this.host = host;
- this.keyspace = keyspace;
this.port = port;
}
}
@@ -19,7 +19,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -33,10 +32,9 @@
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
-import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
-import org.apache.thrift.transport.TTransport;
+import org.easycassandra.EasyCassandraException;
import org.easycassandra.ReplicaStrategy;
import org.easycassandra.util.DomUtil;
@@ -96,29 +94,38 @@ public static Client getClient(String keySpace, String host, int port){
* @return the Cassandra's Client
*/
public static Client getClient(String keySpace, String host, int port,ReplicaStrategy replicaStrategy,int replicaFator) {
- EasyCassandraClient conection = new EasyCassandraClient(host,
- keySpace, port);
- if (conections.contains(conection)) {
- return conections.get(conections.indexOf(conection)).getClient();
+ EasyCassandraClient easyClient = new EasyCassandraClient(host, port);
+ if (conections.contains(easyClient)) {
+ easyClient= conections.get(conections.indexOf(easyClient));
+
+ return makeConnection(keySpace, replicaStrategy, replicaFator,easyClient);
+
+
}
- TTransport transport = null;
- TProtocol protocol = null;
- Cassandra.Client client = null;
-
- try {
- transport = new TFramedTransport(new TSocket(host, port));
- protocol = new TBinaryProtocol(transport);
- client = new Cassandra.Client(protocol);
- transport.open();
+ easyClient.setTransport(new TFramedTransport(new TSocket(host, port)));
+ easyClient.setProtocol(new TBinaryProtocol(easyClient.getTransport()));
+
+ return makeConnection(keySpace, replicaStrategy, replicaFator,easyClient);
+ }
+ private static Client makeConnection(String keySpace,
+ ReplicaStrategy replicaStrategy, int replicaFator,
+ EasyCassandraClient conection) {
+ Cassandra.Client client=null;
+ try {
+
+ client = new Cassandra.Client(conection.getProtocol());
+ if(!conection.getTransport().isOpen()){
+ conection.getTransport().open();
+ }
client.set_keyspace(keySpace);
- addConnection(conection, transport, client);
+ addConnection(conection);
return client;
}catch (InvalidRequestException exception){
if(((InvalidRequestException)exception).getWhy().contains("Keyspace ".concat(keySpace).concat(" does not exist"))){
try {
createKeySpace(keySpace, replicaStrategy, replicaFator, client);
- addConnection(conection, transport, client);
+ addConnection(conection);
return client;
} catch (Exception e) {
Logger.getLogger(EasyCassandraManager.class.getName()).log(Level.SEVERE, null, e);
@@ -130,7 +137,7 @@ public static Client getClient(String keySpace, String host, int port,ReplicaStr
}
return null;
- }
+ }
/**
* Create the keyspace
@@ -167,14 +174,15 @@ private static void createKeySpace(String keySpace,
/**
* Add connection in EasyCassandraManager
* @param conection -
- * @param transport
- * @param client
+ * @param transport -
+ * @param protocol -
+ * @param client -
*/
- private static void addConnection(EasyCassandraClient conection,
- TTransport transport, Cassandra.Client client) {
- conection.setClient(client);
- conection.setTransport(transport);
+ private static void addConnection(EasyCassandraClient conection) {
+
+ if (!conections.contains(conection)) {
conections.add(conection);
+ }
}
/**
@@ -196,6 +204,41 @@ public static Persistence getPersistence(String keySpace, String host,
return new PersistenceSingleClient(client, referenceSuperColunms, keySpace);
}
+ /**
+ * get the Persistence with many clients and randomly access
+ * @param keySpace -keySpace's name
+ * @return - the Persistence
+ */
+ public static Persistence getPersistenceRandom(String keySpace) {
+
+ return new PersistenceRandomClient(getListClient(keySpace),referenceSuperColunms, keySpace);
+}
+ /**
+ * get the Persistence with many clients and sequential access
+ * @param keySpace -keySpace's name
+ * @return - the Persistence
+ */
+ public static Persistence getPersistenceSequencial(String keySpace) {
+
+ return new PersistenceSequencialClient(getListClient(keySpace),referenceSuperColunms, keySpace);
+}
+
+ /**
+ * make the list of client from the keyspace's name
+ * @param keySpace - the keyspace's name
+ * @return the client's list
+ */
+ private static List<Client> getListClient(String keySpace) {
+ List<Client> clients=new ArrayList<Cassandra.Client>();
+ if(conections.size()<1){
+ throw new EasyCassandraException("You should add client connections in EasyCassandraManager");
+
+ }
+ for(EasyCassandraClient easyClient:conections){
+ clients.add(makeConnection(keySpace, ReplicaStrategy.SimpleStrategy, REPLICA_FATOR_DEFAULT, easyClient));
+ }
+ return clients;
+ }
/**
* Add object for CQL in objects
@@ -233,6 +276,7 @@ public static void closeClients() {
if(referenceSuperColunms.get().size()>0){
DomUtil.getFileDom(referenceSuperColunms.get());
}
+ conections=new ArrayList<EasyCassandraClient>();
}
/**
@@ -243,10 +287,7 @@ public static int numberOfClients() {
return conections.size();
}
- public static Client getRandomCleint() {
- Random random=new Random();
- return conections.get(random.nextInt(conections.size())).getClient();
- }
+
private EasyCassandraManager(){
}
@@ -23,8 +23,15 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
+
import org.apache.cassandra.thrift.Cassandra.Client;
-import org.apache.cassandra.thrift.*;
+import org.apache.cassandra.thrift.Column;
+import org.apache.cassandra.thrift.ColumnParent;
+import org.apache.cassandra.thrift.Compression;
+import org.apache.cassandra.thrift.ConsistencyLevel;
+import org.apache.cassandra.thrift.CqlResult;
+import org.apache.cassandra.thrift.CqlRow;
+import org.apache.cassandra.thrift.InvalidRequestException;
import org.easycassandra.ConsistencyLevelCQL;
import org.easycassandra.EasyCassandraException;
import org.easycassandra.util.EncodingUtil;
@@ -37,7 +44,9 @@
*/
public abstract class Persistence extends BasePersistence {
- /**
+ private static final int HASH = 11;
+ private static final int HASH_VALUE = 5;
+ /**
* the value default for list the result
*/
private static final int DEFAULT_VALUE = 10000;
@@ -48,17 +57,20 @@
*
* @param client
* @param superColumnsRef
- * @param keyStore
+ * @param keySpace
*/
Persistence(AtomicReference<ColumnFamilyIds> superColumnsRef,
- String keyStore) {
+ String keySpace) {
setReferenciaSuperColunas(superColumnsRef);
- setKeyStore(keyStore);
+ setKeySpace(keySpace);
Thread thread = new Thread(new WriteDocument(superColumnsRef));
thread.setDaemon(true);
thread.start();
+
}
+ private String keySpace;
+
protected List retriveObject(String condiction, String condictionValue,
Class persistenceClass, ConsistencyLevelCQL consistencyLevel,
int limit, IndexColumnName... index) {
@@ -717,4 +729,28 @@ public JCassandra createJCassandra(String cql) {
}
return jCassandra;
}
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final Persistence other = (Persistence) obj;
+ if ((this.getKeySpace() == null) ? (other.getKeySpace() != null) : !this.getKeySpace().equals(other.getKeySpace())) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+
+ return HASH * HASH_VALUE + (this.getKeySpace() != null ? this.getKeySpace().hashCode() : 0);
+
+ }
+
+
}
Oops, something went wrong.

0 comments on commit 1f3b172

Please sign in to comment.