zoukankan      html  css  js  c++  java
  • #include "CLucene.h" After a rather long search for the cause Strigi crash on PPC (e.g. Mac)use the flag ansi when compiling c++ code

    Common CLucene problems


    After a rather long search for the cause Strigi crash on PPC (e.g. Mac Mini), I found out that it is a good idea to always use the flag -ansi when compiling c++ code.

    What happened?

    Strigi uses CLucene as an index backend. By default it is compiled without -ansi. Strigi has -ansi as a default flag. This causes a problem when using the math.h header. This file, /usr/lib/gcc/powerpc-linux-gnu/4.1.2/include/bits/mathdef.h, defines the type float_t differently depending on the -ansi flag.
    # ifdef __GNUC__
    #  if  defined(__STRICT_ANSI__)
    typedef float float_t;
    typedef double double_t;
    #  else
    typedef double float_t;
    typedef double double_t;
    #  endif
    # else
    typedef double float_t;
    typedef double double_t;
    # endif
    Putting such macro magic in public header files is madness and can cause nasty bugs. For example, you can have a class like this:
    class Planet {
    float_t mass;
    };
    If you new allocate this class with 'new Planet()' you allocate either 4, 8 or 16 bytes depending on your flags and architecture. If this class is
    in a library, and you compile the code that uses the library with different flags than the ones with which you compiled the library, weird things
    will happen. Since your objects are smaller or bigger than you expect you will start writing in the wrong memory positions when working with the
    objects.

    I just spent quite some time tracking down a bug like this and thought it worthwhile to tell you about it so you are aware of this issue and can
    hopefully fix it faster then I did when you encounter it.

    How do I use the multi field search?

    #include "CLucene.h"
    #include "CLucene/util/Reader.h"
    #include <iostream>
    #include <conio.h>
    using namespace lucene::index;
    using namespace lucene::analysis;
    using namespace lucene::util;
    using namespace lucene::store;
    using namespace lucene::document;
    using namespace lucene::search;
    using namespace lucene::queryParser;
     
    #include "CLucene/queryParser/MultiFieldQueryParser.h"
     
    void searchMultiFields(char_t *dir, const char_t* qry, const char_t** fields, const int fieldsLen){
     
    l_byte_t* flags = new l_byte_t[fieldsLen];
     
    for ( int i=0;i<fieldsLen;i++ )
    flags[i] = lucene::queryParser::MultiFieldQueryParser::NORMAL_FIELD;
     
    standard::StandardAnalyzer analyzer;
    Query &q = MultiFieldQueryParser::Parse(qry,fields,fieldsLen,flags,analyzer);
    IndexReader& reader = IndexReader::open(dir);
    IndexSearcher searcher(reader);
    Hits& hits = searcher.search(q);
     
    const char_t toks[] = {','};
     
    for ( int i=0;i<hits.Length();i++ )
    {
    Document& doc = hits.doc(i);
    if ( &doc != NULL )
    {
    char_t* token = stringToken(_T("path"), toks);
    while ( token != NULL )
    {
    const char_t* f = doc.get(token);
    if(f)
    _cout << token << _T(": ") << f << endl;
    token = stringToken( NULL, toks );
    }
    }
    }
     
    delete[] flags;
    }
     
     
    int main()
    {
    wchar_t **fields = new wchar_t*[2];
    fields[0] = new wchar_t[10];
    fields[1] = new wchar_t[10];
     
    stringCopy(fields[0],_T("Title"));
    stringCopy(fields[1],_T("contents"));
     
    searchMultiFields(_T("YOUR INDEX PATH"), _T("QUERY STRING") , (const char_t**) fields, 2);
     
    for ( int i=0;i<2;i++ )
    {
    delete[] fields[i];
    }
    delete[] fields;
     
    return 0;
    }
  • 相关阅读:
    BZOJ4916: 神犇和蒟蒻 杜教筛
    BZOJ 4816: [Sdoi2017]数字表格 莫比乌斯反演
    BZOJ 4407: 于神之怒加强版 莫比乌斯反演 + 线筛积性函数
    BZOJ 3963: [WF2011]MachineWorks 斜率优化 + splay动态维护凸包
    BZOJ 1492: [NOI2007]货币兑换Cash 斜率优化 + splay动态维护凸包
    BZOJ 3306: 树 LCT + set 维护子树信息
    小A与最大子段和 斜率优化 + 二分 + 细节
    BZOJ 3675: [Apio2014]序列分割 动态规划 + 斜率优化 + 卡精度
    BZOJ 2726: [SDOI2012]任务安排 斜率优化 + 凸壳二分 + 卡精
    luoguP2365 任务安排 斜率优化 + 动态规划
  • 原文地址:https://www.cnblogs.com/cy163/p/1216428.html
Copyright © 2011-2022 走看看