Skip to content

Commit

Permalink
change map to contain objects instead of copying them
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Oct 26, 2015
1 parent 841478e commit ad9e493
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 113 deletions.
10 changes: 5 additions & 5 deletions symmetric-client-clib-test/src/util/MapTest.c
Expand Up @@ -24,7 +24,7 @@ static SymMap * SymMapTest_test_createStringMap(int keySize, char **keys, char *
SymMap *map = SymMap_new(NULL, mapSize);
int i;
for (i = 0; i < keySize; i++) {
map->put(map, keys[i], values[i], strlen(values[i]));
map->put(map, keys[i], values[i]);
}
return map;
}
Expand Down Expand Up @@ -72,7 +72,7 @@ void SymMapTest_test3() {
}

SymMap *map = SymMap_new(NULL, 100);
map->put(map, table->name, table, sizeof(SymTable));
map->put(map, table->name, table);
SymTable *out = map->get(map, table->name);

CU_ASSERT(out != NULL);
Expand Down Expand Up @@ -139,7 +139,7 @@ void SymMapTest_testValuesEmpty() {

void SymMapTest_testValuesSingleValue() {
SymMap *map = SymMap_new(NULL, 100);
map->put(map, "key1", "value1", sizeof(char*));
map->put(map, "key1", "value1");
SymList *valuesList = map->values(map);

CU_ASSERT(strcmp(valuesList->get(valuesList, 0), "value1") == 0);
Expand Down Expand Up @@ -190,7 +190,7 @@ void SymMapTest_testEntriesEmpty() {

void SymMapTest_testEntriesSingleValue() {
SymMap *map = SymMap_new(NULL, 100);
map->put(map, "key1", "value1", sizeof(char*));
map->put(map, "key1", "value1");
SymList *entryList = map->entries(map);

CU_ASSERT(strcmp(((SymMapEntry*)entryList->get(entryList, 0))->key, "key1") == 0);
Expand Down Expand Up @@ -249,7 +249,7 @@ void SymMapTest_testKeysEmpty() {

void SymMapTest_testKeysSingleValue() {
SymMap *map = SymMap_new(NULL, 100);
map->put(map, "key1", "value1", sizeof(char*));
map->put(map, "key1", "value1");
SymStringArray *keyList = map->keys(map);

CU_ASSERT(strcmp(keyList->get(keyList, 0), "key1") == 0);
Expand Down
2 changes: 2 additions & 0 deletions symmetric-client-clib/inc/db/sql/Row.h
Expand Up @@ -54,5 +54,7 @@ typedef struct SymRow {
} SymRow;

SymRow * SymRow_new(SymRow *this, int columnCount);
SymRowEntry * SymRowEntry_new(char *value, int sqlType, int size);
void SymRowEntry_destroy(SymRowEntry *this);

#endif
3 changes: 3 additions & 0 deletions symmetric-client-clib/inc/db/sql/mapper/RowMapper.h
Expand Up @@ -23,7 +23,10 @@

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "db/sql/Row.h"
#include "util/List.h"
#include "util/Map.h"

SymRow * SymRowMapper_mapper(SymRow *row);

Expand Down
5 changes: 2 additions & 3 deletions symmetric-client-clib/inc/service/TriggerRouterService.h
Expand Up @@ -86,10 +86,9 @@ SymTriggerRouterService * SymTriggerRouterService_new(SymTriggerRouterService *t
SymConfigurationService *configurationService, SymSequenceService *sequenceService,
SymParameterService *parameterService, SymDatabasePlatform *platform, SymDialect *symmetricDialect);

#define SYM_SQL_SELECT_TRIGGER_ROUTERS "select tr.trigger_id, tr.router_id, tr.create_time, tr.last_update_time, tr.last_update_by, tr.initial_load_order, tr.initial_load_select, tr.initial_load_delete_stmt, tr.initial_load_batch_count, tr.ping_back_enabled, tr.enabled \
from sym_trigger_router tr \
#define SYM_SQL_SELECT_TRIGGER_ROUTERS "from sym_trigger_router tr \
inner join sym_trigger t on tr.trigger_id=t.trigger_id \
inner join sym_router r on tr.router_id=r.router_id "
inner join sym_router r on tr.router_id=r.router_id "

#define SYM_SQL_SELECT_TRIGGERS "select t.trigger_id,t.channel_id,t.reload_channel_id,t.source_table_name,t.source_schema_name,t.source_catalog_name, \
t.sync_on_insert,t.sync_on_update,t.sync_on_delete,t.sync_on_incoming_batch,t.use_stream_lobs, \
Expand Down
8 changes: 4 additions & 4 deletions symmetric-client-clib/inc/util/Map.h
Expand Up @@ -12,26 +12,26 @@
typedef struct {
char *key;
void *value;
int sizeBytes;
void *next;
} SymMapEntry;


typedef struct SymMap {
int size;
SymMapEntry **table;
void (*put)(struct SymMap *this, char *key, void *value, int size);
void (*putByInt)(struct SymMap *this, int key, void *value, int size);
void (*put)(struct SymMap *this, char *key, void *value);
void (*putByInt)(struct SymMap *this, int key, void *value);
void * (*get)(struct SymMap *this, char *key);
void * (*getByInt)(struct SymMap *this, int key);
void * (*remove)(struct SymMap *this, char *key);
void * (*removeByInt)(struct SymMap *this, int key);
SymStringArray * (*keys)(struct SymMap *this);
SymList * (*values)(struct SymMap *this);
SymList * (*entries)(struct SymMap *this);
int (*getBytesSize)(struct SymMap *this, char *key);
void (*reset)(struct SymMap *this);
void (*resetAll)(struct SymMap *this, void (*destroyObject)(void *object), unsigned short shouldFreeKey);
void (*destroy)(struct SymMap *this);
void (*destroyAll)(struct SymMap *this, void (*destroyObject)(void *object), unsigned short shouldFreeKey);
} SymMap;

SymMap *SymMap_new(SymMap *this, int size);
Expand Down
Expand Up @@ -73,8 +73,7 @@ SymList * SymSqliteSqlTemplate_query(SymSqliteSqlTemplate *this, char *sql, SymS
SymRow *row = SymSqliteSqlTemplate_buildRow(stmt);
void *object = map_row(row);
list->add(list, object);
// TODO: need to destroy each row, and the row mapper needs to make a copy
//row->destroy(row);
row->destroy(row);
}

if (rc != SQLITE_DONE) {
Expand All @@ -96,8 +95,7 @@ SymList * SymSqliteSqlTemplate_queryWithUserData(SymSqliteSqlTemplate *this, cha
SymRow *row = SymSqliteSqlTemplate_buildRow(stmt);
void *object = map_row(row, userData);
list->add(list, object);
// TODO: need to destroy each row, and the row mapper needs to make a copy
//row->destroy(row);
row->destroy(row);
}

if (rc != SQLITE_DONE) {
Expand Down
32 changes: 15 additions & 17 deletions symmetric-client-clib/src/db/sql/Row.c
Expand Up @@ -20,14 +20,21 @@
*/
#include "db/sql/Row.h"

void SymRowEntry_destroy(SymRowEntry *this) {
free(this);
}

SymRowEntry * SymRowEntry_new(char *value, int sqlType, int size) {
SymRowEntry *entry = (SymRowEntry *) calloc(1, sizeof(SymRowEntry));
entry->value = value;
entry->sqlType = sqlType;
entry->size = size;
return entry;
}

void SymRow_put(SymRow *this, char *columnName, void *value, int sqlType, int size) {
char *copyValue = NULL;
if (value != NULL) {
copyValue = (char *) memcpy(malloc(size + 1), value, size);
copyValue[size] = '\0';
}
SymRowEntry entry = { copyValue, sqlType, size };
this->map->put(this->map, columnName, &entry, sizeof(SymRowEntry));
SymRowEntry *entry = SymRowEntry_new(value, sqlType, size);
this->map->put(this->map, columnName, entry);
}

char * SymRow_getString(SymRow *this, char *columnName) {
Expand Down Expand Up @@ -144,16 +151,7 @@ SymDate * SymRow_dateValue(SymRow *this) {
}

void SymRow_destroy(SymRow *this) {
/*
int i;
for (i = 0; i < this->map->size; i++) {
if (this->map->table[i]) {
SymRowEntry *entry = (SymRowEntry *) this->map->table[i]->value;
free(entry->value);
}
}
*/
this->map->destroy(this->map);
this->map->destroyAll(this->map, (void *) &SymRowEntry_destroy, 0);
free(this);
}

Expand Down
15 changes: 13 additions & 2 deletions symmetric-client-clib/src/db/sql/mapper/RowMapper.c
Expand Up @@ -21,6 +21,17 @@
#include "db/sql/mapper/RowMapper.h"

SymRow * SymRowMapper_mapper(SymRow *row) {
// TODO: clone the row
return row;
SymRow *newRow = SymRow_new(NULL, row->map->size);
SymList *entries = row->map->entries(row->map);
SymIterator *iter = entries->iterator(entries);
while (iter->hasNext(iter)) {
SymMapEntry *entry = (SymMapEntry *) iter->next(iter);
SymRowEntry *rowEntry = (SymRowEntry *) entry->value;
char *columnName = strdup(entry->key);
char *columnValue = strdup(rowEntry->value);
newRow->put(newRow, columnName, columnValue, rowEntry->sqlType, rowEntry->size);
}
iter->destroy(iter);
entries->destroy(entries);
return newRow;
}
2 changes: 1 addition & 1 deletion symmetric-client-clib/src/io/reader/ProtocolDataReader.c
Expand Up @@ -82,7 +82,7 @@ static void SymProtocolDataReader_parseLine(int eol, void *userData) {
isPrimary = this->keys->contains(this->keys, this->fields->array[i]);
this->table->columns->add(this->table->columns, SymColumn_new(NULL, this->fields->array[i], isPrimary));
}
this->parsedTables->put(this->parsedTables, this->table->name, this->table, sizeof(SymTable));
this->parsedTables->put(this->parsedTables, this->table->name, this->table);
this->writer->startTable(this->writer, this->table);
} else if (strcmp(token, SYM_CSV_NODEID) == 0) {
SymStringBuilder_copyToField(&batch->sourceNodeId, fields->get(fields, 1));
Expand Down
4 changes: 2 additions & 2 deletions symmetric-client-clib/src/io/writer/DefaultDatabaseWriter.c
Expand Up @@ -50,7 +50,7 @@ static SymTable * SymDefaultDatabaseWriter_lookupTableAtTarget(SymDefaultDatabas
SymTable *targetTable = this->platform->getTableFromCache(this->platform, sourceTable->catalog, sourceTable->schema, sourceTable->name, 0);
if (targetTable) {
table = targetTable->copyAndFilterColumns(targetTable, sourceTable->columns, 1);
this->targetTables->put(this->targetTables, tableKey, targetTable, sizeof(SymTable));
this->targetTables->put(this->targetTables, tableKey, targetTable);
}
}
free(tableKey);
Expand Down Expand Up @@ -169,7 +169,7 @@ void SymDefaultDatabaseWriter_sql(SymDefaultDatabaseWriter *this, SymCsvData *da
unsigned short SymDefaultDatabaseWriter_write(SymDefaultDatabaseWriter *this, SymCsvData *data) {
// TODO: check if this->targetTable and if ignore missing tables is on
if (!this->incomingBatch) {
return 0;
return 1;
}
switch (data->dataEventType) {
case SYM_DATA_EVENT_INSERT:
Expand Down
5 changes: 3 additions & 2 deletions symmetric-client-clib/src/io/writer/ProtocolDataWriter.c
Expand Up @@ -85,7 +85,7 @@ static void SymProtocolDataWriter_startTable(SymProtocolDataWriter *this, SymTab
SymList *pkList = table->getPrimaryKeyColumns(table);
SymProtocolDataWriter_printList(this->sb, SYM_CSV_KEYS, pkList);
SymProtocolDataWriter_printList(this->sb, SYM_CSV_COLUMNS, table->columns);
this->processedTables->put(this->processedTables, fullyQualifiedTableName, tableKey, (strlen(tableKey) * sizeof(char)) + 1);
this->processedTables->put(this->processedTables, fullyQualifiedTableName, tableKey);
pkList->destroy(pkList);
}
free(fullyQualifiedTableName);
Expand Down Expand Up @@ -156,8 +156,9 @@ size_t SymProtocolDataWriter_process(SymProtocolDataWriter *this, char *buffer,

if (numBytes > 0) {
memcpy(buffer, this->sb->str, numBytes);
SymLog_debug("Writing data: %s\n", this->sb->str);
}
printf("buffer: %s\n", this->sb->str);

if (bufferFull) {
char *leftOverData = this->sb->substring(this->sb, count, this->sb->pos);
this->sb->reset(this->sb);
Expand Down
4 changes: 1 addition & 3 deletions symmetric-client-clib/src/service/ConfigurationService.c
Expand Up @@ -83,8 +83,6 @@ SymList * SymConfigurationService_getNodeGroupLinksFor(SymConfigurationService *
}
}
iter->destroy(iter);
// TODO: destroy NodeGroupLinks that were not used
links->destroy(links);
return target;
}

Expand Down Expand Up @@ -114,7 +112,7 @@ SymMap * SymConfigurationService_getChannels(SymConfigurationService *this, unsi
SymIterator *iter = list->iterator(list);
while (iter->hasNext(iter)) {
SymChannel *channel = (SymChannel *) iter->next(iter);
channels->put(channels, channel->channelId, channel, sizeof(SymChannel));
channels->put(channels, channel->channelId, channel);
}
iter->destroy(iter);
list->destroy(list);
Expand Down
2 changes: 1 addition & 1 deletion symmetric-client-clib/src/service/NodeService.c
Expand Up @@ -33,7 +33,7 @@ static SymNode * SymNodeService_nodeMapper(SymRow *row) {
node->nodeGroupId = row->getStringNew(row, "node_group_id");
node->externalId = row->getStringNew(row, "external_id");
node->syncEnabled = row->getBoolean(row, "sync_enabled");
node->syncUrl = row->getString(row, "sync_url");
node->syncUrl = row->getStringNew(row, "sync_url");
node->schemaVersion = row->getStringNew(row, "schema_version");
node->databaseType = row->getStringNew(row, "database_type");
node->databaseVersion = row->getStringNew(row, "database_version");
Expand Down
6 changes: 3 additions & 3 deletions symmetric-client-clib/src/service/RouterService.c
Expand Up @@ -78,7 +78,7 @@ static int SymRouterService_insertDataEvents(SymRouterService *this, SymChannelR
SymLog_debug("About to insert a new batch for node %s on the '%s' channel.", nodeId, batch->channelId);

this->outgoingBatchService->insertOutgoingBatch(this->outgoingBatchService, batch);
context->batchesByNodes->put(context->batchesByNodes, nodeId, batch, sizeof(SymOutgoingBatch));
context->batchesByNodes->put(context->batchesByNodes, nodeId, batch);
}

batch->incrementEventCount(batch, dataMetaData->data->eventType);
Expand Down Expand Up @@ -110,7 +110,7 @@ SymList * SymRouterService_findAvailableNodes(SymRouterService *this, SymTrigger
SymLog_error("The router %s has no node group link configured from %s to %s",
router->routerId, router->nodeGroupLink->sourceNodeGroupId, router->nodeGroupLink->targetNodeGroupId);
}
context->availableNodes->put(context->availableNodes, triggerRouter->router->routerId, nodes, sizeof(SymList));
context->availableNodes->put(context->availableNodes, triggerRouter->router->routerId, nodes);
}
return nodes;
}
Expand Down Expand Up @@ -267,7 +267,7 @@ SymRouterService * SymRouterService_new(SymRouterService *this, SymOutgoingBatch
this->triggerRouterService = triggerRouterService;
this->platform = platform;
this->routers = SymMap_new(NULL, 20);
this->routers->put(this->routers, SYM_ROUTER_DEFAULT, SymDefaultDataRouter_new(NULL), sizeof(SymDefaultDataRouter));
this->routers->put(this->routers, SYM_ROUTER_DEFAULT, SymDefaultDataRouter_new(NULL));
this->routeData = (void *) &SymRouterService_routeData;
this->destroy = (void *) &SymRouterService_destroy;
return this;
Expand Down

0 comments on commit ad9e493

Please sign in to comment.