1
/*****************************************************************
2
* Unipro UGENE - Integrated Bioinformatics Suite
3
* Copyright (C) 2008 Unipro, Russia (http://ugene.unipro.ru)
6
* This source code is distributed under the terms of the
7
* GNU General Public License. See the files COPYING and LICENSE
9
*****************************************************************/
12
#include <core_api/AppContext.h>
14
#include <core_api/IOAdapter.h>
15
#include <ioadapter/LocalFileAdapter.h>
16
#include <ioadapter/ZlibAdapter.h>
18
#include "GetDocumentFromIndexTask.h"
22
const QString ACCESS_POINTS_SZ_TAG = "APS";
23
const QString ACCESS_POINT_TAG = "AP";
24
const QString ACCESS_POINT_WND_TAG = "APW";
25
const QString SPACE = " ";
27
const int ACCESS_POINTS_NUMS_AMOUNT = 3;
30
bool GetDocumentFromIndexTask::fillAccessPointNums( GZipIndexAccessPoint& point, const QString& numStr ) {
31
QStringList nums = numStr.split( SPACE, QString::SkipEmptyParts );
32
if( ACCESS_POINTS_NUMS_AMOUNT != nums.size() ) {
37
point.bits = nums[0].toInt( &ok );
38
if( !ok || 0 > point.bits ) {
41
point.in = nums[1].toLongLong( &ok );
45
point.out = nums[2].toLongLong( &ok );
52
bool GetDocumentFromIndexTask::getGzipIndexAccessPoint( GZipIndexAccessPoint& ret, const UIndex::IOSection& ioSec, qint64 offset ) {
53
assert( 0 <= offset );
55
if( !ioSec.keys.contains( ACCESS_POINTS_SZ_TAG ) ) {
59
int indSz = ioSec.keys[ACCESS_POINTS_SZ_TAG].toInt( &ok );
60
if( !ok || 0 >= indSz ) {
64
GZipIndexAccessPoint next;
65
ok = fillAccessPointNums( next, ioSec.keys.value( ACCESS_POINT_TAG + QString::number( 0 ) ) );
70
for( i = 0; i < indSz; ++i ) {
72
if( indSz - 1 == i ) {
76
ok = fillAccessPointNums( next, ioSec.keys.value( ACCESS_POINT_TAG + QString::number( i + 1 ) ) );
80
if( next.out > offset ) {
85
QString wndTag = ACCESS_POINT_WND_TAG + QString::number( i );
86
if( !ioSec.keys.contains( wndTag ) ) {
89
QByteArray wnd = QByteArray::fromBase64( ioSec.keys.value( wndTag ).toAscii() );
90
ret.window = qUncompress( wnd );
91
assert( GZipIndex::WINSIZE == ret.window.size() );
95
IOAdapter* GetDocumentFromIndexTask::getOpenedIOAdapter(const UIndex::ItemSection& itemSec, const UIndex::IOSection& ioSec) {
96
IOAdapterId adId = ioSec.ioAdapterId;
97
IOAdapterFactory* factory = AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById( adId );
99
if( NULL == factory ) {
100
setError(tr( "Can't find IO adapter: %1" ).arg( adId ));
103
IOAdapter* ret = factory->createIOAdapter();
104
bool ok = ret->open( ioSec.url, IOAdapterMode_Read );
107
setError(tr( "Can't open file for read: '%1'" ).arg(ioSec.url));
111
if( BaseIOAdapters::LOCAL_FILE == adId ) {
112
bool ok = ret->skip( itemSec.startOff );
115
setError(tr( "Error positioning in indexed file" ));
119
else if( BaseIOAdapters::GZIPPED_LOCAL_FILE == adId ) {
120
ZlibAdapter* gzAdapter = qobject_cast< ZlibAdapter* >( ret );
121
assert( NULL != gzAdapter );
123
GZipIndexAccessPoint accessPoint;
124
bool ok = getGzipIndexAccessPoint( accessPoint, ioSec, itemSec.startOff );
126
setError(tr( "GZIP index is corrupted" ));
130
ok = gzAdapter->skip( accessPoint, itemSec.startOff );
132
setError(tr( "Error positioning in indexed file" ));
137
else { // others not supported
143
GetDocumentFromIndexTask::GetDocumentFromIndexTask( const UIndex& ind, int num )
144
: Task( tr( "Get document from index" ), TaskFlag_None ), index( ind ), docNum( num ), doc( NULL ) {
145
tpm = Progress_Manual;
146
if( !index.hasItems() ) {
147
stateInfo.setError(tr( "Index is empty" ));
150
if( 0 > docNum || docNum >= index.items.size() ) {
151
stateInfo.setError(tr( "Invalid document number: %1, max: %2").arg(docNum).arg(index.items.size()));
156
GetDocumentFromIndexTask::~GetDocumentFromIndexTask() {
160
void GetDocumentFromIndexTask::run() {
161
if( stateInfo.hasErrors() ) {
164
UIndex::ItemSection itemSec = index.items[docNum];
165
UIndex::IOSection ioSec = index.getIOSection( itemSec.ioSectionId );
166
if( ioSec.sectionId.isEmpty() ) {
167
stateInfo.setError(tr( "Index is corrupted" ));
171
IOAdapter* ioAdapt = getOpenedIOAdapter( itemSec, ioSec);
172
if( NULL == ioAdapt ) {
173
assert( stateInfo.hasErrors() );
176
assert( ioAdapt->isOpen() );
177
DocumentFormat* df = AppContext::getDocumentFormatRegistry()->getFormatById( itemSec.docFormat );
180
stateInfo.setError(tr( "Unknown document format: %1" ).arg(itemSec.docFormat));
184
doc = df->loadExistingDocument( ioAdapt, stateInfo, QVariantMap() );
185
assert( isCanceled() || NULL != doc || hasErrors() );
186
assert( NULL == doc || doc->isLoaded() );
190
Task::ReportResult GetDocumentFromIndexTask::report() {
191
if ( stateInfo.hasErrors() || isCanceled() ) {
192
return ReportResult_Finished;
194
assert( NULL != doc );
195
return ReportResult_Finished;
198
void GetDocumentFromIndexTask::cleanup() {
205
Document* GetDocumentFromIndexTask::getDocument() const {
209
Document* GetDocumentFromIndexTask::takeDocument() {
1
/*****************************************************************
2
* Unipro UGENE - Integrated Bioinformatics Suite
3
* Copyright (C) 2008 Unipro, Russia (http://ugene.unipro.ru)
6
* This source code is distributed under the terms of the
7
* GNU General Public License. See the files COPYING and LICENSE
9
*****************************************************************/
12
#include <core_api/AppContext.h>
14
#include <core_api/IOAdapter.h>
15
#include <ioadapter/LocalFileAdapter.h>
16
#include <ioadapter/ZlibAdapter.h>
18
#include "GetDocumentFromIndexTask.h"
22
const QString ACCESS_POINTS_SZ_TAG = "APS";
23
const QString ACCESS_POINT_TAG = "AP";
24
const QString ACCESS_POINT_WND_TAG = "APW";
25
const QString SPACE = " ";
27
const int ACCESS_POINTS_NUMS_AMOUNT = 3;
30
bool GetDocumentFromIndexTask::fillAccessPointNums( GZipIndexAccessPoint& point, const QString& numStr ) {
31
QStringList nums = numStr.split( SPACE, QString::SkipEmptyParts );
32
if( ACCESS_POINTS_NUMS_AMOUNT != nums.size() ) {
37
point.bits = nums[0].toInt( &ok );
38
if( !ok || 0 > point.bits ) {
41
point.in = nums[1].toLongLong( &ok );
45
point.out = nums[2].toLongLong( &ok );
52
bool GetDocumentFromIndexTask::getGzipIndexAccessPoint( GZipIndexAccessPoint& ret, const UIndex::IOSection& ioSec, qint64 offset ) {
53
assert( 0 <= offset );
55
if( !ioSec.keys.contains( ACCESS_POINTS_SZ_TAG ) ) {
59
int indSz = ioSec.keys[ACCESS_POINTS_SZ_TAG].toInt( &ok );
60
if( !ok || 0 >= indSz ) {
64
GZipIndexAccessPoint next;
65
ok = fillAccessPointNums( next, ioSec.keys.value( ACCESS_POINT_TAG + QString::number( 0 ) ) );
70
for( i = 0; i < indSz; ++i ) {
72
if( indSz - 1 == i ) {
76
ok = fillAccessPointNums( next, ioSec.keys.value( ACCESS_POINT_TAG + QString::number( i + 1 ) ) );
80
if( next.out > offset ) {
85
QString wndTag = ACCESS_POINT_WND_TAG + QString::number( i );
86
if( !ioSec.keys.contains( wndTag ) ) {
89
QByteArray wnd = QByteArray::fromBase64( ioSec.keys.value( wndTag ).toAscii() );
90
ret.window = qUncompress( wnd );
91
assert( GZipIndex::WINSIZE == ret.window.size() );
95
IOAdapter* GetDocumentFromIndexTask::getOpenedIOAdapter(const UIndex::ItemSection& itemSec, const UIndex::IOSection& ioSec) {
96
IOAdapterId adId = ioSec.ioAdapterId;
97
IOAdapterFactory* factory = AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById( adId );
99
if( NULL == factory ) {
100
setError(tr( "Can't find IO adapter: %1" ).arg( adId ));
103
IOAdapter* ret = factory->createIOAdapter();
104
bool ok = ret->open( ioSec.url, IOAdapterMode_Read );
107
setError(tr( "Can't open file for read: '%1'" ).arg(ioSec.url));
111
if( BaseIOAdapters::LOCAL_FILE == adId ) {
112
bool ok = ret->skip( itemSec.startOff );
115
setError(tr( "Error positioning in indexed file" ));
119
else if( BaseIOAdapters::GZIPPED_LOCAL_FILE == adId ) {
120
ZlibAdapter* gzAdapter = qobject_cast< ZlibAdapter* >( ret );
121
assert( NULL != gzAdapter );
123
GZipIndexAccessPoint accessPoint;
124
bool ok = getGzipIndexAccessPoint( accessPoint, ioSec, itemSec.startOff );
126
setError(tr( "GZIP index is corrupted" ));
130
ok = gzAdapter->skip( accessPoint, itemSec.startOff );
132
setError(tr( "Error positioning in indexed file" ));
137
else { // others not supported
143
GetDocumentFromIndexTask::GetDocumentFromIndexTask( const UIndex& ind, int num )
144
: Task( tr( "Get document from index" ), TaskFlag_None ), index( ind ), docNum( num ), doc( NULL ) {
145
tpm = Progress_Manual;
146
if( !index.hasItems() ) {
147
stateInfo.setError(tr( "Index is empty" ));
150
if( 0 > docNum || docNum >= index.items.size() ) {
151
stateInfo.setError(tr( "Invalid document number: %1, max: %2").arg(docNum).arg(index.items.size()));
156
GetDocumentFromIndexTask::~GetDocumentFromIndexTask() {
160
void GetDocumentFromIndexTask::run() {
161
if( stateInfo.hasErrors() ) {
164
UIndex::ItemSection itemSec = index.items[docNum];
165
UIndex::IOSection ioSec = index.getIOSection( itemSec.ioSectionId );
166
if( ioSec.sectionId.isEmpty() ) {
167
stateInfo.setError(tr( "Index is corrupted" ));
171
IOAdapter* ioAdapt = getOpenedIOAdapter( itemSec, ioSec);
172
if( NULL == ioAdapt ) {
173
assert( stateInfo.hasErrors() );
176
assert( ioAdapt->isOpen() );
177
DocumentFormat* df = AppContext::getDocumentFormatRegistry()->getFormatById( itemSec.docFormat );
180
stateInfo.setError(tr( "Unknown document format: %1" ).arg(itemSec.docFormat));
184
doc = df->loadExistingDocument( ioAdapt, stateInfo, QVariantMap() );
185
assert( isCanceled() || NULL != doc || hasErrors() );
186
assert( NULL == doc || doc->isLoaded() );
190
Task::ReportResult GetDocumentFromIndexTask::report() {
191
if ( stateInfo.hasErrors() || isCanceled() ) {
192
return ReportResult_Finished;
194
assert( NULL != doc );
195
return ReportResult_Finished;
198
void GetDocumentFromIndexTask::cleanup() {
205
Document* GetDocumentFromIndexTask::getDocument() const {
209
Document* GetDocumentFromIndexTask::takeDocument() {