Skip to content
Browse files
Cache SqlEngine
  • Loading branch information
justinsb committed Dec 18, 2013
1 parent d7400fb commit 5953bc82b11ac8e849da5e7535044e5be2ae6c42
Showing 1 changed file with 41 additions and 3 deletions.
@@ -1,26 +1,64 @@
package com.cloudata.structured.sql;

import static;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
import javax.inject.Singleton;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cloudata.structured.StructuredStateMachine;
import com.cloudata.structured.StructuredStore;

public class SqlEngineFactory {
private static final Logger log = LoggerFactory.getLogger(SqlEngineFactory.class);

StructuredStateMachine stateMachine;

final ExecutorService executor = Executors.newCachedThreadPool();

public SqlEngine get(long storeId) {
StructuredStore structuredStore = stateMachine.getStructuredStore(storeId);
final LoadingCache<Long, SqlEngine> sqlEngineCache;

return new SqlEngine(structuredStore, executor);
public SqlEngineFactory() {
SqlEngineLoader loader = new SqlEngineLoader();
this.sqlEngineCache = CacheBuilder.newBuilder().recordStats().build(loader);

public SqlEngine get(long storeId) {
try {
return sqlEngineCache.get(storeId);
} catch (ExecutionException e) {
throw Throwables.propagate(e);

final class SqlEngineLoader extends CacheLoader<Long, SqlEngine> {

public SqlEngine load(@Nonnull Long id) throws Exception {
try {
StructuredStore structuredStore = stateMachine.getStructuredStore(id);

return new SqlEngine(structuredStore, executor);
} catch (Exception e) {
log.warn("Error building SqlEngine", e);
throw e;

0 comments on commit 5953bc8

Please sign in to comment.