Index: trunk/ippTools/src/magictool.c
===================================================================
--- trunk/ippTools/src/magictool.c	(revision 23969)
+++ trunk/ippTools/src/magictool.c	(revision 23970)
@@ -457,5 +457,5 @@
     psMetadata *where = psMetadataAlloc();
     PXOPT_COPY_S64(config->args, where, "-magic_id", "magicRun.magic_id", "==");
-    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
+    pxAddLabelSearchArgs (config, where, "-label", "magicRun.label", "==");
 
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
@@ -739,7 +739,8 @@
     psMetadata *where = psMetadataAlloc();
     PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "==");
-    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
+    pxAddLabelSearchArgs (config, where, "-label", "magicRun.label", "==");
 
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
 
     // look for "inputs" that need to processed
@@ -750,13 +751,22 @@
     }
 
-    psString whereClause = NULL;
+    psString whereString = NULL;
     if (psListLength(where->list)) {
-        whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-        psStringAppend(&query, " AND %s", whereClause);
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+        psStringAppend(&whereString, "\nAND %s", whereClause);
+        psFree(whereClause);
     }
     psFree(where);
 
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
+    // treat limit == 0 as "no limit"
+    if (limit) {
+        psString limitString = psDBGenerateLimitSQL(limit);
+        psStringAppend(&query, " %s", limitString);
+        psFree(limitString);
+    }
+
+    if (!p_psDBRunQueryF(config->dbh, query, whereString, whereString)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(whereString);
         psFree(query);
         return false;
@@ -782,5 +792,21 @@
     }
 
+    if (limit) {
+        if (psArrayLength(output) >= limit) {
+            // we've found enough (not that limit was applied to the query so '> limit' won't happen)
+            // negative simple so the default is true
+            if (!ippdbPrintMetadatas(stdout, output, "magicMe", !simple)) {
+                psError(PS_ERR_UNKNOWN, false, "failed to print array");
+                psFree(output);
+                return false;
+            }
+            psFree(output);
+            return true;
+        }
+    }
+
     // look for tree nodes that need to be processed
+    // XXX: This gets all nodes from all magicRuns that are in 'new'state
+    // That doens't seem particularly efficient
     query = pxDataGet("magictool_toprocess_tree.sql");
     if (!query) {
@@ -789,17 +815,12 @@
     }
 
-    if (whereClause) {
-        psString new  = NULL;
-        psStringAppend(&new, "\n AND %s", whereClause);
-        psFree(whereClause);
-        whereClause = new;
-    }
-    if (!p_psDBRunQueryF(config->dbh, query, whereClause ? whereClause :  "")) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(whereClause);
+
+    if (!p_psDBRunQueryF(config->dbh, query, whereString ? whereString :  "")) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(whereString);
         psFree(query);
         return false;
     }
-    psFree(whereClause);
+    psFree(whereString);
     psFree(query);
 
@@ -828,4 +849,7 @@
     while (index <  psArrayLength(magicTree)) {
         bool status;
+        if (limit && (psArrayLength(output) >= limit)) {
+            break;
+        }
         psS64 current_magic_id = psMetadataLookupS64(&status, magicTree->data[index], "magic_id");
         if (!status) {
@@ -879,6 +903,14 @@
 
     }
+    psFree(magicTree);
 
     if (psArrayLength(output)) {
+        if (limit && (limit < psArrayLength(output))) {
+            // truncate the array
+            long arrayLength = psArrayLength(output);
+            for (long i = arrayLength - 1; i >= limit; i--) {
+                psArrayRemoveIndex(output, i);
+            }
+        }
         // negative simple so the default is true
         if (!ippdbPrintMetadatas(stdout, output, "magicMe", !simple)) {
@@ -889,5 +921,4 @@
     }
 
-    psFree(magicTree);
     psFree(output);
 
