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.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.EventBeanFactory;
import com.espertech.esper.client.hook.VirtualDataWindowContext;
import com.espertech.esper.client.hook.VirtualDataWindowLookup;
import com.espertech.esper.client.hook.VirtualDataWindowLookupContext;
/**
* <p>
* Esper lookup to Cassandra.
* </p>
* <p>
* A {@link CassandraVirtualDataWindowLookup} performs queries against a
* Cassandra cluster and inserts the values into a <a href=
* "http://esper.codehaus.org/esper-4.9.0/doc/reference/en-US/html_single/#extension-virtualdw"
* >virtual Esper data window</a>.
* </p>
* <p>
* <strong>Note: The lookup-method of this class, i.e.,
* {@link #lookup(Object[], EventBean[])} is thread-safe.</strong>
* </p>
*
* @author Thomas Scharrenbach
* @version 0.1.3
* @since 0.0.1
*
*/
public class CassandraVirtualDataWindowLookup implements
VirtualDataWindowLookup {
private final Session _session;
private final PreparedStatement _statement;
private final String[] _propertyNames;
private final EventBeanFactory _eventFactory;
//
//
//
//
//
//
/**
* <p>
* Creates a new lookup object that performs queries against a Cassandra
* cluster and inserts the values into a virtual Esper data window.
* </p>
*
* @param session
* the Cassandra session for performing queries against a
* Cassandra cluster.
* @param lookupContext
* the Esper context for creating EventBean objects.
* @param preparedStatement
* the prepared Esper statement for the lookup.
* @param propertyNames
*/
CassandraVirtualDataWindowLookup(Session session,
VirtualDataWindowLookupContext lookupContext,
PreparedStatement preparedStatement,
VirtualDataWindowContext context) {
_session = session;
_statement = preparedStatement;
_propertyNames = context.getEventType().getPropertyNames();
_eventFactory = context.getEventFactory();
}
//
//
//
/**
* <p>
* Execute a database query for the bound statements and create fresh
* {@link EventBean} objects from the query results.
* </p>
* <p>
* <strong>Note: This method is thread-safe.</strong>
* </p>
*/
@Override
public Set<EventBean> lookup(Object[] keys, EventBean[] eventsPerStream) {
final BoundStatement boundStatement = _statement.bind(keys);
final ResultSet cassandraResultList = _session.execute(boundStatement);
final Set<EventBean> esperResultSet = new HashSet<EventBean>();
final Iterator<Row> it = cassandraResultList.iterator();
// Iterate over all results and create fresh EventBean for all
// data results from the database query.
while (it.hasNext()) {
final Map<Object, Object> resultData = new HashMap<Object, Object>();
final Row cassandraRow = it.next();
for (String key : _propertyNames) {
resultData.put(key, cassandraRow.getString(key));
}
esperResultSet.add(_eventFactory.wrap(resultData));
}
return esperResultSet;
}
}