3rdparty/licenseReport: Add seperate LGPL checks
[haiku.git] / src / add-ons / index_server / fulltext / FullTextAnalyser.cpp
blob6330806fd53359576dbe6f220751390007880103
1 /*
2 * Copyright 2010, Haiku.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Clemens Zeidler <haiku@clemens-zeidler.de>
7 */
8 #include "FullTextAnalyser.h"
10 #include <new>
12 #include <Directory.h>
13 #include <String.h>
14 #include <TranslatorFormats.h>
15 #include <TranslatorRoster.h>
17 #include "CLuceneDataBase.h"
18 #include "IndexServerPrivate.h"
21 #define DEBUG_FULLTEXT_ANALYSER
22 #ifdef DEBUG_FULLTEXT_ANALYSER
23 #include <stdio.h>
24 # define STRACE(x...) printf("FullTextAnalyser: " x)
25 #else
26 # define STRACE(x...) ;
27 #endif
30 FullTextAnalyser::FullTextAnalyser(BString name, const BVolume& volume)
32 FileAnalyser(name, volume),
34 fWriteDataBase(NULL),
35 fNUncommited(0)
37 BDirectory dir;
38 volume.GetRootDirectory(&dir);
39 fDataBasePath.SetTo(&dir);
40 fDataBasePath.Append(kIndexServerDirectory);
41 status_t status = fDataBasePath.Append(kFullTextDirectory);
43 if (status == B_OK)
44 fWriteDataBase = new CLuceneWriteDataBase(fDataBasePath);
48 FullTextAnalyser::~FullTextAnalyser()
50 delete fWriteDataBase;
54 status_t
55 FullTextAnalyser::InitCheck()
57 if (fDataBasePath.InitCheck() != B_OK)
58 return fDataBasePath.InitCheck();
59 if (!fWriteDataBase)
60 return B_NO_MEMORY;
62 return fWriteDataBase->InitCheck();
66 void
67 FullTextAnalyser::AnalyseEntry(const entry_ref& ref)
69 if (!_InterestingEntry(ref))
70 return;
72 //STRACE("FullTextAnalyser AnalyseEntry: %s %s\n", ref.name, path.Path());
73 fWriteDataBase->AddDocument(ref);
75 fNUncommited++;
76 if (fNUncommited > 100)
77 LastEntry();
81 void
82 FullTextAnalyser::DeleteEntry(const entry_ref& ref)
84 if (_IsInIndexDirectory(ref))
85 return;
86 STRACE("FullTextAnalyser DeleteEntry: %s\n", ref.name);
87 fWriteDataBase->RemoveDocument(ref);
91 void
92 FullTextAnalyser::MoveEntry(const entry_ref& oldRef, const entry_ref& newRef)
94 if (!_InterestingEntry(newRef))
95 return;
96 STRACE("FullTextAnalyser MoveEntry: %s to %s\n", oldRef.name, newRef.name);
97 fWriteDataBase->RemoveDocument(oldRef);
98 AnalyseEntry(newRef);
102 void
103 FullTextAnalyser::LastEntry()
105 fWriteDataBase->Commit();
106 fNUncommited = 0;
110 bool
111 FullTextAnalyser::_InterestingEntry(const entry_ref& ref)
113 if (_IsInIndexDirectory(ref))
114 return false;
116 BFile file(&ref, B_READ_ONLY);
117 translator_info translatorInfo;
118 if (BTranslatorRoster::Default()->Identify(&file, NULL, &translatorInfo, 0,
119 NULL, B_TRANSLATOR_TEXT) != B_OK)
120 return false;
122 return true;
126 bool
127 FullTextAnalyser::_IsInIndexDirectory(const entry_ref& ref)
129 BPath path(&ref);
130 if (BString(path.Path()).FindFirst(fDataBasePath.Path()) == 0)
131 return true;
133 if (BString(path.Path()).FindFirst("/boot/system/cache/tmp") == 0)
134 return true;
136 return false;
140 FullTextAddOn::FullTextAddOn(image_id id, const char* name)
142 IndexServerAddOn(id, name)
148 FileAnalyser*
149 FullTextAddOn::CreateFileAnalyser(const BVolume& volume)
151 return new (std::nothrow)FullTextAnalyser(Name(), volume);
155 extern "C" IndexServerAddOn* (instantiate_index_server_addon)(image_id id,
156 const char* name)
158 return new (std::nothrow)FullTextAddOn(id, name);