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
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include <map>
|
|
|
|
|
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,
|
2010-03-31 18:45:40 +02:00
|
|
|
class Type,
|
|
|
|
class PTR
|
2009-01-15 16:17:11 +01:00
|
|
|
>
|
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
|
|
|
};
|
|
|
|
PTR _buf;
|
2009-01-15 16:17:11 +01:00
|
|
|
int n;
|
2009-06-04 18:05:40 +02:00
|
|
|
int maxChain;
|
2009-01-15 16:17:11 +01:00
|
|
|
|
2010-03-31 18:45:40 +02:00
|
|
|
Node& nodes(int i) {
|
2010-03-31 21:49:28 +02:00
|
|
|
return *((Node*) _buf.at(i * sizeof(Node), sizeof(Node)));
|
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;
|
|
|
|
}
|
|
|
|
if( chain >= maxChain ) {
|
2010-01-19 16:58:35 +01:00
|
|
|
if ( firstNonUsed >= 0 )
|
|
|
|
return firstNonUsed;
|
2009-06-04 18:05:40 +02:00
|
|
|
out() << "error: hashtable " << name << " max chain n:" << n << 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-03-31 18:45:40 +02:00
|
|
|
HashTable(PTR 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-03-31 18:45:40 +02:00
|
|
|
_buf = buf;
|
|
|
|
//nodes = (Node *) buf;
|
2009-01-15 16:17:11 +01:00
|
|
|
|
|
|
|
assert( sizeof(Node) == 628 );
|
2009-01-15 17:26:38 +01:00
|
|
|
//out() << "HashTable() " << _name << " sizeof(node):" << sizeof(Node) << " n:" << n << endl;
|
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-03-31 18:45:40 +02:00
|
|
|
Node& n = nodes(i);
|
|
|
|
n.k.kill();
|
|
|
|
n.setUnused();
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
|
|
|
}
|
2009-10-19 22:53:58 +02:00
|
|
|
/*
|
2009-07-29 21:53:14 +02:00
|
|
|
void drop(const Key& k) {
|
|
|
|
bool found;
|
|
|
|
int i = _find(k, found);
|
|
|
|
if ( i >= 0 && found ) {
|
|
|
|
nodes[i].setUnused();
|
|
|
|
}
|
|
|
|
}
|
2009-10-19 22:53:58 +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-03-31 18:45:40 +02:00
|
|
|
Node& n = nodes(i);
|
2009-01-15 16:17:11 +01:00
|
|
|
if ( !found ) {
|
2010-03-31 18:45:40 +02:00
|
|
|
n.k = k;
|
|
|
|
n.hash = k.hash();
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-03-31 18:45:40 +02:00
|
|
|
assert( n.hash == k.hash() );
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
2010-03-31 18:45:40 +02:00
|
|
|
n.value = value;
|
2009-06-04 18:05:40 +02:00
|
|
|
return true;
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
2010-01-15 22:11:00 +01:00
|
|
|
|
|
|
|
typedef void (*IteratorCallback)( const Key& k , Type& v );
|
|
|
|
|
2010-01-21 19:40:18 +01:00
|
|
|
void iterAll( IteratorCallback callback ){
|
2010-01-15 22:11:00 +01:00
|
|
|
for ( int i=0; i<n; i++ ){
|
2010-03-31 18:45:40 +02:00
|
|
|
if ( ! nodes(i).inUse() )
|
2010-01-15 22:11:00 +01:00
|
|
|
continue;
|
2010-03-31 18:45:40 +02:00
|
|
|
callback( nodes(i).k , nodes(i).value );
|
2010-01-15 22:11:00 +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
|