incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject [01/10] git commit: Adding documentation and doc api for the command manager.
Date Tue, 23 Sep 2014 12:04:30 GMT
Repository: incubator-blur
Updated Branches:
  refs/heads/master 2a61bb5d6 -> bd00416db


Adding documentation and doc api for the command manager.


Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/7103446f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/7103446f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/7103446f

Branch: refs/heads/master
Commit: 7103446fbd3a23681b2d017a6a35b6b3bc531243
Parents: 21ff083
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Mon Sep 22 16:57:27 2014 -0400
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Mon Sep 22 16:57:27 2014 -0400

----------------------------------------------------------------------
 .../org/apache/blur/command/DocumentCount.java  |  5 ++
 .../blur/command/DocumentCountCombiner.java     |  5 ++
 .../blur/command/DocumentCountNoCombine.java    |  5 ++
 .../apache/blur/command/BaseCommandManager.java | 59 +++++++++++++++++---
 .../blur/command/ShardCommandManager.java       |  1 -
 .../blur/command/annotation/Arguments.java      | 26 ---------
 .../command/annotation/OptionalArguments.java   | 26 +++++++++
 .../command/annotation/RequiredArguments.java   | 26 +++++++++
 .../blur/command/ShardCommandManagerTest.java   | 24 +++++++-
 .../org/apache/blur/command/WaitForSeconds.java | 10 ++++
 10 files changed, 151 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7103446f/blur-command/src/main/java/org/apache/blur/command/DocumentCount.java
----------------------------------------------------------------------
diff --git a/blur-command/src/main/java/org/apache/blur/command/DocumentCount.java b/blur-command/src/main/java/org/apache/blur/command/DocumentCount.java
index 62b4e04..03c2bad 100644
--- a/blur-command/src/main/java/org/apache/blur/command/DocumentCount.java
+++ b/blur-command/src/main/java/org/apache/blur/command/DocumentCount.java
@@ -21,8 +21,13 @@ import java.io.IOException;
 import org.apache.blur.command.Command;
 import org.apache.blur.command.IndexContext;
 import org.apache.blur.command.IndexReadCommand;
+import org.apache.blur.command.annotation.Argument;
+import org.apache.blur.command.annotation.RequiredArguments;
+import org.apache.blur.command.annotation.OptionalArguments;
 
 @SuppressWarnings("serial")
+@RequiredArguments({ @Argument(name = "table", value = "The name of the table to execute
the document count command.") })
+@OptionalArguments({ @Argument(name = "shard", value = "The shard id to execute the document
count command.") })
 public class DocumentCount extends Command implements IndexReadCommand<Integer> {
 
   private static final String DOC_COUNT = "docCount";

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7103446f/blur-command/src/main/java/org/apache/blur/command/DocumentCountCombiner.java
----------------------------------------------------------------------
diff --git a/blur-command/src/main/java/org/apache/blur/command/DocumentCountCombiner.java
b/blur-command/src/main/java/org/apache/blur/command/DocumentCountCombiner.java
index dd397b7..decea2e 100644
--- a/blur-command/src/main/java/org/apache/blur/command/DocumentCountCombiner.java
+++ b/blur-command/src/main/java/org/apache/blur/command/DocumentCountCombiner.java
@@ -27,8 +27,13 @@ import org.apache.blur.command.IndexContext;
 import org.apache.blur.command.IndexReadCombiningCommand;
 import org.apache.blur.command.Server;
 import org.apache.blur.command.Shard;
+import org.apache.blur.command.annotation.Argument;
+import org.apache.blur.command.annotation.RequiredArguments;
+import org.apache.blur.command.annotation.OptionalArguments;
 
 @SuppressWarnings("serial")
+@RequiredArguments({ @Argument(name = "table", value = "The name of the table to execute
the document count command.") })
+@OptionalArguments({ @Argument(name = "shard", value = "The shard id to execute the document
count command.") })
 public class DocumentCountCombiner extends Command implements ClusterCommand<Long>,
     IndexReadCombiningCommand<Integer, Long> {
 

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7103446f/blur-command/src/main/java/org/apache/blur/command/DocumentCountNoCombine.java
----------------------------------------------------------------------
diff --git a/blur-command/src/main/java/org/apache/blur/command/DocumentCountNoCombine.java
b/blur-command/src/main/java/org/apache/blur/command/DocumentCountNoCombine.java
index d3de1f6..8c3a010 100644
--- a/blur-command/src/main/java/org/apache/blur/command/DocumentCountNoCombine.java
+++ b/blur-command/src/main/java/org/apache/blur/command/DocumentCountNoCombine.java
@@ -26,8 +26,13 @@ import org.apache.blur.command.ClusterContext;
 import org.apache.blur.command.IndexContext;
 import org.apache.blur.command.IndexReadCommand;
 import org.apache.blur.command.Shard;
+import org.apache.blur.command.annotation.Argument;
+import org.apache.blur.command.annotation.RequiredArguments;
+import org.apache.blur.command.annotation.OptionalArguments;
 
 @SuppressWarnings("serial")
+@RequiredArguments({ @Argument(name = "table", value = "The name of the table to execute
the document count command.") })
+@OptionalArguments({ @Argument(name = "shard", value = "The shard id to execute the document
count command.") })
 public class DocumentCountNoCombine extends Command implements IndexReadCommand<Integer>,
ClusterCommand<Long> {
 
   private static final String DOC_COUNT_NO_COMBINE = "docCountNoCombine";

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7103446f/blur-core/src/main/java/org/apache/blur/command/BaseCommandManager.java
----------------------------------------------------------------------
diff --git a/blur-core/src/main/java/org/apache/blur/command/BaseCommandManager.java b/blur-core/src/main/java/org/apache/blur/command/BaseCommandManager.java
index b36c63a..8c13b94 100644
--- a/blur-core/src/main/java/org/apache/blur/command/BaseCommandManager.java
+++ b/blur-core/src/main/java/org/apache/blur/command/BaseCommandManager.java
@@ -11,6 +11,7 @@ import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -29,6 +30,9 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.blur.command.annotation.Argument;
+import org.apache.blur.command.annotation.OptionalArguments;
+import org.apache.blur.command.annotation.RequiredArguments;
 import org.apache.blur.concurrent.Executors;
 import org.apache.blur.log.Log;
 import org.apache.blur.log.LogFactory;
@@ -66,6 +70,7 @@ public class BaseCommandManager implements Closeable {
   private final ExecutorService _executorService;
   private final ExecutorService _executorServiceDriver;
 
+  protected final Map<String, BigInteger> _commandLoadTime = new ConcurrentHashMap<String,
BigInteger>();
   protected final Map<String, Command> _command = new ConcurrentHashMap<String,
Command>();
   protected final Map<Class<? extends Command>, String> _commandNameLookup =
new ConcurrentHashMap<Class<? extends Command>, String>();
   protected final ConcurrentMap<ExecutionId, Future<Response>> _runningMap;
@@ -74,7 +79,7 @@ public class BaseCommandManager implements Closeable {
   protected final String _commandPath;
   protected final Timer _timer;
   protected final long _pollingPeriod = TimeUnit.SECONDS.toMillis(15);
-  protected final Map<Path, BigInteger> _commandLastChange = new ConcurrentHashMap<Path,
BigInteger>();
+  protected final Map<Path, BigInteger> _commandPathLastChange = new ConcurrentHashMap<Path,
BigInteger>();
   protected final Configuration _configuration;
 
   public BaseCommandManager(String tmpPath, String commandPath, int workerThreadCount, int
driverThreadCount,
@@ -98,6 +103,42 @@ public class BaseCommandManager implements Closeable {
     }
   }
 
+  public Map<String, BigInteger> getCommands() {
+    return new HashMap<String, BigInteger>(_commandLoadTime);
+  }
+
+  public Map<String, String> getRequiredArguments(String commandName) {
+    return getArguments(commandName, false);
+  }
+
+  public Map<String, String> getOptionalArguments(String commandName) {
+    return getArguments(commandName, true);
+  }
+
+  private Map<String, String> getArguments(String commandName, boolean optional) {
+    Command command = _command.get(commandName);
+    if (command == null) {
+      return null;
+    }
+    Class<? extends Command> clazz = command.getClass();
+    Map<String, String> arguments = new TreeMap<String, String>();
+    Argument[] args = getArgumentArray(clazz, optional);
+    for (Argument argument : args) {
+      arguments.put(argument.name(), argument.value());
+    }
+    return arguments;
+  }
+
+  private Argument[] getArgumentArray(Class<? extends Command> clazz, boolean optional)
{
+    if (optional) {
+      OptionalArguments requiredArguments = clazz.getAnnotation(OptionalArguments.class);
+      return requiredArguments.value();
+    } else {
+      RequiredArguments requiredArguments = clazz.getAnnotation(RequiredArguments.class);
+      return requiredArguments.value();
+    }
+  }
+
   protected TimerTask getNewCommandTimerTask() {
     return new TimerTask() {
       @Override
@@ -122,10 +163,10 @@ public class BaseCommandManager implements Closeable {
     for (FileStatus fileStatus : listStatus) {
       BigInteger contentsCheck = checkContents(fileStatus, fileSystem);
       Path entryPath = fileStatus.getPath();
-      BigInteger currentValue = _commandLastChange.get(entryPath);
+      BigInteger currentValue = _commandPathLastChange.get(entryPath);
       if (!contentsCheck.equals(currentValue)) {
         loadNewCommand(fileSystem, fileStatus, contentsCheck);
-        _commandLastChange.put(entryPath, contentsCheck);
+        _commandPathLastChange.put(entryPath, contentsCheck);
       }
     }
   }
@@ -143,7 +184,7 @@ public class BaseCommandManager implements Closeable {
     copyLocal(fileSystem, fileStatus.getPath(), file);
     URLClassLoader loader = new URLClassLoader(getUrls(file).toArray(new URL[] {}));
     Enumeration<URL> resources = loader.getResources(META_INF_SERVICES_ORG_APACHE_BLUR_COMMAND_COMMANDS);
-    loadCommandClasses(resources, loader);
+    loadCommandClasses(resources, loader, hashOfContents);
   }
 
   protected List<URL> getUrls(File file) throws MalformedURLException {
@@ -197,11 +238,12 @@ public class BaseCommandManager implements Closeable {
   protected void lookForCommandsToRegisterInClassPath() throws IOException {
     Enumeration<URL> systemResources = ClassLoader
         .getSystemResources(META_INF_SERVICES_ORG_APACHE_BLUR_COMMAND_COMMANDS);
-    loadCommandClasses(systemResources, getClass().getClassLoader());
+    loadCommandClasses(systemResources, getClass().getClassLoader(), BigInteger.ZERO);
   }
 
   @SuppressWarnings("unchecked")
-  protected void loadCommandClasses(Enumeration<URL> enumeration, ClassLoader loader)
throws IOException {
+  protected void loadCommandClasses(Enumeration<URL> enumeration, ClassLoader loader,
BigInteger version)
+      throws IOException {
     Properties properties = new Properties();
     while (enumeration.hasMoreElements()) {
       URL url = enumeration.nextElement();
@@ -214,7 +256,7 @@ public class BaseCommandManager implements Closeable {
       String classNameToRegister = o.toString();
       LOG.info("Loading class [{0}]", classNameToRegister);
       try {
-        register((Class<? extends Command>) loader.loadClass(classNameToRegister));
+        register((Class<? extends Command>) loader.loadClass(classNameToRegister),
version);
       } catch (ClassNotFoundException e) {
         throw new IOException(e);
       }
@@ -277,10 +319,11 @@ public class BaseCommandManager implements Closeable {
     }
   }
 
-  public void register(Class<? extends Command> commandClass) throws IOException {
+  public void register(Class<? extends Command> commandClass, BigInteger version) throws
IOException {
     try {
       Command command = commandClass.newInstance();
       _command.put(command.getName(), command);
+      _commandLoadTime.put(command.getName(), version);
       _commandNameLookup.put(commandClass, command.getName());
       LOG.info("Command [{0}] from class [{1}] registered.", command.getName(), commandClass.getName());
     } catch (InstantiationException e) {

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7103446f/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 4298cdf..0a9b674 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
@@ -230,5 +230,4 @@ public class ShardCommandManager extends BaseCommandManager {
     System.out.println("IMPLEMENT ME!!!!");
   }
 
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7103446f/blur-core/src/main/java/org/apache/blur/command/annotation/Arguments.java
----------------------------------------------------------------------
diff --git a/blur-core/src/main/java/org/apache/blur/command/annotation/Arguments.java b/blur-core/src/main/java/org/apache/blur/command/annotation/Arguments.java
deleted file mode 100644
index 35fa7de..0000000
--- a/blur-core/src/main/java/org/apache/blur/command/annotation/Arguments.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package org.apache.blur.command.annotation;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-
-/**
- * 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.
- */
-
-@Retention(RetentionPolicy.RUNTIME)
-public @interface Arguments {
-  Argument[] value();
-}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7103446f/blur-core/src/main/java/org/apache/blur/command/annotation/OptionalArguments.java
----------------------------------------------------------------------
diff --git a/blur-core/src/main/java/org/apache/blur/command/annotation/OptionalArguments.java
b/blur-core/src/main/java/org/apache/blur/command/annotation/OptionalArguments.java
new file mode 100644
index 0000000..c978bf2
--- /dev/null
+++ b/blur-core/src/main/java/org/apache/blur/command/annotation/OptionalArguments.java
@@ -0,0 +1,26 @@
+package org.apache.blur.command.annotation;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * 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.
+ */
+
+@Retention(RetentionPolicy.RUNTIME)
+public @interface OptionalArguments {
+  Argument[] value();
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7103446f/blur-core/src/main/java/org/apache/blur/command/annotation/RequiredArguments.java
----------------------------------------------------------------------
diff --git a/blur-core/src/main/java/org/apache/blur/command/annotation/RequiredArguments.java
b/blur-core/src/main/java/org/apache/blur/command/annotation/RequiredArguments.java
new file mode 100644
index 0000000..fdba1cd
--- /dev/null
+++ b/blur-core/src/main/java/org/apache/blur/command/annotation/RequiredArguments.java
@@ -0,0 +1,26 @@
+package org.apache.blur.command.annotation;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * 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.
+ */
+
+@Retention(RetentionPolicy.RUNTIME)
+public @interface RequiredArguments {
+  Argument[] value();
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7103446f/blur-core/src/test/java/org/apache/blur/command/ShardCommandManagerTest.java
----------------------------------------------------------------------
diff --git a/blur-core/src/test/java/org/apache/blur/command/ShardCommandManagerTest.java
b/blur-core/src/test/java/org/apache/blur/command/ShardCommandManagerTest.java
index ef28841..93016f4 100644
--- a/blur-core/src/test/java/org/apache/blur/command/ShardCommandManagerTest.java
+++ b/blur-core/src/test/java/org/apache/blur/command/ShardCommandManagerTest.java
@@ -16,12 +16,14 @@
  */
 package org.apache.blur.command;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.math.BigInteger;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -83,6 +85,26 @@ public class ShardCommandManagerTest {
   }
 
   @Test
+  public void testGetCommands() {
+    Map<String, BigInteger> commands = _manager.getCommands();
+    assertEquals(1, commands.size());
+    assertTrue(commands.containsKey("wait"));
+    assertEquals(BigInteger.ZERO, commands.get("wait"));
+  }
+
+  @Test
+  public void testDocumentation() {
+    Map<String, String> requiredArgs = _manager.getRequiredArguments("wait");
+    assertTrue(requiredArgs.containsKey("table"));
+    assertEquals(1, requiredArgs.size());
+
+    Map<String, String> optionalArgs = _manager.getOptionalArguments("wait");
+    assertTrue(optionalArgs.containsKey("seconds"));
+    assertTrue(optionalArgs.containsKey("shard"));
+    assertEquals(2, optionalArgs.size());
+  }
+
+  @Test
   public void testNewCommandLoading() throws IOException, TimeoutException, InterruptedException
{
     _manager.close();
     new File(_tmpPath).mkdirs();

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7103446f/blur-core/src/test/java/org/apache/blur/command/WaitForSeconds.java
----------------------------------------------------------------------
diff --git a/blur-core/src/test/java/org/apache/blur/command/WaitForSeconds.java b/blur-core/src/test/java/org/apache/blur/command/WaitForSeconds.java
index f078436..50180de 100644
--- a/blur-core/src/test/java/org/apache/blur/command/WaitForSeconds.java
+++ b/blur-core/src/test/java/org/apache/blur/command/WaitForSeconds.java
@@ -22,8 +22,18 @@ import java.util.concurrent.TimeUnit;
 import org.apache.blur.command.Command;
 import org.apache.blur.command.IndexContext;
 import org.apache.blur.command.IndexReadCommand;
+import org.apache.blur.command.annotation.Argument;
+import org.apache.blur.command.annotation.RequiredArguments;
+import org.apache.blur.command.annotation.OptionalArguments;
 
 @SuppressWarnings("serial")
+@RequiredArguments({ @Argument(name = "table", value = "The name of the table to execute
the wait for N number of seconds command.") })
+@OptionalArguments({
+
+@Argument(name = "shard", value = "The shard id to execute the wait for N number of seconds
command."),
+    @Argument(name = "seconds", value = "The number of seconds to sleep, the default is 30
seconds.")
+
+})
 public class WaitForSeconds extends Command implements IndexReadCommand<Boolean> {
 
   @Override


Mime
View raw message