0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/util/hashtab.h

180 lines
5.2 KiB
C
Raw Normal View History

2008-06-06 15:43:15 +02:00
/* hashtab.h
Simple, fixed size hash table. Darn simple.
Uses a contiguous block of memory, so you can put it in a memory mapped file very easily.
*/
/* Copyright 2009 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2008-12-29 02:28:49 +01:00
#pragma once
2008-06-06 15:43:15 +02:00
2010-04-27 21:27:52 +02:00
#include "../pch.h"
2008-06-06 15:43:15 +02:00
#include <map>
2010-09-27 18:35:22 +02:00
#include "../db/dur.h"
2008-06-06 15:43:15 +02:00
2009-01-14 23:09:51 +01:00
namespace mongo {
#pragma pack(1)
2008-06-06 15:43:15 +02:00
/* you should define:
int Key::hash() return > 0 always.
*/
template <
class Key,
2011-01-04 06:40:41 +01:00
class Type
>
class HashTable : boost::noncopyable {
public:
const char *name;
struct Node {
int hash;
Key k;
Type value;
bool inUse() {
return hash != 0;
2008-12-29 02:28:49 +01:00
}
void setUnused() {
hash = 0;
2008-12-29 02:28:49 +01:00
}
2010-03-31 18:45:40 +02:00
};
2010-11-27 21:25:08 +01:00
void* _buf;
2011-03-02 07:52:35 +01:00
int n; // number of hashtable buckets
int maxChain;
2011-01-04 06:40:41 +01:00
Node& nodes(int i) {
2010-11-27 21:25:08 +01:00
Node *nodes = (Node *) _buf;
2011-01-04 06:40:41 +01:00
return nodes[i];
2010-09-30 23:27:40 +02:00
}
2010-03-31 18:45:40 +02:00
int _find(const Key& k, bool& found) {
found = false;
int h = k.hash();
int i = h % n;
int start = i;
int chain = 0;
int firstNonUsed = -1;
while ( 1 ) {
2010-03-31 18:45:40 +02:00
if ( !nodes(i).inUse() ) {
if ( firstNonUsed < 0 )
firstNonUsed = i;
}
2010-03-31 18:45:40 +02:00
if ( nodes(i).hash == h && nodes(i).k == k ) {
if ( chain >= 200 )
out() << "warning: hashtable " << name << " long chain " << endl;
found = true;
return i;
}
chain++;
i = (i+1) % n;
if ( i == start ) {
// shouldn't get here / defensive for infinite loops
out() << "error: hashtable " << name << " is full n:" << n << endl;
return -1;
}
2011-01-04 06:40:41 +01:00
if( chain >= maxChain ) {
if ( firstNonUsed >= 0 )
return firstNonUsed;
out() << "error: hashtable " << name << " max chain reached:" << maxChain << endl;
return -1;
}
2008-12-29 02:28:49 +01:00
}
}
public:
/* buf must be all zeroes on initialization. */
2010-11-27 21:25:08 +01:00
HashTable(void* buf, int buflen, const char *_name) : name(_name) {
int m = sizeof(Node);
// out() << "hashtab init, buflen:" << buflen << " m:" << m << endl;
n = buflen / m;
if ( (n & 1) == 0 )
n--;
maxChain = (int) (n * 0.05);
2010-09-30 23:27:40 +02:00
_buf = buf;
2010-03-31 18:45:40 +02:00
//nodes = (Node *) buf;
2011-01-04 06:40:41 +01:00
if ( sizeof(Node) != 628 ) {
2010-05-26 19:22:31 +02:00
out() << "HashTable() " << _name << " sizeof(node):" << sizeof(Node) << " n:" << n << " sizeof(Key): " << sizeof(Key) << " sizeof(Type):" << sizeof(Type) << endl;
2010-05-26 17:35:16 +02:00
assert( sizeof(Node) == 628 );
}
2008-12-29 02:28:49 +01:00
}
Type* get(const Key& k) {
bool found;
int i = _find(k, found);
if ( found )
2010-03-31 18:45:40 +02:00
return &nodes(i).value;
return 0;
2008-12-29 02:28:49 +01:00
}
void kill(const Key& k) {
bool found;
int i = _find(k, found);
if ( i >= 0 && found ) {
2010-09-27 18:35:22 +02:00
Node* n = &nodes(i);
n = getDur().writing(n);
2010-09-27 18:35:22 +02:00
n->k.kill();
n->setUnused();
}
}
2010-09-27 18:35:22 +02:00
/** returns false if too full */
bool put(const Key& k, const Type& value) {
bool found;
int i = _find(k, found);
if ( i < 0 )
return false;
Node* n = getDur().writing( &nodes(i) );
if ( !found ) {
2010-09-27 18:35:22 +02:00
n->k = k;
n->hash = k.hash();
}
else {
2010-09-27 18:35:22 +02:00
assert( n->hash == k.hash() );
}
2010-09-27 18:35:22 +02:00
n->value = value;
return true;
2008-12-29 02:28:49 +01:00
}
2011-01-04 06:40:41 +01:00
2010-01-15 22:11:00 +01:00
typedef void (*IteratorCallback)( const Key& k , Type& v );
2011-01-04 06:40:41 +01:00
void iterAll( IteratorCallback callback ) {
for ( int i=0; i<n; i++ ) {
2011-03-17 20:53:34 +01:00
if ( nodes(i).inUse() ) {
callback( nodes(i).k , nodes(i).value );
}
2010-01-15 22:11:00 +01:00
}
}
// TODO: should probably use boost::bind for this, but didn't want to look at it
typedef void (*IteratorCallback2)( const Key& k , Type& v , void * extra );
2011-01-04 06:40:41 +01:00
void iterAll( IteratorCallback2 callback , void * extra ) {
for ( int i=0; i<n; i++ ) {
2011-03-17 20:53:34 +01:00
if ( nodes(i).inUse() ) {
callback( nodes(i).k , nodes(i).value , extra );
}
}
}
2011-01-04 06:40:41 +01:00
};
2008-06-06 15:43:15 +02:00
#pragma pack()
2009-01-14 23:09:51 +01:00
} // namespace mongo