Command context refactoring.
Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/9f0af85e Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/9f0af85e Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/9f0af85e Branch: refs/heads/blur-384-random-port-cleanup Commit: 9f0af85e9846d43c9a53242ab5e1a057adb47c0d Parents: 7701555 Author: Aaron McCurry <[email protected]> Authored: Tue Sep 30 08:47:17 2014 -0400 Committer: Aaron McCurry <[email protected]> Committed: Tue Sep 30 08:47:17 2014 -0400 ---------------------------------------------------------------------- .../org/apache/blur/command/BaseContext.java | 32 ++++++++++++ .../org/apache/blur/command/ClusterContext.java | 21 ++------ .../apache/blur/command/CombiningContext.java | 12 +---- .../java/org/apache/blur/command/Command.java | 9 ++++ .../org/apache/blur/command/IndexContext.java | 10 ++-- .../blur/command/ShardCommandManager.java | 54 ++++++++++++-------- 6 files changed, 85 insertions(+), 53 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f0af85e/blur-core/src/main/java/org/apache/blur/command/BaseContext.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/BaseContext.java b/blur-core/src/main/java/org/apache/blur/command/BaseContext.java new file mode 100644 index 0000000..4859cd6 --- /dev/null +++ b/blur-core/src/main/java/org/apache/blur/command/BaseContext.java @@ -0,0 +1,32 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You 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 org.apache.blur.command; + +import java.io.IOException; + +import org.apache.blur.BlurConfiguration; +import org.apache.blur.server.TableContext; + +public abstract class BaseContext { + + public abstract Args getArgs(); + + public abstract TableContext getTableContext(String table) throws IOException; + + public abstract BlurConfiguration getBlurConfiguration(String table) throws IOException; +} http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f0af85e/blur-core/src/main/java/org/apache/blur/command/ClusterContext.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/ClusterContext.java b/blur-core/src/main/java/org/apache/blur/command/ClusterContext.java index 20ed372..1020d07 100644 --- a/blur-core/src/main/java/org/apache/blur/command/ClusterContext.java +++ b/blur-core/src/main/java/org/apache/blur/command/ClusterContext.java @@ -4,9 +4,6 @@ import java.io.IOException; import java.util.Map; import java.util.concurrent.Future; -import org.apache.blur.BlurConfiguration; -import org.apache.blur.server.TableContext; - /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this @@ -24,29 +21,21 @@ import org.apache.blur.server.TableContext; * the License. */ -public abstract class ClusterContext { - - public abstract Args getArgs(); +public abstract class ClusterContext extends BaseContext { - public abstract TableContext getTableContext(String table) throws IOException; - - public abstract BlurConfiguration getBlurConfiguration(String table) throws IOException; - - public abstract <T> Map<Shard, T> readIndexes(Args args, Class<? extends IndexRead<T>> clazz) - throws IOException; + public abstract <T> Map<Shard, T> readIndexes(Args args, Class<? extends IndexRead<T>> clazz) throws IOException; public abstract <T> Map<Shard, Future<T>> readIndexesAsync(Args args, Class<? extends IndexRead<T>> clazz) throws IOException; public abstract <T> T readIndex(Args args, Class<? extends IndexRead<T>> clazz) throws IOException; - public abstract <T> Future<T> readIndexAsync(Args args, Class<? extends IndexRead<T>> clazz) - throws IOException; + public abstract <T> Future<T> readIndexAsync(Args args, Class<? extends IndexRead<T>> clazz) throws IOException; public abstract <T> Map<Server, T> readServers(Args args, Class<? extends IndexReadCombining<?, T>> clazz) throws IOException; - public abstract <T> Map<Server, Future<T>> readServersAsync(Args args, - Class<? extends IndexReadCombining<?, T>> clazz) throws IOException; + public abstract <T> Map<Server, Future<T>> readServersAsync(Args args, Class<? extends IndexReadCombining<?, T>> clazz) + throws IOException; } http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f0af85e/blur-core/src/main/java/org/apache/blur/command/CombiningContext.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/CombiningContext.java b/blur-core/src/main/java/org/apache/blur/command/CombiningContext.java index 679fc63..7619bdc 100644 --- a/blur-core/src/main/java/org/apache/blur/command/CombiningContext.java +++ b/blur-core/src/main/java/org/apache/blur/command/CombiningContext.java @@ -16,17 +16,7 @@ */ package org.apache.blur.command; -import java.io.IOException; -import org.apache.blur.BlurConfiguration; -import org.apache.blur.server.TableContext; - -public abstract class CombiningContext { - - public abstract Args getArgs(); - - public abstract TableContext getTableContext(String table) throws IOException; - - public abstract BlurConfiguration getBlurConfiguration(String table) throws IOException; +public abstract class CombiningContext extends BaseContext { } http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f0af85e/blur-core/src/main/java/org/apache/blur/command/Command.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/Command.java b/blur-core/src/main/java/org/apache/blur/command/Command.java index 780d7f0..e8fa29d 100644 --- a/blur-core/src/main/java/org/apache/blur/command/Command.java +++ b/blur-core/src/main/java/org/apache/blur/command/Command.java @@ -17,6 +17,7 @@ package org.apache.blur.command; import java.io.IOException; +import java.util.Set; import org.apache.blur.command.annotation.Argument; import org.apache.blur.command.annotation.OptionalArguments; @@ -32,6 +33,14 @@ public abstract class Command<R> implements Cloneable { public abstract R run(Args arguments, String connectionStr) throws IOException; + public Set<Shard> resolveShards(BaseContext context) { + return null; + } + + public Set<String> resolveTables(BaseContext context) { + return null; + } + @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Command<R> clone() { http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f0af85e/blur-core/src/main/java/org/apache/blur/command/IndexContext.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/IndexContext.java b/blur-core/src/main/java/org/apache/blur/command/IndexContext.java index c1a1c06..97f0e09 100644 --- a/blur-core/src/main/java/org/apache/blur/command/IndexContext.java +++ b/blur-core/src/main/java/org/apache/blur/command/IndexContext.java @@ -1,5 +1,7 @@ package org.apache.blur.command; +import java.io.IOException; + import org.apache.blur.BlurConfiguration; import org.apache.blur.server.TableContext; import org.apache.lucene.index.IndexReader; @@ -22,11 +24,9 @@ import org.apache.lucene.search.IndexSearcher; * the License. */ -public abstract class IndexContext { - - public abstract Args getArgs(); +public abstract class IndexContext extends BaseContext { - public abstract TableContext getTableContext(); + public abstract TableContext getTableContext() throws IOException; public abstract Shard getShard(); @@ -34,6 +34,6 @@ public abstract class IndexContext { public abstract IndexSearcher getIndexSearcher(); - public abstract BlurConfiguration getBlurConfiguration(); + public abstract BlurConfiguration getBlurConfiguration() throws IOException; } http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f0af85e/blur-core/src/main/java/org/apache/blur/command/ShardCommandManager.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/ShardCommandManager.java b/blur-core/src/main/java/org/apache/blur/command/ShardCommandManager.java index 67a837d..4c3b006 100644 --- a/blur-core/src/main/java/org/apache/blur/command/ShardCommandManager.java +++ b/blur-core/src/main/java/org/apache/blur/command/ShardCommandManager.java @@ -118,7 +118,6 @@ public class ShardCommandManager extends BaseCommandManager { Set<Shard> shardSet = shardMap.get(table); boolean checkShards = !shardSet.isEmpty(); - TableContext tableContext = tableContextFactory.getTableContext(table); Map<String, BlurIndex> indexes = _indexServer.getIndexes(table); for (Entry<String, BlurIndex> e : indexes.entrySet()) { String shardId = e.getKey(); @@ -132,11 +131,11 @@ public class ShardCommandManager extends BaseCommandManager { Callable<Object> callable; if (command instanceof IndexRead) { final IndexRead<?> readCommand = (IndexRead<?>) command.clone(); - callable = getCallable(shardServerContext, tableContext, args, shard, blurIndex, readCommand); + callable = getCallable(shardServerContext, tableContextFactory, table, args, shard, blurIndex, readCommand); } else if (command instanceof IndexReadCombining) { - final IndexReadCombining<?, ?> readCombiningCommand = (IndexReadCombining<?, ?>) command - .clone(); - callable = getCallable(shardServerContext, tableContext, args, shard, blurIndex, readCombiningCommand); + final IndexReadCombining<?, ?> readCombiningCommand = (IndexReadCombining<?, ?>) command.clone(); + callable = getCallable(shardServerContext, tableContextFactory, table, args, shard, blurIndex, + readCombiningCommand); } else { throw new IOException("Command type of [" + command.getClass() + "] not supported."); } @@ -169,50 +168,53 @@ public class ShardCommandManager extends BaseCommandManager { return resultMap; } - private Callable<Object> getCallable(final ShardServerContext shardServerContext, final TableContext tableContext, - final Args args, final Shard shard, final BlurIndex blurIndex, - final IndexReadCombining<?, ?> readCombiningCommand) { + private Callable<Object> getCallable(final ShardServerContext shardServerContext, + final TableContextFactory tableContextFactory, final String table, final Args args, final Shard shard, + final BlurIndex blurIndex, final IndexReadCombining<?, ?> readCombiningCommand) { return new Callable<Object>() { @Override public Object call() throws Exception { - String table = tableContext.getTable(); String shardId = shard.getShard(); IndexSearcherClosable searcher = shardServerContext.getIndexSearcherClosable(table, shardId); if (searcher == null) { searcher = blurIndex.getIndexSearcher(); shardServerContext.setIndexSearcherClosable(table, shardId, searcher); } - return readCombiningCommand.execute(new ShardIndexContext(tableContext, shard, searcher, args)); + return readCombiningCommand.execute(new ShardIndexContext(tableContextFactory, table, shard, searcher, args)); } }; } - private Callable<Object> getCallable(final ShardServerContext shardServerContext, final TableContext tableContext, - final Args args, final Shard shard, final BlurIndex blurIndex, final IndexRead<?> readCommand) { + private Callable<Object> getCallable(final ShardServerContext shardServerContext, + final TableContextFactory tableContextFactory, final String table, final Args args, final Shard shard, + final BlurIndex blurIndex, final IndexRead<?> readCommand) { return new Callable<Object>() { @Override public Object call() throws Exception { - String table = tableContext.getTable(); + String shardId = shard.getShard(); IndexSearcherClosable searcher = shardServerContext.getIndexSearcherClosable(table, shardId); if (searcher == null) { searcher = blurIndex.getIndexSearcher(); shardServerContext.setIndexSearcherClosable(table, shardId, searcher); } - return readCommand.execute(new ShardIndexContext(tableContext, shard, searcher, args)); + return readCommand.execute(new ShardIndexContext(tableContextFactory, table, shard, searcher, args)); } }; } static class ShardIndexContext extends IndexContext { - private final TableContext _tableContext; private final Shard _shard; private final IndexSearcher _searcher; private final Args _args; + private final TableContextFactory _tableContextFactory; + private final String _table; - public ShardIndexContext(TableContext tableContext, Shard shard, IndexSearcher searcher, Args args) { - _tableContext = tableContext; + public ShardIndexContext(TableContextFactory tableContextFactory, String table, Shard shard, + IndexSearcher searcher, Args args) { + _tableContextFactory = tableContextFactory; + _table = table; _shard = shard; _searcher = searcher; _args = args; @@ -234,8 +236,8 @@ public class ShardCommandManager extends BaseCommandManager { } @Override - public TableContext getTableContext() { - return _tableContext; + public TableContext getTableContext() throws IOException { + return _tableContextFactory.getTableContext(_table); } @Override @@ -244,8 +246,18 @@ public class ShardCommandManager extends BaseCommandManager { } @Override - public BlurConfiguration getBlurConfiguration() { - return _tableContext.getBlurConfiguration(); + public BlurConfiguration getBlurConfiguration() throws IOException { + return getTableContext().getBlurConfiguration(); + } + + @Override + public BlurConfiguration getBlurConfiguration(String table) throws IOException { + return getTableContext(table).getBlurConfiguration(); + } + + @Override + public TableContext getTableContext(String table) throws IOException { + return _tableContextFactory.getTableContext(table); } }
