Skip to content
Snippets Groups Projects
Commit 8450af1f authored by root's avatar root
Browse files

my hash map

parent 01798a5c
No related branches found
No related tags found
No related merge requests found
#pragma once
/**
* Copyright 2017 HashMap Development Team
*
* 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.
*
*/
#include "HashNode.h"
#include <cstddef>
#include "Key.h"
#include "Buffer.h"
#define TABLE_SIZE 50000
namespace RAMCloud
{
class HashMap
{
public:
HashMap() : table()
{}
~HashMap()
{
// destroy all buckets one by one
for (size_t i = 0; i < TABLE_SIZE; ++i)
{
HashNode *entry = table[i];
while (entry != NULL)
{
HashNode *prev = entry;
entry = entry->getNext();
delete prev;
}
table[i] = NULL;
}
}
bool get(Key &key, Buffer &value)
{
unsigned long hashValue = hashFunc(key);
// HashNode *entry = table[hashValue];
HashNode *entry = table[hashValue];
while (entry != NULL)
{
if (entry->getKey() == key)
{
void **ptr;
entry->getValue().peek(0,ptr);
value.appendCopy(*ptr, entry->getValue().size()); // verify if should be dereferenced
return true;
}
entry = entry->getNext();
}
return false;
}
void put(Key &key, const Buffer &value)
{
unsigned long hashValue = hashFunc(key);
HashNode *prev = NULL;
HashNode *entry = table[hashValue];
while (entry != NULL && entry->getKey() != key)
{
prev = entry;
entry = entry->getNext();
}
if (entry == NULL)
{
entry = new HashNode(key, value);
if (prev == NULL)
{
// insert as first bucket
table[hashValue] = entry;
}
else
{
prev->setNext(entry);
}
}
else
{
// just update the value
entry->setValue(value);
}
}
void remove(Key &key)
{
unsigned long hashValue = hashFunc(key);
HashNode *prev = NULL;
HashNode *entry = table[hashValue];
while (entry != NULL && entry->getKey() != key)
{
prev = entry;
entry = entry->getNext();
}
if (entry == NULL)
{
// key not found
return;
}
else
{
if (prev == NULL)
{
// remove first bucket of the list
table[hashValue] = entry->getNext();
}
else
{
prev->setNext(entry->getNext());
}
delete entry;
}
}
private:
HashMap(const HashMap &other);
const HashMap &operator=(const HashMap &other);
// hash table
HashNode *table[TABLE_SIZE];
unsigned long hashFunc(Key &k) const
{
return k.getHash();
}
};
}
#pragma once
/**
* Copyright 2017 HashMap Development Team
*
* 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.
*
*/
#include <cstddef>
#include "Key.h"
#include "Buffer.h"
namespace RAMCloud
{
// Hash node class template
// template <typename Key, typename Buffer>
class HashNode
{
private:
// key-value pair
// next bucket with the same key
public:
Key _key;
Buffer _value;
HashNode *_next;
// disallow copy and assignment
HashNode(const HashNode &);
HashNode &operator=(const HashNode &);
HashNode(const Key &key, Buffer &value) :
_key(
key.getTableId(),
key.getStringKey(),
key.getStringKeyLength()
),
_value(),
_next(NULL)
{
void **ptr;
value.peek(0,ptr);
_value.appendCopy(*ptr, value.size());
}
Key getKey() const
{
return _key;
}
Buffer getValue() const
{
return _value;
}
void setValue(Buffer& value)
{
void **ptr;
value.peek(0, ptr);
_value.appendCopy(*ptr, value.size());
// _value = value;
}
HashNode *getNext() const
{
return _next;
}
void setNext(HashNode *next)
{
_next = next;
}
};
}
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "WallTime.h" #include "WallTime.h"
#include "MasterService.h" #include "MasterService.h"
#include <emmintrin.h> // _mm_clflush and _mm_mfence #include <emmintrin.h> // _mm_clflush and _mm_mfence
#include "HashMap.h"
namespace RAMCloud { namespace RAMCloud {
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include "MasterTableMetadata.h" #include "MasterTableMetadata.h"
#include "UnackedRpcResults.h" #include "UnackedRpcResults.h"
#include "LockTable.h" #include "LockTable.h"
#include "HashMap.h"
namespace RAMCloud { namespace RAMCloud {
...@@ -416,6 +417,14 @@ class ObjectManager : public LogEntryHandlers, ...@@ -416,6 +417,14 @@ class ObjectManager : public LogEntryHandlers,
*/ */
int tombstoneProtectorCount; int tombstoneProtectorCount;
/**
* This hash table maps the key to a pointer to the value in DRAM.
*
*/
HashMap cacheTable;
friend class CleanerCompactionBenchmark; friend class CleanerCompactionBenchmark;
friend class ObjectManagerBenchmark; friend class ObjectManagerBenchmark;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment