Added Session Callback Interface.

Added describeRing() to Operations and Template.
This commit is contained in:
dwebb
2013-11-11 11:41:57 -05:00
parent 63c92fbcf6
commit 2df492391b
4 changed files with 144 additions and 1 deletions

View File

@@ -18,15 +18,23 @@ package org.springframework.data.cassandra.core;
import java.util.List;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.vo.RingMember;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.querybuilder.Update;
/**
* @author Alex Shvid
*/
public interface CassandraOperations {
/**
* Describe the current Ring
*
* @return The list of ring tokens that are active in the cluster
*/
List<RingMember> describeRing();
/**
* The table name used for the specified class by this template.
*

View File

@@ -18,17 +18,22 @@ package org.springframework.data.cassandra.core;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
import org.springframework.data.cassandra.mapping.CassandraPersistentProperty;
import org.springframework.data.cassandra.vo.RingMember;
import org.springframework.data.convert.EntityReader;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.util.Assert;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
@@ -56,6 +61,54 @@ public class CassandraTemplate implements CassandraOperations {
this.mappingContext = this.cassandraConverter.getMappingContext();
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#describeRing()
*/
@Override
public List<RingMember> describeRing() {
/*
* Initialize the return variable
*/
List<RingMember> ring = new ArrayList<RingMember>();
/*
* Get the cluster metadata for this session
*/
Metadata clusterMetadata = session.getCluster().getMetadata();
/*
* Get all hosts in the cluster
*/
Set<Host> hosts = clusterMetadata.getAllHosts();
/*
* Loop variables
*/
RingMember member = null;
/*
* Populate Ring with Host Metadata
*/
for (Host h: hosts) {
member = new RingMember();
member.hostName = h.getAddress().getHostName();
member.address = h.getAddress().getHostAddress();
member.DC = h.getDatacenter();
member.rack = h.getRack();
ring.add(member);
}
/*
* Return
*/
return ring;
}
public String getTableName(Class<?> entityClass) {
return determineTableName(entityClass);
}
@@ -226,4 +279,24 @@ public class CassandraTemplate implements CassandraOperations {
return resolved == null ? ex : resolved;
}
/**
* Execute a command at the Session Level
*
* @param callback
* @return
*/
protected <T> T execute(SessionCallback<T> callback) {
Assert.notNull(callback);
try {
return callback.doInSession(session);
} catch (DataAccessException e) {
throw potentiallyConvertRuntimeException(e);
}
}
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2010-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.core;
import org.springframework.dao.DataAccessException;
import com.datastax.driver.core.Session;
public interface SessionCallback<T> {
T doInSession(Session s) throws DataAccessException;
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.vo;
import java.io.Serializable;
/**
* @author David Webb
*
*/
public class RingMember implements Serializable {
/*
* Ring attributes
*/
public String hostName;
public String address;
public String DC;
public String rack;
public String status;
public String state;
}