rework Relation_next and HashVector_next to void query handling in HashVector
[rrq/rrqmisc.git] / vector / Relation.c
index 4a6417053c73bff4f223f00064f2c839b2d8504d..0f671abce2d09c475f8092e22ff760825211250c 100644 (file)
@@ -61,10 +61,16 @@ typedef struct {
 // for ignoring full matches to the key tuple.
 static int knockout_check(VectorIndex index,void *item,void *data) {
     Knockout *kod = (Knockout*) data;
+    void *key = kod->item;
+    HashVector *hv = (HashVector*) item;
+    TupleSchema *type = (TupleSchema *)hv->type;
     VectorIndex i = 0;
-    for ( ; i < ((HashVector*) item)->table.size; i++ ) {
-       void *old = HashVector_next( (HashVector*) item, &i, kod->item );
+    for ( ; i < hv->table.size; i++ ) {
+       void *old = HashVector_next( hv, &i );
        if ( old ) {
+           if ( key && type->base.haskey( type, old, key ) == 0 ) {
+               continue;
+           }
            HashVector_add( &kod->knockouts, old );
        }
     }
@@ -109,7 +115,7 @@ static int knockout_clear(Knockout *this,Relation *r,Tuple *item,int add) {
        // Delete them from all tables
        VectorIndex i;
        for ( i = 0; i < this->knockouts.table.size; i++ ) {
-           void *t = HashVector_next( &this->knockouts, &i, 0 );
+           void *t = HashVector_next( &this->knockouts, &i );
            if ( t ) {
                HashVector_delete( &r->content, t );
                Vector_iterate( &r->constraints, 0, knockout_delete, t );
@@ -139,6 +145,19 @@ Vector *Relation_delete(Relation *r,Tuple *item) {
 }
 
 void *Relation_next(Relation *r,VectorIndex *index,Tuple *query) {
-    return HashVector_next( &r->content, index, query );
+    HashVector *hv = &r->content;
+    void *key = query;
+    TupleSchema *type = (TupleSchema *) hv->type;
+    for ( ; (*index) < hv->table.size; (*index)++ ) {
+       void *old = HashVector_next( hv, index );
+       if ( old ) {
+           if ( key && type->base.haskey( type, old, key ) == 0 ) {
+               continue;
+           }
+           return old;
+       }
+    }
+    (*index) = hv->table.size;
+    return 0;
 }