Files

117 lines
3.1 KiB
C++
Raw Permalink Normal View History

2019-04-01 18:34:39 +02:00
/*
* This file is part of the Flowee project
2021-02-07 18:08:59 +01:00
* Copyright (C) 2019-2021 Tom Zander <tom@flowee.org>
2019-04-01 18:34:39 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HASHSTORAGE_p_H
#define HASHSTORAGE_p_H
2019-04-06 12:19:13 +02:00
/*
* WARNING USAGE OF THIS HEADER IS RESTRICTED.
* This Header file is part of the private API and is meant to be used solely by the HashStorage component.
*
* Usage of this API will likely mean your code will break in interesting ways in the future,
* or even stop to compile.
*
* YOU HAVE BEEN WARNED!!
*/
2019-04-01 22:53:39 +02:00
#include <qvector.h>
2019-04-01 18:34:39 +02:00
#include <uint256.h>
#include <boost/filesystem.hpp>
#include <qfile.h>
#include <qmap.h>
#include <qmutex.h>
2021-02-07 13:46:57 +01:00
#include <unordered_map>
2019-04-01 18:34:39 +02:00
inline uint32_t qHash(const uint256 &key, uint32_t seed) {
2019-04-05 13:50:41 +02:00
return *reinterpret_cast<const uint32_t*>(key.begin() + (seed % 5));
}
2019-04-09 11:19:03 +02:00
class HashListPart
{
public:
HashListPart(const QString &partBase);
2019-04-09 13:24:18 +02:00
~HashListPart();
2019-04-09 11:19:03 +02:00
void openFiles();
void closeFiles();
int find(const uint256 &hash) const;
const uint256 &at(int index) const;
2019-04-09 11:19:03 +02:00
uchar *sorted = nullptr;
QFile sortedFile;
2021-02-07 10:57:45 +01:00
qint64 sortedFileSize = 0;
2019-04-09 11:19:03 +02:00
uchar *reverseLookup = nullptr;
QFile reverseLookupFile;
};
2019-04-01 18:34:39 +02:00
class HashList {
public:
HashList(const QString &dbBase);
2022-09-07 15:08:34 +02:00
HashList(const HashList &other) = delete;
2019-04-01 18:34:39 +02:00
~HashList();
2019-04-08 13:19:54 +02:00
static HashList *createEmpty(const QString &dbBase, int index);
2019-04-01 18:34:39 +02:00
int append(const uint256 &hash);
int lookup(const uint256 &hash) const;
const uint256 &at(int index) const;
2021-02-07 18:08:59 +01:00
// iterate over big file and fill 'm_offsets'
void fillOffsetsTable();
2019-04-09 11:19:03 +02:00
void writeInfoFile() const;
// write the m_cache to disk (sorted) and start a new one.
void stabilize();
// copy all parts into one file and switch to (single) file lookups only
2019-04-01 21:15:24 +02:00
void finalize();
2019-04-01 18:34:39 +02:00
2019-04-08 13:19:54 +02:00
const QString m_filebase;
2019-04-09 11:19:03 +02:00
QList<HashListPart*> m_parts;
2019-04-08 13:19:54 +02:00
2019-04-01 18:34:39 +02:00
// for the memmapped, sorted section.
uchar *m_sorted = nullptr;
2019-04-08 13:19:54 +02:00
QFile m_sortedFile;
2021-02-07 10:57:45 +01:00
qint64 m_sortedFileSize = 0;
2019-04-08 13:19:54 +02:00
uchar *m_reverseLookup = nullptr;
QFile m_reverseLookupFile;
2022-09-07 15:08:34 +02:00
quint64 m_reverseLookupFileSize = 0;
2021-02-07 18:08:59 +01:00
quint32 m_offsets[256];
2019-04-01 18:34:39 +02:00
// the unsorted part
QFile *m_log = nullptr;
2021-02-07 13:46:57 +01:00
std::unordered_map<uint256, int, HashShortener, HashComparison> m_cacheMap;
2019-04-01 18:34:39 +02:00
int m_nextId = 0;
mutable QMutex m_mutex;
2022-09-07 15:08:34 +02:00
HashList *operator=(const HashList&) = delete;
2019-04-01 18:34:39 +02:00
};
class HashStoragePrivate
{
public:
HashStoragePrivate(const boost::filesystem::path &basedir);
~HashStoragePrivate();
QList<HashList*> dbs;
const QString basedir;
2019-04-01 18:34:39 +02:00
static uint256 s_null;
2019-04-01 18:34:39 +02:00
};
#endif