Update of /cvsroot/monetdb/sql/src/backends/monet5/merovingian
In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv31201

Modified Files:
        merovingian_controlrunner.c monetdb_set.c properties.c 
        properties.h 
Log Message:
- trade efficiency for code maintainability, correctness and repetition
- collect semantics on properties + their values in setProp



U merovingian_controlrunner.c
Index: merovingian_controlrunner.c
===================================================================
RCS file: 
/cvsroot/monetdb/sql/src/backends/monet5/merovingian/merovingian_controlrunner.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- merovingian_controlrunner.c 9 Sep 2009 13:28:55 -0000       1.10
+++ merovingian_controlrunner.c 9 Sep 2009 19:11:24 -0000       1.11
@@ -18,6 +18,50 @@
  */
 
 static void
+leavedb(char *name)
+{
+       char buf[128];
+       snprintf(buf, sizeof(buf),
+                       "LEAV %s mapi:monetdb://%s:%hu/",
+                       name, _mero_hostname, _mero_port);
+       broadcast(buf);
+}
+
+static void
+leavedbS(sabdb *stats)
+{
+       confkeyval *kv, *props = getDefaultProps();
+       readProps(props, stats->path);
+       kv = findConfKey(props, "shared");
+       if (stats->locked != 1 && (kv->val == NULL || strcmp(kv->val, "no") != 
0))
+               leavedb(stats->dbname);
+       freeConfFile(props);
+       GDKfree(props);
+}
+
+static void
+anncdbS(sabdb *stats)
+{
+       char buf[128];
+       confkeyval *kv, *props = getDefaultProps();
+       readProps(props, stats->path);
+       kv = findConfKey(props, "shared");
+       if (stats->locked != 1 && (kv->val == NULL || strcmp(kv->val, "no") != 
0)) {
+               snprintf(buf, sizeof(buf),
+                               "ANNC %s%s%s mapi:monetdb://%s:%hu/ %d",
+                               stats->dbname,
+                               kv->val == NULL ? "" : "/",
+                               kv->val == NULL ? "" : kv->val,
+                               _mero_hostname,
+                               _mero_port,
+                               _mero_discoveryttl + 60);
+               broadcast(buf);
+       }
+       freeConfFile(props);
+       GDKfree(props);
+}
+
+static void
 controlRunner(void *d)
 {
        int sock = *(int *)d;
@@ -191,21 +235,17 @@
                                                 * generates an "leave request 
for unknown
                                                 * database" if not shared 
(e.g. when under
                                                 * maintenance) */
-                                               snprintf(buf2, sizeof(buf2),
-                                                               "LEAV %s 
mapi:monetdb://%s:%hu/",
-                                                               q, 
_mero_hostname, _mero_port);
-                                               broadcast(buf2);
+                                               leavedb(q);
                                                Mfprintf(_mero_ctlout, 
"destroyed database '%s'\n", q);
                                                len = snprintf(buf2, 
sizeof(buf2), "OK\n");
                                                send(msgsock, buf2, len, 0);
                                        }
                                } else if (strncmp(p, "share=", 
strlen("share=")) == 0) {
                                        sabdb *stats;
-                                       sabdb *topdb;
                                        err e;
-                                       confkeyval *kv, *props = 
getDefaultProps();
-                                       char *value;
+                                       confkeyval *kv;
 
+                                       /* bail out if we don't do discovery at 
all */
                                        kv = findConfKey(_mero_props, "shared");
                                        if (strcmp(kv->val, "no") == 0) {
                                                /* can't do much */
@@ -219,7 +259,7 @@
                                                continue;
                                        }
 
-                                       if ((e = SABAOTHgetStatus(&stats, 
NULL)) != MAL_SUCCEED) {
+                                       if ((e = SABAOTHgetStatus(&stats, q)) 
!= MAL_SUCCEED) {
                                                len = snprintf(buf2, 
sizeof(buf2),
                                                                "internal 
error, please review the logs\n");
                                                send(msgsock, buf2, len, 0);
@@ -228,109 +268,39 @@
                                                freeErr(e);
                                                continue;
                                        }
+                                       if (stats == NULL) {
+                                               Mfprintf(_mero_ctlerr, 
"received share signal for "
+                                                               "unknown 
database: %s\n", q);
+                                               len = snprintf(buf2, 
sizeof(buf2),
+                                                               "unknown 
database: %s\n", q);
+                                               send(msgsock, buf2, len, 0);
+                                               continue;
+                                       }
 
-                                       /* check if tag matches [A-Za-z0-9./]+ 
*/
                                        p += strlen("share=");
-                                       value = p;
-                                       while (*value != '\0') {
-                                               if (!(
-                                                                       (*value 
>= 'A' && *value <= 'Z') ||
-                                                                       (*value 
>= 'a' && *value <= 'z') ||
-                                                                       (*value 
>= '0' && *value <= '9') ||
-                                                                       (*value 
== '.' || *value == '/')
-                                                        ))
-                                               {
-                                                       len = snprintf(buf2, 
sizeof(buf2),
-                                                                       
"invalid character '%c' at %d "
-                                                                       "in tag 
name '%s'\n",
-                                                                       *value, 
(int)(value - p), p);
-                                                       send(msgsock, buf2, 
len, 0);
-                                                       buf2[len] = '\0';
-                                                       Mfprintf(_mero_ctlerr, 
"set: %s", buf2);
-                                                       value = NULL;
-                                                       break;
-                                               }
-                                               value++;
+                                       if (*p == '\0') {
+                                               /* empty, inherit (e.g. remove 
local opt) */
+                                               p = NULL;
                                        }
-                                       if (value == NULL)
-                                               continue;
-
-                                       topdb = stats;
-                                       while (stats != NULL) {
-                                               if (strcmp(q, stats->dbname) == 
0) {
-                                                       readProps(props, 
stats->path);
-                                                       kv = findConfKey(props, 
"shared");
-                                                       if (stats->locked != 1 
&& 
-                                                                       
(kv->val == NULL ||
-                                                                        
strcmp(kv->val, "no") != 0))
-                                                       {
-                                                               /* we can leave 
without tag, will remove all */
-                                                               snprintf(buf2, 
sizeof(buf2),
-                                                                               
"LEAV %s mapi:monetdb://%s:%hu/",
-                                                                               
stats->dbname, _mero_hostname,
-                                                                               
_mero_port);
-                                                               broadcast(buf2);
-                                                       }
-                                                       if (kv->val != NULL) {
-                                                               
GDKfree(kv->val);
-                                                               kv->val = NULL;
-                                                       }
-                                                       /* the prophecy:
-                                                        * <empty> inherit (bit 
useless)
-                                                        * yes     share 
without tag
-                                                        * no      don't share
-                                                        * *       share with * 
as tag
-                                                        */
-                                                       if (*p == '\0') {
-                                                               /* empty, 
inherit (e.g. remove local opt),
-                                                                * but keep 
enabled, otherwise we never
-                                                                * came here */
-                                                       } else if (strcmp(p, 
"yes") == 0) {
-                                                               /* make this 
share an empty tag */
-                                                               *p = '\0';
-                                                               kv->val = 
GDKstrdup("yes");
-                                                       } else if (strcmp(p, 
"no") == 0) {
-                                                               /* do not share 
(any more) */
-                                                               kv->val = 
GDKstrdup("no");
-                                                               
writeProps(props, stats->path);
-                                                               break;
-                                                       } else {
-                                                               /* tag, include 
'/' for easy life
-                                                                * afterwards */
-                                                               *--p = '/';
-                                                               kv->val = 
GDKstrdup(p + 1);
-                                                       }
 
-                                                       snprintf(buf2, 
sizeof(buf2),
-                                                                       "ANNC 
%s%s mapi:monetdb://%s:%hu/ %d",
-                                                                       
stats->dbname, p, _mero_hostname,
-                                                                       
_mero_port, _mero_discoveryttl + 60);
-                                                       if (stats->locked != 1)
-                                                               broadcast(buf2);
-                                                       writeProps(props, 
stats->path);
-                                                       Mfprintf(_mero_ctlout, 
"shared database '%s' "
-                                                                       "as 
'%s%s'\n", stats->dbname,
-                                                                       
stats->dbname, p);
-                                                       break;
-                                               }
-                                               stats = stats->next;
-                                       }
-                                       if (stats == NULL) {
-                                               Mfprintf(_mero_ctlerr, 
"received share signal for "
-                                                               "database not 
under merovingian control: %s\n",
-                                                               q);
-                                               len = snprintf(buf2, 
sizeof(buf2),
-                                                               "'%s' is not 
controlled by merovingian\n", q);
+                                       leavedbS(stats);
+                                       if ((e = setProp(stats->path, "shared", 
p)) != NULL) {
+                                               /* reannounce again, there was 
an error */
+                                               anncdbS(stats);
+                                               Mfprintf(_mero_ctlerr, "failed 
to share: %s\n", e);
+                                               len = snprintf(buf2, 
sizeof(buf2), "%s\n", e);
+                                               send(msgsock, buf2, len, 0);
+                                       } else {
+                                               anncdbS(stats);
+                                               Mfprintf(_mero_ctlout, "shared 
database '%s' "
+                                                               "as '%s%s'\n", 
stats->dbname,
+                                                               stats->dbname, 
p);
+                                               len = snprintf(buf2, 
sizeof(buf2), "OK\n");
                                                send(msgsock, buf2, len, 0);
                                        }
-                                       len = snprintf(buf2, sizeof(buf2), 
"OK\n");
-                                       send(msgsock, buf2, len, 0);
-                                       SABAOTHfreeStatus(&topdb);
-                                       freeConfFile(props);
-                                       GDKfree(props);
+                                       SABAOTHfreeStatus(&stats);
                                } else if (strncmp(p, "name=", strlen("name=")) 
== 0) {
                                        char *e;
-                                       confkeyval *kv, *props = 
getDefaultProps();
 
                                        p += strlen("name=");
                                        e = db_rename(q, p);
@@ -347,30 +317,8 @@
                                                        /* should not fail, 
since the rename was
                                                         * already successful */
                                                } else {
-                                                       kv = 
findConfKey(_mero_props, "shared");
-                                                       if (strcmp(kv->val, 
"no") != 0) {
-                                                               
readProps(props, stats->path);
-                                                               kv = 
findConfKey(props, "shared");
-                                                               if 
(stats->locked != 1 && 
-                                                                               
(kv->val == NULL ||
-                                                                               
 strcmp(kv->val, "no") != 0))
-                                                               {
-                                                                       
snprintf(buf2, sizeof(buf2),
-                                                                               
        "LEAV %s mapi:monetdb://%s:%hu/",
-                                                                               
        q, _mero_hostname, _mero_port);
-                                                                       
broadcast(buf2);
-                                                                       
snprintf(buf2, sizeof(buf2),
-                                                                               
        "ANNC %s%s%s mapi:monetdb://%s:%hu/ %d",
-                                                                               
        stats->dbname,
-                                                                               
        kv->val == NULL ? "" : "/",
-                                                                               
        kv->val == NULL ? "" : kv->val,
-                                                                               
        _mero_hostname,
-                                                                               
        _mero_port,
-                                                                               
        _mero_discoveryttl + 60);
-                                                                       
broadcast(buf2);
-                                                               }
-                                                               
freeConfFile(props);
-                                                       }
+                                                       leavedb(q); /* could be 
spam, but shouldn't harm */
+                                                       anncdbS(stats);
                                                        
SABAOTHfreeStatus(&stats);
                                                }
                                                Mfprintf(_mero_ctlout, "renamed 
database '%s' "

U monetdb_set.c
Index: monetdb_set.c
===================================================================
RCS file: /cvsroot/monetdb/sql/src/backends/monet5/merovingian/monetdb_set.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- monetdb_set.c       9 Sep 2009 13:28:55 -0000       1.8
+++ monetdb_set.c       9 Sep 2009 19:11:24 -0000       1.9
@@ -25,13 +25,13 @@
 static void
 command_set(int argc, char *argv[], meroset type)
 {
-       char *p;
-       char *property = NULL, *value = NULL;
+       char *p, *value = NULL;
+       char property[24] = "";
        err e;
        int i;
        int state = 0;
-       sabdb *orig, *stats, *w;
-       confkeyval *kv, *props = getDefaultProps();
+       sabdb *stats;
+       confkeyval *props = getDefaultProps();
 
        if (argc >= 1 && argc <= 2) {
                /* print help message for this command */
@@ -62,32 +62,72 @@
                        /* make this option no longer available, for easy use
                         * lateron */
                        argv[i] = NULL;
-               } else if (property == NULL) {
+               } else if (property[0] == '\0') {
                        /* first non-option is property, rest is database */
-                       property = argv[i];
-                       argv[i] = NULL;
+                       p = argv[i];
                        if (type == SET) {
-                               if ((p = strchr(property, '=')) == NULL) {
+                               if ((p = strchr(argv[i], '=')) == NULL) {
                                        fprintf(stderr, "set: need 
property=value\n");
                                        command_help(2, &argv[-1]);
                                        exit(1);
                                }
-                               *p++ = '\0';
+                               *p = '\0';
+                               snprintf(property, sizeof(property), "%s", 
argv[i]);
+                               *p++ = '=';
                                value = p;
+                               p = argv[i];
                        }
+                       argv[i] = NULL;
                }
        }
 
-       if (property == NULL) {
+       if (property[0] == '\0') {
                fprintf(stderr, "%s: need a property argument\n", argv[0]);
                command_help(2, &argv[0]);
                exit(1);
        }
 
-       w = NULL;
-       orig = NULL;
+       /* handle rename separately due to single argument constraint */
+       if (strcmp(property, "name") == 0) {
+               if (type == INHERIT) {
+                       fprintf(stderr, "inherit: cannot default to a database 
name\n");
+                       exit(1);
+               }
+
+               if (argc > 3) {
+                       fprintf(stderr, "%s: cannot rename multiple databases 
to "
+                                       "the same name\n", argv[0]);
+                       exit(1);
+               }
+
+               if (mero_running == 0) {
+                       if ((e = db_rename(stats->dbname, value)) != NULL) {
+                               fprintf(stderr, "set: %s\n", e);
+                               free(e);
+                               exit(1);
+                       }
+               } else {
+                       char *res;
+                       char *out;
+
+                       out = control_send(&res, mero_control, -1, argv[2], p);
+                       if (out != NULL || strcmp(res, "OK") != 0) {
+                               res = out == NULL ? res : out;
+                               fprintf(stderr, "%s: %s\n", argv[0], res);
+                               state |= 1;
+                       }
+                       free(res);
+               }
+
+               GDKfree(props);
+               exit(state);
+       }
+
        for (i = 1; i < argc; i++) {
-               if (argv[i] != NULL) {
+               if (argv[i] == NULL)
+                       continue;
+
+               if (mero_running == 0) {
                        if ((e = SABAOTHgetStatus(&stats, argv[i])) != 
MAL_SUCCEED) {
                                fprintf(stderr, "%s: internal error: %s\n", 
argv[0], e);
                                GDKfree(e);
@@ -97,108 +137,32 @@
                        if (stats == NULL) {
                                fprintf(stderr, "%s: no such database: %s\n",
                                                argv[0], argv[i]);
-                               argv[i] = NULL;
-                       } else {
-                               if (stats->state == SABdbRunning) {
-                                       fprintf(stderr, "%s: database '%s' is 
still running, "
-                                                       "stop database 
first\n", argv[0], stats->dbname);
-                                       SABAOTHfreeStatus(&stats);
-                                       state |= 1;
-                                       continue;
-                               }
-                               if (orig == NULL) {
-                                       orig = stats;
-                                       w = stats;
-                               } else {
-                                       w = w->next = stats;
-                               }
-                       }
-               }
-       }
-
-       for (stats = orig; stats != NULL; stats = stats->next) {
-               if (strcmp(property, "name") == 0) {
-                       char name[4069];
-                       char *res;
-                       char *out;
-
-                       /* special virtual case */
-                       if (type == INHERIT) {
-                               fprintf(stderr, "inherit: cannot default to a 
database name\n");
                                state |= 1;
                                continue;
                        }
 
-                       if (value[0] == '\0')
+                       if ((e = setProp(stats->path, property, value)) != 
NULL) {
+                               fprintf(stderr, "%s: %s\n", argv[0], e);
+                               free(e);
+                               state |= 1;
                                continue;
-
-                       if (mero_running == 0) {
-                               if ((e = db_rename(stats->dbname, value)) != 
NULL) {
-                                       fprintf(stderr, "set: %s\n", e);
-                                       free(e);
-                                       state |= 1;
-                                       continue;
-                               }
-                       } else {
-                               snprintf(name, sizeof(name), "name=%s", value);
-                               out = control_send(&res, mero_control, -1, 
stats->dbname, name);
-                               if (out != NULL || strcmp(res, "OK") != 0) {
-                                       res = out == NULL ? res : out;
-                                       fprintf(stderr, "%s: %s\n", argv[0], 
res);
-                                       state |= 1;
-                               }
-                               free(res);
                        }
-               } else if (strcmp(property, "shared") == 0) {
-                       char share[4069];
+
+                       SABAOTHfreeStatus(&stats);
+               } else {
                        char *res;
                        char *out;
 
-                       if (type == INHERIT)
-                               value = "";
-
-                       snprintf(share, sizeof(share), "share=%s", value);
-                       out = control_send(&res, mero_control, 0, 
stats->dbname, share);
+                       out = control_send(&res, mero_control, 0, argv[i], p);
                        if (out != NULL || strcmp(res, "OK") != 0) {
                                res = out == NULL ? res : out;
                                fprintf(stderr, "%s: %s\n", argv[0], res);
                                state |= 1;
                        }
                        free(res);
-               } else {
-                       char *err;
-                       readProps(props, stats->path);
-                       kv = findConfKey(props, property);
-                       if (kv == NULL) {
-                               fprintf(stderr, "%s: no such property: %s\n", 
argv[0], property);
-                               state |= 1;
-                               continue;
-                       }
-                       if ((err = setConfVal(kv, type == SET ? value : NULL)) 
!= NULL) {
-                               fprintf(stderr, "%s: %s\n", argv[0], err);
-                               GDKfree(err);
-                               state |= 1;
-                               continue;
-                       }
-
-                       if (strcmp(kv->key, "forward") == 0 && kv->val != NULL) 
{
-                               if (strcmp(kv->val, "proxy") != 0 &&
-                                               strcmp(kv->val, "redirect") != 
0)
-                               {
-                                       fprintf(stderr, "%s: expected 'proxy' 
or 'redirect' for "
-                                                       "property 'forward', 
got: %s\n", argv[0], kv->val);
-                                       state |= 1;
-                                       continue;
-                               }
-                       }
-
-                       writeProps(props, stats->path);
-                       freeConfFile(props);
                }
        }
 
-       if (orig != NULL)
-               SABAOTHfreeStatus(&orig);
        GDKfree(props);
        exit(state);
 }

U properties.h
Index: properties.h
===================================================================
RCS file: /cvsroot/monetdb/sql/src/backends/monet5/merovingian/properties.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- properties.h        3 Jun 2009 11:04:56 -0000       1.3
+++ properties.h        9 Sep 2009 19:11:24 -0000       1.4
@@ -27,6 +27,7 @@
 confkeyval *getDefaultProps(void);
 void writeProps(confkeyval *ckv, char *path);
 void readProps(confkeyval *ckv, char *path);
+char *setProp(char *path, char *key, char *val);
 
 #endif
 

U properties.c
Index: properties.c
===================================================================
RCS file: /cvsroot/monetdb/sql/src/backends/monet5/merovingian/properties.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- properties.c        4 Sep 2009 12:37:16 -0000       1.9
+++ properties.c        9 Sep 2009 19:11:24 -0000       1.10
@@ -97,4 +97,66 @@
        }
 }
 
+/**
+ * Sets a single property, performing all necessary checks to validate
+ * the key and associated value.
+ */
+char *
+setProp(char *path, char *key, char *val)
+{
+       char *err;
+       char buf[8096];
+       confkeyval *props = getDefaultProps();
+       confkeyval *kv;
+
+       readProps(props, path);
+       kv = findConfKey(props, key);
+       if (kv == NULL) {
+               snprintf(buf, sizeof(buf), "no such property: %s", key);
+               return(strdup(buf));
+       }
+
+       /* first just attempt to set the value (type-check) in memory */
+       if ((err = setConfVal(kv, val)) != NULL)
+               return(err);
+
+       if (val != NULL) {
+               /* handle the semantially enriched types */
+               if (strcmp(key, "forward") == 0) {
+                       if (strcmp(val, "proxy") != 0 && strcmp(val, 
"redirect") != 0) {
+                               snprintf(buf, sizeof(buf), "expected 'proxy' or 
'redirect' "
+                                               "for property 'forward', got: 
%s", val);
+                               return(strdup(buf));
+                       }
+               } else if (strcmp(key, "shared") == 0) {
+                       char *value = val;
+                       /* check if tag matches [A-Za-z0-9./]+ */
+                       if (*value == '\0')
+                               return(strdup("tag to share cannot be empty"));
+                       while (*value != '\0') {
+                               if (!(
+                                                       (*value >= 'A' && 
*value <= 'Z') ||
+                                                       (*value >= 'a' && 
*value <= 'z') ||
+                                                       (*value >= '0' && 
*value <= '9') ||
+                                                       (*value == '.' || 
*value == '/')
+                                        ))
+                               {
+                                       snprintf(buf, sizeof(buf),
+                                                       "invalid character '%c' 
at %d "
+                                                       "in tag name '%s'\n",
+                                                       *value, (int)(value - 
val), val);
+                                       return(strdup(buf));
+                               }
+                               value++;
+                       }
+               }
+       }
+
+       /* ok, if we've reached this point we can write this stuff out! */
+       writeProps(props, path);
+       freeConfFile(props);
+
+       return(NULL);
+}
+
 /* vim:set ts=4 sw=4 noexpandtab: */


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Monetdb-sql-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/monetdb-sql-checkins

Reply via email to