From 04a4adeb8a142124ab4d302ca1263b04909cbb97 Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Wed, 15 Apr 2015 21:37:16 +0300 Subject: [PATCH] Added ordering of the output of SHOW DATABASES for schemarouter. --- .../routing/schemarouter/schemarouter.c | 73 +++++++++++++++---- 1 file changed, 57 insertions(+), 16 deletions(-) diff --git a/server/modules/routing/schemarouter/schemarouter.c b/server/modules/routing/schemarouter/schemarouter.c index 44a12f0a0..b874c2e68 100644 --- a/server/modules/routing/schemarouter/schemarouter.c +++ b/server/modules/routing/schemarouter/schemarouter.c @@ -1543,6 +1543,12 @@ void check_create_tmp_table( free(tblname); } } + +int cmpfn(const void* a, const void *b) +{ + return strcmp(*(char**)a,*(char**)b); +} + /** * Generates a custom SHOW DATABASES result set from all the databases in the * hashtable. Only backend servers that are up and in a proper state are listed @@ -1653,37 +1659,71 @@ gen_show_dbs_response(ROUTER_INSTANCE* router, ROUTER_CLIENT_SES* client) memcpy(ptr, eof, sizeof(eof)); unsigned int packet_num = 4; + int j = 0,ndbs = 0, bufsz = 10; + char** dbs; + + if((dbs = malloc(sizeof(char*)*bufsz)) == NULL) + { + gwbuf_free(rval); + hashtable_iterator_free(iter); + return NULL; + } while((value = (char*) hashtable_next(iter))) { char* bend = hashtable_fetch(ht, value); + for(i = 0; backends[i]; i++) { if(strcmp(bref[i].bref_backend->backend_server->unique_name, bend) == 0 && BREF_IS_IN_USE(&bref[i]) && !BREF_IS_CLOSED(&bref[i])) { + ndbs++; - GWBUF* temp; - int plen = strlen(value) + 1; + if(ndbs >= bufsz) + { + bufsz += bufsz / 2; + char** tmp = realloc(dbs,sizeof(char*)*bufsz); + if(tmp == NULL) + { + gwbuf_free(rval); + hashtable_iterator_free(iter); + for(i=0;istart; - *ptr++ = plen; - *ptr++ = plen >> 8; - *ptr++ = plen >> 16; - *ptr++ = packet_num++; - *ptr++ = plen - 1; - memcpy(ptr, dbname, plen - 1); - - /** Append the row*/ - rval = gwbuf_append(rval, temp); - + dbs[j++] = strdup(value); } } } + qsort(&dbs[0],(size_t)ndbs,sizeof(char*),cmpfn); + + for(j = 0;jstart; + *ptr++ = plen; + *ptr++ = plen >> 8; + *ptr++ = plen >> 16; + *ptr++ = packet_num++; + *ptr++ = plen - 1; + memcpy(ptr, dbname, plen - 1); + + /** Append the row*/ + rval = gwbuf_append(rval, temp); + free(dbs[j]); + } + eof[3] = packet_num; GWBUF* last_packet = gwbuf_alloc(sizeof(eof)); @@ -1692,6 +1732,7 @@ gen_show_dbs_response(ROUTER_INSTANCE* router, ROUTER_CLIENT_SES* client) rval = gwbuf_make_contiguous(rval); hashtable_iterator_free(iter); + free(dbs); return rval; }