Added Query functions. Major renaming of stuff to use camelcase type names.
[rrq/rrqmisc.git] / vector / TupleSchema.c
1 #include <stdarg.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <TupleSchema.h>
5
6 #define COLUMN def->columns
7
8 /**
9  * This callback function returns the hashcode of a key.
10  *
11  * \param this is a pointer to the ItemKeyFun record from where this
12  * callback got invoked
13  *
14  * \param key is the key to produce a hascode for
15  *
16  * \returns the hashcode which is a VectorIndex (i.e. unsigned long)
17  *
18  * The hashcode is used for indexing into the backing Vector for
19  * finding the an item via its key. The same key must map consistently
20  * to the same hashcode while the hashtable contains an item with that
21  * key. Different keys map map to the same hashcode, in which case the
22  * Vector placement is made at the first empty or hole slot following
23  * the hashcode index.
24  */
25 static unsigned long TupleSchema_hashcode(void *this,void *key) {
26     TupleSchema *def = (TupleSchema *) this;
27     tuple *kp = (tuple*) key;
28     int i = 0;
29     unsigned long value = 5381;
30     for ( ; i < def->arity; i++ ) {
31         if ( COLUMN[i] ) {
32             value <<= 3;
33             if ( (*kp)[i] ) {
34                 value += COLUMN[i]->hashcode( COLUMN[i], (*kp)[i] );
35             }
36         }
37         value += 17;
38     }
39     return value;
40 }
41
42 /**
43  * This callback function determines whether an item has a
44  * given key or not.
45  */
46 static int TupleSchema_haskey(void *this,void *item,void *key) {
47     TupleSchema *def = (TupleSchema *) this;
48     tuple *kp = (tuple*) key;
49     tuple *tp = (tuple*) item;
50     int i = 0;
51     for ( ; i < def->arity; i++ ) {
52         if ( COLUMN[i] && (*kp)[i] ) {
53             if ( COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] ) == 0 ) {
54                 return 0;
55             }
56         }
57     }
58     return 1;
59 }
60
61
62 /**
63  * This callback function returns the key of an item by considering
64  * the arity and mask.
65  */
66 static void *tupleitem_itemkey(void *this,void *item) {
67     TupleSchema *def = (TupleSchema *) this;
68     tuple *tp = (tuple*) item;
69     int i;
70     int keylen = def->arity;
71     void **parts = calloc( keylen, sizeof( void* ) );
72     for ( i = 0; i < def->arity; i++ ) {
73         if ( COLUMN[i] ) {
74             parts[i] = COLUMN[i]->itemkey( COLUMN[i], (*tp)[i] );
75         }
76     }
77     return (void*) parts;
78 }
79
80 /**
81  * This callback function handles a key obtained from the itemkey
82  * callback function to reclaim temporary allocation.
83  */
84 static void tupleitem_releasekey(void *this,void *key) {
85     TupleSchema *def = (TupleSchema *) this;
86     tuple *kp = (tuple*) key;
87     int i;
88     for ( i = 0; i < def->arity; i++ ) {
89         if ( COLUMN[i] ) {
90             COLUMN[i]->releasekey( COLUMN[i], (*kp)[i] );
91         }
92     }
93     free( key );
94 }
95
96 #define OUT(X) a = X; if ( a > limit ) return 0; buffer += a; limit -= a
97
98 /**
99  * This callback function writes a representation of an item into
100  * a character buffer.
101  */
102 static int tupleitem_tostring(void *this,void *item,char *buffer,int limit) {
103     TupleSchema *def = (TupleSchema *) this;
104     tuple *t = (tuple*) item;
105     char *x = "<";
106     int a, i;
107     for ( i = 0; i < def->arity; i++ ) {
108         OUT( snprintf( buffer, limit, x ) );
109         x = ",";
110         OUT( def->columns[i]->tostring(
111                  def->columns[i], (*t)[i], buffer, limit ) );
112     }
113     OUT( snprintf( buffer, limit, ">" ) );
114     return a;
115 }
116
117
118 // Allocate
119 tuple *tuple_create(int arity,...) {
120     va_list ap;
121     int i;
122     tuple *t = (tuple *)malloc( arity * sizeof( void* ) );
123     va_start( ap, arity );
124     for ( i = 0; i < arity; i++ ) {
125         (*t)[i] = va_arg( ap, void* );
126     }
127     va_end( ap );
128     return t;
129 }
130
131 tuple *tuple_clone(int arity,tuple *t) {
132     tuple *ct = (tuple *)malloc( arity * sizeof( void* ) );
133     memcpy( ct, t, arity * sizeof( void* ) );
134     return ct;
135 }
136
137 ItemKeyFun TupleSchema_callbacks = {
138     .hashcode = TupleSchema_hashcode,
139     .haskey = TupleSchema_haskey,
140     .itemkey = tupleitem_itemkey,
141     .releasekey = tupleitem_releasekey,
142     .tostring = tupleitem_tostring
143 };
144
145 TupleSchema *TupleSchema_create(int arity,tuple *columns) {
146     TupleSchema *ts = (TupleSchema*) malloc( sizeof( TupleSchema ) );
147     (*ts) = (TupleSchema) {
148         .base = TupleSchema_callbacks,
149         .arity = arity,
150         .columns = (ItemKeyFun**) columns
151     };
152     return ts;
153 }
154
155 #define COPYA(T,P,N) (T*) memcpy( malloc( N * sizeof(T) ), P, N * sizeof( T ) )
156 #define COPY(T,P) COPYA(T,P,1)
157
158 // Duplicate a TupleSchema with optionally some columns reset.
159 TupleSchema *TupleSchema_mask(TupleSchema *schema,...) {
160     TupleSchema *masked = COPY(TupleSchema,schema);
161     masked->columns = COPYA( ItemKeyFun*, schema->columns, schema->arity );
162     va_list ap;
163     int i;
164     va_start( ap, schema );
165     for ( ;; ) {
166         i = va_arg( ap, int );
167         if ( i < 0 || i >= schema->arity ) {
168             break;
169         }
170         masked->columns[i] = 0;
171     };
172     va_end( ap );
173     return masked;
174 }
175
176 unsigned long tuple_mask(int arity,tuple *t) {
177     unsigned long mask = 0;
178     while ( arity-- > 0 ) {
179         mask <<= 1;
180         if ( (*t)[ arity ] ) {
181             mask++;
182         }
183     }
184     return mask;
185 }