-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.c
102 lines (89 loc) · 2.34 KB
/
query.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// query.c ... query scan functions
// part of signature indexed files
// Manage creating and using Query objects
// Written by John Shepherd, March 2019
#include "defs.h"
#include "query.h"
#include "reln.h"
#include "tuple.h"
#include "bits.h"
#include "tsig.h"
#include "psig.h"
#include "bsig.h"
#include <stdbool.h>
// check whether a query is valid for a relation
// e.g. same number of attributes
int checkQuery(Reln r, char *q)
{
if (*q == '\0') return 0;
char *c;
int nattr = 1;
for (c = q; *c != '\0'; c++)
if (*c == ',') nattr++;
return (nattr == nAttrs(r));
}
// take a query string (e.g. "1234,?,abc,?")
// set up a QueryRep object for the scan
Query startQuery(Reln r, char *q, char sigs)
{
Query new = malloc(sizeof(QueryRep));
assert(new != NULL);
if (!checkQuery(r,q)) return NULL;
new->rel = r;
new->qstring = q;
new->nsigs = new->nsigpages = 0;
new->ntuples = new->ntuppages = new->nfalse = 0;
new->pages = newBits(nPages(r));
switch (sigs) {
case 't': findPagesUsingTupSigs(new); break;
case 'p': findPagesUsingPageSigs(new); break;
case 'b': findPagesUsingBitSlices(new); break;
default: setAllBits(new->pages); break;
}
new->curpage = 0;
return new;
}
// scan through selected pages (q->pages)
// search for matching tuples and show each
// accumulate query stats
void scanAndDisplayMatchingTuples(Query q)
{
assert(q != NULL);
//TODO
Reln r = q->rel;
for (int pid = 0; pid < nPages(r); pid++) {
if (!bitIsSet(q->pages, pid)) {
continue;
}
Bool flag = FALSE;
Page p = getPage(dataFile(r), pid);
for (int tid = 0; tid < pageNitems(p); tid++) {
Tuple t = getTupleFromPage(r, p, tid);
q->ntuples++;
if (tupleMatch(r, q->qstring, t)) {
showTuple(r, t);
flag = TRUE;
}
}
free(p);
if (!flag) {
q->nfalse++;
}
q->ntuppages++;
}
}
// print statistics on query
void queryStats(Query q)
{
printf("# sig pages read: %d\n", q->nsigpages);
printf("# signatures read: %d\n", q->nsigs);
printf("# data pages read: %d\n", q->ntuppages);
printf("# tuples examined: %d\n", q->ntuples);
printf("# false match pages: %d\n", q->nfalse);
}
// clean up a QueryRep object and associated data
void closeQuery(Query q)
{
free(q->pages);
free(q);
}