3
Simple, fixed size hash table. Darn simple.
5
Uses a contiguous block of memory, so you can put it in a memory mapped file very easily.
8
/* Copyright 2009 10gen Inc.
10
* Licensed under the Apache License, Version 2.0 (the "License");
11
* you may not use this file except in compliance with the License.
12
* You may obtain a copy of the License at
14
* http://www.apache.org/licenses/LICENSE-2.0
16
* Unless required by applicable law or agreed to in writing, software
17
* distributed under the License is distributed on an "AS IS" BASIS,
18
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
* See the License for the specific language governing permissions and
20
* limitations under the License.
25
#include "mongo/pch.h"
27
#include "../db/dur.h"
35
int Key::hash() return > 0 always.
38
template <class Key,class Type>
39
class HashTable : boost::noncopyable {
54
int n; // number of hashtable buckets
58
Node *nodes = (Node *) _buf;
62
int _find(const Key& k, bool& found) {
68
int firstNonUsed = -1;
70
if ( !nodes(i).inUse() ) {
71
if ( firstNonUsed < 0 )
75
if ( nodes(i).hash == h && nodes(i).k == k ) {
77
out() << "warning: hashtable " << name << " long chain " << endl;
84
// shouldn't get here / defensive for infinite loops
85
out() << "error: hashtable " << name << " is full n:" << n << endl;
88
if( chain >= maxChain ) {
89
if ( firstNonUsed >= 0 )
91
out() << "error: hashtable " << name << " max chain reached:" << maxChain << endl;
98
/* buf must be all zeroes on initialization. */
99
HashTable(void* buf, int buflen, const char *_name) : name(_name) {
100
int m = sizeof(Node);
101
// out() << "hashtab init, buflen:" << buflen << " m:" << m << endl;
105
maxChain = (int) (n * 0.05);
107
//nodes = (Node *) buf;
109
if ( sizeof(Node) != 628 ) {
110
out() << "HashTable() " << _name << " sizeof(node):" << sizeof(Node) << " n:" << n << " sizeof(Key): " << sizeof(Key) << " sizeof(Type):" << sizeof(Type) << endl;
111
verify( sizeof(Node) == 628 );
116
Type* get(const Key& k) {
118
int i = _find(k, found);
120
return &nodes(i).value;
124
void kill(const Key& k) {
126
int i = _find(k, found);
127
if ( i >= 0 && found ) {
129
n = getDur().writing(n);
135
/** returns false if too full */
136
bool put(const Key& k, const Type& value) {
138
int i = _find(k, found);
141
Node* n = getDur().writing( &nodes(i) );
147
verify( n->hash == k.hash() );
153
typedef void (*IteratorCallback)( const Key& k , Type& v );
154
void iterAll( IteratorCallback callback ) {
155
for ( int i=0; i<n; i++ ) {
156
if ( nodes(i).inUse() ) {
157
callback( nodes(i).k , nodes(i).value );
162
// TODO: should probably use boost::bind for this, but didn't want to look at it
163
typedef void (*IteratorCallback2)( const Key& k , Type& v , void * extra );
164
void iterAll( IteratorCallback2 callback , void * extra ) {
165
for ( int i=0; i<n; i++ ) {
166
if ( nodes(i).inUse() ) {
167
callback( nodes(i).k , nodes(i).value , extra );