Drizzled Public API Documentation

my_hash.h

00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 /* Dynamic hashing of record with different key-length */
00021 
00022 #pragma once
00023 
00024 #include <drizzled/dynamic_array.h>
00025 
00026 namespace drizzled {
00027 
00028 typedef struct charset_info_st CHARSET_INFO;
00029 
00030 /*
00031   Overhead to store an element in hash
00032   Can be used to approximate memory consumption for a hash
00033  */
00034 #define HASH_OVERHEAD (sizeof(char*)*2)
00035 
00036 /* flags for hash_init */
00037 #define HASH_UNIQUE     1       /* hash_insert fails on duplicate key */
00038 
00039 typedef unsigned char *(*hash_get_key)(const unsigned char *,size_t*,bool);
00040 typedef void (*hash_free_key)(void *);
00041 
00042 struct HASH_LINK 
00043 {
00044   /* index to next key */
00045   uint32_t next;
00046   /* data for current entry */
00047   unsigned char *data;
00048 } ;
00049 
00050 struct HASH
00051 {
00052   // typedef std::vector<HASH_LINK> array_t;
00053   typedef DYNAMIC_ARRAY array_t;
00054   /* Length of key if const length */
00055   size_t key_offset,key_length;
00056   uint32_t blength;
00057   uint32_t records;
00058   uint32_t flags;
00059   /* Place for hash_keys */
00060   array_t array;
00061   hash_get_key get_key;
00062   hash_free_key free;
00063   const CHARSET_INFO *charset;
00064 };
00065 
00066 /* A search iterator state */
00067 typedef uint32_t HASH_SEARCH_STATE;
00068 
00069 bool
00070 _hash_init(HASH *hash,uint32_t growth_size, const CHARSET_INFO * const charset,
00071            uint32_t size, size_t key_offset, size_t key_length,
00072            hash_get_key get_key,
00073            hash_free_key free_element, uint32_t flags);
00074 #define hash_init(A,B,C,D,E,F,G,H) _hash_init(A,0,B,C,D,E,F,G,H)
00075 void hash_free(HASH *tree);
00076 unsigned char *hash_element(HASH *hash,uint32_t idx);
00077 unsigned char *hash_search(const HASH *info, const unsigned char *key,
00078                            size_t length);
00079 unsigned char *hash_first(const HASH *info, const unsigned char *key,
00080                           size_t length, HASH_SEARCH_STATE *state);
00081 unsigned char *hash_next(const HASH *info, const unsigned char *key,
00082                          size_t length, HASH_SEARCH_STATE *state);
00083 bool my_hash_insert(HASH *info,const unsigned char *data);
00084 bool hash_delete(HASH *hash,unsigned char *record);
00085 
00086 #define hash_inited(H) ((H)->array.buffer != 0)
00087 
00088 } /* namespace drizzled */
00089