summaryrefslogtreecommitdiff
path: root/sql/sp_cache.h
blob: 1ea71160a3ad6cc8057f2cbbb8a2170cdd144ea6 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*- C++ -*- */
/* Copyright (C) 2002 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef _SP_CACHE_H_
#define _SP_CACHE_H_

#ifdef USE_PRAGMA_INTERFACE
#pragma interface			/* gcc class implementation */
#endif

/*
  Stored procedures/functions cache. This is used as follows:
   * Each thread has its own cache.
   * Each sp_head object is put into its thread cache after creation and is
     removed from there on its deletion.
*/

class sp_head;
class sp_cache;

/* Initialize the SP caching once at startup */
void sp_cache_init();

/* Clear the cache *cp and set *cp to NULL */
void sp_cache_clear(sp_cache **cp);

/* Insert an SP into cache. If 'cp' points to NULL, it's set to a new cache */
void sp_cache_insert(sp_cache **cp, sp_head *sp);

/* Lookup an SP in cache */
sp_head *sp_cache_lookup(sp_cache **cp, sp_name *name);

/* 
  Remove an SP from cache, and also bump the Cversion number so all other 
  caches are invalidated. 
  Returns true if something was removed.
*/
bool sp_cache_remove(sp_cache **cp, sp_name *name);

/* Invalidate all existing SP caches by bumping Cversion number. */
void sp_cache_invalidate();


/*
 *
 * The cache class. Don't use this directly, use the C API above
 *
 */

class sp_cache
{
public:

  ulong version;

  sp_cache();

  ~sp_cache();

  void
  init();

  void
  cleanup();

  inline void
  insert(sp_head *sp)
  {
    my_hash_insert(&m_hashtable, (const byte *)sp);
  }

  inline sp_head *
  lookup(char *name, uint namelen)
  {
    return (sp_head *)hash_search(&m_hashtable, (const byte *)name, namelen);
  }

  inline bool
  remove(char *name, uint namelen)
  {
    sp_head *sp= lookup(name, namelen);

    if (sp)
    {
      hash_delete(&m_hashtable, (byte *)sp);
      return TRUE;
    }
    return FALSE;
  }

  inline void
  remove_all()
  {
    cleanup();
    init();
  }

private:

  HASH m_hashtable;

}; // class sp_cache

#endif /* _SP_CACHE_H_ */