2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "btreeidx.hh"
|
|
|
|
#include "folding.hh"
|
|
|
|
#include "utf8.hh"
|
2009-03-26 19:00:08 +00:00
|
|
|
#include <QRunnable>
|
|
|
|
#include <QThreadPool>
|
|
|
|
#include <QSemaphore>
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <math.h>
|
2009-03-26 19:00:08 +00:00
|
|
|
#include <string.h>
|
2009-01-30 01:20:37 +00:00
|
|
|
#include <stdlib.h>
|
2011-06-19 18:50:11 +00:00
|
|
|
#include "dprintf.hh"
|
2013-07-06 18:18:43 +00:00
|
|
|
#include "wstring_qt.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
//#define __BTREE_USE_LZO
|
|
|
|
// LZO mode is experimental and unsupported. Tests didn't show any substantial
|
|
|
|
// speed improvements.
|
|
|
|
|
|
|
|
#ifdef __BTREE_USE_LZO
|
|
|
|
#include <lzo/lzo1x.h>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct __LzoInit
|
|
|
|
{
|
|
|
|
__LzoInit()
|
|
|
|
{
|
|
|
|
lzo_init();
|
|
|
|
}
|
|
|
|
} __lzoInit;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace BtreeIndexing {
|
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
using gd::wstring;
|
|
|
|
using gd::wchar;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
BtreeMinElements = 64,
|
|
|
|
BtreeMaxElements = 2048
|
|
|
|
};
|
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
BtreeIndex::BtreeIndex():
|
|
|
|
idxFile( 0 ), rootNodeLoaded( false )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
BtreeDictionary::BtreeDictionary( string const & id,
|
|
|
|
vector< string > const & dictionaryFiles ):
|
2009-10-25 22:49:24 +00:00
|
|
|
Dictionary::Class( id, dictionaryFiles )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
string const & BtreeDictionary::ensureInitDone()
|
|
|
|
{
|
|
|
|
static string empty;
|
|
|
|
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
void BtreeIndex::openIndex( IndexInfo const & indexInfo,
|
|
|
|
File::Class & file, Mutex & mutex )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-14 16:35:47 +00:00
|
|
|
indexNodeSize = indexInfo.btreeMaxElements;
|
|
|
|
rootOffset = indexInfo.rootOffset;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
idxFile = &file;
|
2009-03-26 19:00:08 +00:00
|
|
|
idxFileMutex = &mutex;
|
2009-04-14 16:35:47 +00:00
|
|
|
|
|
|
|
rootNodeLoaded = false;
|
|
|
|
rootNode.clear();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
vector< WordArticleLink > BtreeIndex::findArticles( wstring const & str )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
vector< WordArticleLink > result;
|
|
|
|
|
|
|
|
wstring folded = Folding::apply( str );
|
2012-09-14 23:07:01 +00:00
|
|
|
if( folded.empty() )
|
|
|
|
folded = Folding::applyWhitespaceOnly( str );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
bool exactMatch;
|
|
|
|
|
|
|
|
vector< char > leaf;
|
|
|
|
uint32_t nextLeaf;
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
char const * leafEnd;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
char const * chainOffset = findChainOffsetExactOrPrefix( folded, exactMatch,
|
2009-04-14 16:35:47 +00:00
|
|
|
leaf, nextLeaf,
|
|
|
|
leafEnd );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( chainOffset && exactMatch )
|
|
|
|
{
|
|
|
|
result = readChain( chainOffset );
|
|
|
|
|
|
|
|
antialias( str, result );
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
class BtreeWordSearchRequest;
|
|
|
|
|
|
|
|
class BtreeWordSearchRunnable: public QRunnable
|
|
|
|
{
|
|
|
|
BtreeWordSearchRequest & r;
|
|
|
|
QSemaphore & hasExited;
|
|
|
|
|
|
|
|
public:
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
BtreeWordSearchRunnable( BtreeWordSearchRequest & r_,
|
|
|
|
QSemaphore & hasExited_ ): r( r_ ),
|
|
|
|
hasExited( hasExited_ )
|
|
|
|
{}
|
|
|
|
|
|
|
|
~BtreeWordSearchRunnable()
|
|
|
|
{
|
|
|
|
hasExited.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void run();
|
|
|
|
};
|
|
|
|
|
|
|
|
class BtreeWordSearchRequest: public Dictionary::WordSearchRequest
|
|
|
|
{
|
|
|
|
friend class BtreeWordSearchRunnable;
|
|
|
|
|
|
|
|
BtreeDictionary & dict;
|
|
|
|
wstring str;
|
|
|
|
unsigned long maxResults;
|
2009-04-17 13:51:50 +00:00
|
|
|
unsigned minLength;
|
|
|
|
int maxSuffixVariation;
|
|
|
|
bool allowMiddleMatches;
|
2009-03-26 19:00:08 +00:00
|
|
|
QAtomicInt isCancelled;
|
|
|
|
QSemaphore hasExited;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
BtreeWordSearchRequest( BtreeDictionary & dict_,
|
|
|
|
wstring const & str_,
|
2009-04-17 13:51:50 +00:00
|
|
|
unsigned minLength_,
|
|
|
|
int maxSuffixVariation_,
|
|
|
|
bool allowMiddleMatches_,
|
2009-03-26 19:00:08 +00:00
|
|
|
unsigned long maxResults_ ):
|
2009-04-17 13:51:50 +00:00
|
|
|
dict( dict_ ), str( str_ ),
|
|
|
|
maxResults( maxResults_ ),
|
|
|
|
minLength( minLength_ ),
|
|
|
|
maxSuffixVariation( maxSuffixVariation_ ),
|
|
|
|
allowMiddleMatches( allowMiddleMatches_ )
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
|
|
|
QThreadPool::globalInstance()->start(
|
|
|
|
new BtreeWordSearchRunnable( *this, hasExited ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(); // Run from another thread by BtreeWordSearchRunnable
|
|
|
|
|
|
|
|
virtual void cancel()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
~BtreeWordSearchRequest()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
hasExited.acquire();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void BtreeWordSearchRunnable::run()
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
r.run();
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void BtreeWordSearchRequest::run()
|
|
|
|
{
|
|
|
|
if ( isCancelled )
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2009-05-17 13:35:19 +00:00
|
|
|
|
|
|
|
if ( dict.ensureInitDone().size() )
|
|
|
|
{
|
|
|
|
setErrorString( QString::fromUtf8( dict.ensureInitDone().c_str() ) );
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
wstring folded = Folding::apply( str );
|
2012-09-14 23:07:01 +00:00
|
|
|
if( folded.empty() )
|
|
|
|
folded = Folding::applyWhitespaceOnly( str );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
int initialFoldedSize = folded.size();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
int charsLeftToChop = 0;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
if ( maxSuffixVariation >= 0 )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-17 13:51:50 +00:00
|
|
|
charsLeftToChop = initialFoldedSize - (int)minLength;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
if ( charsLeftToChop < 0 )
|
|
|
|
charsLeftToChop = 0;
|
2009-01-28 20:55:45 +00:00
|
|
|
else
|
2009-04-17 13:51:50 +00:00
|
|
|
if ( charsLeftToChop > maxSuffixVariation )
|
|
|
|
charsLeftToChop = maxSuffixVariation;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-09-17 14:12:39 +00:00
|
|
|
try
|
2009-04-17 13:51:50 +00:00
|
|
|
{
|
|
|
|
for( ; ; )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2013-09-17 14:12:39 +00:00
|
|
|
bool exactMatch;
|
2012-09-14 23:07:01 +00:00
|
|
|
|
2013-09-17 14:12:39 +00:00
|
|
|
vector< char > leaf;
|
|
|
|
uint32_t nextLeaf;
|
|
|
|
char const * leafEnd;
|
|
|
|
|
|
|
|
char const * chainOffset = dict.findChainOffsetExactOrPrefix( folded, exactMatch,
|
|
|
|
leaf, nextLeaf,
|
|
|
|
leafEnd );
|
|
|
|
|
|
|
|
if ( chainOffset )
|
|
|
|
for( ; ; )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2013-09-17 14:12:39 +00:00
|
|
|
if ( isCancelled )
|
|
|
|
break;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-09-17 14:12:39 +00:00
|
|
|
//DPRINTF( "offset = %u, size = %u\n", chainOffset - &leaf.front(), leaf.size() );
|
|
|
|
|
|
|
|
vector< WordArticleLink > chain = dict.readChain( chainOffset );
|
|
|
|
|
|
|
|
wstring chainHead = Utf8::decode( chain[ 0 ].word );
|
|
|
|
|
|
|
|
wstring resultFolded = Folding::apply( chainHead );
|
|
|
|
if( resultFolded.empty() )
|
|
|
|
resultFolded = Folding::applyWhitespaceOnly( chainHead );
|
|
|
|
|
|
|
|
if ( resultFolded.size() >= folded.size() && !resultFolded.compare( 0, folded.size(), folded ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2013-09-17 14:12:39 +00:00
|
|
|
// Exact or prefix match
|
|
|
|
|
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < chain.size(); ++x )
|
|
|
|
{
|
|
|
|
// Skip middle matches, if requested. If suffix variation is specified,
|
|
|
|
// make sure the string isn't larger than requested.
|
|
|
|
if ( ( allowMiddleMatches || Folding::apply( Utf8::decode( chain[ x ].prefix ) ).empty() ) &&
|
|
|
|
( maxSuffixVariation < 0 || (int)resultFolded.size() - initialFoldedSize <= maxSuffixVariation ) )
|
|
|
|
matches.push_back( Utf8::decode( chain[ x ].prefix + chain[ x ].word ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( matches.size() >= maxResults )
|
|
|
|
{
|
|
|
|
// For now we actually allow more than maxResults if the last
|
|
|
|
// chain yield more than one result. That's ok and maybe even more
|
|
|
|
// desirable.
|
|
|
|
break;
|
|
|
|
}
|
2009-04-17 13:51:50 +00:00
|
|
|
}
|
2013-09-17 14:12:39 +00:00
|
|
|
else
|
|
|
|
// Neither exact nor a prefix match, end this
|
2009-04-17 13:51:50 +00:00
|
|
|
break;
|
2013-09-17 14:12:39 +00:00
|
|
|
|
|
|
|
// Fetch new leaf if we're out of chains here
|
|
|
|
|
|
|
|
if ( chainOffset >= leafEnd )
|
2009-04-17 13:51:50 +00:00
|
|
|
{
|
2013-09-17 14:12:39 +00:00
|
|
|
// We're past the current leaf, fetch the next one
|
|
|
|
|
|
|
|
//DPRINTF( "advancing\n" );
|
|
|
|
|
|
|
|
if ( nextLeaf )
|
2009-04-17 13:51:50 +00:00
|
|
|
{
|
2013-09-17 14:12:39 +00:00
|
|
|
Mutex::Lock _( *dict.idxFileMutex );
|
|
|
|
|
|
|
|
dict.readNode( nextLeaf, leaf );
|
|
|
|
leafEnd = &leaf.front() + leaf.size();
|
|
|
|
|
|
|
|
nextLeaf = dict.idxFile->read< uint32_t >();
|
|
|
|
chainOffset = &leaf.front() + sizeof( uint32_t );
|
|
|
|
|
|
|
|
uint32_t leafEntries = *(uint32_t *)&leaf.front();
|
|
|
|
|
|
|
|
if ( leafEntries == 0xffffFFFF )
|
|
|
|
{
|
|
|
|
//DPRINTF( "bah!\n" );
|
|
|
|
exit( 1 );
|
|
|
|
}
|
2009-04-17 13:51:50 +00:00
|
|
|
}
|
2013-09-17 14:12:39 +00:00
|
|
|
else
|
|
|
|
break; // That was the last leaf
|
2009-04-17 13:51:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 14:12:39 +00:00
|
|
|
if ( isCancelled )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ( charsLeftToChop && !isCancelled )
|
|
|
|
{
|
|
|
|
--charsLeftToChop;
|
|
|
|
folded.resize( folded.size() - 1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2009-04-17 13:51:50 +00:00
|
|
|
}
|
2013-09-17 14:12:39 +00:00
|
|
|
}
|
|
|
|
catch( std::exception & e )
|
|
|
|
{
|
|
|
|
FDPRINTF( stderr, "Index searching failed: %s, error: %s\n",
|
|
|
|
dict.getName()->c_str(), e.what() );
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
FDPRINTF( stderr, "Index searching failed: %s\n",
|
|
|
|
dict.getName()->c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::WordSearchRequest > BtreeDictionary::prefixMatch(
|
|
|
|
wstring const & str, unsigned long maxResults )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
2009-04-17 13:51:50 +00:00
|
|
|
return new BtreeWordSearchRequest( *this, str, 0, -1, true, maxResults );
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::WordSearchRequest > BtreeDictionary::stemmedMatch(
|
|
|
|
wstring const & str, unsigned minLength, unsigned maxSuffixVariation,
|
|
|
|
unsigned long maxResults )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
return new BtreeWordSearchRequest( *this, str, minLength, (int)maxSuffixVariation,
|
|
|
|
false, maxResults );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
void BtreeIndex::readNode( uint32_t offset, vector< char > & out )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
idxFile->seek( offset );
|
|
|
|
|
|
|
|
uint32_t uncompressedSize = idxFile->read< uint32_t >();
|
|
|
|
uint32_t compressedSize = idxFile->read< uint32_t >();
|
|
|
|
|
2011-06-19 18:50:11 +00:00
|
|
|
//DPRINTF( "%x,%x\n", uncompressedSize, compressedSize );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
out.resize( uncompressedSize );
|
|
|
|
|
|
|
|
vector< unsigned char > compressedData( compressedSize );
|
|
|
|
|
|
|
|
idxFile->read( &compressedData.front(), compressedData.size() );
|
|
|
|
|
|
|
|
#ifdef __BTREE_USE_LZO
|
|
|
|
|
|
|
|
lzo_uint decompressedLength = out.size();
|
|
|
|
|
|
|
|
if ( lzo1x_decompress( &compressedData.front(), compressedData.size(),
|
|
|
|
(unsigned char *)&out.front(), &decompressedLength, 0 )
|
|
|
|
!= LZO_E_OK || decompressedLength != out.size() )
|
|
|
|
throw exFailedToDecompressNode();
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
unsigned long decompressedLength = out.size();
|
|
|
|
|
|
|
|
if ( uncompress( (unsigned char *)&out.front(),
|
|
|
|
&decompressedLength,
|
|
|
|
&compressedData.front(),
|
|
|
|
compressedData.size() ) != Z_OK ||
|
|
|
|
decompressedLength != out.size() )
|
|
|
|
throw exFailedToDecompressNode();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
char const * BtreeIndex::findChainOffsetExactOrPrefix( wstring const & target,
|
|
|
|
bool & exactMatch,
|
|
|
|
vector< char > & extLeaf,
|
|
|
|
uint32_t & nextLeaf,
|
|
|
|
char const * & leafEnd )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
if ( !idxFile )
|
|
|
|
throw exIndexWasNotOpened();
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
Mutex::Lock _( *idxFileMutex );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
// Lookup the index by traversing the index btree
|
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
vector< wchar > wcharBuffer;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
exactMatch = false;
|
|
|
|
|
|
|
|
// Read a node
|
|
|
|
|
|
|
|
uint32_t currentNodeOffset = rootOffset;
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
if ( !rootNodeLoaded )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-14 16:35:47 +00:00
|
|
|
// Time to load our root node. We do it only once, at the first request.
|
|
|
|
readNode( rootOffset, rootNode );
|
|
|
|
rootNodeLoaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const * leaf = &rootNode.front();
|
|
|
|
leafEnd = leaf + rootNode.size();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
for( ; ; )
|
|
|
|
{
|
2009-01-28 20:55:45 +00:00
|
|
|
// Is it a leaf or a node?
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
uint32_t leafEntries = *(uint32_t *)leaf;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( leafEntries == 0xffffFFFF )
|
|
|
|
{
|
|
|
|
// A node
|
|
|
|
|
2011-06-19 18:50:11 +00:00
|
|
|
//DPRINTF( "=>a node\n" );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
uint32_t const * offsets = (uint32_t *)leaf + 1;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
char const * ptr = leaf + sizeof( uint32_t ) +
|
2009-01-28 20:55:45 +00:00
|
|
|
( indexNodeSize + 1 ) * sizeof( uint32_t );
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
// ptr now points to a span of zero-separated strings, up to leafEnd.
|
|
|
|
// We find our match using a binary search.
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
char const * closestString;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
int compareResult;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
char const * window = ptr;
|
|
|
|
unsigned windowSize = leafEnd - ptr;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
// We boldly shoot in the middle of the whole mess, and then adjust
|
|
|
|
// to the beginning of the string that we've hit.
|
|
|
|
char const * testPoint = window + windowSize/2;
|
|
|
|
|
|
|
|
closestString = testPoint;
|
|
|
|
|
|
|
|
while( closestString > ptr && closestString[ -1 ] )
|
|
|
|
--closestString;
|
|
|
|
|
|
|
|
size_t wordSize = strlen( closestString );
|
|
|
|
|
|
|
|
if ( wcharBuffer.size() <= wordSize )
|
|
|
|
wcharBuffer.resize( wordSize + 1 );
|
|
|
|
|
|
|
|
long result = Utf8::decode( closestString, wordSize, &wcharBuffer.front() );
|
2012-09-14 23:07:01 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( result < 0 )
|
2009-04-14 16:35:47 +00:00
|
|
|
throw Utf8::exCantDecode( closestString );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
wcharBuffer[ result ] = 0;
|
|
|
|
|
2011-06-19 18:50:11 +00:00
|
|
|
//DPRINTF( "Checking against %s\n", closestString );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
compareResult = target.compare( &wcharBuffer.front() );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( !compareResult )
|
|
|
|
{
|
2009-04-14 16:35:47 +00:00
|
|
|
// The target string matches the current one. Finish the search.
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( compareResult < 0 )
|
|
|
|
{
|
|
|
|
// The target string is smaller than the current one.
|
|
|
|
// Go to the left.
|
2009-04-14 16:35:47 +00:00
|
|
|
windowSize = closestString - window;
|
|
|
|
|
|
|
|
if ( !windowSize )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The target string is larger than the current one.
|
|
|
|
// Go to the right.
|
|
|
|
windowSize -= ( closestString - window ) + wordSize + 1;
|
|
|
|
window = closestString + wordSize + 1;
|
|
|
|
|
|
|
|
if ( !windowSize )
|
|
|
|
break;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-04-14 16:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "The winner is %s, compareResult = %d\n", closestString, compareResult );
|
2009-04-14 16:35:47 +00:00
|
|
|
|
|
|
|
if ( closestString != ptr )
|
|
|
|
{
|
|
|
|
char const * left = closestString -1;
|
|
|
|
|
|
|
|
while( left != ptr && left[ -1 ] )
|
|
|
|
--left;
|
|
|
|
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "To the left: %s\n", left );
|
2009-04-14 16:35:47 +00:00
|
|
|
}
|
|
|
|
else
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "To the lest -- nothing\n" );
|
2009-04-14 16:35:47 +00:00
|
|
|
|
|
|
|
char const * right = closestString + strlen( closestString ) + 1;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
if ( right != leafEnd )
|
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "To the right: %s\n", right );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-04-14 16:35:47 +00:00
|
|
|
else
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "To the right -- nothing\n" );
|
2009-04-14 16:35:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Now, whatever the outcome (compareResult) is, we need to find
|
|
|
|
// entry number for the closestMatch string.
|
|
|
|
|
|
|
|
unsigned entry = 0;
|
|
|
|
|
|
|
|
for( char const * next = ptr; next != closestString;
|
|
|
|
next += strlen( next ) + 1, ++entry ) ;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
// Ok, now check the outcome
|
|
|
|
|
|
|
|
if ( !compareResult )
|
|
|
|
{
|
|
|
|
// The target string matches the one found.
|
|
|
|
// Go to the right, since it's there where we store such results.
|
|
|
|
currentNodeOffset = offsets[ entry + 1 ];
|
|
|
|
}
|
|
|
|
if ( compareResult < 0 )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-14 16:35:47 +00:00
|
|
|
// The target string is smaller than the one found.
|
|
|
|
// Go to the left.
|
2009-01-28 20:55:45 +00:00
|
|
|
currentNodeOffset = offsets[ entry ];
|
|
|
|
}
|
2009-04-14 16:35:47 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// The target string is larger than the one found.
|
|
|
|
// Go to the right.
|
|
|
|
currentNodeOffset = offsets[ entry + 1 ];
|
|
|
|
}
|
|
|
|
|
2011-06-19 18:50:11 +00:00
|
|
|
//DPRINTF( "reading node at %x\n", currentNodeOffset );
|
2009-04-14 16:35:47 +00:00
|
|
|
readNode( currentNodeOffset, extLeaf );
|
|
|
|
leaf = &extLeaf.front();
|
|
|
|
leafEnd = leaf + extLeaf.size();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
//DPRINTF( "=>a leaf\n" );
|
2009-01-28 20:55:45 +00:00
|
|
|
// A leaf
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
// If this leaf is the root, there's no next leaf, it just can't be.
|
|
|
|
// We do this check because the file's position indicator just won't
|
|
|
|
// be in the right place for root node anyway, since we precache it.
|
|
|
|
nextLeaf = ( currentNodeOffset != rootOffset ? idxFile->read< uint32_t >() : 0 );
|
|
|
|
|
|
|
|
if ( !leafEntries )
|
|
|
|
{
|
|
|
|
// Empty leaf? This may only be possible for entirely empty trees only.
|
|
|
|
if ( currentNodeOffset != rootOffset )
|
|
|
|
throw exCorruptedChainData();
|
|
|
|
else
|
|
|
|
return 0; // No match
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
// Build an array containing all chain pointers
|
|
|
|
char const * ptr = leaf + sizeof( uint32_t );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
uint32_t chainSize;
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
vector< char const * > chainOffsets( leafEntries );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-14 16:35:47 +00:00
|
|
|
char const ** nextOffset = &chainOffsets.front();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
while( leafEntries-- )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-14 16:35:47 +00:00
|
|
|
*nextOffset++ = ptr;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
memcpy( &chainSize, ptr, sizeof( uint32_t ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2011-06-19 18:50:11 +00:00
|
|
|
//DPRINTF( "%s + %s\n", ptr + sizeof( uint32_t ), ptr + sizeof( uint32_t ) + strlen( ptr + sizeof( uint32_t ) ) + 1 );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
ptr += sizeof( uint32_t ) + chainSize;
|
|
|
|
}
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
// Now do a binary search in it, aiming to find where our target
|
|
|
|
// string lands.
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
char const ** window = &chainOffsets.front();
|
|
|
|
unsigned windowSize = chainOffsets.size();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
for( ; ; )
|
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
//DPRINTF( "window = %u, ws = %u\n", window - &chainOffsets.front(), windowSize );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
char const ** chainToCheck = window + windowSize/2;
|
|
|
|
ptr = *chainToCheck;
|
|
|
|
|
|
|
|
memcpy( &chainSize, ptr, sizeof( uint32_t ) );
|
|
|
|
ptr += sizeof( uint32_t );
|
|
|
|
|
|
|
|
size_t wordSize = strlen( ptr );
|
|
|
|
|
|
|
|
if ( wcharBuffer.size() <= wordSize )
|
|
|
|
wcharBuffer.resize( wordSize + 1 );
|
|
|
|
|
2011-06-19 18:50:11 +00:00
|
|
|
//DPRINTF( "checking agaist word %s, left = %u\n", ptr, leafEntries );
|
2009-04-14 16:35:47 +00:00
|
|
|
|
|
|
|
long result = Utf8::decode( ptr, wordSize, &wcharBuffer.front() );
|
|
|
|
|
|
|
|
if ( result < 0 )
|
|
|
|
throw Utf8::exCantDecode( ptr );
|
|
|
|
|
|
|
|
wcharBuffer[ result ] = 0;
|
|
|
|
|
|
|
|
wstring foldedWord = Folding::apply( &wcharBuffer.front() );
|
2012-09-14 23:07:01 +00:00
|
|
|
if( foldedWord.empty() )
|
|
|
|
foldedWord = Folding::applyWhitespaceOnly( &wcharBuffer.front() );
|
2009-04-14 16:35:47 +00:00
|
|
|
|
|
|
|
int compareResult = target.compare( foldedWord );
|
|
|
|
|
|
|
|
if ( !compareResult )
|
|
|
|
{
|
|
|
|
// Exact match -- return and be done
|
|
|
|
exactMatch = true;
|
|
|
|
|
|
|
|
return ptr - sizeof( uint32_t );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( compareResult < 0 )
|
|
|
|
{
|
|
|
|
// The target string is smaller than the current one.
|
|
|
|
// Go to the first half
|
|
|
|
|
|
|
|
windowSize /= 2;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
if ( !windowSize )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-14 16:35:47 +00:00
|
|
|
// That finishes our search. Since our target string
|
|
|
|
// landed before the last tested chain, we return a possible
|
|
|
|
// prefix match against that chain.
|
2009-01-28 20:55:45 +00:00
|
|
|
return ptr - sizeof( uint32_t );
|
|
|
|
}
|
|
|
|
}
|
2009-04-14 16:35:47 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// The target string is larger than the current one.
|
|
|
|
// Go to the second half
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
windowSize -= windowSize/2 + 1;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
if ( !windowSize )
|
|
|
|
{
|
|
|
|
// That finishes our search. Since our target string
|
|
|
|
// landed after the last tested chain, we return the next
|
|
|
|
// chain. If there's no next chain in this leaf, this
|
|
|
|
// would mean the first element in the next leaf.
|
|
|
|
if ( chainToCheck == &chainOffsets.back() )
|
|
|
|
{
|
|
|
|
if ( nextLeaf )
|
|
|
|
{
|
|
|
|
readNode( nextLeaf, extLeaf );
|
|
|
|
|
|
|
|
leafEnd = &extLeaf.front() + extLeaf.size();
|
|
|
|
|
|
|
|
nextLeaf = idxFile->read< uint32_t >();
|
|
|
|
|
|
|
|
return &extLeaf.front() + sizeof( uint32_t );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0; // This was the last leaf
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return chainToCheck[ 1 ];
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
window = chainToCheck + 1;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
vector< WordArticleLink > BtreeIndex::readChain( char const * & ptr )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
uint32_t chainSize;
|
|
|
|
|
|
|
|
memcpy( &chainSize, ptr, sizeof( uint32_t ) );
|
|
|
|
|
|
|
|
ptr += sizeof( uint32_t );
|
|
|
|
|
|
|
|
vector< WordArticleLink > result;
|
|
|
|
|
|
|
|
vector< char > charBuffer;
|
|
|
|
|
|
|
|
while( chainSize )
|
|
|
|
{
|
|
|
|
string str = ptr;
|
|
|
|
ptr += str.size() + 1;
|
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
string prefix = ptr;
|
|
|
|
ptr += prefix.size() + 1;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
uint32_t articleOffset;
|
|
|
|
|
|
|
|
memcpy( &articleOffset, ptr, sizeof( uint32_t ) );
|
|
|
|
|
|
|
|
ptr += sizeof( uint32_t );
|
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
result.push_back( WordArticleLink( str, articleOffset, prefix ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
if ( chainSize < str.size() + 1 + prefix.size() + 1 + sizeof( uint32_t ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
throw exCorruptedChainData();
|
|
|
|
else
|
2009-04-08 16:02:12 +00:00
|
|
|
chainSize -= str.size() + 1 + prefix.size() + 1 + sizeof( uint32_t );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
void BtreeIndex::antialias( wstring const & str,
|
|
|
|
vector< WordArticleLink > & chain )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2013-07-06 18:18:43 +00:00
|
|
|
wstring caseFolded = Folding::applySimpleCaseOnly( gd::normalize( str ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
for( unsigned x = chain.size(); x--; )
|
|
|
|
{
|
|
|
|
// If after applying case folding to each word they wouldn't match, we
|
|
|
|
// drop the entry.
|
2013-07-06 18:18:43 +00:00
|
|
|
if ( Folding::applySimpleCaseOnly( gd::normalize( Utf8::decode( chain[ x ].prefix + chain[ x ].word ) ) ) !=
|
2009-01-28 20:55:45 +00:00
|
|
|
caseFolded )
|
|
|
|
chain.erase( chain.begin() + x );
|
2009-04-08 16:02:12 +00:00
|
|
|
else
|
|
|
|
if ( chain[ x ].prefix.size() ) // If there's a prefix, merge it with the word,
|
|
|
|
// since it's what dictionaries expect
|
|
|
|
{
|
|
|
|
chain[ x ].word.insert( 0, chain[ x ].prefix );
|
|
|
|
chain[ x ].prefix.clear();
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// A function which recursively creates btree node.
|
|
|
|
/// The nextIndex iterator is being iterated over and increased when building
|
|
|
|
/// leaf nodes.
|
|
|
|
static uint32_t buildBtreeNode( IndexedWords::const_iterator & nextIndex,
|
|
|
|
size_t indexSize,
|
|
|
|
File::Class & file, size_t maxElements,
|
|
|
|
uint32_t & lastLeafLinkOffset )
|
|
|
|
{
|
|
|
|
// We compress all the node data. This buffer would hold it.
|
|
|
|
vector< unsigned char > uncompressedData;
|
|
|
|
|
|
|
|
bool isLeaf = indexSize <= maxElements;
|
|
|
|
|
|
|
|
if ( isLeaf )
|
|
|
|
{
|
|
|
|
// A leaf.
|
|
|
|
|
|
|
|
uint32_t totalChainsLength = 0;
|
|
|
|
|
|
|
|
IndexedWords::const_iterator nextWord = nextIndex;
|
|
|
|
|
|
|
|
for( unsigned x = indexSize; x--; ++nextWord )
|
|
|
|
{
|
|
|
|
totalChainsLength += sizeof( uint32_t );
|
|
|
|
|
|
|
|
vector< WordArticleLink > const & chain = nextWord->second;
|
|
|
|
|
|
|
|
for( unsigned y = 0; y < chain.size(); ++y )
|
2009-04-08 16:02:12 +00:00
|
|
|
totalChainsLength += chain[ y ].word.size() + 1 + chain[ y ].prefix.size() + 1 + sizeof( uint32_t );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uncompressedData.resize( sizeof( uint32_t ) + totalChainsLength );
|
|
|
|
|
|
|
|
// First uint32_t indicates that this is a leaf.
|
|
|
|
*(uint32_t *)&uncompressedData.front() = indexSize;
|
|
|
|
|
|
|
|
unsigned char * ptr = &uncompressedData.front() + sizeof( uint32_t );
|
|
|
|
|
|
|
|
for( unsigned x = indexSize; x--; ++nextIndex )
|
|
|
|
{
|
|
|
|
vector< WordArticleLink > const & chain = nextIndex->second;
|
|
|
|
|
|
|
|
unsigned char * saveSizeHere = ptr;
|
|
|
|
|
|
|
|
ptr += sizeof( uint32_t );
|
|
|
|
|
|
|
|
uint32_t size = 0;
|
|
|
|
|
|
|
|
for( unsigned y = 0; y < chain.size(); ++y )
|
|
|
|
{
|
|
|
|
memcpy( ptr, chain[ y ].word.c_str(), chain[ y ].word.size() + 1 );
|
|
|
|
ptr += chain[ y ].word.size() + 1;
|
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
memcpy( ptr, chain[ y ].prefix.c_str(), chain[ y ].prefix.size() + 1 );
|
|
|
|
ptr += chain[ y ].prefix.size() + 1;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
memcpy( ptr, &(chain[ y ].articleOffset), sizeof( uint32_t ) );
|
|
|
|
ptr += sizeof( uint32_t );
|
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
size += chain[ y ].word.size() + 1 + chain[ y ].prefix.size() + 1 + sizeof( uint32_t );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( saveSizeHere, &size, sizeof( uint32_t ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// A node which will have children.
|
|
|
|
|
|
|
|
uncompressedData.resize( sizeof( uint32_t ) + ( maxElements + 1 ) * sizeof( uint32_t ) );
|
|
|
|
|
|
|
|
// First uint32_t indicates that this is a node.
|
|
|
|
*(uint32_t *)&uncompressedData.front() = 0xffffFFFF;
|
|
|
|
|
|
|
|
unsigned prevEntry = 0;
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < maxElements; ++x )
|
|
|
|
{
|
|
|
|
unsigned curEntry = (uint64_t) indexSize * ( x + 1 ) / ( maxElements + 1 );
|
|
|
|
|
|
|
|
uint32_t offset = buildBtreeNode( nextIndex,
|
|
|
|
curEntry - prevEntry,
|
|
|
|
file, maxElements,
|
|
|
|
lastLeafLinkOffset );
|
|
|
|
|
|
|
|
memcpy( &uncompressedData.front() + sizeof( uint32_t ) + x * sizeof( uint32_t ), &offset, sizeof( uint32_t ) );
|
|
|
|
|
2009-04-19 13:45:14 +00:00
|
|
|
size_t sz = nextIndex->first.size() + 1;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
size_t prevSize = uncompressedData.size();
|
2009-04-19 13:45:14 +00:00
|
|
|
uncompressedData.resize( prevSize + sz );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-19 13:45:14 +00:00
|
|
|
memcpy( &uncompressedData.front() + prevSize, nextIndex->first.c_str(),
|
|
|
|
sz );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
prevEntry = curEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rightmost child
|
|
|
|
uint32_t offset = buildBtreeNode( nextIndex,
|
|
|
|
indexSize - prevEntry,
|
|
|
|
file, maxElements,
|
|
|
|
lastLeafLinkOffset );
|
|
|
|
memcpy( &uncompressedData.front() + sizeof( uint32_t ) +
|
|
|
|
maxElements * sizeof( uint32_t ), &offset, sizeof( offset ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the result.
|
|
|
|
|
|
|
|
#ifdef __BTREE_USE_LZO
|
|
|
|
|
|
|
|
vector< unsigned char > compressedData( uncompressedData.size() + uncompressedData.size() / 16 + 64 + 3 );
|
|
|
|
|
|
|
|
char workMem[ LZO1X_1_MEM_COMPRESS ];
|
|
|
|
|
|
|
|
lzo_uint compressedSize;
|
|
|
|
|
|
|
|
if ( lzo1x_1_compress( &uncompressedData.front(), uncompressedData.size(),
|
|
|
|
&compressedData.front(), &compressedSize, workMem )
|
|
|
|
!= LZO_E_OK )
|
|
|
|
{
|
2011-06-19 20:29:11 +00:00
|
|
|
FDPRINTF( stderr, "Failed to compress btree node.\n" );
|
2009-01-28 20:55:45 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
vector< unsigned char > compressedData( compressBound( uncompressedData.size() ) );
|
|
|
|
|
|
|
|
unsigned long compressedSize = compressedData.size();
|
|
|
|
|
|
|
|
if ( compress( &compressedData.front(), &compressedSize,
|
|
|
|
&uncompressedData.front(), uncompressedData.size() ) != Z_OK )
|
|
|
|
{
|
2011-06-19 20:29:11 +00:00
|
|
|
FDPRINTF( stderr, "Failed to compress btree node.\n" );
|
2009-01-28 20:55:45 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint32_t offset = file.tell();
|
|
|
|
|
|
|
|
file.write< uint32_t >( uncompressedData.size() );
|
|
|
|
file.write< uint32_t >( compressedSize );
|
|
|
|
file.write( &compressedData.front(), compressedSize );
|
|
|
|
|
|
|
|
if ( isLeaf )
|
|
|
|
{
|
|
|
|
// A link to the next leef, which is zero and which will be updated
|
|
|
|
// should we happen to have another leaf.
|
|
|
|
|
|
|
|
file.write( ( uint32_t ) 0 );
|
|
|
|
|
|
|
|
uint32_t here = file.tell();
|
|
|
|
|
|
|
|
if ( lastLeafLinkOffset )
|
|
|
|
{
|
|
|
|
// Update the previous leaf to have the offset of this one.
|
|
|
|
file.seek( lastLeafLinkOffset );
|
|
|
|
file.write( offset );
|
|
|
|
file.seek( here );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure next leaf knows where to write its offset for us.
|
|
|
|
lastLeafLinkOffset = here - sizeof( uint32_t );
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2013-01-11 09:58:14 +00:00
|
|
|
void IndexedWords::addWord( wstring const & word, uint32_t articleOffset, unsigned int maxHeadwordSize )
|
2009-04-08 16:02:12 +00:00
|
|
|
{
|
2009-04-18 17:20:12 +00:00
|
|
|
wchar const * wordBegin = word.c_str();
|
2009-04-08 16:02:12 +00:00
|
|
|
string::size_type wordSize = word.size();
|
|
|
|
|
2011-07-27 04:26:59 +00:00
|
|
|
// Safeguard us against various bugs here. Don't attempt adding words
|
|
|
|
// which are freakishly huge.
|
2013-01-11 09:58:14 +00:00
|
|
|
if ( wordSize > maxHeadwordSize )
|
2011-07-27 04:26:59 +00:00
|
|
|
return;
|
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
// Skip any leading whitespace
|
|
|
|
while( *wordBegin && Folding::isWhitespace( *wordBegin ) )
|
|
|
|
{
|
|
|
|
++wordBegin;
|
|
|
|
--wordSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip any trailing whitespace
|
|
|
|
while( wordSize && Folding::isWhitespace( wordBegin[ wordSize - 1 ] ) )
|
|
|
|
--wordSize;
|
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
wchar const * nextChar = wordBegin;
|
2009-04-08 16:02:12 +00:00
|
|
|
|
|
|
|
vector< char > utfBuffer( wordSize * 4 );
|
|
|
|
|
2012-09-14 23:07:01 +00:00
|
|
|
int wordsAdded = 0; // Number of stored parts
|
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
// Skip any whitespace/punctuation
|
|
|
|
for( ; ; ++nextChar )
|
|
|
|
{
|
2012-09-14 23:07:01 +00:00
|
|
|
if ( !*nextChar ) // End of string ends everything
|
|
|
|
{
|
|
|
|
if( wordsAdded == 0)
|
|
|
|
{
|
|
|
|
wstring folded = Folding::applyWhitespaceOnly( wstring( wordBegin, wordSize ) );
|
|
|
|
if( !folded.empty() )
|
|
|
|
{
|
|
|
|
iterator i = insert(
|
|
|
|
IndexedWords::value_type(
|
|
|
|
string( &utfBuffer.front(),
|
|
|
|
Utf8::encode( folded.data(), folded.size(), &utfBuffer.front() ) ),
|
|
|
|
vector< WordArticleLink >() ) ).first;
|
|
|
|
|
|
|
|
// Try to conserve memory somewhat -- slow insertions are ok
|
|
|
|
i->second.reserve( i->second.size() + 1 );
|
|
|
|
|
|
|
|
string utfWord( &utfBuffer.front(),
|
|
|
|
Utf8::encode( wordBegin, wordSize, &utfBuffer.front() ) );
|
|
|
|
string utfPrefix;
|
|
|
|
i->second.push_back( WordArticleLink( utfWord, articleOffset, utfPrefix ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2009-04-08 16:02:12 +00:00
|
|
|
|
|
|
|
if ( !Folding::isWhitespace( *nextChar ) && !Folding::isPunct( *nextChar ) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert this word
|
2009-04-19 13:45:14 +00:00
|
|
|
wstring folded = Folding::apply( nextChar );
|
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
iterator i = insert(
|
|
|
|
IndexedWords::value_type(
|
2009-04-19 13:45:14 +00:00
|
|
|
string( &utfBuffer.front(),
|
|
|
|
Utf8::encode( folded.data(), folded.size(), &utfBuffer.front() ) ),
|
2009-04-08 16:02:12 +00:00
|
|
|
vector< WordArticleLink >() ) ).first;
|
|
|
|
|
|
|
|
if ( ( i->second.size() < 1024 ) || ( nextChar == wordBegin ) ) // Don't overpopulate chains with middle matches
|
|
|
|
{
|
|
|
|
// Try to conserve memory somewhat -- slow insertions are ok
|
|
|
|
i->second.reserve( i->second.size() + 1 );
|
|
|
|
|
|
|
|
string utfWord( &utfBuffer.front(),
|
|
|
|
Utf8::encode( nextChar, wordSize - ( nextChar - wordBegin ), &utfBuffer.front() ) );
|
|
|
|
|
|
|
|
string utfPrefix( &utfBuffer.front(),
|
|
|
|
Utf8::encode( wordBegin, nextChar - wordBegin, &utfBuffer.front() ) );
|
|
|
|
|
|
|
|
i->second.push_back( WordArticleLink( utfWord, articleOffset, utfPrefix ) );
|
|
|
|
}
|
|
|
|
|
2012-09-14 23:07:01 +00:00
|
|
|
wordsAdded += 1;
|
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
// Skip all non-whitespace/punctuation
|
|
|
|
for( ++nextChar; ; ++nextChar )
|
|
|
|
{
|
|
|
|
if ( !*nextChar )
|
|
|
|
return; // End of string ends everything
|
|
|
|
|
|
|
|
if ( Folding::isWhitespace( *nextChar ) || Folding::isPunct( *nextChar ) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
void IndexedWords::addSingleWord( wstring const & word, uint32_t articleOffset )
|
|
|
|
{
|
2012-09-14 23:07:01 +00:00
|
|
|
wstring folded = Folding::apply( word );
|
|
|
|
if( folded.empty() )
|
|
|
|
folded = Folding::applyWhitespaceOnly( word );
|
|
|
|
operator []( Utf8::encode( folded ) ).push_back(
|
2012-01-24 04:59:16 +00:00
|
|
|
WordArticleLink( Utf8::encode( word ), articleOffset ) );
|
2009-10-25 22:49:24 +00:00
|
|
|
}
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
IndexInfo buildIndex( IndexedWords const & indexedWords, File::Class & file )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
size_t indexSize = indexedWords.size();
|
|
|
|
IndexedWords::const_iterator nextIndex = indexedWords.begin();
|
|
|
|
|
|
|
|
// Skip any empty words. No point in indexing those, and some dictionaries
|
|
|
|
// are known to have buggy empty-word entries (Stardict's jargon for instance).
|
|
|
|
|
|
|
|
while( indexSize && nextIndex->first.empty() )
|
|
|
|
{
|
|
|
|
indexSize--;
|
|
|
|
++nextIndex;
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
// We try to stick to two-level tree for most dictionaries. Try finding
|
|
|
|
// the right size for it.
|
|
|
|
|
2009-04-29 23:18:26 +00:00
|
|
|
size_t btreeMaxElements = ( (size_t) sqrt( (double) indexSize ) ) + 1;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( btreeMaxElements < BtreeMinElements )
|
|
|
|
btreeMaxElements = BtreeMinElements;
|
|
|
|
else
|
|
|
|
if ( btreeMaxElements > BtreeMaxElements )
|
|
|
|
btreeMaxElements = BtreeMaxElements;
|
|
|
|
|
2012-10-31 13:58:35 +00:00
|
|
|
DPRINTF( "Building a tree of %u elements\n", (unsigned) btreeMaxElements );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
uint32_t lastLeafOffset = 0;
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
uint32_t rootOffset = buildBtreeNode( nextIndex, indexSize,
|
2009-01-28 20:55:45 +00:00
|
|
|
file, btreeMaxElements,
|
|
|
|
lastLeafOffset );
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
return IndexInfo( btreeMaxElements, rootOffset );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|