package ch.uzh.ifi.ddis.ifp.esper.cassandra;
/*
* #%L
* Cassandra for Esper
* %%
* Copyright (C) 2013 University of Zurich
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.hook.VirtualDataWindow;
import com.espertech.esper.client.hook.VirtualDataWindowContext;
import com.espertech.esper.client.hook.VirtualDataWindowEvent;
import com.espertech.esper.client.hook.VirtualDataWindowLookupContext;
import com.espertech.esper.client.hook.VirtualDataWindowLookupFieldDesc;
/**
* The virtual data window for Esper which stores results from a lookup to
* Cassandra.
*
* @author Thomas Scharrenbach
* @version 0.0.1
* @since 0.0.1
*
*/
public class CassandraVirtualDataWindow implements VirtualDataWindow {
private static final Logger _log = LoggerFactory
.getLogger(CassandraVirtualDataWindow.class);
//
//
//
private static String createQueryString(VirtualDataWindowContext context,
VirtualDataWindowLookupContext lookupContext) {
final StringBuffer queryBuffer = new StringBuffer();
boolean firstColumn = true;
queryBuffer.append("SELECT ");
for (String propertyName : context.getEventType().getPropertyNames()) {
if (firstColumn) {
firstColumn = false;
} else {
queryBuffer.append(", ");
}
queryBuffer.append(String.format("%s ", propertyName));
}
queryBuffer.append("FROM users ");
// Only add where clause if hints for hash or btree lookups are
// provided.
if (!(lookupContext.getHashFields().isEmpty() && lookupContext
.getBtreeFields().isEmpty())) {
queryBuffer.append("WHERE ");
boolean firstStatement = true;
for (VirtualDataWindowLookupFieldDesc field : lookupContext
.getHashFields()) {
if (firstStatement) {
firstStatement = false;
} else {
queryBuffer.append(" AND ");
}
queryBuffer.append(String.format(" %s %s ?",
field.getPropertyName(), field.getOperator().getOp()));
}
}
return queryBuffer.toString();
}
//
//
//
private final Session _session;
private final VirtualDataWindowContext _context;
//
//
//
/**
* Simple copy constructor.
*
* @param cluster
* the reference to the Cassandra cluster.
* @param context
* the Esper context.
* @param preparedStatementString
*/
CassandraVirtualDataWindow(Session session, VirtualDataWindowContext context) {
_context = context;
_session = session;
}
//
//
//
/**
* <p>
* Creates a new {@link CassandraVirtualDataWindowLookup} with a fresh
* prepared statement.
* </p>
* <p>
* <strong>Note: This method is thread-safe.</strong>
* </p>
*
* @see VirtualDataWindow#getLookup(VirtualDataWindowLookupContext)
* @return a new {@link CassandraVirtualDataWindowLookup} with this window's
* Cassandra session and Esper context.
*/
@Override
public CassandraVirtualDataWindowLookup getLookup(
VirtualDataWindowLookupContext lookupContext) {
final String queryString = createQueryString(_context, lookupContext);
_log.debug("Created query {}", queryString);
CassandraVirtualDataWindowLookup result = null;
synchronized (_session) {
final PreparedStatement statement = _session.prepare(queryString);
result = new CassandraVirtualDataWindowLookup(_session,
lookupContext, statement, _context);
}
return result;
}
/**
* Currently, this method performs nothing.
*/
@Override
public void handleEvent(VirtualDataWindowEvent theEvent) {
}
/**
* <p>
* Currently simply posts into the insert and remove stream what is
* received.
* </p>
*
* @see VirtualDataWindow#update(EventBean[], EventBean[])
*/
@Override
public void update(EventBean[] newData, EventBean[] oldData) {
_context.getOutputStream().update(newData, oldData);
}
/**
* <p>
* Shutdown the Cassandra {@link Session} if it is not null.
* </p>
*
* @throws RuntimeException
* in case shutting down the session fails.
*/
@Override
public void destroy() {
if (_session != null) {
try {
_log.info("Started shutting down session to Cassandra cluster...");
_session.shutdown();
_log.info("Finished shutting down session to Cassandra cluster.");
}
//
catch (Exception e) {
_log.error("Error shutting down session to Cassandra cluster!",
e);
throw new RuntimeException(e);
}
}
}
/**
* Currently returns an empty iterator. This might change in the future.
*/
@Override
public Iterator<EventBean> iterator() {
return java.util.Collections.<EventBean> emptyList().iterator();
}
//
//
//
}