1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * querycache.cpp --
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | *********************************************************************/
|
---|
25 |
|
---|
26 | #include "querycache.h"
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 |
|
---|
31 | resultcacheel::resultcacheel ()
|
---|
32 | {
|
---|
33 | accessnum = -1;
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 | querycache::querycache (int themaxcachesize)
|
---|
39 | {
|
---|
40 | if (themaxcachesize < 1) themaxcachesize = 1;
|
---|
41 |
|
---|
42 | resultcache = new resultcacheel[themaxcachesize];
|
---|
43 |
|
---|
44 | maxcachesize = themaxcachesize;
|
---|
45 | nextaccessnum = 1;
|
---|
46 | }
|
---|
47 |
|
---|
48 | querycache::~querycache ()
|
---|
49 | {
|
---|
50 | delete [] resultcache;
|
---|
51 | }
|
---|
52 |
|
---|
53 | // returns true if the query was found in the cache
|
---|
54 | // if the query was found then queryresults contains
|
---|
55 | // the results
|
---|
56 | bool querycache::find (const queryparamclass &queryparams,
|
---|
57 | queryresultsclass &queryresults)
|
---|
58 | {
|
---|
59 | int i;
|
---|
60 |
|
---|
61 | for (i=0; i < maxcachesize; i++)
|
---|
62 | {
|
---|
63 | if (resultcache[i].queryparameters == queryparams)
|
---|
64 | {
|
---|
65 | queryresults = resultcache[i].queryresults;
|
---|
66 | resultcache[i].accessnum = getnextaccessnum ();
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | void querycache::cache (const queryparamclass &queryparams,
|
---|
76 | const queryresultsclass &queryresults)
|
---|
77 | {
|
---|
78 | int i = getfreecachenum();
|
---|
79 | resultcache[i].queryparameters = queryparams;
|
---|
80 | resultcache[i].queryresults = queryresults;
|
---|
81 | resultcache[i].accessnum = getnextaccessnum ();
|
---|
82 | }
|
---|
83 |
|
---|
84 | int querycache::getnextaccessnum ()
|
---|
85 | {
|
---|
86 | return nextaccessnum++;
|
---|
87 | }
|
---|
88 |
|
---|
89 | int querycache::getfreecachenum ()
|
---|
90 | {
|
---|
91 | int i;
|
---|
92 | int minaccessnum = 0;
|
---|
93 | int minaccessi = 0;
|
---|
94 |
|
---|
95 | for (i=0; i < maxcachesize; i++)
|
---|
96 | {
|
---|
97 | if (resultcache[i].accessnum < minaccessnum) {
|
---|
98 | minaccessnum = resultcache[i].accessnum;
|
---|
99 | minaccessi = i;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | return minaccessi;
|
---|
104 | }
|
---|