tupleschema *def = (tupleschema *) this;
tuple *kp = (tuple*) key;
int i = 0;
- unsigned long value = 0;
+ unsigned long value = 5381;
for ( ; i < def->arity; i++ ) {
if ( COLUMN[i] ) {
value <<= 3;
- value += COLUMN[i]->hashcode( COLUMN[i], (*kp)[i] );
+ if ( (*kp)[i] ) {
+ value += COLUMN[i]->hashcode( COLUMN[i], (*kp)[i] );
+ }
}
+ value += 17;
}
return value;
}
tuple *tp = (tuple*) item;
int i = 0;
for ( ; i < def->arity; i++ ) {
- if ( COLUMN[i] == 0 ) {
- if ( (*kp)[i] && (*tp)[i] != (*kp)[i] ) {
+ if ( COLUMN[i] && (*kp)[i] ) {
+ if ( COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] ) == 0 ) {
return 0;
}
- continue;
- }
- if ( (*kp)[i] &&
- COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] ) == 0 ) {
- return 0;
}
}
return 1;
static void *tupleitem_itemkey(void *this,void *item) {
tupleschema *def = (tupleschema *) this;
tuple *tp = (tuple*) item;
- int i, j;
- int keylen = 0;
- for ( i = 0 ; i < def->arity; i++ ) {
- if ( COLUMN[i] ) {
- keylen++;
- }
- }
+ int i;
+ int keylen = def->arity;
void **parts = calloc( keylen, sizeof( void* ) );
- for ( i = 0, j = 0; i < def->arity; i++ ) {
+ for ( i = 0; i < def->arity; i++ ) {
if ( COLUMN[i] ) {
- parts[j++] = COLUMN[i]->itemkey( COLUMN[i], (*tp)[i] );
+ parts[i] = COLUMN[i]->itemkey( COLUMN[i], (*tp)[i] );
}
}
return (void*) parts;
static void tupleitem_releasekey(void *this,void *key) {
tupleschema *def = (tupleschema *) this;
tuple *kp = (tuple*) key;
- int i,j;
- for ( i = 0, j = 0; i < def->arity; i++ ) {
+ int i;
+ for ( i = 0; i < def->arity; i++ ) {
if ( COLUMN[i] ) {
- COLUMN[i]->releasekey( COLUMN[i], (*kp)[j++] );
+ COLUMN[i]->releasekey( COLUMN[i], (*kp)[i] );
}
}
free( key );
}
+#define OUT(X) a = X; if ( a > limit ) return 0; buffer += a; limit -= a
+
+/**
+ * This callback function writes a representation of an item into
+ * a character buffer.
+ */
+static int tupleitem_tostring(void *this,void *item,char *buffer,int limit) {
+ tupleschema *def = (tupleschema *) this;
+ tuple *t = (tuple*) item;
+ char *x = "<";
+ int a, i;
+ for ( i = 0; i < def->arity; i++ ) {
+ OUT( snprintf( buffer, limit, x ) );
+ x = ",";
+ OUT( def->columns[i]->tostring(
+ def->columns[i], (*t)[i], buffer, limit ) );
+ }
+ OUT( snprintf( buffer, limit, ">" ) );
+ return a;
+}
+
+
// Allocate
tuple *tuple_create(int arity,...) {
va_list ap;
return t;
}
+itemkeyfun tupleschema_callbacks = {
+ .hashcode = tupleitem_hashcode,
+ .haskey = tupleitem_haskey,
+ .itemkey = tupleitem_itemkey,
+ .releasekey = tupleitem_releasekey,
+ .tostring = tupleitem_tostring
+};
+
tupleschema *tupleschema_create(int arity,tuple *columns) {
tupleschema *ts = (tupleschema*) malloc( sizeof( tupleschema ) );
(*ts) = (tupleschema) {
- .base = {
- .hashcode = tupleitem_hashcode,
- .haskey = tupleitem_haskey,
- .itemkey = tupleitem_itemkey,
- .releasekey = tupleitem_releasekey
- },
+ .base = tupleschema_callbacks,
.arity = arity,
.columns = (itemkeyfun**) columns
};
}
return mask;
}
-