3 * $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
5 * This file is part of the Nepomuk KDE project.
6 * Copyright (C) 2006-2008 Sebastian Trueg <trueg@kde.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 * See the file "COPYING" for the exact licensing terms.
15 #include "repository.h"
16 #include "nepomukstorage-config.h"
17 #include "modelcopyjob.h"
20 #include "cluceneanalyzer.h"
23 #include <Soprano/Backend>
24 #include <Soprano/Global>
25 #include <Soprano/Version>
26 #include <Soprano/StorageModel>
27 #include <Soprano/Error/Error>
28 #include <Soprano/Vocabulary/Xesam>
29 #include <Soprano/Vocabulary/RDF>
31 #ifdef HAVE_SOPRANO_INDEX
32 #include <Soprano/Index/IndexFilterModel>
33 #include <Soprano/Index/CLuceneIndex>
36 #include <KStandardDirs>
38 #include <KConfigGroup>
39 #include <KSharedConfig>
41 #include <KNotification>
44 #include <QtCore/QTimer>
45 #include <QtCore/QThread>
46 #include <QtCore/QCoreApplication>
50 QString
createStoragePath( const QString
& repositoryId
)
52 return KStandardDirs::locateLocal( "data", "nepomuk/repository/" + repositoryId
+ "/" );
55 #if defined(HAVE_SOPRANO_INDEX) && defined(HAVE_CLUCENE) && SOPRANO_IS_VERSION(2,1,64)
56 class RebuildIndexThread
: public QThread
59 RebuildIndexThread( Soprano::Index::IndexFilterModel
* model
)
64 m_model
->rebuildIndex();
68 Soprano::Index::IndexFilterModel
* m_model
;
74 Nepomuk::Repository::Repository( const QString
& name
)
85 Nepomuk::Repository::~Repository()
91 void Nepomuk::Repository::close()
93 if ( m_state
== OPEN
) {
94 #ifdef HAVE_SOPRANO_INDEX
112 void Nepomuk::Repository::open()
114 Q_ASSERT( m_state
== CLOSED
);
119 // =================================
120 const Soprano::Backend
* backend
= activeSopranoBackend();
123 emit
opened( this, false );
128 // =================================
129 KConfigGroup repoConfig
= KSharedConfig::openConfig( "nepomukserverrc" )->group( name() + " Settings" );
130 QString oldBackendName
= repoConfig
.readEntry( "Used Soprano Backend", backend
->pluginName() );
131 QString oldBasePath
= repoConfig
.readPathEntry( "Storage Dir", QString() ); // backward comp: empty string means old storage path
133 // If possible we want to keep the old storage path. exception: oldStoragePath is empty. In that case we stay backwards
134 // compatible and convert the data to the new default location createStoragePath( name ) + "data/" + backend->pluginName()
136 // If we have a proper oldStoragePath and a different backend we use the oldStoragePath as basePath
137 // newDataPath = oldStoragePath + "data/" + backend->pluginName()
138 // oldDataPath = oldStoragePath + "data/" + oldBackendName
141 // create storage paths
142 // =================================
143 m_basePath
= oldBasePath
.isEmpty() ? createStoragePath( name() ) : oldBasePath
;
144 QString indexPath
= m_basePath
+ "index";
145 QString storagePath
= m_basePath
+ "data/" + backend
->pluginName();
147 KStandardDirs::makeDir( indexPath
);
148 KStandardDirs::makeDir( storagePath
);
150 kDebug(300002) << "opening repository '" << name() << "' at '" << m_basePath
<< "'";
154 // =================================
155 m_model
= backend
->createModel( QList
<Soprano::BackendSetting
>() << Soprano::BackendSetting( Soprano::BackendOptionStorageDir
, storagePath
) );
157 kDebug(300002) << "Unable to create model for repository" << name();
159 emit
opened( this, false );
163 kDebug(300002) << "Successfully created new model for repository" << name();
165 #if defined(HAVE_SOPRANO_INDEX) && defined(HAVE_CLUCENE)
166 m_analyzer
= new CLuceneAnalyzer();
167 m_index
= new Soprano::Index::CLuceneIndex( m_analyzer
);
169 if ( m_index
->open( indexPath
, true ) ) {
170 kDebug(300002) << "Successfully created new index for repository" << name();
171 m_indexModel
= new Soprano::Index::IndexFilterModel( m_index
, m_model
);
173 // FIXME: find a good value here
174 m_indexModel
->setTransactionCacheSize( 100 );
176 #if SOPRANO_IS_VERSION(2,0,99)
177 // no need for the whole content in the store, we only need it for searching
178 // (compare the strigi backend)
179 m_indexModel
->addIndexOnlyPredicate( Soprano::Vocabulary::Xesam::asText() );
181 #if SOPRANO_IS_VERSION(2,1,64)
182 m_indexModel
->addForceIndexPredicate( Soprano::Vocabulary::RDF::type() );
185 setParentModel( m_indexModel
);
188 kDebug(300002) << "Unable to open CLucene index for repo '" << name() << "': " << m_index
->lastError();
195 emit
opened( this, false );
199 setParentModel( m_model
);
202 // check if we have to convert
203 // =================================
204 bool convertingData
= false;
206 // if the backend changed we convert
207 // in case only the storage dir changes we normally would not have to convert but
208 // it is just simpler this way
209 if ( oldBackendName
!= backend
->pluginName() ||
210 oldBasePath
.isEmpty() ) {
212 kDebug() << "Previous backend:" << oldBackendName
<< "- new backend:" << backend
->pluginName();
213 kDebug() << "Old path:" << oldBasePath
<< "- new path:" << m_basePath
;
215 if ( oldBasePath
.isEmpty() ) {
216 // backward comp: empty string means old storage path
217 // and before we stored the data directly in the default basePath
218 m_oldStoragePath
= createStoragePath( name() );
221 m_oldStoragePath
= m_basePath
+ "data/" + oldBackendName
;
224 // try creating a model for the old storage
225 Soprano::Model
* oldModel
= 0;
226 m_oldStorageBackend
= Soprano::discoverBackendByName( oldBackendName
);
227 if ( m_oldStorageBackend
) {
228 // FIXME: even if there is no old data we still create a model here which results in a new empty db!
229 oldModel
= m_oldStorageBackend
->createModel( QList
<Soprano::BackendSetting
>() << Soprano::BackendSetting( Soprano::BackendOptionStorageDir
, m_oldStoragePath
) );
233 if ( !oldModel
->isEmpty() ) {
234 kDebug() << "Starting model conversion";
236 convertingData
= true;
237 // No need to use the index filter as it already contains the data
238 ModelCopyJob
* copyJob
= new ModelCopyJob( oldModel
, m_model
, this );
239 connect( copyJob
, SIGNAL( result( KJob
* ) ), this, SLOT( copyFinished( KJob
* ) ) );
247 // FIXME: inform the user
248 kDebug( 300002 ) << "Unable to convert old model.";
253 kDebug() << "no need to convert" << name();
258 // =================================
259 // do not save when converting yet. If converting is cancelled we would loose data.
260 // this way conversion is restarted the next time
261 if ( !convertingData
) {
262 repoConfig
.writeEntry( "Used Soprano Backend", backend
->pluginName() );
263 repoConfig
.writePathEntry( "Storage Dir", m_basePath
);
264 repoConfig
.sync(); // even if we crash the model has been created
266 if( m_state
== OPEN
) {
267 if ( !rebuildIndexIfNecessary() ) {
268 emit
opened( this, true );
273 KNotification::event( "convertingNepomukData",
274 i18nc("@info - notification message",
275 "Converting Nepomuk data to a new backend. This might take a while."),
276 KIcon( "nepomuk" ).pixmap( 32, 32 ) );
281 void Nepomuk::Repository::rebuildingIndexFinished()
283 KNotification::event( "rebuldingNepomukIndexDone",
284 i18nc("@info - notification message",
285 "Rebuilding Nepomuk full text search index for new features done."),
286 KIcon( "nepomuk" ).pixmap( 32, 32 ) );
288 // save our new settings
289 KConfigGroup repoConfig
= KSharedConfig::openConfig( "nepomukserverrc" )->group( name() + " Settings" );
290 repoConfig
.writeEntry( "rebuilt index for type indexing", true );
292 // inform that we are open and done
294 emit
opened( this, true );
298 void Nepomuk::Repository::copyFinished( KJob
* job
)
300 if ( job
->error() ) {
301 KNotification::event( "convertingNepomukDataFailed",
302 i18nc("@info - notification message",
303 "Converting Nepomuk data to the new backend failed. Data may still be recovered manually though."),
304 KIcon( "nepomuk" ).pixmap( 32, 32 ) );
306 kDebug( 300002 ) << "Converting old model failed.";
309 KNotification::event( "convertingNepomukDataDone",
310 i18nc("@info - notification message",
311 "Successfully converted Nepomuk data to the new backend."),
312 KIcon( "nepomuk" ).pixmap( 32, 32 ) );
314 kDebug() << "Successfully converted model data for repo" << name();
316 // delete the old model
317 ModelCopyJob
* copyJob
= qobject_cast
<ModelCopyJob
*>( job
);
318 delete copyJob
->source();
320 // cleanup the actual data
321 m_oldStorageBackend
->deleteModelData( QList
<Soprano::BackendSetting
>() << Soprano::BackendSetting( Soprano::BackendOptionStorageDir
, m_oldStoragePath
) );
323 // save our new settings
324 KConfigGroup repoConfig
= KSharedConfig::openConfig( "nepomukserverrc" )->group( name() + " Settings" );
325 repoConfig
.writeEntry( "Used Soprano Backend", activeSopranoBackend()->pluginName() );
326 repoConfig
.writePathEntry( "Storage Dir", m_basePath
);
329 if ( rebuildIndexIfNecessary() ) {
330 // opened will be emitted in rebuildingIndexFinished
335 // although converting might have failed, the new model is open anyway
337 emit
opened( this, true );
341 void Nepomuk::Repository::optimize()
343 QTimer::singleShot( 0, this, SLOT( slotDoOptimize() ) );
347 void Nepomuk::Repository::slotDoOptimize()
349 #ifdef HAVE_SOPRANO_INDEX
350 #if SOPRANO_IS_VERSION(2,1,60)
357 bool Nepomuk::Repository::rebuildIndexIfNecessary()
359 #if defined(HAVE_SOPRANO_INDEX) && defined(HAVE_CLUCENE) && SOPRANO_IS_VERSION(2,1,64)
360 KConfigGroup repoConfig
= KSharedConfig::openConfig( "nepomukserverrc" )->group( name() + " Settings" );
361 if( !repoConfig
.readEntry( "rebuilt index for type indexing", false ) ) {
362 KNotification::event( "rebuldingNepomukIndex",
363 i18nc("@info - notification message",
364 "Rebuilding Nepomuk full text search index for new features. This will only be done once and might take a while."),
365 KIcon( "nepomuk" ).pixmap( 32, 32 ) );
366 RebuildIndexThread
* rit
= new RebuildIndexThread( m_indexModel
);
367 connect( rit
, SIGNAL( finished() ), this, SLOT( rebuildingIndexFinished() ) );
368 connect( rit
, SIGNAL( finished() ), rit
, SLOT( deleteLater() ) );
377 const Soprano::Backend
* Nepomuk::Repository::activeSopranoBackend()
379 QString backendName
= KSharedConfig::openConfig( "nepomukserverrc" )->group( "Basic Settings" ).readEntry( "Soprano Backend", "sesame2" );
380 const Soprano::Backend
* backend
= ::Soprano::discoverBackendByName( backendName
);
382 kDebug(300002) << "(Nepomuk::Core::Core) could not find backend" << backendName
<< ". Falling back to default.";
383 backend
= ::Soprano::usedBackend();
386 kDebug(300002) << "(Nepomuk::Core::Core) could not find a backend.";
391 #include "repository.moc"