~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/hash_filo.h

MergedĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2003, 2005 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
 
 
16
 
 
17
 
/*
18
 
** A class for static sized hash tables where old entries are deleted in
19
 
** first-in-last-out to usage.
20
 
*/
21
 
 
22
 
#ifndef  HASH_FILO_H
23
 
#define  HASH_FILO_H
24
 
 
25
 
#ifdef USE_PRAGMA_INTERFACE
26
 
#pragma interface                       /* gcc class interface */
27
 
#endif
28
 
 
29
 
class hash_filo_element
30
 
{
31
 
  hash_filo_element *next_used,*prev_used;
32
 
 public:
33
 
  hash_filo_element() {}
34
 
  friend class hash_filo;
35
 
};
36
 
 
37
 
 
38
 
class hash_filo
39
 
{
40
 
  const uint size, key_offset, key_length;
41
 
  const hash_get_key get_key;
42
 
  hash_free_key free_element;
43
 
  bool init;
44
 
  CHARSET_INFO *hash_charset;
45
 
 
46
 
  hash_filo_element *first_link,*last_link;
47
 
public:
48
 
  pthread_mutex_t lock;
49
 
  HASH cache;
50
 
 
51
 
  hash_filo(uint size_arg, uint key_offset_arg , uint key_length_arg,
52
 
            hash_get_key get_key_arg, hash_free_key free_element_arg,
53
 
            CHARSET_INFO *hash_charset_arg)
54
 
    :size(size_arg), key_offset(key_offset_arg), key_length(key_length_arg),
55
 
    get_key(get_key_arg), free_element(free_element_arg),init(0),
56
 
    hash_charset(hash_charset_arg)
57
 
  {
58
 
    bzero((char*) &cache,sizeof(cache));
59
 
  }
60
 
 
61
 
  ~hash_filo()
62
 
  {
63
 
    if (init)
64
 
    {
65
 
      if (cache.array.buffer)   /* Avoid problems with thread library */
66
 
        (void) hash_free(&cache);
67
 
      pthread_mutex_destroy(&lock);
68
 
    }
69
 
  }
70
 
  void clear(bool locked=0)
71
 
  {
72
 
    if (!init)
73
 
    {
74
 
      init=1;
75
 
      (void) pthread_mutex_init(&lock,MY_MUTEX_INIT_FAST);
76
 
    }
77
 
    if (!locked)
78
 
      (void) pthread_mutex_lock(&lock);
79
 
    (void) hash_free(&cache);
80
 
    (void) hash_init(&cache,hash_charset,size,key_offset, 
81
 
                     key_length, get_key, free_element,0);
82
 
    if (!locked)
83
 
      (void) pthread_mutex_unlock(&lock);
84
 
    first_link=last_link=0;
85
 
  }
86
 
 
87
 
  hash_filo_element *search(uchar* key, size_t length)
88
 
  {
89
 
    hash_filo_element *entry=(hash_filo_element*)
90
 
      hash_search(&cache,(uchar*) key,length);
91
 
    if (entry)
92
 
    {                                           // Found; link it first
93
 
      if (entry != first_link)
94
 
      {                                         // Relink used-chain
95
 
        if (entry == last_link)
96
 
          last_link=entry->prev_used;
97
 
        else
98
 
        {
99
 
          entry->next_used->prev_used = entry->prev_used;
100
 
          entry->prev_used->next_used = entry->next_used;
101
 
        }
102
 
        if ((entry->next_used= first_link))
103
 
          first_link->prev_used=entry;
104
 
        first_link=entry;
105
 
      }
106
 
    }
107
 
    return entry;
108
 
  }
109
 
 
110
 
  my_bool add(hash_filo_element *entry)
111
 
  {
112
 
    if (cache.records == size)
113
 
    {
114
 
      hash_filo_element *tmp=last_link;
115
 
      last_link=last_link->prev_used;
116
 
      hash_delete(&cache,(uchar*) tmp);
117
 
    }
118
 
    if (my_hash_insert(&cache,(uchar*) entry))
119
 
    {
120
 
      if (free_element)
121
 
        (*free_element)(entry);         // This should never happen
122
 
      return 1;
123
 
    }
124
 
    if ((entry->next_used=first_link))
125
 
      first_link->prev_used=entry;
126
 
    else
127
 
      last_link=entry;
128
 
    first_link=entry;
129
 
    return 0;
130
 
  }
131
 
};
132
 
 
133
 
#endif