-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
3 deletions.
There are no files selected for viewing
44 changes: 41 additions & 3 deletions
44
cloudata-structured/src/main/java/com/cloudata/structured/sql/SqlEngineFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,64 @@ | ||
package com.cloudata.structured.sql; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
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; | ||
import com.google.common.base.Throwables; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
|
||
@Singleton | ||
public class SqlEngineFactory { | ||
private static final Logger log = LoggerFactory.getLogger(SqlEngineFactory.class); | ||
|
||
@Inject | ||
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); | ||
} | ||
} | ||
|
||
@Immutable | ||
final class SqlEngineLoader extends CacheLoader<Long, SqlEngine> { | ||
|
||
@Override | ||
public SqlEngine load(@Nonnull Long id) throws Exception { | ||
checkNotNull(id); | ||
try { | ||
StructuredStore structuredStore = stateMachine.getStructuredStore(id); | ||
|
||
return new SqlEngine(structuredStore, executor); | ||
} catch (Exception e) { | ||
log.warn("Error building SqlEngine", e); | ||
throw e; | ||
} | ||
} | ||
} | ||
} |