|
|
1.1 root 1: #include <libc.h>
2: #include <fio.h>
3: #include <string.h>
4: #include "dbtypes.h"
5:
6: /*
7: * free a tuple and all the attributes making it up
8: */
9: Tuple::~Tuple()
10: {
11: register Attribute *ap, *nap;
12:
13: for(ap=first; ap; ap=nap){
14: nap = ap->next;
15: delete ap;
16: }
17: }
18:
19: /*
20: * Parse a list of attributes and add the attributes to this tuple.
21: * If o is non-zero add each attribute to that ordered list.
22: */
23: Tuple::Tuple(char *str, Ordered *o, Tuple **alist, char wildcard)
24: {
25: char *attributes[MAXPAIRS];
26: int i, n;
27:
28: first = last = (Attribute *)0;
29: tail = (Tuple *)0;
30:
31: /*
32: * parse and create the tuple
33: */
34: setfields(" \t\n");
35: n = getmfields(str, attributes, MAXPAIRS);
36: for(i=0; i<n; i++){
37: if(*attributes[i]=='\0')
38: break;
39: if(first==(Attribute *)0)
40: first = last =new Attribute(attributes[i], this, wildcard);
41: else {
42: last->next = new Attribute(attributes[i], this, wildcard);
43: last = last->next;
44: }
45: /*
46: * insert each attribute into an ordered list
47: */
48: if(o)
49: last->insert(o);
50: }
51: /*
52: * chain into an allocation list for later deallocation
53: */
54: if(alist){
55: anext = *alist;
56: *alist = this;
57: }
58: }
59:
60: /*
61: * concatenate two tuples
62: */
63: void
64: Tuple::cat(Tuple *ntail)
65: {
66: tail = ntail;
67: }
68:
69: /*
70: * Calculate the distance of this tuple from some other tuple. The
71: * distance measure is the minimum of the distances between all of this
72: * tuple's attributes and all of the origin's.
73: */
74: void
75: Tuple::distance(Tuple *origin)
76: {
77: Attribute *o;
78: Attribute *p;
79: Tuple *t;
80: int ndist;
81:
82: dist = MAXDISTANCE;
83: for(t=this; t; t=t->tail){
84: for(p=t->first; p; p=p->next){
85: for(o=origin->first; o; o=o->next){
86: ndist = p->distance(o);
87: if(ndist<dist) {
88: dist = ndist;
89: }
90: }
91: }
92: }
93: }
94:
95: /*
96: * Return 0 if the tuple doesn't match the pattern, non-zero otherwise.
97: */
98: Tuple::match(Tuple *pattern)
99: {
100: Attribute *p;
101: Attribute *me;
102: Tuple *t;
103:
104: for(p=pattern->first; p; p=p->next){
105: /*
106: * each tuple can be a list of tuples
107: */
108: for(t=this; t; t=t->tail){
109: for(me=t->first; me; me=me->next){
110: if(me->compare(p)==0)
111: break;
112: }
113: if(me)
114: break;
115: }
116: if(me==(Attribute *)0)
117: return 0;
118: }
119: return 1;
120: }
121:
122: /*
123: * output a linear version of the tuple on fd
124: */
125: int
126: Tuple::print(int fd)
127: {
128: Attribute *p;
129: Tuple *t;
130:
131: for(t=this; t; t=t->tail){
132: for(p=t->first; p; p=p->next){
133: if (p->simpleprint(fd) < 0)
134: return -1;
135: }
136: }
137: return 0;
138: }
139:
140: /*
141: * Free a list of tuples
142: */
143: void
144: freetuplelist(Tuple **list)
145: {
146: Tuple *t, *nt;
147:
148: if(*list==(Tuple *)0)
149: return;
150:
151: for(t=*list; t; t=nt){
152: nt = t->anext;
153: delete t;
154: }
155:
156: *list = (Tuple *)0;
157: }
158:
159: /*
160: * Lookup a tuple in an ordered list. Return the set of matches.
161: */
162: Set *
163: lookup(char *ks, Ordered *o)
164: {
165: Tuple *kt;
166: Attribute *a;
167: Set *s=0;
168:
169: /*
170: * parse the key
171: */
172: kt = new Tuple(ks, '*');
173: /*
174: * Using first attribute as the primary key, find the first
175: * attribute in the ordered list that matches.
176: */
177: a = (Attribute *)kt->first->search(o);
178: /*
179: * while the next attribute in the ordered list
180: * matches the search key, check the rest of the
181: * attributes for a match
182: */
183: while(a && a->compatible(kt->first)) {
184: if(a->tuple->match(kt)){
185: /*
186: * add each matching tuple to the
187: * set to be returned
188: */
189: if(s==(Set*)0)
190: s = new Set;
191: s->add(a->tuple);
192: }
193: a = (Attribute *)a->succ();
194: }
195: delete kt;
196: return s;
197: }
198:
199: /*
200: * Print the value of the first attribute matching type. Return 0 if a value
201: * is printed.
202: */
203: int
204: Tuple::printvalue(int fd, char *type)
205: {
206: Tuple *t;
207: Attribute *a;
208:
209: for(t=this; t; t=t->tail)
210: for(a=t->first; a; a=a->next)
211: if(a->printvalue(fd, type)==0)
212: return 0;
213: return -1;
214: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.