diff --git a/db/hash_partition.c b/db/hash_partition.c index 36dce6f323..4c54c82b79 100644 --- a/db/hash_partition.c +++ b/db/hash_partition.c @@ -82,8 +82,8 @@ static void free_hash_view(hash_view_t *mView) } hash_view_t *create_hash_view(const char *viewname, const char *tablename, uint32_t num_columns, - char columns[][MAXCOLNAME], uint32_t num_partitions, - char partitions[][MAXPARTITIONLEN], struct errstat *err) + char **columns, uint32_t num_partitions, + char **partitions, struct errstat *err) { hash_view_t *mView; @@ -94,6 +94,7 @@ hash_view_t *create_hash_view(const char *viewname, const char *tablename, uint3 goto oom; } + logmsg(LOGMSG_USER, "CREATING A VIEW WITH NAME %s\n", viewname); mView->viewname = strdup(viewname); if (!mView->viewname) { logmsg(LOGMSG_ERROR, "%s: Failed to allocate view name string %s\n", __func__, viewname); @@ -113,6 +114,7 @@ hash_view_t *create_hash_view(const char *viewname, const char *tablename, uint3 } for (int i = 0; i < mView->num_keys; i++) { + logmsg(LOGMSG_USER, "ADDING COLUMN %s\n", columns[i]); mView->keynames[i] = strdup(columns[i]); if (!mView->keynames[i]) { logmsg(LOGMSG_ERROR, "%s: Failed to allocate key name string %s\n", __func__, columns[i]); @@ -123,6 +125,7 @@ hash_view_t *create_hash_view(const char *viewname, const char *tablename, uint3 mView->partitions = (char **)calloc(1, sizeof(char *) * num_partitions); for (int i = 0; i < num_partitions; i++) { + logmsg(LOGMSG_USER, "ADDING PARTITION %s\n", partitions[i]); mView->partitions[i] = strdup(partitions[i]); if (!mView->partitions[i]) { goto oom; @@ -323,7 +326,7 @@ int hash_partition_llmeta_write(void *tran, hash_view_t *view, struct errstat *e goto done; } - logmsg(LOGMSG_USER, "%s\n", view_str); + logmsg(LOGMSG_USER, "WRITING THE VIEWS STRING : %s TO LLMETA\n", view_str); /* save the view */ rc = hash_views_write_view(tran, view->viewname, view_str, 0); if (rc != VIEW_NOERR) { diff --git a/db/hash_partition.h b/db/hash_partition.h index 1859073d49..e45b2eee54 100644 --- a/db/hash_partition.h +++ b/db/hash_partition.h @@ -15,8 +15,8 @@ typedef struct systable_hashpartitions { } systable_hashpartition_t; hash_view_t *create_hash_view(const char *viewname, const char *tablename, uint32_t num_columns, - char columns[][MAXCOLNAME], uint32_t num_partitions, - char partitions[][MAXPARTITIONLEN], struct errstat *err); + char **columns, uint32_t num_partitions, + char **partitions, struct errstat *err); int hash_create_inmem_view(hash_view_t *); int hash_destroy_inmem_view(hash_view_t *); const char *hash_view_get_viewname(struct hash_view *view); diff --git a/db/osqlcomm.c b/db/osqlcomm.c index eedb3cb2d1..3b92f875f2 100644 --- a/db/osqlcomm.c +++ b/db/osqlcomm.c @@ -838,11 +838,19 @@ static uint8_t *osqlcomm_schemachange_rpl_type_put(osql_rpl_t *hd, struct schema static uint8_t *osqlcomm_packed_schemachange_uuid_rpl_type_put(osql_uuid_rpl_t *hd, void *packed_sc, size_t sc_len, uint8_t *p_buf, uint8_t *p_buf_end) { + + logmsg(LOGMSG_USER, "sc_len: %ld, p_buf_end - p_buf : %ld\n", sc_len, p_buf_end - p_buf); if (p_buf_end < p_buf || OSQLCOMM_UUID_RPL_TYPE_LEN + sc_len > (p_buf_end - p_buf)) return NULL; p_buf = osqlcomm_uuid_rpl_type_put(hd, p_buf, p_buf_end); + if (!p_buf) { + logmsg(LOGMSG_USER, "osqlcomm_uuid_rpl_type_put returned NULL\n"); + } p_buf = buf_no_net_put(packed_sc, sc_len, p_buf, p_buf_end); + if (!p_buf) { + logmsg(LOGMSG_USER, "buf_no_net_put returned NULL\n"); + } return p_buf; } @@ -852,6 +860,7 @@ static uint8_t *osqlcomm_schemachange_uuid_rpl_type_put(osql_uuid_rpl_t *hd, str { size_t sc_len = schemachange_packed_size(sc); + logmsg(LOGMSG_USER, "sc_len: %ld, p_buf_end - p_buf : %ld\n", sc_len, p_buf_end - p_buf); if (p_buf_end < p_buf || OSQLCOMM_UUID_RPL_TYPE_LEN + sc_len > (p_buf_end - p_buf)) return NULL; diff --git a/db/views_serial.c b/db/views_serial.c index f956700188..dcbc76cd26 100644 --- a/db/views_serial.c +++ b/db/views_serial.c @@ -1989,6 +1989,7 @@ hash_view_t *hash_deserialize_view(const char *view_str, struct errstat *err) const char *err_str; int num_partitions = 0, num_keys = 0; int rc; + char **keynames = NULL, **dbnames = NULL; /* parse string */ rc = cson_parse_string(&rootVal, view_str, strlen(view_str)); @@ -2028,14 +2029,14 @@ hash_view_t *hash_deserialize_view(const char *view_str, struct errstat *err) goto error; } - char keynames[MAXCOLUMNS][MAXCOLNAME]; + keynames = (char **)malloc(sizeof(char*) * num_keys); for (int i = 0; i < num_keys; i++) { arrVal = cson_array_get(keys_arr, i); if (!cson_value_is_string(arrVal)) { err_str = "INVALID CSON. Array element is not a string"; goto error; } - strcpy(keynames[i], cson_value_get_cstr(arrVal)); + keynames[i] = strdup(cson_value_get_cstr(arrVal)); } /* NUMPARTITIONS */ @@ -2052,15 +2053,15 @@ hash_view_t *hash_deserialize_view(const char *view_str, struct errstat *err) goto error; } - char dbnames[MAXPARTITIONS][MAXPARTITIONLEN]; + dbnames = (char **)malloc(sizeof(char*) * num_partitions); for (int i = 0; i < num_partitions; i++) { arrVal = cson_array_get(partitions, i); if (!cson_value_is_string(arrVal)) { err_str = "INVALID CSON. Array element is not a string"; goto error; } - strcpy(dbnames[i], cson_value_get_cstr(arrVal)); + dbnames[i] = strdup(cson_value_get_cstr(arrVal)); } view = create_hash_view(viewname, tablename, num_keys, keynames, num_partitions, dbnames, err); diff --git a/schemachange/sc_struct.c b/schemachange/sc_struct.c index eb3ebdae02..d6e7d47a6a 100644 --- a/schemachange/sc_struct.c +++ b/schemachange/sc_struct.c @@ -134,13 +134,15 @@ static size_t _partition_packed_size(struct comdb2_partition *p) sizeof(p->u.mergetable.version); case PARTITION_ADD_COL_HASH: for (int i = 0; i < p->u.hash.num_partitions; i++) { - shardNamesSize += sizeof(p->u.hash.partitions[i]); + logmsg(LOGMSG_USER, "length of %s is %ld\n", p->u.hash.partitions[i], strlen(p->u.hash.partitions[i])); + shardNamesSize += sizeof(size_t) + strlen(p->u.hash.partitions[i]); } for (int i = 0; i < p->u.hash.num_columns; i++) { - columnNamesSize += sizeof(p->u.hash.columns[i]); + logmsg(LOGMSG_USER, "length of %s is %ld\n", p->u.hash.columns[i], strlen(p->u.hash.columns[i])); + columnNamesSize += sizeof(size_t) + strlen(p->u.hash.columns[i]); } - return sizeof(p->type) + sizeof(p->u.hash.viewname) + sizeof(p->u.hash.num_partitions) + + return sizeof(p->type) + sizeof(size_t) + strlen(p->u.hash.viewname) + sizeof(p->u.hash.num_partitions) + sizeof(p->u.hash.num_columns) + shardNamesSize + columnNamesSize; default: logmsg(LOGMSG_ERROR, "Unimplemented partition type %d\n", p->type); @@ -450,15 +452,17 @@ int unpack_schema_change_protobuf(struct schema_change_type *s, void *packed_sc, break; } case PARTITION_ADD_COL_HASH: { - strncpy(s->partition.u.hash.viewname,sc->hashviewname, sizeof(s->partition.u.hash.viewname)); + s->partition.u.hash.viewname = strdup(sc->hashviewname); s->partition.u.hash.num_partitions = sc->hashnumpartitions; s->partition.u.hash.num_columns = sc->hashnumcolumns; + s->partition.u.hash.partitions = (char **)malloc(sizeof(char *) * s->partition.u.hash.num_partitions); + s->partition.u.hash.columns = (char **)malloc(sizeof(char *) * s->partition.u.hash.num_columns); for (int i=0;i < sc->n_hashcolumns; i++) { - strncpy(s->partition.u.hash.columns[i], sc->hashcolumns[i], sizeof(s->partition.u.hash.columns[i])); + s->partition.u.hash.columns[i] = strdup(sc->hashcolumns[i]); } for(int i=0; in_hashpartitions;i++) { - strncpy(s->partition.u.hash.partitions[i], sc->hashpartitions[i], sizeof(s->partition.u.hash.partitions[i])); + s->partition.u.hash.partitions[i] = strdup(sc->hashpartitions[i]); } s->partition.u.hash.createQuery = strdup(sc->hashcreatequery); break; @@ -487,7 +491,7 @@ void *buf_get_schemachange_protobuf(struct schema_change_type *s, void *p_buf, v void *buf_put_schemachange(struct schema_change_type *s, void *p_buf, void *p_buf_end) { - + size_t len = 0; if (p_buf >= p_buf_end) return NULL; p_buf = buf_put(&s->kind, sizeof(s->kind), p_buf, p_buf_end); @@ -614,16 +618,22 @@ void *buf_put_schemachange(struct schema_change_type *s, void *p_buf, void *p_bu break; } case PARTITION_ADD_COL_HASH: { - p_buf = buf_no_net_put(s->partition.u.hash.viewname, sizeof(s->partition.u.hash.viewname), p_buf, p_buf_end); + len = strlen(s->partition.u.hash.viewname); + p_buf = buf_put(&len, sizeof(len), p_buf, p_buf_end); + p_buf = buf_no_net_put(s->partition.u.hash.viewname, strlen(s->partition.u.hash.viewname), p_buf, p_buf_end); p_buf = buf_put(&s->partition.u.hash.num_columns, sizeof(s->partition.u.hash.num_columns), p_buf, p_buf_end); for (int i = 0; i < s->partition.u.hash.num_columns; i++) { + len = strlen(s->partition.u.hash.columns[i]); + p_buf = buf_put(&len, sizeof(len), p_buf, p_buf_end); p_buf = - buf_no_net_put(s->partition.u.hash.columns[i], sizeof(s->partition.u.hash.columns[i]), p_buf, p_buf_end); + buf_no_net_put(s->partition.u.hash.columns[i], strlen(s->partition.u.hash.columns[i]), p_buf, p_buf_end); } p_buf = buf_put(&s->partition.u.hash.num_partitions, sizeof(s->partition.u.hash.num_partitions), p_buf, p_buf_end); for (int i = 0; i < s->partition.u.hash.num_partitions; i++) { + len = strlen(s->partition.u.hash.partitions[i]); + p_buf = buf_put(&len, sizeof(len), p_buf, p_buf_end); p_buf = - buf_no_net_put(s->partition.u.hash.partitions[i], sizeof(s->partition.u.hash.partitions[i]), p_buf, p_buf_end); + buf_no_net_put(s->partition.u.hash.partitions[i], strlen(s->partition.u.hash.partitions[i]), p_buf, p_buf_end); } break; } @@ -877,7 +887,7 @@ void *buf_get_schemachange_v1(struct schema_change_type *s, void *p_buf, void *buf_get_schemachange_v2(struct schema_change_type *s, void *p_buf, void *p_buf_end) { - + size_t len = 0; if (p_buf >= p_buf_end) return NULL; p_buf = (uint8_t *)buf_get(&s->kind, sizeof(s->kind), p_buf, p_buf_end); @@ -1046,19 +1056,34 @@ void *buf_get_schemachange_v2(struct schema_change_type *s, break; } case PARTITION_ADD_COL_HASH: { - p_buf = (uint8_t *)buf_no_net_get(s->partition.u.hash.viewname, sizeof(s->partition.u.hash.viewname), p_buf, + p_buf = (uint8_t *)buf_get(&len, sizeof(len), p_buf, p_buf_end); + logmsg(LOGMSG_USER, "GOT LEN AS %ld for viewname\n", len); + s->partition.u.hash.viewname = (char *)malloc(sizeof(char) * len + 1); + p_buf = (uint8_t *)buf_no_net_get(s->partition.u.hash.viewname, len, p_buf, p_buf_end); + logmsg(LOGMSG_USER, "GOT VIEWNAME AS %s \n", s->partition.u.hash.viewname); + s->partition.u.hash.viewname[len]='\0'; p_buf = (uint8_t *)buf_get(&s->partition.u.hash.num_columns, sizeof(s->partition.u.hash.num_columns), p_buf, p_buf_end); + s->partition.u.hash.columns = (char **)malloc(sizeof(char *) * s->partition.u.hash.num_columns); for (int i = 0; i < s->partition.u.hash.num_columns; i++) { - p_buf = (uint8_t *)buf_no_net_get(s->partition.u.hash.columns[i], sizeof(s->partition.u.hash.columns[i]), + p_buf = (uint8_t *)buf_get(&len, sizeof(len), p_buf, p_buf_end); + logmsg(LOGMSG_USER, "GOT LEN AS %ld for column %d\n", len, i); + s->partition.u.hash.columns[i] = (char *)malloc(sizeof(char) * len + 1); + p_buf = (uint8_t *)buf_no_net_get(s->partition.u.hash.columns[i], len, p_buf, p_buf_end); + logmsg(LOGMSG_USER, "GOT COLUMN AS %s \n", s->partition.u.hash.columns[i]); } p_buf = (uint8_t *)buf_get(&s->partition.u.hash.num_partitions, sizeof(s->partition.u.hash.num_partitions), p_buf, p_buf_end); + s->partition.u.hash.partitions = (char **)malloc(sizeof(char *) * s->partition.u.hash.num_partitions); for (int i = 0; i < s->partition.u.hash.num_partitions; i++) { - p_buf = (uint8_t *)buf_no_net_get(s->partition.u.hash.partitions[i], sizeof(s->partition.u.hash.partitions[i]), p_buf, + p_buf = (uint8_t *)buf_get(&len, sizeof(len), p_buf, p_buf_end); + logmsg(LOGMSG_USER, "GOT LEN AS %ld for partition %d\n", len, i); + s->partition.u.hash.partitions[i] = (char *)malloc(sizeof(char) * len + 1); + p_buf = (uint8_t *)buf_no_net_get(s->partition.u.hash.partitions[i], len, p_buf, p_buf_end); + logmsg(LOGMSG_USER, "GOT PARTITION AS %s \n", s->partition.u.hash.partitions[i]); } break; } diff --git a/schemachange/schemachange.h b/schemachange/schemachange.h index 4dd97d9911..9bc29f7feb 100644 --- a/schemachange/schemachange.h +++ b/schemachange/schemachange.h @@ -85,12 +85,11 @@ struct comdb2_partition { int version; } mergetable; struct hash { - char viewname[MAXTABLELEN]; + char *viewname; uint32_t num_partitions; uint32_t num_columns; // in partitoning key - char columns[MAXCOLUMNS][MAXCOLNAME]; - uint32_t keys[MAXPARTITIONS]; - char partitions[MAXPARTITIONS][MAXPARTITIONLEN]; + char **columns; // columns in partitioning key + char **partitions; char *createQuery; } hash; } u; diff --git a/sqlite/src/comdb2build.c b/sqlite/src/comdb2build.c index e61896253b..8081648dc3 100644 --- a/sqlite/src/comdb2build.c +++ b/sqlite/src/comdb2build.c @@ -5069,7 +5069,7 @@ void comdb2CreateTableEnd( if (ctx->partition && ctx->partition->type == PARTITION_ADD_COL_HASH) { char tmp_str[MAXTABLELEN] = {0}; - strcpy(ctx->partition->u.hash.viewname, ctx->tablename); + ctx->partition->u.hash.viewname = strdup(ctx->tablename); strcpy(tmp_str , "hash_"); strcpy(tmp_str + 5, ctx->tablename); strcpy(sc->tablename, tmp_str); @@ -7748,8 +7748,8 @@ void create_default_consumer_sp(Parse *p, char *spname) } static int comdb2GetHashPartitionParams(Parse* pParse, IdList *pColumn, IdList *pPartitions, - char cols[][MAXCOLNAME], uint32_t *oNumPartitions, uint32_t *oNumColumns, - char partitions[][MAXPARTITIONLEN]) + char ***cols, uint32_t *oNumPartitions, uint32_t *oNumColumns, + char ***partitions) { int column_exists = 0; struct comdb2_column *cur_col; @@ -7764,6 +7764,7 @@ static int comdb2GetHashPartitionParams(Parse* pParse, IdList *pColumn, IdList * /* Copy the sharding key names after asserting their existence in the table*/ *oNumColumns = pColumn->nId; + *cols = (char **)malloc(sizeof(char *) * pColumn->nId); int i; for (i=0; i nId;i++) { column_exists = 0; @@ -7780,18 +7781,66 @@ static int comdb2GetHashPartitionParams(Parse* pParse, IdList *pColumn, IdList * return -1; } else { // column exists, copy it - strncpy0(cols[i], pColumn->a[i].zName, strlen(pColumn->a[i].zName) + 1); + *cols[i] = strdup(pColumn->a[i].zName); } } /*Copy the table partition names*/ *oNumPartitions = pPartitions->nId; + *partitions = (char **)malloc(sizeof(char *) * pPartitions->nId); for(i=0;inId;i++) { - strncpy0(partitions[i], pPartitions->a[i].zName, strlen(pPartitions->a[i].zName)+1); + *partitions[i] = strdup(pPartitions->a[i].zName); } return 0; } +struct comdb2_partition *_get_comdb2_hash_partition(Parse *pParse, IdList *pColumn, IdList *pPartitions, int remove) { + struct comdb2_partition *partition; + partition = _get_partition(pParse, 0); + partition->type = PARTITION_ADD_COL_HASH; + int column_exists = 0; + struct comdb2_column *cur_col; + struct comdb2_ddl_context *ctx = pParse->comdb2_ddl_ctx; + if (ctx == 0) { + /* An error must have been set. */ + assert(pParse->rc != 0); + return NULL; + } + + assert(pColumn!=0 && pColumn->nId>0); + + /* Copy the sharding key names after asserting their existence in the table*/ + partition->u.hash.num_columns = pColumn->nId; + partition->u.hash.columns = (char **)malloc(sizeof(char *) * pColumn->nId); + int i; + for (i=0; i nId;i++) { + column_exists = 0; + LISTC_FOR_EACH(&ctx->schema->column_list, cur_col, lnk) + { + if ((strcasecmp(pColumn->a[i].zName, cur_col->name) == 0)) { + column_exists = 1; + break; + } + } + if (column_exists==0) { + setError(pParse, SQLITE_MISUSE, comdb2_asprintf("Column %s does not exist", + pColumn->a[i].zName)); + return NULL; + } else { + // column exists, copy it + partition->u.hash.columns[i] = strdup(pColumn->a[i].zName); + } + } + + /*Copy the table partition names*/ + partition->u.hash.num_partitions = pPartitions->nId; + partition->u.hash.partitions = (char **)malloc(sizeof(char *) * pPartitions->nId); + for(i=0;inId;i++) { + partition->u.hash.partitions[i] = strdup(pPartitions->a[i].zName); + } + return partition; +} + void comdb2CreateHashPartition(Parse *pParse, IdList *pColumn, IdList *pPartitions, int createPartitions, int createTables) { struct comdb2_partition *partition; @@ -7800,19 +7849,18 @@ void comdb2CreateHashPartition(Parse *pParse, IdList *pColumn, IdList *pPartitio return; } - partition = _get_partition(pParse, 0); + partition = _get_comdb2_hash_partition(pParse,pColumn,pPartitions, 0); if (!partition) return; - partition->type = PARTITION_ADD_COL_HASH; - if (comdb2GetHashPartitionParams(pParse, pColumn, pPartitions, - partition->u.hash.columns, + /*if (comdb2GetHashPartitionParams(pParse, pColumn, pPartitions, + &partition->u.hash.columns, (uint32_t*)&partition->u.hash.num_partitions, (uint32_t*)&partition->u.hash.num_columns, - partition->u.hash.partitions)) { + &partition->u.hash.partitions)) { free_ddl_context(pParse); return; - } + }*/ GET_CLNT; if (clnt && clnt->sql) {