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