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