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.
|
|
|
|
*/
|
|
|
|
|
2009-10-27 20:58:27 +01:00
|
|
|
/* 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 {
|
|
|
|
|
2009-02-02 04:21:32 +01:00
|
|
|
#pragma pack(1)
|
2008-06-06 15:43:15 +02:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
/* you should define:
|
|
|
|
|
|
|
|
int Key::hash() return > 0 always.
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <
|
|
|
|
class Key,
|
2011-01-04 06:40:41 +01:00
|
|
|
class Type
|
|
|
|
>
|
2010-01-15 22:05:14 +01:00
|
|
|
class HashTable : boost::noncopyable {
|
2009-01-15 16:17:11 +01:00
|
|
|
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
|
|
|
}
|
2009-01-15 16:17:11 +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
|
2009-06-04 18:05:40 +02:00
|
|
|
int maxChain;
|
2009-01-15 16:17:11 +01:00
|
|
|
|
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
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
int _find(const Key& k, bool& found) {
|
|
|
|
found = false;
|
|
|
|
int h = k.hash();
|
|
|
|
int i = h % n;
|
|
|
|
int start = i;
|
|
|
|
int chain = 0;
|
2010-01-19 16:58:35 +01:00
|
|
|
int firstNonUsed = -1;
|
2009-01-15 16:17:11 +01:00
|
|
|
while ( 1 ) {
|
2010-03-31 18:45:40 +02:00
|
|
|
if ( !nodes(i).inUse() ) {
|
2010-01-19 16:58:35 +01:00
|
|
|
if ( firstNonUsed < 0 )
|
|
|
|
firstNonUsed = i;
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
2010-01-19 16:58:35 +01:00
|
|
|
|
2010-03-31 18:45:40 +02:00
|
|
|
if ( nodes(i).hash == h && nodes(i).k == k ) {
|
2010-01-19 16:58:35 +01:00
|
|
|
if ( chain >= 200 )
|
|
|
|
out() << "warning: hashtable " << name << " long chain " << endl;
|
2009-01-15 16:17:11 +01:00
|
|
|
found = true;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
chain++;
|
|
|
|
i = (i+1) % n;
|
|
|
|
if ( i == start ) {
|
2009-06-04 18:05:40 +02:00
|
|
|
// 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 ) {
|
2010-01-19 16:58:35 +01:00
|
|
|
if ( firstNonUsed >= 0 )
|
|
|
|
return firstNonUsed;
|
2010-10-13 22:11:15 +02:00
|
|
|
out() << "error: hashtable " << name << " max chain reached:" << maxChain << endl;
|
2009-01-15 16:17:11 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-15 16:17:11 +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) {
|
2009-01-15 16:17:11 +01:00
|
|
|
int m = sizeof(Node);
|
2009-01-15 17:26:38 +01:00
|
|
|
// out() << "hashtab init, buflen:" << buflen << " m:" << m << endl;
|
2009-01-15 16:17:11 +01:00
|
|
|
n = buflen / m;
|
|
|
|
if ( (n & 1) == 0 )
|
|
|
|
n--;
|
2009-06-04 18:05:40 +02:00
|
|
|
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;
|
2009-01-15 16:17:11 +01:00
|
|
|
|
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
|
|
|
}
|
2009-01-15 16:17:11 +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;
|
2009-01-15 16:17:11 +01:00
|
|
|
return 0;
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
2009-01-15 16:17:11 +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);
|
2010-12-09 20:44:08 +01:00
|
|
|
n = getDur().writing(n);
|
2010-09-27 18:35:22 +02:00
|
|
|
n->k.kill();
|
|
|
|
n->setUnused();
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
|
|
|
}
|
2010-09-27 18:35:22 +02:00
|
|
|
|
2009-06-04 18:05:40 +02:00
|
|
|
/** returns false if too full */
|
|
|
|
bool put(const Key& k, const Type& value) {
|
2009-01-15 16:17:11 +01:00
|
|
|
bool found;
|
|
|
|
int i = _find(k, found);
|
|
|
|
if ( i < 0 )
|
2009-06-04 18:05:40 +02:00
|
|
|
return false;
|
2010-12-09 20:44:08 +01:00
|
|
|
Node* n = getDur().writing( &nodes(i) );
|
2009-01-15 16:17:11 +01:00
|
|
|
if ( !found ) {
|
2010-09-27 18:35:22 +02:00
|
|
|
n->k = k;
|
|
|
|
n->hash = k.hash();
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-09-27 18:35:22 +02:00
|
|
|
assert( n->hash == k.hash() );
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
2010-09-27 18:35:22 +02:00
|
|
|
n->value = value;
|
2009-06-04 18:05:40 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2010-04-26 05:15:42 +02: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 );
|
|
|
|
}
|
2010-04-26 05:15:42 +02:00
|
|
|
}
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
};
|
2008-06-06 15:43:15 +02:00
|
|
|
|
2009-02-02 04:21:32 +01:00
|
|
|
#pragma pack()
|
2009-01-14 23:09:51 +01:00
|
|
|
|
|
|
|
} // namespace mongo
|