Permalink
Browse files

Add --batchSize parameter to Application sync()

  • Loading branch information...
1 parent cb6792a commit 54910390b1d2a980630de3908660dff33b27f9f1 Ryan Morgan committed Sep 28, 2009
Showing with 27 additions and 6 deletions.
  1. +27 −6 src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java
@@ -20,6 +20,7 @@
private static String[] COMMANDS = { CMD_LIST, CMD_SYNC, CMD_DELETE };
private static String OPT_ID = "id";
+ private static String OPT_BATCH_SIZE = "batchSize";
private void printUsage() {
System.err.println("One of " + Arrays.toString(COMMANDS) + " required");
@@ -63,20 +64,40 @@ private void sync(String[] args) throws Exception {
OptionParser p = getOptionParser();
+ p.accepts(OPT_BATCH_SIZE, "Process the sync in batches of the given size").
+ withRequiredArg().ofType(Integer.class);
+
OptionSet options = getOptions(p, args);
- ApplicationApi applicationApi = getApi(options).getApplicationApi();
+ ApplicationApi api = getApi(options).getApplicationApi();
InputStream is = getInputStream(options);
-
ApplicationsResponse resp = XmlUtil.deserialize(ApplicationsResponse.class, is);
-
List<Application> applications = resp.getApplication();
- ApplicationsResponse syncResponse = applicationApi.syncApplications(applications);
- checkSuccess(syncResponse);
+ int numSynced = 0;
+ if (options.has(OPT_BATCH_SIZE)) {
+ int batchSize = (Integer)options.valueOf(OPT_BATCH_SIZE);
+ int numBatches = (int)Math.ceil(applications.size()/((double)batchSize));
+
+ for (int i = 0; i < numBatches; i++) {
+ System.out.println("Syncing batch " + (i + 1) + " of " + numBatches);
+ int fromIndex = i * batchSize;
+ int toIndex = (fromIndex + batchSize) > applications.size() ?
+ applications.size() : (fromIndex + batchSize);
+ ApplicationsResponse syncResponse =
+ api.syncApplications(applications.subList(fromIndex,
+ toIndex));
+ checkSuccess(syncResponse);
+ numSynced += (toIndex - fromIndex);
+ }
+ } else {
+ ApplicationsResponse syncResponse = api.syncApplications(applications);
+ checkSuccess(syncResponse);
+ numSynced = applications.size();
+ }
- System.out.println("Successfully synced " + applications.size() + " applications.");
+ System.out.println("Successfully synced " + numSynced + " applications.");
}
private void delete(String[] args) throws Exception {

0 comments on commit 5491039

Please sign in to comment.