1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * querycache.cpp --
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * PUT COPYRIGHT NOTICE HERE
|
---|
7 | *
|
---|
8 | * $Id: querycache.cpp 114 1999-01-19 01:38:20Z rjmcnab $
|
---|
9 | *
|
---|
10 | *********************************************************************/
|
---|
11 |
|
---|
12 | /*
|
---|
13 | $Log$
|
---|
14 | Revision 1.3 1999/01/19 01:38:17 rjmcnab
|
---|
15 |
|
---|
16 | Made the source more portable.
|
---|
17 |
|
---|
18 | Revision 1.2 1999/01/12 01:51:02 rjmcnab
|
---|
19 |
|
---|
20 | Standard header.
|
---|
21 |
|
---|
22 | Revision 1.1 1999/01/08 09:02:18 rjmcnab
|
---|
23 |
|
---|
24 | Moved from src/library.
|
---|
25 |
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | #include "querycache.h"
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 | resultcacheel::resultcacheel ()
|
---|
35 | {
|
---|
36 | accessnum = -1;
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 | querycache::querycache (int themaxcachesize)
|
---|
42 | {
|
---|
43 | if (themaxcachesize < 1) themaxcachesize = 1;
|
---|
44 |
|
---|
45 | resultcache = new resultcacheel[themaxcachesize];
|
---|
46 |
|
---|
47 | maxcachesize = themaxcachesize;
|
---|
48 | nextaccessnum = 1;
|
---|
49 | }
|
---|
50 |
|
---|
51 | querycache::~querycache ()
|
---|
52 | {
|
---|
53 | delete [] resultcache;
|
---|
54 | }
|
---|
55 |
|
---|
56 | // returns true if the query was found in the cache
|
---|
57 | // if the query was found then queryresults contains
|
---|
58 | // the results
|
---|
59 | bool querycache::find (const queryparamclass &queryparams,
|
---|
60 | queryresultsclass &queryresults)
|
---|
61 | {
|
---|
62 | int i;
|
---|
63 |
|
---|
64 | for (i=0; i < maxcachesize; i++)
|
---|
65 | {
|
---|
66 | if (resultcache[i].queryparameters == queryparams)
|
---|
67 | {
|
---|
68 | queryresults = resultcache[i].queryresults;
|
---|
69 | resultcache[i].accessnum = getnextaccessnum ();
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | void querycache::cache (const queryparamclass &queryparams,
|
---|
79 | const queryresultsclass &queryresults)
|
---|
80 | {
|
---|
81 | int i = getfreecachenum();
|
---|
82 | resultcache[i].queryparameters = queryparams;
|
---|
83 | resultcache[i].queryresults = queryresults;
|
---|
84 | resultcache[i].accessnum = getnextaccessnum ();
|
---|
85 | }
|
---|
86 |
|
---|
87 | int querycache::getnextaccessnum ()
|
---|
88 | {
|
---|
89 | return nextaccessnum++;
|
---|
90 | }
|
---|
91 |
|
---|
92 | int querycache::getfreecachenum ()
|
---|
93 | {
|
---|
94 | int i;
|
---|
95 | int minaccessnum = 0;
|
---|
96 | int minaccessi = 0;
|
---|
97 |
|
---|
98 | for (i=0; i < maxcachesize; i++)
|
---|
99 | {
|
---|
100 | if (resultcache[i].accessnum < minaccessnum) {
|
---|
101 | minaccessnum = resultcache[i].accessnum;
|
---|
102 | minaccessi = i;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | return minaccessi;
|
---|
107 | }
|
---|
108 |
|
---|