[sheepdog] [PATCH] tools/zk_control: add sort options for list subcommand

Meng Lingkun starmlk at 163.com
Wed Mar 4 07:02:11 CET 2015


From: Meng Lingkun <menglingkun at cmss.chinamobile.com>

It's more useable to list the sequential znodes in queue with sort function.

Signed-off-by: Meng Lingkun <menglingkun at cmss.chinamobile.com>
---
 tools/zk_control.c | 43 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/tools/zk_control.c b/tools/zk_control.c
index 75783e1..4bf47ff 100644
--- a/tools/zk_control.c
+++ b/tools/zk_control.c
@@ -11,6 +11,8 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <getopt.h>
+#include <stdlib.h>
 #include <zookeeper/zookeeper.h>
 #include <string.h>
 #include <arpa/inet.h>
@@ -231,6 +233,16 @@ err:
 	return -1;
 }
 
+static int seq_cmp(const void *a, const void *b)
+{
+	return strcmp(*(const char **)b, *(const char **)a);
+}
+
+static int seq_cmp2(const void *a, const void *b)
+{
+	return strcmp(*(const char **)a, *(const char **)b);
+}
+
 static int do_list(int argc, char **argv)
 {
 	struct String_vector strs;
@@ -242,6 +254,14 @@ static int do_list(int argc, char **argv)
 	struct Stat stat;
 	int32_t seq;
 	struct node_id *nid;
+	char opts[] = "sS", ch;
+	int longindex;
+	struct option lopts[] = {
+		{ "sort", 0, NULL, 's' },
+		{ "SORT", 0, NULL, 'S' },
+		{ 0, 0, 0, 0 }
+	};
+	int sort = 0;
 
 	fprintf(stdout, "     QUEUE                ID          TYPE"
 		"                 SENDER  MSG LEN    NR  BUF LEN"
@@ -249,6 +269,26 @@ static int do_list(int argc, char **argv)
 	rc = zk_get_children(QUEUE_ZNODE, &strs);
 	switch (rc) {
 	case ZOK:
+		while ((ch = getopt_long(argc, argv, opts, lopts,
+				&longindex)) >= 0) {
+			switch (ch) {
+			case 's':
+				sort = 1;
+				break;
+			case 'S':
+				sort = 2;
+				break;
+			default:
+				break;
+			}
+		}
+
+		if (1 == sort) {
+			qsort(strs.data, strs.count, sizeof(char *), seq_cmp);
+		} else if (2 == sort) {
+			qsort(strs.data, strs.count, sizeof(char *), seq_cmp2);
+		}
+
 		FOR_EACH_ZNODE(QUEUE_ZNODE, path, &strs) {
 			len = sizeof(struct zk_event);
 			rc = zk_get_data(path, &ev, &len, &stat);
@@ -366,7 +406,8 @@ static struct control_handler {
 } handlers[] = {
 	{ "kill", do_kill, "Kill the session" },
 	{ "remove", do_remove, "Remove the node recursively" },
-	{ "list", do_list, "List the data in queue node" },
+	{ "list", do_list, "List the data in queue node."
+				"options: [sS] (-s=--sort, -S=--SORT) " },
 	{ "purge", do_purge, "Remove the data in queue node" },
 	{ NULL, NULL, NULL },
 };
-- 
1.9.1





More information about the sheepdog mailing list