mariadb/sql/hash_filo.h
unknown 33fc0d53b5 Merge with 4.0
BitKeeper/etc/gone:
  auto-union
BitKeeper/etc/ignore:
  auto-union
BitKeeper/etc/logging_ok:
  auto-union
BitKeeper/deleted/.del-.my_sys.h.swp~f6a4a7f8dae03f18:
  Auto merged
BitKeeper/etc/config:
  Auto merged
acconfig.h:
  Auto merged
acinclude.m4:
  Auto merged
Docs/manual.texi:
  Auto merged
client/mysqlcheck.c:
  Auto merged
client/mysqlshow.c:
  Auto merged
include/myisam.h:
  Auto merged
include/violite.h:
  Auto merged
isam/pack_isam.c:
  Auto merged
libmysql/libmysql.c:
  Auto merged
libmysqld/lib_sql.cc:
  Auto merged
myisam/Makefile.am:
  Auto merged
myisam/ft_nlq_search.c:
  Auto merged
myisam/mi_open.c:
  Auto merged
myisam/mi_write.c:
  Auto merged
myisam/sort.c:
  Auto merged
mysql-test/mysql-test-run.sh:
  Auto merged
mysql-test/r/fulltext.result:
  Auto merged
mysql-test/r/func_test.result:
  Auto merged
mysql-test/r/isam.result:
  Auto merged
mysql-test/r/rpl_replicate_do.result:
  Auto merged
mysql-test/r/variables.result:
  Auto merged
mysql-test/t/func_test.test:
  Auto merged
mysql-test/t/myisam.test:
  Auto merged
mysql-test/t/rpl_rotate_logs.test:
  Auto merged
mysql-test/t/variables.test:
  Auto merged
mysys/hash.c:
  Auto merged
mysys/tree.c:
  Auto merged
scripts/Makefile.am:
  Auto merged
sql/Makefile.am:
  Auto merged
sql/filesort.cc:
  Auto merged
sql/gen_lex_hash.cc:
  Auto merged
sql/ha_berkeley.cc:
  Auto merged
sql/ha_innodb.cc:
  Auto merged
sql/handler.cc:
  Auto merged
sql/handler.h:
  Auto merged
sql/hash_filo.h:
  Auto merged
sql/hostname.cc:
  Auto merged
sql/item_func.cc:
  Auto merged
sql/item_func.h:
  Auto merged
sql/item_timefunc.cc:
  Auto merged
sql/lex.h:
  Auto merged
sql/lock.cc:
  Auto merged
sql/log.cc:
  Auto merged
sql/log_event.h:
  Auto merged
sql/mini_client.cc:
  Auto merged
sql/opt_range.cc:
  Auto merged
sql/opt_sum.cc:
  Auto merged
sql/repl_failsafe.cc:
  Auto merged
sql/set_var.h:
  Auto merged
sql/slave.h:
  Auto merged
sql/sql_acl.cc:
  Auto merged
sql/sql_analyse.cc:
  Auto merged
sql/sql_cache.cc:
  Auto merged
sql/sql_insert.cc:
  Auto merged
sql/sql_load.cc:
  Auto merged
sql/sql_rename.cc:
  Auto merged
sql/sql_repl.cc:
  Auto merged
sql/sql_show.cc:
  Auto merged
sql/sql_table.cc:
  Auto merged
sql/sql_udf.cc:
  Auto merged
sql/share/czech/errmsg.txt:
  Auto merged
sql/share/danish/errmsg.txt:
  Auto merged
sql/share/dutch/errmsg.txt:
  Auto merged
sql/share/english/errmsg.txt:
  Auto merged
sql/share/estonian/errmsg.txt:
  Auto merged
sql/share/french/errmsg.txt:
  Auto merged
sql/share/german/errmsg.txt:
  Auto merged
sql/share/greek/errmsg.txt:
  Auto merged
sql/share/hungarian/errmsg.txt:
  Auto merged
sql/share/italian/errmsg.txt:
  Auto merged
sql/share/japanese/errmsg.txt:
  Auto merged
sql/share/korean/errmsg.txt:
  Auto merged
sql/share/norwegian/errmsg.txt:
  Auto merged
sql/table.h:
  Auto merged
sql/unireg.cc:
  Auto merged
sql-bench/server-cfg.sh:
  Auto merged
sql/share/norwegian-ny/errmsg.txt:
  Auto merged
sql/share/polish/errmsg.txt:
  Auto merged
sql/share/portuguese/errmsg.txt:
  Auto merged
sql/share/romanian/errmsg.txt:
  Auto merged
sql/share/russian/errmsg.txt:
  Auto merged
sql/share/slovak/errmsg.txt:
  Auto merged
sql/share/spanish/errmsg.txt:
  Auto merged
sql/share/swedish/errmsg.txt:
  Auto merged
sql/share/ukrainian/errmsg.txt:
  Auto merged
sql/ha_myisam.cc:
  Merge with 4.0
  Removed some commented code
sql/sql_db.cc:
  Merge with 4.0
  Optimized the logging of the drop db call a bit
sql/sql_update.cc:
  Added comment
2002-11-21 15:56:48 +02:00

131 lines
3.3 KiB
C++

/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult 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 */
/*
** A class for static sized hash tables where old entries are deleted in
** first-in-last-out to usage.
*/
#ifndef HASH_FILO_H
#define HASH_FILO_H
#ifdef __GNUC__
#pragma interface /* gcc class interface */
#endif
class hash_filo_element
{
hash_filo_element *next_used,*prev_used;
public:
hash_filo_element() {}
friend class hash_filo;
};
class hash_filo
{
const uint size, key_offset, key_length;
const hash_get_key get_key;
hash_free_key free_element;
bool init;
hash_filo_element *first_link,*last_link;
public:
pthread_mutex_t lock;
HASH cache;
hash_filo(uint size_arg, uint key_offset_arg , uint key_length_arg,
hash_get_key get_key_arg, hash_free_key free_element_arg)
:size(size_arg), key_offset(key_offset_arg), key_length(key_length_arg),
get_key(get_key_arg), free_element(free_element_arg),init(0)
{
bzero((char*) &cache,sizeof(cache));
}
~hash_filo()
{
if (init)
{
if (cache.array.buffer) /* Avoid problems with thread library */
(void) hash_free(&cache);
pthread_mutex_destroy(&lock);
}
}
void clear(bool locked=0)
{
if (!init)
{
init=1;
(void) pthread_mutex_init(&lock,MY_MUTEX_INIT_FAST);
}
if (!locked)
(void) pthread_mutex_lock(&lock);
(void) hash_free(&cache);
(void) hash_init(&cache,system_charset_info,size,key_offset,
key_length, get_key, free_element,0);
if (!locked)
(void) pthread_mutex_unlock(&lock);
first_link=last_link=0;
}
hash_filo_element *search(gptr key,uint length)
{
hash_filo_element *entry=(hash_filo_element*)
hash_search(&cache,(byte*) key,length);
if (entry)
{ // Found; link it first
if (entry != first_link)
{ // Relink used-chain
if (entry == last_link)
last_link=entry->prev_used;
else
{
entry->next_used->prev_used = entry->prev_used;
entry->prev_used->next_used = entry->next_used;
}
if ((entry->next_used= first_link))
first_link->prev_used=entry;
first_link=entry;
}
}
return entry;
}
my_bool add(hash_filo_element *entry)
{
if (cache.records == size)
{
hash_filo_element *tmp=last_link;
last_link=last_link->prev_used;
hash_delete(&cache,(byte*) tmp);
}
if (hash_insert(&cache,(byte*) entry))
{
if (free_element)
(*free_element)(entry); // This should never happen
return 1;
}
if ((entry->next_used=first_link))
first_link->prev_used=entry;
else
last_link=entry;
first_link=entry;
return 0;
}
};
#endif