4 relation *relation_create(tupleschema *schema) {
5 relation *r = (relation *) malloc( sizeof( relation ) );
7 .content = (hashvector) {
9 .variant = nibble_index_levels,
13 .fill = 0, .holes = 0, .type = (itemkeyfun*) schema
15 .constraints = (vector) {
16 .variant = single_index_level,
24 #define COPYA(T,P,N) (T*) memcpy( malloc( N * sizeof(T) ), P, N * sizeof( T ) )
25 #define COPY(T,P) COPYA(T,P,1)
27 // Add an indexing hashvector to the relation using the nominated
28 // column indexes being the value part. the key must be a clone of the
29 // relation columns but with some columns reset.
30 int relation_add_contraint(relation *r,tupleschema *key) {
31 tupleschema *primary = (tupleschema *) r->content.type;
32 if ( primary->arity != key->arity ) {
36 for ( ; i < primary->arity; i++ ) {
37 if ( key->columns[i] && primary->columns[i] != key->columns[i] ) {
41 i = (int) r->constraints.size;
44 hashvector_create( nibble_index_levels, &key->base ) );
48 //============== Adding an item =============
49 // Iteration context for adding or querying a relation
56 // Determine matches to ((knockout*)data)->key in
57 // (hashvector*)item, optionally using ((knockout*)data)->columns
58 // for ignoring full matches to the key tuple.
59 static int knockout_check(vector_index index,void *item,void *data) {
60 knockout *kod = (knockout*) data;
61 void *old = hashvector_next( (hashvector*) item, 0, kod->item );
63 vector_append( &kod->knockouts, old );
68 // delete the (tuple*)item from the (hashvector*)data
69 static int knockout_delete_item(vector_index index,void *item,void *data) {
70 hashvector_delete( (hashvector*)data, item );
74 // delete the tuples of (vector*)data from the (hashvector*)item
75 static int knockout_delete(vector_index index,void *item,void *data) {
76 vector_iterate( (vector*)data, 0, knockout_delete_item, item );
80 // add the (tuple*)data to the (hashvector*)item
81 static int knockout_add(vector_index 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) {
93 .variant = bitpair_index_levels,
94 .size = 0, .entries = 0
98 knockout_check( 0, &r->content, this );
99 if ( add && this->knockouts.size > 0 ) {
102 // Find all constraint knockouts
103 vector_iterate( &r->constraints, 0, knockout_check, this );
104 if ( this->knockouts.size > 0 ) {
105 // Delete them from all tables
107 &this->knockouts, 0, knockout_delete_item, &r->content );
109 &r->constraints, 0, knockout_delete, &this->knockouts );
114 // add a tuple to a relation and return a vector of knocked out
115 // tuples, if any, or 0 otherwise.
116 vector *relation_add(relation *r,tuple *item) {
118 if ( knockout_clear( &data, r, item, 1 ) ) {
120 hashvector_add( &r->content, item );
121 vector_iterate( &r->constraints, 0, knockout_add, item );
122 if ( data.knockouts.size > 0 ) {
123 return vector_clone( single_index_level, &data.knockouts );
129 vector *relation_delete(relation *r,tuple *item) {
131 (void) knockout_clear( &data, r, item, 0 );
132 if ( data.knockouts.size > 0 ) {
133 return vector_clone( single_index_level, &data.knockouts );
138 void *relation_next(relation *r,vector_index *index,tuple *query) {
139 return hashvector_next( &r->content, index, query );