Skip to content

Commit

Permalink
add table key and hashcode
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Oct 12, 2015
1 parent d41ae6e commit e50ff53
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions symmetric-client-clib/inc/db/model/Table.h
Expand Up @@ -37,6 +37,8 @@ typedef struct SymTable {
void (*copyColumnTypesFrom)(struct SymTable *this, struct SymTable *source);
SymColumn * (*findColumn)(struct SymTable *this, char *name, unsigned short caseSensitive);
char * (*toString)(struct SymTable *this);
int (*calculateTableHashcode)(struct SymTable *this);
char * (*getTableKey)(struct SymTable *this);
void (*destroy)(struct SymTable *this);
} SymTable;

Expand Down
30 changes: 30 additions & 0 deletions symmetric-client-clib/src/db/model/Table.c
Expand Up @@ -32,6 +32,34 @@ char * SymTable_getFullTableName(SymTable *this, char *delimiterToken, char *cat
return sb->destroyAndReturn(sb);
}

static int SymTable_calculateHashcodeForColumns(int prime, SymList *cols) {
int result = 1;
SymIterator *iter = cols->iterator(cols);
while (iter->hasNext(iter)) {
SymColumn *col = (SymColumn *) iter->next(iter);
result = prime * result + SymStringBuilder_hashCode(col->name);
result = prime * result + col->sqlType;
}
iter->destroy(iter);
return result;
}

int SymTable_calculateTableHashcode(SymTable *this) {
int prime = 31;
int result = 1;
result = prime * result + SymStringBuilder_hashCode(this->name);
result = prime * result + SymTable_calculateHashcodeForColumns(prime, this->columns);
return result;
}

char * SymTable_getTableKey(SymTable *this) {
char *name = SymTable_getFullTableName(this, "", ".", ".");
SymStringBuilder *sb = SymStringBuilder_newWithString(name);
sb->append(sb, "-")->appendf(sb, "%d", SymTable_calculateTableHashcode(this));
free(name);
return sb->destroyAndReturn(sb);
}

SymColumn * SymTable_findColumn(SymTable *this, char *name, unsigned short caseSensitive) {
SymColumn *column = NULL;
SymIterator *iter = this->columns->iterator(this->columns);
Expand Down Expand Up @@ -117,6 +145,8 @@ SymTable * SymTable_new(SymTable *this) {
this->copyColumnTypesFrom = (void *) &SymTable_copyColumnTypesFrom;
this->findColumn = (void *) &SymTable_findColumn;
this->toString = (void *) &SymTable_toString;
this->calculateTableHashcode = (void *) &SymTable_calculateTableHashcode;
this->getTableKey = (void *) &SymTable_getTableKey;
this->destroy = (void *) &SymTable_destroy;
return this;
}
Expand Down

0 comments on commit e50ff53

Please sign in to comment.