// Path: myeslib-util/src/main/java/org/myeslib/util/jdbi/ClobToStringMapper.java
// public class ClobToStringMapper extends TypedMapper<String> {
// 	
// 	public ClobToStringMapper(){
// 		super();
// 	}
// 
// 	public ClobToStringMapper(int index)
//     {
//         super(index);
//     }
// 
// 	public ClobToStringMapper(String name)
//     {
//         super(name);
//     }
// 	
// 	@Override
// 	protected String extractByName(ResultSet r, String name) throws SQLException {
// 		Clob clob = r.getClob(name);
// 		try {
// 			return CharStreams.toString(clob.getCharacterStream());
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		} finally {
// 			clob.free();
// 		}
// 		return null;	
// 	}
// 
// 	@Override
// 	protected String extractByIndex(ResultSet r, int index) throws SQLException {
// 		Clob clob = r.getClob(index);
// 		try {
// 			return CharStreams.toString(clob.getCharacterStream());
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		} finally {
// 			clob.free();
// 		}
// 		return null;		
// 	}
// 
//     public static final ClobToStringMapper FIRST = new ClobToStringMapper();
// }


import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.myeslib.util.jdbi.ClobToStringMapper;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.PreparedBatch;
import org.skife.jdbi.v2.TransactionCallback;
import org.skife.jdbi.v2.TransactionIsolationLevel;
import org.skife.jdbi.v2.TransactionStatus;
import org.skife.jdbi.v2.tweak.HandleCallback;
import org.skife.jdbi.v2.util.StringMapper;
import com.hazelcast.core.MapStore;
	@Override
	public Set<UUID> loadAllKeys() {
		// TODO define how many keys will be pre-loaded
		log.debug("loading all keys from table {}", tableName);
		Set<UUID> result = dbi.withHandle(new HandleCallback<Set<UUID>>() {
			@Override
			public Set<UUID> withHandle(Handle h) throws Exception {
				List<String> strResult = h.createQuery(String.format("select id from %s", tableName))
										  .map(new StringMapper()).list();
				Set<UUID> uResult = new HashSet<>();
				for (String uuid : strResult){
					uResult.add(UUID.fromString(uuid));
				}
				return uResult;
			}
		});
		log.debug("{} keys within table {} were loaded", result.size(), tableName);
		return result;	
	}
	
	@Override
	public String load(final UUID id) {
		String result = null;
		try {
			log.debug("will load {} from table {}", id.toString(), tableName);
			result = dbi.inTransaction(TransactionIsolationLevel.READ_COMMITTED,
					new TransactionCallback<String>() {
						@Override
						public String inTransaction(Handle h, TransactionStatus ts) throws Exception {
							String sql = String.format("select aggregate_root_data from %s where id = :id", tableName);
// Path: myeslib-util/src/main/java/org/myeslib/util/jdbi/ClobToStringMapper.java
// public class ClobToStringMapper extends TypedMapper<String> {
// 	
// 	public ClobToStringMapper(){
// 		super();
// 	}
// 
// 	public ClobToStringMapper(int index)
//     {
//         super(index);
//     }
// 
// 	public ClobToStringMapper(String name)
//     {
//         super(name);
//     }
// 	
// 	@Override
// 	protected String extractByName(ResultSet r, String name) throws SQLException {
// 		Clob clob = r.getClob(name);
// 		try {
// 			return CharStreams.toString(clob.getCharacterStream());
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		} finally {
// 			clob.free();
// 		}
// 		return null;	
// 	}
// 
// 	@Override
// 	protected String extractByIndex(ResultSet r, int index) throws SQLException {
// 		Clob clob = r.getClob(index);
// 		try {
// 			return CharStreams.toString(clob.getCharacterStream());
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		} finally {
// 			clob.free();
// 		}
// 		return null;		
// 	}
// 
//     public static final ClobToStringMapper FIRST = new ClobToStringMapper();
// }


// Path: myeslib-util/src/main/java/org/myeslib/util/hazelcast/HzStringMapStore.java
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.myeslib.util.jdbi.ClobToStringMapper;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.PreparedBatch;
import org.skife.jdbi.v2.TransactionCallback;
import org.skife.jdbi.v2.TransactionIsolationLevel;
import org.skife.jdbi.v2.TransactionStatus;
import org.skife.jdbi.v2.tweak.HandleCallback;
import org.skife.jdbi.v2.util.StringMapper;
import com.hazelcast.core.MapStore;

	@Override
	public Set<UUID> loadAllKeys() {
		// TODO define how many keys will be pre-loaded
		log.debug("loading all keys from table {}", tableName);
		Set<UUID> result = dbi.withHandle(new HandleCallback<Set<UUID>>() {
			@Override
			public Set<UUID> withHandle(Handle h) throws Exception {
				List<String> strResult = h.createQuery(String.format("select id from %s", tableName))
										  .map(new StringMapper()).list();
				Set<UUID> uResult = new HashSet<>();
				for (String uuid : strResult){
					uResult.add(UUID.fromString(uuid));
				}
				return uResult;
			}
		});
		log.debug("{} keys within table {} were loaded", result.size(), tableName);
		return result;	
	}
	
	@Override
	public String load(final UUID id) {
		String result = null;
		try {
			log.debug("will load {} from table {}", id.toString(), tableName);
			result = dbi.inTransaction(TransactionIsolationLevel.READ_COMMITTED,
					new TransactionCallback<String>() {
						@Override
						public String inTransaction(Handle h, TransactionStatus ts) throws Exception {
							String sql = String.format("select aggregate_root_data from %s where id = :id", tableName);
							return h.createQuery(sql).bind("id", id.toString()).map(ClobToStringMapper.FIRST).first();