Deleted old version
This commit is contained in:
parent
28998b5c14
commit
5751d381af
|
@ -1,447 +0,0 @@
|
|||
#include "hash.h"
|
||||
|
||||
HashTable::HashTable()
|
||||
{
|
||||
//necessary to do this!
|
||||
memset(m_Table, 0, sizeof(m_Table));
|
||||
}
|
||||
|
||||
HashTable::~HashTable()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
//Returns an iterator
|
||||
HashTable::iterator HashTable::Enumerate()
|
||||
{
|
||||
return HashTable::iterator(m_Table, HT_SIZE);
|
||||
}
|
||||
|
||||
//Retrieves a value from the hash table
|
||||
HashTable::htNode *HashTable::Retrieve(const char *key)
|
||||
{
|
||||
return _FindNode(key);
|
||||
}
|
||||
|
||||
//Adds an entry into the hash table with current time
|
||||
void HashTable::Store(const char *key, const char *value, bool temporary)
|
||||
{
|
||||
time_t stamp = 0;
|
||||
|
||||
if (temporary)
|
||||
stamp = time(NULL);
|
||||
|
||||
_Insert(key, value, stamp);
|
||||
}
|
||||
|
||||
//Adds an entry into the hash table with preset time
|
||||
void HashTable::Store(const char *key, const char *value, time_t stamp)
|
||||
{
|
||||
_Insert(key, value, stamp);
|
||||
}
|
||||
|
||||
//Erases a key
|
||||
void HashTable::EraseKey(const char *key)
|
||||
{
|
||||
HashTable::htNodeSet *set = _FindNodeSet(key);
|
||||
HashTable::htNode *node = _FindNode(key, false);
|
||||
|
||||
if (set && node)
|
||||
_Unlink(set, node);
|
||||
}
|
||||
|
||||
//Deletes all keys between the two times.
|
||||
// 0 specifies "match all"
|
||||
//All specifies whether permanent keys (time_t = 0) are erased
|
||||
size_t HashTable::Prune(time_t begin, time_t end, bool all)
|
||||
{
|
||||
HashTable::htNode *node, *temp;
|
||||
size_t total = 0;
|
||||
bool fire;
|
||||
|
||||
for (uint32_t i=0; i<HT_SIZE; i++)
|
||||
{
|
||||
if (m_Table[i])
|
||||
{
|
||||
node = m_Table[i]->head;
|
||||
|
||||
while (node != NULL)
|
||||
{
|
||||
temp = node->next;
|
||||
fire = false;
|
||||
if (!begin && !end)
|
||||
{
|
||||
fire = true;
|
||||
} else if (!begin && end) {
|
||||
if (node->stamp <= end)
|
||||
fire = true;
|
||||
} else if (begin && !end) {
|
||||
if (node->stamp >= begin)
|
||||
fire = true;
|
||||
} else {
|
||||
if (node->stamp >= begin && node->stamp <= end)
|
||||
fire = true;
|
||||
}
|
||||
if (fire && ((node->stamp == 0) && !all))
|
||||
fire = false;
|
||||
if (fire)
|
||||
{
|
||||
_Unlink(m_Table[i], node);
|
||||
total++;
|
||||
}
|
||||
node = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
void HashTable::Clear()
|
||||
{
|
||||
HashTable::htNode *node, *temp;
|
||||
|
||||
for (uint32_t i=0; i<HT_SIZE; i++)
|
||||
{
|
||||
if (m_Table[i])
|
||||
{
|
||||
node = m_Table[i]->head;
|
||||
|
||||
while (node != NULL)
|
||||
{
|
||||
temp = node->next;
|
||||
delete node;
|
||||
node = temp;
|
||||
}
|
||||
|
||||
delete m_Table[i];
|
||||
m_Table[i] = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool HashTable::KeyExists(const char *key)
|
||||
{
|
||||
return (_FindNode(key, false) != NULL);
|
||||
}
|
||||
|
||||
size_t HashTable::UsedHashes()
|
||||
{
|
||||
size_t num = 0;
|
||||
|
||||
for (uint32_t i=0; i<HT_SIZE; i++)
|
||||
{
|
||||
if (m_Table[i])
|
||||
num++;
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// Iterator stuff //
|
||||
////////////////////
|
||||
|
||||
HashTable::iterator::iterator(HashTable::htNodeSet **table, uint32_t tableSize)
|
||||
{
|
||||
d = NULL;
|
||||
t = table;
|
||||
s = tableSize;
|
||||
|
||||
for (r = 0; r<s; r++)
|
||||
{
|
||||
if (t[r] && t[r]->head)
|
||||
{
|
||||
d = t[r]->head;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HashTable::iterator::next()
|
||||
{
|
||||
if (d->next)
|
||||
{
|
||||
d = d->next;
|
||||
} else {
|
||||
d=NULL;
|
||||
r++;
|
||||
for (; r<s; r++)
|
||||
{
|
||||
if (t[r] && t[r]->head)
|
||||
{
|
||||
d = t[r]->head;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool HashTable::iterator::done()
|
||||
{
|
||||
if (d == NULL && r >= s)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *HashTable::iterator::val()
|
||||
{
|
||||
return d->val.c_str();
|
||||
}
|
||||
|
||||
const char *HashTable::iterator::key()
|
||||
{
|
||||
return d->key.c_str();
|
||||
}
|
||||
|
||||
time_t HashTable::iterator::stamp()
|
||||
{
|
||||
return d->stamp;
|
||||
}
|
||||
|
||||
uint32_t HashTable::iterator::hash()
|
||||
{
|
||||
return r;
|
||||
}
|
||||
|
||||
HashTable::htNode *HashTable::iterator::operator *()
|
||||
{
|
||||
return d;
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Private implementations //
|
||||
/////////////////////////////
|
||||
|
||||
//Erases a node
|
||||
void HashTable::_Unlink(HashTable::htNodeSet *set, HashTable::htNode *node)
|
||||
{
|
||||
if (set->head == node)
|
||||
{
|
||||
set->head = NULL;
|
||||
set->tail = NULL;
|
||||
|
||||
delete node;
|
||||
} else {
|
||||
HashTable::htNode *link = set->head;
|
||||
|
||||
while (link != NULL)
|
||||
{
|
||||
if (link->next == node)
|
||||
{
|
||||
link->next = node->next;
|
||||
|
||||
delete node;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
link = link->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Finds a node by key
|
||||
HashTable::htNode *HashTable::_FindNode(const char *key, bool autoMake)
|
||||
{
|
||||
HashTable::htNodeSet *set;
|
||||
HashTable::htNode *node;
|
||||
|
||||
set = _FindNodeSet(key);
|
||||
node = set->head;
|
||||
|
||||
while (node)
|
||||
{
|
||||
if (node->key.compare(key) == 0)
|
||||
return node;
|
||||
node = node->next;
|
||||
}
|
||||
|
||||
if (autoMake)
|
||||
return _InsertIntoNodeSet(set, key, true);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//Inserts a new set of data into a bucket
|
||||
void HashTable::_Insert(const char *key, const char *value, time_t stamp)
|
||||
{
|
||||
HashTable::htNodeSet *set;
|
||||
HashTable::htNode *node;
|
||||
|
||||
set = _FindNodeSet(key);
|
||||
node = _InsertIntoNodeSet(set, key);
|
||||
|
||||
node->val.assign(value);
|
||||
node->stamp = stamp;
|
||||
}
|
||||
|
||||
//Inserts a new key into a bucket
|
||||
HashTable::htNode *HashTable::_InsertIntoNodeSet(HashTable::htNodeSet *set, const char *key, bool skip)
|
||||
{
|
||||
HashTable::htNode *node;
|
||||
|
||||
if (!skip)
|
||||
{
|
||||
node = set->head;
|
||||
while (node != NULL)
|
||||
{
|
||||
if (strcmp(node->key.c_str(), key) == 0)
|
||||
return node;
|
||||
node = node->next;
|
||||
}
|
||||
}
|
||||
|
||||
node = new HashTable::htNode;
|
||||
node->key.assign(key);
|
||||
node->next = NULL;
|
||||
|
||||
if (set->head == NULL)
|
||||
{
|
||||
set->head = node;
|
||||
set->tail = node;
|
||||
} else {
|
||||
set->tail->next = node;
|
||||
set->tail = node;
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
//Finds a bucket head by key
|
||||
HashTable::htNodeSet *HashTable::_FindNodeSet(const char *key)
|
||||
{
|
||||
uint16_t dv = HashTable::HashString(key);
|
||||
|
||||
if (!m_Table[dv])
|
||||
{
|
||||
m_Table[dv] = new HashTable::htNodeSet;
|
||||
memset(m_Table[dv], 0, sizeof(HashTable::htNodeSet));
|
||||
}
|
||||
|
||||
return m_Table[dv];
|
||||
}
|
||||
|
||||
uint16_t HashTable::HashString(const char *str)
|
||||
{
|
||||
uint8_t v1 = 0, v2 = 0;
|
||||
uint16_t dv = 0;
|
||||
|
||||
v1 = HashTable::_HashString1(str);
|
||||
v2 = HashTable::_HashString2(str);
|
||||
|
||||
//Combine the two hashes into an 11bit key
|
||||
dv = ((uint16_t)(v1) << 3) ^ (uint16_t)v2;
|
||||
dv &= 0x7FF;
|
||||
|
||||
return dv;
|
||||
}
|
||||
|
||||
uint8_t HashTable::_HashString1(const char *str)
|
||||
{
|
||||
uint8_t v = 0;
|
||||
while (*str)
|
||||
v = Hash_CRCTable1[v ^ (uint8_t)(*str++)];
|
||||
return v;
|
||||
}
|
||||
|
||||
uint8_t HashTable::_HashString2(const char *str)
|
||||
{
|
||||
uint8_t v = 0;
|
||||
while (*str)
|
||||
v = Hash_CRCTable2[v ^ (uint8_t)(*str++)];
|
||||
return v;
|
||||
}
|
||||
|
||||
//Contains last two bytes of standard CRC32
|
||||
const uint8_t Hash_CRCTable1[256] = {
|
||||
0x00, 0x96, 0x2c, 0xba, 0x19, 0x8f,
|
||||
0x35, 0xa3, 0x32, 0xa4, 0x1e, 0x88,
|
||||
0x2b, 0xbd, 0x07, 0x91, 0x64, 0xf2,
|
||||
0x48, 0xde, 0x7d, 0xeb, 0x51, 0xc7,
|
||||
0x56, 0xc0, 0x7a, 0xec, 0x4f, 0xd9,
|
||||
0x63, 0xf5, 0xc8, 0x5e, 0xe4, 0x72,
|
||||
0xd1, 0x47, 0xfd, 0x6b, 0xfa, 0x6c,
|
||||
0xd6, 0x40, 0xe3, 0x75, 0xcf, 0x59,
|
||||
0xac, 0x3a, 0x80, 0x16, 0xb5, 0x23,
|
||||
0x99, 0x0f, 0x9e, 0x08, 0xb2, 0x24,
|
||||
0x87, 0x11, 0xab, 0x3d, 0x90, 0x06,
|
||||
0xbc, 0x2a, 0x89, 0x1f, 0xa5, 0x33,
|
||||
0xa2, 0x34, 0x8e, 0x18, 0xbb, 0x2d,
|
||||
0x97, 0x01, 0xf4, 0x62, 0xd8, 0x4e,
|
||||
0xed, 0x7b, 0xc1, 0x57, 0xc6, 0x50,
|
||||
0xea, 0x7c, 0xdf, 0x49, 0xf3, 0x65,
|
||||
0x58, 0xce, 0x74, 0xe2, 0x41, 0xd7,
|
||||
0x6d, 0xfb, 0x6a, 0xfc, 0x46, 0xd0,
|
||||
0x73, 0xe5, 0x5f, 0xc9, 0x3c, 0xaa,
|
||||
0x10, 0x86, 0x25, 0xb3, 0x09, 0x9f,
|
||||
0x0e, 0x98, 0x22, 0xb4, 0x17, 0x81,
|
||||
0x3b, 0xad, 0x20, 0xb6, 0x0c, 0x9a,
|
||||
0x39, 0xaf, 0x15, 0x83, 0x12, 0x84,
|
||||
0x3e, 0xa8, 0x0b, 0x9d, 0x27, 0xb1,
|
||||
0x44, 0xd2, 0x68, 0xfe, 0x5d, 0xcb,
|
||||
0x71, 0xe7, 0x76, 0xe0, 0x5a, 0xcc,
|
||||
0x6f, 0xf9, 0x43, 0xd5, 0xe8, 0x7e,
|
||||
0xc4, 0x52, 0xf1, 0x67, 0xdd, 0x4b,
|
||||
0xda, 0x4c, 0xf6, 0x60, 0xc3, 0x55,
|
||||
0xef, 0x79, 0x8c, 0x1a, 0xa0, 0x36,
|
||||
0x95, 0x03, 0xb9, 0x2f, 0xbe, 0x28,
|
||||
0x92, 0x04, 0xa7, 0x31, 0x8b, 0x1d,
|
||||
0xb0, 0x26, 0x9c, 0x0a, 0xa9, 0x3f,
|
||||
0x85, 0x13, 0x82, 0x14, 0xae, 0x38,
|
||||
0x9b, 0x0d, 0xb7, 0x21, 0xd4, 0x42,
|
||||
0xf8, 0x6e, 0xcd, 0x5b, 0xe1, 0x77,
|
||||
0xe6, 0x70, 0xca, 0x5c, 0xff, 0x69,
|
||||
0xd3, 0x45, 0x78, 0xee, 0x54, 0xc2,
|
||||
0x61, 0xf7, 0x4d, 0xdb, 0x4a, 0xdc,
|
||||
0x66, 0xf0, 0x53, 0xc5, 0x7f, 0xe9,
|
||||
0x1c, 0x8a, 0x30, 0xa6, 0x05, 0x93,
|
||||
0x29, 0xbf, 0x2e, 0xb8, 0x02, 0x94,
|
||||
0x37, 0xa1, 0x1b, 0x8d};
|
||||
|
||||
//Contains second pair of bytes from standard CRC32
|
||||
const uint8_t Hash_CRCTable2[256] = {
|
||||
0x00, 0x07, 0x0e, 0x09, 0x6d, 0x6a,
|
||||
0x63, 0x64, 0xdb, 0xdc, 0xd5, 0xd2,
|
||||
0xb6, 0xb1, 0xb8, 0xbf, 0xb7, 0xb0,
|
||||
0xb9, 0xbe, 0xda, 0xdd, 0xd4, 0xd3,
|
||||
0x6c, 0x6b, 0x62, 0x65, 0x01, 0x06,
|
||||
0x0f, 0x08, 0x6e, 0x69, 0x60, 0x67,
|
||||
0x03, 0x04, 0x0d, 0x0a, 0xb5, 0xb2,
|
||||
0xbb, 0xbc, 0xd8, 0xdf, 0xd6, 0xd1,
|
||||
0xd9, 0xde, 0xd7, 0xd0, 0xb4, 0xb3,
|
||||
0xba, 0xbd, 0x02, 0x05, 0x0c, 0x0b,
|
||||
0x6f, 0x68, 0x61, 0x66, 0xdc, 0xdb,
|
||||
0xd2, 0xd5, 0xb1, 0xb6, 0xbf, 0xb8,
|
||||
0x07, 0x00, 0x09, 0x0e, 0x6a, 0x6d,
|
||||
0x64, 0x63, 0x6b, 0x6c, 0x65, 0x62,
|
||||
0x06, 0x01, 0x08, 0x0f, 0xb0, 0xb7,
|
||||
0xbe, 0xb9, 0xdd, 0xda, 0xd3, 0xd4,
|
||||
0xb2, 0xb5, 0xbc, 0xbb, 0xdf, 0xd8,
|
||||
0xd1, 0xd6, 0x69, 0x6e, 0x67, 0x60,
|
||||
0x04, 0x03, 0x0a, 0x0d, 0x05, 0x02,
|
||||
0x0b, 0x0c, 0x68, 0x6f, 0x66, 0x61,
|
||||
0xde, 0xd9, 0xd0, 0xd7, 0xb3, 0xb4,
|
||||
0xbd, 0xba, 0xb8, 0xbf, 0xb6, 0xb1,
|
||||
0xd5, 0xd2, 0xdb, 0xdc, 0x63, 0x64,
|
||||
0x6d, 0x6a, 0x0e, 0x09, 0x00, 0x07,
|
||||
0x0f, 0x08, 0x01, 0x06, 0x62, 0x65,
|
||||
0x6c, 0x6b, 0xd4, 0xd3, 0xda, 0xdd,
|
||||
0xb9, 0xbe, 0xb7, 0xb0, 0xd6, 0xd1,
|
||||
0xd8, 0xdf, 0xbb, 0xbc, 0xb5, 0xb2,
|
||||
0x0d, 0x0a, 0x03, 0x04, 0x60, 0x67,
|
||||
0x6e, 0x69, 0x61, 0x66, 0x6f, 0x68,
|
||||
0x0c, 0x0b, 0x02, 0x05, 0xba, 0xbd,
|
||||
0xb4, 0xb3, 0xd7, 0xd0, 0xd9, 0xde,
|
||||
0x64, 0x63, 0x6a, 0x6d, 0x09, 0x0e,
|
||||
0x07, 0x00, 0xbf, 0xb8, 0xb1, 0xb6,
|
||||
0xd2, 0xd5, 0xdc, 0xdb, 0xd3, 0xd4,
|
||||
0xdd, 0xda, 0xbe, 0xb9, 0xb0, 0xb7,
|
||||
0x08, 0x0f, 0x06, 0x01, 0x65, 0x62,
|
||||
0x6b, 0x6c, 0x0a, 0x0d, 0x04, 0x03,
|
||||
0x67, 0x60, 0x69, 0x6e, 0xd1, 0xd6,
|
||||
0xdf, 0xd8, 0xbc, 0xbb, 0xb2, 0xb5,
|
||||
0xbd, 0xba, 0xb3, 0xb4, 0xd0, 0xd7,
|
||||
0xde, 0xd9, 0x66, 0x61, 0x68, 0x6f,
|
||||
0x0b, 0x0c, 0x05, 0x02};
|
|
@ -1,104 +0,0 @@
|
|||
#ifndef _INCLUDE_AMXX_HASH_H
|
||||
#define _INCLUDE_AMXX_HASH_H
|
||||
|
||||
#include <time.h>
|
||||
#include "sdk/amxxmodule.h"
|
||||
#include "sdk/CString.h"
|
||||
|
||||
/**
|
||||
* (C)2005 David "BAILOPAN" Anderson
|
||||
* Licensed under the GNU General Public License, version 2
|
||||
*
|
||||
* Small hash table implementation
|
||||
*/
|
||||
|
||||
#if defined WIN32 || defined _WIN32
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef __int8 int8_t;
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Hash Table implementation
|
||||
* This is not designed to be flexible for size/type, it's hardcoded.
|
||||
* The table size is 2^11, which should be good enough for fast small lookups.
|
||||
* A hash is computed by two chopped up 8bit versions of CRC32, which is then combined to
|
||||
* form an 11bit key. That key is then an index into the appropriate bucket.
|
||||
*/
|
||||
|
||||
extern const uint8_t Hash_CRCTable1[];
|
||||
extern const uint8_t Hash_CRCTable2[];
|
||||
|
||||
//You should not change this without understanding the hash algorithm
|
||||
#define HT_SIZE 2048
|
||||
|
||||
class HashTable
|
||||
{
|
||||
public: //STRUCTORS
|
||||
HashTable();
|
||||
~HashTable();
|
||||
public: //PRE-DEF
|
||||
class iterator;
|
||||
struct htNode;
|
||||
friend class Vault;
|
||||
private: //PRE-DEF
|
||||
struct htNodeSet;
|
||||
public: //PUBLIC FUNCTIONS
|
||||
void Store(const char *key, const char *value, bool temporary=true);
|
||||
void Store(const char *key, const char *value, time_t stamp);
|
||||
htNode *Retrieve(const char *key);
|
||||
iterator Enumerate();
|
||||
size_t Prune(time_t begin, time_t end, bool all=false);
|
||||
void Clear();
|
||||
bool KeyExists(const char *key);
|
||||
size_t UsedHashes();
|
||||
void EraseKey(const char *key);
|
||||
public: //PUBLIC CLASSES
|
||||
class iterator
|
||||
{
|
||||
public:
|
||||
iterator(htNodeSet **table, uint32_t tableSize);
|
||||
const char *key();
|
||||
const char *val();
|
||||
time_t stamp();
|
||||
bool done();
|
||||
void next();
|
||||
uint32_t hash();
|
||||
htNode *operator *();
|
||||
private:
|
||||
htNodeSet **t; //table
|
||||
uint32_t s; //size
|
||||
uint32_t r; //current
|
||||
htNode *d; //delta
|
||||
};
|
||||
private: //PRIVATE API
|
||||
void _Insert(const char *key, const char *val, time_t stamp);
|
||||
htNode *_FindNode(const char *key, bool autoMake=true);
|
||||
htNodeSet *_FindNodeSet(const char *key);
|
||||
htNode *_InsertIntoNodeSet(htNodeSet *set, const char *key, bool skip=false);
|
||||
void _Unlink(htNodeSet *set, htNode *node);
|
||||
public: //PUBLIC STATIC API
|
||||
static uint16_t HashString(const char *str);
|
||||
private: //PRIVATE STATIC API
|
||||
static uint8_t _HashString1(const char *str);
|
||||
static uint8_t _HashString2(const char *str);
|
||||
public: //PUBLIC STRUCTURES
|
||||
struct htNode
|
||||
{
|
||||
String key;
|
||||
String val;
|
||||
time_t stamp;
|
||||
htNode *next;
|
||||
};
|
||||
private: //PRIVATE STRUCTURES
|
||||
struct htNodeSet
|
||||
{
|
||||
htNode *head;
|
||||
htNode *tail;
|
||||
};
|
||||
private: //INTERNAL VARIABLES
|
||||
htNodeSet *m_Table[HT_SIZE];
|
||||
};
|
||||
|
||||
#endif //_INCLUDE_AMXX_HASH_H
|
|
@ -1,328 +0,0 @@
|
|||
#include <stdlib.h>
|
||||
#include "journal.h"
|
||||
#include "sdk/CVector.h"
|
||||
|
||||
struct j_info
|
||||
{
|
||||
int id;
|
||||
Vault *vault;
|
||||
};
|
||||
|
||||
Journal::Journal(const char *file)
|
||||
{
|
||||
m_File.assign(file);
|
||||
m_Fp = NULL;
|
||||
m_LastId = 0;
|
||||
}
|
||||
|
||||
#define rd(v,s) if (fread(&v, sizeof(s), 1, m_Fp) != 1) { \
|
||||
fclose(m_Fp); \
|
||||
m_Fp = NULL; \
|
||||
goto _error; }
|
||||
#define rds(v,l) if (fread(v, sizeof(char), l, m_Fp) != l) { \
|
||||
fclose(m_Fp); \
|
||||
m_Fp = NULL; \
|
||||
goto _error; } else { \
|
||||
v[l] = '\0'; }
|
||||
|
||||
bool Journal::Replay(size_t &files, size_t &ops)
|
||||
{
|
||||
m_Fp = fopen(m_File.c_str(), "rb");
|
||||
|
||||
files = 0;
|
||||
ops = 0;
|
||||
|
||||
if (!m_Fp)
|
||||
return false;
|
||||
|
||||
//this must come before the first jump...
|
||||
CVector<j_info *> table;
|
||||
uint32_t magic;
|
||||
|
||||
rd(magic, uint32_t);
|
||||
if (magic != JOURNAL_MAGIC)
|
||||
return false;
|
||||
|
||||
j_info *j;
|
||||
uint8_t op, klen;
|
||||
uint16_t vlen;
|
||||
uint32_t id;
|
||||
size_t i;
|
||||
char *key=NULL, *val=NULL;
|
||||
while (!feof(m_Fp))
|
||||
{
|
||||
if (fread(&op, sizeof(uint8_t), 1, m_Fp) != 1)
|
||||
{
|
||||
if (feof(m_Fp))
|
||||
break;
|
||||
else
|
||||
goto _error;
|
||||
}
|
||||
switch (op)
|
||||
{
|
||||
case Journal_Nop:
|
||||
{
|
||||
break;
|
||||
}
|
||||
case Journal_Name:
|
||||
{
|
||||
rd(id, uint32_t);
|
||||
rd(klen, uint8_t);
|
||||
key = new char[klen+1];
|
||||
rds(key, klen);
|
||||
j = new j_info;
|
||||
j->id = id;
|
||||
j->vault = new Vault(key);
|
||||
j->vault->ReadFromFile();
|
||||
table.push_back(j);
|
||||
files++;
|
||||
delete [] key;
|
||||
key = NULL;
|
||||
break;
|
||||
}
|
||||
case Journal_Store:
|
||||
{
|
||||
//Stores key/val (id,time,klen,vlen,[],[])
|
||||
uint32_t stamp;
|
||||
rd(id, uint32_t);
|
||||
rd(stamp, uint32_t);
|
||||
rd(klen, uint8_t);
|
||||
rd(vlen, uint16_t);
|
||||
key = new char[klen+1];
|
||||
val = new char[vlen+1];
|
||||
rds(key, klen);
|
||||
rds(val, vlen);
|
||||
for (i=0; i<table.size(); i++)
|
||||
{
|
||||
if (table.at(i)->id == id)
|
||||
{
|
||||
table.at(i)->vault->Store(key, val, (time_t)stamp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
delete [] key;
|
||||
delete [] val;
|
||||
key = NULL;
|
||||
val = NULL;
|
||||
break;
|
||||
}
|
||||
case Journal_Erase:
|
||||
{
|
||||
//Erases key (id,klen,[])
|
||||
rd(id, uint32_t);
|
||||
rd(klen, uint8_t);
|
||||
key = new char[klen+1];
|
||||
rds(key, klen);
|
||||
for (i=0; i<table.size(); i++)
|
||||
{
|
||||
if (table.at(i)->id == id)
|
||||
{
|
||||
table.at(i)->vault->EraseKey(key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
delete [] key;
|
||||
key = NULL;
|
||||
break;
|
||||
}
|
||||
case Journal_Clear:
|
||||
{
|
||||
//Clears (id)
|
||||
rd(id, uint32_t);
|
||||
for (i=0; i<table.size(); i++)
|
||||
{
|
||||
if (table.at(i)->id == id)
|
||||
{
|
||||
table.at(i)->vault->Clear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Journal_Prune:
|
||||
{
|
||||
//Prunes (id,t1,t2,all)
|
||||
rd(id, uint32_t);
|
||||
uint32_t begin, end;
|
||||
uint8_t all;
|
||||
rd(begin, uint32_t);
|
||||
rd(end, uint32_t);
|
||||
rd(all, uint8_t);
|
||||
for (i=0; i<table.size(); i++)
|
||||
{
|
||||
if (table.at(i)->id == id)
|
||||
{
|
||||
table.at(i)->vault->Prune((time_t)begin, (time_t)end, all?true:false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
goto _error;
|
||||
}
|
||||
} //end while
|
||||
ops++;
|
||||
}
|
||||
|
||||
for (uint32_t i=0; i<table.size(); i++)
|
||||
{
|
||||
j = table.at(i);
|
||||
j->vault->WriteToFile();
|
||||
delete j->vault;
|
||||
delete j;
|
||||
}
|
||||
|
||||
fclose(m_Fp);
|
||||
|
||||
return true;
|
||||
_error:
|
||||
for (uint32_t i=0; i<table.size(); i++)
|
||||
{
|
||||
j = table.at(i);
|
||||
j->vault->WriteToFile();
|
||||
delete j->vault;
|
||||
delete j;
|
||||
}
|
||||
if (key)
|
||||
{
|
||||
delete [] key;
|
||||
key = NULL;
|
||||
}
|
||||
if (val)
|
||||
{
|
||||
delete [] val;
|
||||
val = NULL;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Journal::ClearJournal()
|
||||
{
|
||||
m_Fp = fopen(m_File.c_str(), "wb");
|
||||
|
||||
if (m_Fp)
|
||||
{
|
||||
fclose(m_Fp);
|
||||
m_Fp = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool Journal::StartJournal()
|
||||
{
|
||||
m_Fp = fopen(m_File.c_str(), "wb");
|
||||
|
||||
if (!m_Fp)
|
||||
return false;
|
||||
|
||||
uint32_t magic = JOURNAL_MAGIC;
|
||||
|
||||
fwrite(&magic, sizeof(uint32_t), 1, m_Fp);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Journal::EndJournal()
|
||||
{
|
||||
if (m_Fp)
|
||||
{
|
||||
fclose(m_Fp);
|
||||
m_Fp = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
//Stores key/val (id,time,klen,vlen,[],[]
|
||||
void Journal::Store(const char *name, const char *key, const char *val, time_t stamp)
|
||||
{
|
||||
uint32_t time32 = (uint32_t)stamp;
|
||||
uint16_t vlen = (uint16_t)strlen(val);
|
||||
uint8_t klen = (uint8_t)strlen(key);
|
||||
|
||||
BeginOp(name, Journal_Store);
|
||||
WriteInt(time32);
|
||||
WriteByte(klen);
|
||||
WriteShort(vlen);
|
||||
WriteString(key);
|
||||
WriteString(val);
|
||||
EndOp();
|
||||
}
|
||||
|
||||
//Erases key (id,klen,[])
|
||||
void Journal::Erase(const char *name, const char *key)
|
||||
{
|
||||
uint8_t klen = (uint8_t)strlen(key);
|
||||
|
||||
BeginOp(name, Journal_Erase);
|
||||
WriteByte(klen);
|
||||
WriteString(key);
|
||||
EndOp();
|
||||
}
|
||||
|
||||
//Clears (id)
|
||||
void Journal::Clear(const char *name)
|
||||
{
|
||||
BeginOp(name, Journal_Clear);
|
||||
EndOp();
|
||||
}
|
||||
|
||||
//Prunes (id,t1,t2,all)
|
||||
void Journal::Prune(const char *name, time_t begin, time_t end, bool all)
|
||||
{
|
||||
uint32_t begin32 = (uint32_t)begin;
|
||||
uint32_t end32 = (uint32_t)end;
|
||||
uint8_t all8 = (uint8_t)all;
|
||||
|
||||
BeginOp(name, Journal_Prune);
|
||||
WriteInt(begin32);
|
||||
WriteInt(end32);
|
||||
WriteByte(all8);
|
||||
EndOp();
|
||||
}
|
||||
|
||||
void Journal::BeginOp(const char *name, JournalOp jop)
|
||||
{
|
||||
uint32_t id;
|
||||
|
||||
if (!m_Names.KeyExists(name))
|
||||
{
|
||||
char name_buf[12];
|
||||
id = ++m_LastId;
|
||||
sprintf(name_buf, "%d", id);
|
||||
m_Names.Store(name, name_buf, false);
|
||||
WriteByte(Journal_Name);
|
||||
WriteInt(id);
|
||||
WriteByte(strlen(name));
|
||||
WriteString(name);
|
||||
} else {
|
||||
id = atoi(m_Names.Retrieve(name)->val.c_str());
|
||||
}
|
||||
|
||||
WriteByte(jop);
|
||||
WriteInt(id);
|
||||
}
|
||||
|
||||
void Journal::WriteByte(uint8_t num)
|
||||
{
|
||||
fwrite(&num, sizeof(uint8_t), 1, m_Fp);
|
||||
}
|
||||
|
||||
void Journal::WriteShort(uint16_t num)
|
||||
{
|
||||
fwrite(&num, sizeof(uint16_t), 1, m_Fp);
|
||||
}
|
||||
|
||||
void Journal::WriteInt(uint32_t num)
|
||||
{
|
||||
fwrite(&num, sizeof(uint32_t), 1, m_Fp);
|
||||
}
|
||||
|
||||
void Journal::WriteString(const char *str)
|
||||
{
|
||||
fwrite(str, sizeof(char), strlen(str), m_Fp);
|
||||
}
|
||||
|
||||
size_t Journal::EndOp()
|
||||
{
|
||||
return 1;
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
#ifndef _INCLUDE_JOURNAL_H
|
||||
#define _INCLUDE_JOURNAL_H
|
||||
|
||||
#include "nvault.h"
|
||||
|
||||
/**
|
||||
* (C)2005 David "BAILOPAN" Anderson
|
||||
* Licensed under the GNU General Public License, version 2
|
||||
*
|
||||
* Adds Journaling capabilities for an nVault
|
||||
*/
|
||||
|
||||
#define JOURNAL_MAGIC 0x6E564A4C
|
||||
|
||||
class Journal
|
||||
{
|
||||
public:
|
||||
enum JournalOp
|
||||
{
|
||||
Journal_Nop, //Nothing
|
||||
Journal_Name, //Maps name to Id (id,len,[])
|
||||
Journal_Store, //Stores key/val (id,time,klen,vlen,[],[])
|
||||
Journal_Erase, //Erases key (id,klen,[])
|
||||
Journal_Clear, //Clears (id)
|
||||
Journal_Prune //Prunes (id,t1,t2,all)
|
||||
};
|
||||
public:
|
||||
Journal(const char *file);
|
||||
public:
|
||||
bool Replay(size_t &files, size_t &ops);
|
||||
void ClearJournal();
|
||||
bool StartJournal();
|
||||
void EndJournal();
|
||||
public:
|
||||
void Store(const char *name, const char *key, const char *val, time_t stamp);
|
||||
void Erase(const char *name, const char *key);
|
||||
void Clear(const char *name);
|
||||
void Prune(const char *name, time_t begin, time_t end, bool all);
|
||||
private:
|
||||
void BeginOp(const char *name, JournalOp jop);
|
||||
void WriteByte(uint8_t num);
|
||||
void WriteShort(uint16_t num);
|
||||
void WriteInt(uint32_t num);
|
||||
void WriteString(const char *str);
|
||||
size_t EndOp();
|
||||
private:
|
||||
String m_File;
|
||||
FILE *m_Fp;
|
||||
HashTable m_Names;
|
||||
uint32_t m_LastId;
|
||||
};
|
||||
|
||||
#endif //_INCLUDE_JOURNAL_H
|
|
@ -1,211 +0,0 @@
|
|||
#include "nvault.h"
|
||||
|
||||
Vault::Vault(const char *name)
|
||||
{
|
||||
m_File.assign(name);
|
||||
m_Vault = NULL;
|
||||
}
|
||||
|
||||
Vault::~Vault()
|
||||
{
|
||||
if (m_Vault)
|
||||
{
|
||||
delete m_Vault;
|
||||
m_Vault = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
const char *Vault::GetFileName()
|
||||
{
|
||||
return m_File.c_str();
|
||||
}
|
||||
|
||||
void Vault::Clear()
|
||||
{
|
||||
if (m_Vault)
|
||||
m_Vault->Clear();
|
||||
}
|
||||
|
||||
void Vault::EraseKey(const char *key)
|
||||
{
|
||||
if (m_Vault)
|
||||
m_Vault->EraseKey(key);
|
||||
}
|
||||
|
||||
HashTable::htNode *Vault::Find(const char *key)
|
||||
{
|
||||
if (m_Vault)
|
||||
return m_Vault->Retrieve(key);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool Vault::KeyExists(const char *key)
|
||||
{
|
||||
if (m_Vault)
|
||||
return m_Vault->KeyExists(key);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t Vault::Prune(time_t begin, time_t end, bool all)
|
||||
{
|
||||
if (m_Vault)
|
||||
return m_Vault->Prune(begin, end, all);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Vault::Store(const char *key, const char *value, bool temporary)
|
||||
{
|
||||
if (m_Vault)
|
||||
m_Vault->Store(key, value, temporary);
|
||||
}
|
||||
|
||||
void Vault::Store(const char *key, const char *value, time_t stamp)
|
||||
{
|
||||
if (m_Vault)
|
||||
m_Vault->Store(key, value, stamp);
|
||||
}
|
||||
|
||||
#define wr(v,s) fwrite(&v, sizeof(s), 1, fp)
|
||||
|
||||
bool Vault::WriteToFile()
|
||||
{
|
||||
FILE *fp = fopen(m_File.c_str(), "wb");
|
||||
|
||||
if (!fp)
|
||||
return false;
|
||||
|
||||
uint32_t hashes = m_Vault->UsedHashes();
|
||||
|
||||
_WriteHeaders(fp, hashes);
|
||||
|
||||
HashTable::htNode *node;
|
||||
uint32_t keys, stamp;
|
||||
uint16_t vChars;
|
||||
uint8_t kChars;
|
||||
for (uint32_t i=0; i<HT_SIZE; i++)
|
||||
{
|
||||
if (m_Vault->m_Table[i])
|
||||
{
|
||||
keys = 0;
|
||||
node = m_Vault->m_Table[i]->head;;
|
||||
while (node != NULL)
|
||||
{
|
||||
keys++;
|
||||
node = node->next;
|
||||
}
|
||||
wr(i, uint32_t);
|
||||
wr(keys, uint32_t);
|
||||
node = m_Vault->m_Table[i]->head;
|
||||
while (node != NULL)
|
||||
{
|
||||
stamp = (uint32_t)(node->stamp);
|
||||
wr(stamp, uint32_t);
|
||||
kChars = (uint8_t)(node->key.size());
|
||||
vChars = (uint16_t)(node->val.size());
|
||||
wr(kChars, uint8_t);
|
||||
wr(vChars, uint16_t);
|
||||
fwrite(node->key.c_str(), sizeof(char), kChars, fp);
|
||||
fwrite(node->val.c_str(), sizeof(char), vChars, fp);
|
||||
node = node->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#define rd(v,s) if (fread(&v, sizeof(s), 1, fp) != 1) { \
|
||||
fclose(fp); \
|
||||
return Vault_ReadFail; }
|
||||
|
||||
Vault::VaultError Vault::ReadFromFile()
|
||||
{
|
||||
FILE *fp = fopen(m_File.c_str(), "rb");
|
||||
|
||||
if (!fp)
|
||||
{
|
||||
fp = fopen(m_File.c_str(), "wb");
|
||||
if (!fp)
|
||||
return Vault_ReadFail;
|
||||
_WriteHeaders(fp, 0);
|
||||
fclose(fp);
|
||||
m_Vault = new HashTable();
|
||||
return Vault_Ok;
|
||||
}
|
||||
|
||||
uint32_t magic, keysize, hashes;
|
||||
uint8_t timesize;
|
||||
|
||||
rd(magic, uint32_t);
|
||||
if (magic != VAULT_MAGIC)
|
||||
{
|
||||
fclose(fp);
|
||||
return Vault_BadMagic;
|
||||
}
|
||||
rd(timesize, uint8_t);
|
||||
rd(keysize, uint32_t);
|
||||
rd(hashes, uint32_t);
|
||||
|
||||
m_Vault = new HashTable();
|
||||
|
||||
uint32_t hash, keys, stamp;
|
||||
uint16_t vChars;
|
||||
uint8_t kChars;
|
||||
char *key, *value;
|
||||
for (uint32_t i=0; i<hashes; i++)
|
||||
{
|
||||
rd(hash, uint32_t);
|
||||
rd(keys, uint32_t);
|
||||
for (uint32_t d=0; d<keys; d++)
|
||||
{
|
||||
rd(stamp, uint32_t);
|
||||
rd(kChars, uint8_t);
|
||||
rd(vChars, uint16_t);
|
||||
key = new char[kChars+1];
|
||||
if (fread(key, sizeof(char), kChars, fp) != kChars)
|
||||
{
|
||||
delete [] key;
|
||||
fclose(fp);
|
||||
return Vault_ReadFail;
|
||||
}
|
||||
value = new char[vChars+1];
|
||||
if (fread(value, sizeof(char), vChars, fp) != vChars)
|
||||
{
|
||||
delete [] key;
|
||||
delete [] value;
|
||||
return Vault_ReadFail;
|
||||
}
|
||||
key[kChars] = '\0';
|
||||
value[vChars] = '\0';
|
||||
m_Vault->Store(key, value, (time_t)stamp);
|
||||
delete [] key;
|
||||
delete [] value;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return Vault_Ok;
|
||||
}
|
||||
|
||||
///////////////////
|
||||
// Private stuff //
|
||||
///////////////////
|
||||
|
||||
void Vault::_WriteHeaders(FILE *fp, uint32_t keys)
|
||||
{
|
||||
uint32_t magic = VAULT_MAGIC;
|
||||
uint32_t keysize = (1<<11);
|
||||
uint8_t timesize = sizeof(time_t);
|
||||
|
||||
fwrite(&magic, sizeof(uint32_t), 1, fp);
|
||||
fwrite(×ize, sizeof(uint8_t), 1, fp);
|
||||
fwrite(&keysize, sizeof(uint32_t), 1, fp);
|
||||
fwrite(&keys, sizeof(uint32_t), 1, fp);
|
||||
}
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
#ifndef _INCLUDE_NVAULT_H
|
||||
#define _INCLUDE_NVAULT_H
|
||||
|
||||
#include "sdk/CString.h"
|
||||
#include "hash.h"
|
||||
|
||||
/**
|
||||
* (C)2005 David "BAILOPAN" Anderson
|
||||
* Licensed under the GNU General Public License, version 2
|
||||
*
|
||||
* Vault implementation using a Hash Table
|
||||
*/
|
||||
|
||||
/**
|
||||
* Vault file format:
|
||||
* Headers
|
||||
* uint32_t - nVLT
|
||||
* uint8_t - sizeof(time_t)
|
||||
* uint32_t - key size (will be used in future maybe)
|
||||
* uint32_t - number of hashes stored
|
||||
* Data
|
||||
* uint32_t - key hash
|
||||
* uint32_t - # of keys in this hash
|
||||
* Data
|
||||
* uint32_t - Time
|
||||
* uint8_t - Characters in key
|
||||
* uint16_t - Characters in value
|
||||
* char[] - Key
|
||||
* char[] - Value
|
||||
*/
|
||||
|
||||
#define VAULT_MAGIC 0x6E564C54
|
||||
|
||||
class Vault
|
||||
{
|
||||
public:
|
||||
Vault(const char *name);
|
||||
~Vault();
|
||||
enum VaultError
|
||||
{
|
||||
Vault_Ok=0,
|
||||
Vault_ReadFail,
|
||||
Vault_BadMagic,
|
||||
};
|
||||
public:
|
||||
bool WriteToFile();
|
||||
VaultError ReadFromFile();
|
||||
public:
|
||||
void Store(const char *key, const char *value, bool temporary=true);
|
||||
void Store(const char *key, const char *value, time_t stamp);
|
||||
size_t Prune(time_t begin, time_t end, bool all=false);
|
||||
HashTable::htNode *Find(const char *key);
|
||||
bool KeyExists(const char *key);
|
||||
void Clear();
|
||||
void EraseKey(const char *key);
|
||||
const char *GetFileName();
|
||||
private:
|
||||
void _WriteHeaders(FILE *fp, uint32_t hashes);
|
||||
private:
|
||||
String m_File;
|
||||
HashTable *m_Vault;
|
||||
};
|
||||
|
||||
#endif //_INCLUDE_NVAULT_H
|
Loading…
Reference in New Issue
Block a user