Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2014 Eugene Valchkou.
*
* 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 com.datastax.driver.mapping;
import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto;
import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.datastax.driver.core.DataType;
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.datastax.driver.core.Statement;
import com.datastax.driver.core.querybuilder.Delete;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import com.datastax.driver.mapping.schemasync.SchemaSync;
/**
* MappingSession is API to work with entities to be persisted in Cassandra.
* This is lightweight wrapper for the datastax Session
* Usage: create one instance per datastax Session or have a new one for each request.
* MappingSession msession = new MappingSession(keyspace, session);
* msession.save(entity);
* msession.get(Entity.class, id);
* msession.delete(entity);
*/
public class MappingSession {
private Session session;
private String keyspace;
private static Map deleteCache = new HashMap();
private static Map selectCache = new HashMap();
public MappingSession(String keyspace, Session session) {
this.session = session;
this.keyspace = keyspace;
}
/**
* Return the persistent instance of the given entity class with the given identifier,
* or null if there is no such persistent instance
*
* @param clazz - a persistent class
* @param id - an identifier
* @return a persistent instance or null
*/
public T get(Class clazz, Object id) {
maybeSync(clazz);
BoundStatement bs = prepareSelect(clazz, id);
ResultSet rs = session.execute(bs);
List all = getFromResultSet(clazz, rs);
if (all.size() > 0) {
return all.get(0);
}
return null;
}
/**
* Delete the given instance
* @param entity - an instance of a persistent class
*/
public void delete(E entity) {
maybeSync(entity.getClass());
BoundStatement bs = prepareDelete(entity);
session.execute(bs);
}
/**
* Persist the given instance
* Entity must have a property id or a property annotated with @Id
* @param entity - an instance of a persistent class
* @return saved instance
*/
public void save(E entity) {
maybeSync(entity.getClass());
Statement insert = prepareInsert(entity);
session.execute(insert);
}
/**
* Execute the query and populate the list with items of given class.
*
* @param clazz
* @param query Statement
* @return List of items
*/
public List getByQuery(Class clazz, Statement query) {
maybeSync(clazz);
return getFromResultSet(clazz, session.execute(query));
}
/**
* Statement to persist an entity in Cassandra
* @param entity to be inserted
* @return com.datastax.driver.core.BoundStatement
*/
private Statement prepareInsert(E entity) {
Class> clazz = entity.getClass();
EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(clazz);
String table = entityMetadata.getTableName();
List fields = entityMetadata.getFields();
List pkCols = entityMetadata.getPkColumns();
List