~ubuntu-branches/ubuntu/raring/clucene-core/raring-proposed

« back to all changes in this revision

Viewing changes to src/demo/IndexFiles.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-08-11 09:33:38 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120811093338-fgrx41ftqew3qt6a
Tags: 2.3.3.4-1
* New upstream release (Closes: #661703).
* Convert package to multiarch.
* Drop obsolete patches:
  - 01_add_missing_include_bug505667.diff
  - 02_posixness_fix_bug530308.diff
* Add patches:
  - Fixing_ZLIB_configuration_in_shared_CMakeLists.patch
  - Fix-pkgconfig-file-by-adding-clucene-shared-library.patch
  - Install-contribs-lib.patch
  - multiarch.patch
* Update debian/compat: bump to 8.
* Update debian/control:
  - update build dependencies (add cmake, libboost-dev and libz-dev).
  - bump Standards-Version to 3.9.3.
  - rename packages due to ABI bump: libclucene0ldbl -> libclucene-core1.
  - add libclucene-contribs1 package.
* Update debian/rules:
  - rewrite to use CMake.
  - add multiarch support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
* Distributable under the terms of either the Apache License (Version 2.0) or 
5
5
* the GNU Lesser General Public License, as specified in the COPYING file.
6
6
------------------------------------------------------------------------------*/
7
 
#include "stdafx.h"
 
7
 
 
8
#include <stdio.h>
 
9
#include <iostream>
 
10
#include <fstream>
 
11
#include <sys/stat.h>
 
12
#include <cctype>
 
13
#include <string.h>
 
14
#include <algorithm>
 
15
 
 
16
#include "CLucene/StdHeader.h"
 
17
#include "CLucene/_clucene-config.h"
8
18
 
9
19
#include "CLucene.h"
10
 
#include "CLucene/util/Reader.h"
 
20
#include "CLucene/util/CLStreams.h"
 
21
#include "CLucene/util/dirent.h"
 
22
#include "CLucene/config/repl_tchar.h"
11
23
#include "CLucene/util/Misc.h"
12
 
#include "CLucene/util/dirent.h"
13
 
#include <iostream>
14
 
#include <fstream>
 
24
#include "CLucene/util/StringBuffer.h"
15
25
 
16
26
using namespace std;
17
27
using namespace lucene::index;
20
30
using namespace lucene::store;
21
31
using namespace lucene::document;
22
32
 
23
 
Document* FileDocument(const char* f){
24
 
        // make a new, empty document
25
 
        Document* doc = _CLNEW Document();
26
 
 
27
 
        // Add the path of the file as a field named "path".  Use a Tex t field, so
28
 
        // that the index stores the path, and so that the path is searchable
29
 
   TCHAR tf[CL_MAX_DIR];
30
 
   STRCPY_AtoT(tf,f,CL_MAX_DIR);
31
 
   doc->add( *_CLNEW Field(_T("path"), tf, Field::STORE_YES | Field::INDEX_UNTOKENIZED ) );
32
 
 
33
 
        // Add the last modified date of the file a field named "modified".  Use a
34
 
        // Keyword field, so that it's searchable, but so that no attempt is made
35
 
        // to tokenize the field into words.
36
 
        //doc->add( *Field.Keyword("modified", DateField.timeToString(f->lastModified())));
37
 
 
38
 
        // Add the contents of the file a field named "contents".  Use a Text
39
 
        // field, specifying a Reader, so that the text of the file is tokenized.
40
 
 
41
 
    //read the data without any encoding. if you want to use special encoding
42
 
    //see the contrib/jstreams - they contain various types of stream readers
 
33
void FileDocument(const char* f, Document* doc){
 
34
 
 
35
    // Add the path of the file as a field named "path".  Use an indexed and stored field, so
 
36
    // that the index stores the path, and so that the path is searchable.
 
37
    TCHAR tf[CL_MAX_DIR];
 
38
    STRCPY_AtoT(tf,f,CL_MAX_DIR);
 
39
    doc->add( *_CLNEW Field(_T("path"), tf, Field::STORE_YES | Field::INDEX_UNTOKENIZED ) );
 
40
 
 
41
    // Add the last modified date of the file a field named "modified". Again, we make it
 
42
    // searchable, but no attempt is made to tokenize the field into words.
 
43
    //doc->add( *_CLNEW Field(_T("modified"), DateTools::timeToString(f->lastModified()), Field::STORE_YES | Field::INDEX_NO));
 
44
 
 
45
    // Add the contents of the file a field named "contents".  This time we use a tokenized
 
46
        // field so that the text can be searched for words in it.
 
47
 
 
48
    // Here we read the data without any encoding. If you want to use special encoding
 
49
    // see the contrib/jstreams - they contain various types of stream readers
43
50
    FILE* fh = fopen(f,"r");
44
51
        if ( fh != NULL ){
45
52
                StringBuffer str;
46
 
                // use fstat for portability
47
 
                int fn = fileno(fh);
48
 
                struct stat filestat;
49
 
                fstat(fn, &filestat);
50
 
                str.reserve(filestat.st_size);
51
 
                //str.reserve(fileSize(fh->_file));
52
53
                char abuf[1024];
53
54
                TCHAR tbuf[1024];
54
55
                size_t r;
61
62
                }while(r>0);
62
63
                fclose(fh);
63
64
 
64
 
                doc->add( *_CLNEW Field(_T("contents"),str.getBuffer(), Field::STORE_YES | Field::INDEX_TOKENIZED) );
65
 
        }
66
 
 
67
 
        //_tprintf(_T("%s\n"),doc->toString());
68
 
        // return the document
69
 
        return doc;
70
 
}
71
 
 
72
 
void indexDocs(IndexWriter* writer, char* directory) {
73
 
        DIR* dir = opendir(directory);
74
 
        if ( dir != NULL ){
75
 
                struct dirent* fl;
76
 
                
77
 
                struct fileStat buf;
78
 
 
79
 
                char path[CL_MAX_DIR];
80
 
                strcpy(path,directory);
81
 
                strcat(path,PATH_DELIMITERA);
82
 
                char* pathP = path + strlen(path);
83
 
 
84
 
                fl = readdir(dir);
85
 
                while ( fl != NULL ){
86
 
                        if ( (strcmp(fl->d_name, ".")) && (strcmp(fl->d_name, "..")) ) {
87
 
                        pathP[0]=0;
88
 
                        strcat(pathP,fl->d_name);
89
 
                        int32_t ret = fileStat(path,&buf);
90
 
                        if ( buf.st_mode & S_IFDIR ) {
91
 
                                indexDocs(writer, path );
92
 
                        }else{
93
 
                                printf( "adding: %s\n", fl->d_name );
94
 
 
95
 
                                Document* doc = FileDocument( path );
96
 
                                writer->addDocument( doc );
97
 
                                _CLDELETE(doc);
98
 
                        }
99
 
                }
100
 
                fl = readdir(dir);
101
 
 
102
 
                }
103
 
                closedir(dir);
104
 
        }else{
105
 
                    printf( "adding: %s\n", directory);
106
 
 
107
 
                    Document* doc = FileDocument( directory );
108
 
                    writer->addDocument( doc );
109
 
                    _CLDELETE(doc);
110
 
        }
111
 
}
112
 
void IndexFiles(char* path, char* target, const bool clearIndex){
 
65
                doc->add( *_CLNEW Field(_T("contents"), str.getBuffer(), Field::STORE_YES | Field::INDEX_TOKENIZED) );
 
66
        }
 
67
}
 
68
 
 
69
void indexDocs(IndexWriter* writer, const char* directory) {
 
70
    vector<string> files;
 
71
    std::sort(files.begin(),files.end());
 
72
    Misc::listFiles(directory,files,true);
 
73
    vector<string>::iterator itr = files.begin();
 
74
    
 
75
    // Re-use the document object
 
76
    Document doc;
 
77
    int i=0;
 
78
    while ( itr != files.end() ){
 
79
        const char* path = itr->c_str();
 
80
        printf( "adding file %d: %s\n", ++i, path );
 
81
 
 
82
        doc.clear();
 
83
        FileDocument( path, &doc );
 
84
        writer->addDocument( &doc );
 
85
        ++itr;
 
86
    }
 
87
}
 
88
void IndexFiles(const char* path, const char* target, const bool clearIndex){
113
89
        IndexWriter* writer = NULL;
114
 
        //lucene::analysis::SimpleAnalyzer* an = *_CLNEW lucene::analysis::SimpleAnalyzer();
115
 
        lucene::analysis::standard::StandardAnalyzer an;
 
90
        lucene::analysis::WhitespaceAnalyzer an;
116
91
        
117
92
        if ( !clearIndex && IndexReader::indexExists(target) ){
118
93
                if ( IndexReader::isLocked(target) ){
124
99
        }else{
125
100
                writer = _CLNEW IndexWriter( target ,&an, true);
126
101
        }
127
 
        writer->setMaxFieldLength(IndexWriter::DEFAULT_MAX_FIELD_LENGTH);
128
 
        /*printf("Set MaxFieldLength: ");
129
 
        char mfl[250];
130
 
        fgets(mfl,250,stdin);
131
 
        mfl[strlen(mfl)-1] = 0;
132
 
        if ( mfl[0] != 0 )
133
 
                writer->setMaxFieldLength(atoi(mfl));*/
134
 
        //writer->infoStream = cout; //TODO: infoStream - unicode
135
 
 
136
 
        uint64_t str = lucene::util::Misc::currentTimeMillis();
 
102
 
 
103
    //writer->setInfoStream(&std::cout);
 
104
 
 
105
    // We can tell the writer to flush at certain occasions
 
106
    //writer->setRAMBufferSizeMB(0.5);
 
107
    //writer->setMaxBufferedDocs(3);
 
108
 
 
109
    // To bypass a possible exception (we have no idea what we will be indexing...)
 
110
    writer->setMaxFieldLength(0x7FFFFFFFL); // LUCENE_INT32_MAX_SHOULDBE
 
111
    
 
112
    // Turn this off to make indexing faster; we'll turn it on later before optimizing
 
113
    writer->setUseCompoundFile(false);
 
114
 
 
115
        uint64_t str = Misc::currentTimeMillis();
137
116
 
138
117
        indexDocs(writer, path);
139
 
        writer->optimize();
140
 
        writer->close();
141
 
        _CLDELETE(writer);
 
118
        
 
119
    // Make the index use as little files as possible, and optimize it
 
120
    writer->setUseCompoundFile(true);
 
121
    writer->optimize();
 
122
        
 
123
    // Close and clean up
 
124
    writer->close();
 
125
        _CLLDELETE(writer);
142
126
 
143
 
        printf("Indexing took: %d ms.\n\n", lucene::util::Misc::currentTimeMillis() - str);
 
127
        printf("Indexing took: %d ms.\n\n", (int32_t)(Misc::currentTimeMillis() - str));
144
128
}