5 Relation *Relation_create(TupleSchema *schema) {
6 Relation *r = (Relation *) malloc( sizeof( Relation ) );
8 .content = (HashVector) {
10 .variant = Nibble_index_levels,
14 .fill = 0, .holes = 0, .type = (ItemKeyFun*) schema
16 .constraints = (Vector) {
17 .variant = single_index_level,
25 #define COPYA(T,P,N) (T*) memcpy( malloc( N * sizeof(T) ), P, N * sizeof( T ) )
26 #define COPY(T,P) COPYA(T,P,1)
28 // Add an indexing HashVector to the Relation using the given column
29 // flags with 1 indicating key column and 0 indicating value column.
30 int Relation_add_constraint(Relation *r,...) {
32 TupleSchema *ts = (TupleSchema *) r->content.type;
33 Tuple *columns = (Tuple*) malloc(
34 sizeof( Tuple ) + ts->arity * sizeof( void* ) );
37 for ( ; i < ts->arity; i++ ) {
38 if ( va_arg( ap, int ) ) {
39 columns->elements[i] = ts->columns->elements[i];
43 ts = TupleSchema_create( ts->arity, columns );
44 i = (int) r->constraints.size;
47 HashVector_create( Nibble_index_levels, (ItemKeyFun*) ts ) );
51 //============== Adding an item =============
52 // Iteration context for adding or Querying a Relation
59 // Determine matches to ((Knockout*)data)->key in
60 // (HashVector*)item, optionally using ((Knockout*)data)->columns
61 // for ignoring full matches to the key tuple.
62 static int knockout_check(VectorIndex index,void *item,void *data) {
63 Knockout *kod = (Knockout*) data;
65 for ( ; i < ((HashVector*) item)->table.size; i++ ) {
66 void *old = HashVector_next( (HashVector*) item, &i, kod->item );
68 HashVector_add( &kod->knockouts, old );
74 // delete the (tuple*)item from the (HashVector*)data
75 static int knockout_delete(VectorIndex index,void *item,void *data) {
76 HashVector_delete( (HashVector*) item, data );
80 // add the (tuple*)data to the (HashVector*)item
81 static int knockout_add(VectorIndex index,void *item,void *data) {
82 HashVector_add( (HashVector*)item, data );
86 // Find and remove all collisions for a Query, unless "add" is
87 // non-zero in which case the function aborts if there is any match in
89 static int knockout_clear(Knockout *this,Relation *r,Tuple *item,int add) {
90 (*this) = (Knockout) {
94 .variant = Nibble_index_levels, .size = 16, .entries = 0
96 .fill = 0, .holes = 0, .type = r->content.type,
100 knockout_check( 0, &r->content, this );
102 if ( this->knockouts.fill > 0 ) {
105 // Find all constraint knockouts for addition
106 Vector_iterate( &r->constraints, 0, knockout_check, this );
108 if ( this->knockouts.fill > 0 ) {
109 // Delete them from all tables
111 for ( i = 0; i < this->knockouts.table.size; i++ ) {
112 void *t = HashVector_next( &this->knockouts, &i, 0 );
114 HashVector_delete( &r->content, t );
115 Vector_iterate( &r->constraints, 0, knockout_delete, t );
122 // add a tuple to a Relation and return a Vector of knocked out
123 // tuples, if any, or 0 otherwise.
124 Vector *Relation_add(Relation *r,Tuple *item) {
126 if ( knockout_clear( &data, r, item, 1 ) ) {
128 HashVector_add( &r->content, item );
129 Vector_iterate( &r->constraints, 0, knockout_add, item );
130 return HashVector_contents( &data.knockouts, single_index_level, 0 );
135 Vector *Relation_delete(Relation *r,Tuple *item) {
137 (void) knockout_clear( &data, r, item, 0 );
138 return HashVector_contents( &data.knockouts, single_index_level, 0 );
141 void *Relation_next(Relation *r,VectorIndex *index,Tuple *query) {
142 return HashVector_next( &r->content, index, query );