~ubuntu-branches/ubuntu/karmic/ugene/karmic

« back to all changes in this revision

Viewing changes to src/core/src/util_tasks/GetDocumentFromIndexTask.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Morten Kjeldgaard
  • Date: 2009-06-16 13:20:00 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090616132000-a0eezpre9uvx91a5
Tags: 1.4.2+repack-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************
2
 
* Unipro UGENE - Integrated Bioinformatics Suite
3
 
* Copyright (C) 2008 Unipro, Russia (http://ugene.unipro.ru)
4
 
* All Rights Reserved
5
 
6
 
*     This source code is distributed under the terms of the
7
 
*     GNU General Public License. See the files COPYING and LICENSE
8
 
*     for details.
9
 
*****************************************************************/
10
 
 
11
 
 
12
 
#include <core_api/AppContext.h>
13
 
 
14
 
#include <core_api/IOAdapter.h>
15
 
#include <ioadapter/LocalFileAdapter.h>
16
 
#include <ioadapter/ZlibAdapter.h>
17
 
 
18
 
#include "GetDocumentFromIndexTask.h"
19
 
 
20
 
namespace GB2 {
21
 
 
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                = " ";
26
 
 
27
 
const int ACCESS_POINTS_NUMS_AMOUNT = 3;
28
 
 
29
 
 
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() ) {
33
 
        return false;
34
 
    }
35
 
    bool ok = false;
36
 
    
37
 
    point.bits = nums[0].toInt( &ok );
38
 
    if( !ok || 0 > point.bits ) {
39
 
        return false;
40
 
    }
41
 
    point.in = nums[1].toLongLong( &ok );
42
 
    if( !ok ) {
43
 
        return false;
44
 
    }
45
 
    point.out = nums[2].toLongLong( &ok );
46
 
    if( !ok ) {
47
 
        return false;
48
 
    }
49
 
    return true;
50
 
}
51
 
 
52
 
bool GetDocumentFromIndexTask::getGzipIndexAccessPoint( GZipIndexAccessPoint& ret, const UIndex::IOSection& ioSec, qint64 offset ) {
53
 
    assert( 0 <= offset );
54
 
    
55
 
    if( !ioSec.keys.contains( ACCESS_POINTS_SZ_TAG ) ) {
56
 
        return false;
57
 
    }
58
 
    bool ok = false;
59
 
    int indSz = ioSec.keys[ACCESS_POINTS_SZ_TAG].toInt( &ok );
60
 
    if( !ok || 0 >= indSz ) {
61
 
        return false;
62
 
    }
63
 
    
64
 
    GZipIndexAccessPoint next;
65
 
    ok = fillAccessPointNums( next, ioSec.keys.value( ACCESS_POINT_TAG + QString::number( 0 ) ) );
66
 
    if( !ok ) {
67
 
        return false;
68
 
    }
69
 
    int i = 0;
70
 
    for( i = 0; i < indSz; ++i ) {
71
 
        ret = next;
72
 
        if( indSz - 1 == i ) {
73
 
            break;
74
 
        }
75
 
        else {
76
 
            ok = fillAccessPointNums( next, ioSec.keys.value( ACCESS_POINT_TAG + QString::number( i + 1 ) ) );
77
 
            if( !ok ) {
78
 
                return false;
79
 
            }
80
 
            if( next.out > offset ) {
81
 
                break;
82
 
            }
83
 
        }
84
 
    }
85
 
    QString wndTag = ACCESS_POINT_WND_TAG + QString::number( i );
86
 
    if( !ioSec.keys.contains( wndTag ) ) {
87
 
        return false;
88
 
    }
89
 
    QByteArray wnd = QByteArray::fromBase64( ioSec.keys.value( wndTag ).toAscii() );
90
 
    ret.window = qUncompress( wnd );
91
 
    assert( GZipIndex::WINSIZE == ret.window.size() );
92
 
    return true;
93
 
}
94
 
 
95
 
IOAdapter* GetDocumentFromIndexTask::getOpenedIOAdapter(const UIndex::ItemSection& itemSec, const UIndex::IOSection& ioSec) {
96
 
    IOAdapterId adId = ioSec.ioAdapterId;
97
 
    IOAdapterFactory* factory = AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById( adId );
98
 
    
99
 
    if( NULL == factory ) {
100
 
        setError(tr( "Can't find IO adapter: %1" ).arg( adId ));
101
 
        return NULL;
102
 
    }
103
 
    IOAdapter* ret = factory->createIOAdapter();
104
 
    bool ok = ret->open( ioSec.url, IOAdapterMode_Read );
105
 
    if( !ok ) {
106
 
        delete ret;
107
 
        setError(tr( "Can't open file for read: '%1'" ).arg(ioSec.url));
108
 
        return NULL;
109
 
    }
110
 
    
111
 
    if( BaseIOAdapters::LOCAL_FILE == adId ) {
112
 
        bool ok = ret->skip( itemSec.startOff );
113
 
        if( !ok ) {
114
 
            delete ret;
115
 
            setError(tr( "Error positioning in indexed file" ));
116
 
            return NULL;
117
 
        }
118
 
    }
119
 
    else if( BaseIOAdapters::GZIPPED_LOCAL_FILE == adId ) {
120
 
        ZlibAdapter* gzAdapter = qobject_cast< ZlibAdapter* >( ret );
121
 
        assert( NULL != gzAdapter );
122
 
        
123
 
        GZipIndexAccessPoint accessPoint;
124
 
        bool ok = getGzipIndexAccessPoint( accessPoint, ioSec, itemSec.startOff );
125
 
        if( !ok ) {
126
 
            setError(tr( "GZIP index is corrupted" ));
127
 
            delete ret;
128
 
            return NULL;
129
 
        }
130
 
        ok = gzAdapter->skip( accessPoint, itemSec.startOff );
131
 
        if( !ok ) {
132
 
            setError(tr( "Error positioning in indexed file" ));
133
 
            delete ret;
134
 
            return NULL;
135
 
        }
136
 
    }
137
 
    else { // others not supported
138
 
        return NULL;
139
 
    }
140
 
    return ret;
141
 
}
142
 
 
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" ));
148
 
        return;
149
 
    }
150
 
    if( 0 > docNum || docNum >= index.items.size() ) {
151
 
        stateInfo.setError(tr( "Invalid document number: %1, max: %2").arg(docNum).arg(index.items.size()));
152
 
        return;
153
 
    }
154
 
}
155
 
 
156
 
GetDocumentFromIndexTask::~GetDocumentFromIndexTask() {
157
 
    cleanup();
158
 
}
159
 
 
160
 
void GetDocumentFromIndexTask::run() {
161
 
    if( stateInfo.hasErrors() ) {
162
 
        return;
163
 
    }
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" ));
168
 
        return;
169
 
    }
170
 
    
171
 
    IOAdapter* ioAdapt = getOpenedIOAdapter( itemSec, ioSec);
172
 
    if( NULL == ioAdapt ) {
173
 
        assert( stateInfo.hasErrors() );
174
 
        return;
175
 
    }
176
 
    assert( ioAdapt->isOpen() );
177
 
    DocumentFormat* df = AppContext::getDocumentFormatRegistry()->getFormatById( itemSec.docFormat );
178
 
    if( NULL == df ) {
179
 
        delete ioAdapt;
180
 
        stateInfo.setError(tr( "Unknown document format: %1" ).arg(itemSec.docFormat));
181
 
        return;
182
 
    }
183
 
    
184
 
    doc = df->loadExistingDocument( ioAdapt, stateInfo, QVariantMap() );
185
 
    assert( isCanceled() || NULL != doc || hasErrors() );
186
 
    assert( NULL == doc || doc->isLoaded() );
187
 
    delete ioAdapt;
188
 
}
189
 
 
190
 
Task::ReportResult GetDocumentFromIndexTask::report() {
191
 
    if ( stateInfo.hasErrors() || isCanceled() ) {
192
 
        return ReportResult_Finished;
193
 
    }
194
 
    assert( NULL != doc );
195
 
    return ReportResult_Finished;
196
 
}
197
 
 
198
 
void GetDocumentFromIndexTask::cleanup() {
199
 
    if( NULL != doc ) {
200
 
        delete doc;
201
 
        doc = NULL;
202
 
    }
203
 
}
204
 
 
205
 
Document* GetDocumentFromIndexTask::getDocument() const {
206
 
    return doc;
207
 
}
208
 
 
209
 
Document* GetDocumentFromIndexTask::takeDocument() {
210
 
    Document* ret = doc;
211
 
    doc = NULL;
212
 
    return ret;
213
 
}
214
 
 
215
 
} // GB2
 
1
/*****************************************************************
 
2
* Unipro UGENE - Integrated Bioinformatics Suite
 
3
* Copyright (C) 2008 Unipro, Russia (http://ugene.unipro.ru)
 
4
* All Rights Reserved
 
5
 
6
*     This source code is distributed under the terms of the
 
7
*     GNU General Public License. See the files COPYING and LICENSE
 
8
*     for details.
 
9
*****************************************************************/
 
10
 
 
11
 
 
12
#include <core_api/AppContext.h>
 
13
 
 
14
#include <core_api/IOAdapter.h>
 
15
#include <ioadapter/LocalFileAdapter.h>
 
16
#include <ioadapter/ZlibAdapter.h>
 
17
 
 
18
#include "GetDocumentFromIndexTask.h"
 
19
 
 
20
namespace GB2 {
 
21
 
 
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                = " ";
 
26
 
 
27
const int ACCESS_POINTS_NUMS_AMOUNT = 3;
 
28
 
 
29
 
 
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() ) {
 
33
        return false;
 
34
    }
 
35
    bool ok = false;
 
36
    
 
37
    point.bits = nums[0].toInt( &ok );
 
38
    if( !ok || 0 > point.bits ) {
 
39
        return false;
 
40
    }
 
41
    point.in = nums[1].toLongLong( &ok );
 
42
    if( !ok ) {
 
43
        return false;
 
44
    }
 
45
    point.out = nums[2].toLongLong( &ok );
 
46
    if( !ok ) {
 
47
        return false;
 
48
    }
 
49
    return true;
 
50
}
 
51
 
 
52
bool GetDocumentFromIndexTask::getGzipIndexAccessPoint( GZipIndexAccessPoint& ret, const UIndex::IOSection& ioSec, qint64 offset ) {
 
53
    assert( 0 <= offset );
 
54
    
 
55
    if( !ioSec.keys.contains( ACCESS_POINTS_SZ_TAG ) ) {
 
56
        return false;
 
57
    }
 
58
    bool ok = false;
 
59
    int indSz = ioSec.keys[ACCESS_POINTS_SZ_TAG].toInt( &ok );
 
60
    if( !ok || 0 >= indSz ) {
 
61
        return false;
 
62
    }
 
63
    
 
64
    GZipIndexAccessPoint next;
 
65
    ok = fillAccessPointNums( next, ioSec.keys.value( ACCESS_POINT_TAG + QString::number( 0 ) ) );
 
66
    if( !ok ) {
 
67
        return false;
 
68
    }
 
69
    int i = 0;
 
70
    for( i = 0; i < indSz; ++i ) {
 
71
        ret = next;
 
72
        if( indSz - 1 == i ) {
 
73
            break;
 
74
        }
 
75
        else {
 
76
            ok = fillAccessPointNums( next, ioSec.keys.value( ACCESS_POINT_TAG + QString::number( i + 1 ) ) );
 
77
            if( !ok ) {
 
78
                return false;
 
79
            }
 
80
            if( next.out > offset ) {
 
81
                break;
 
82
            }
 
83
        }
 
84
    }
 
85
    QString wndTag = ACCESS_POINT_WND_TAG + QString::number( i );
 
86
    if( !ioSec.keys.contains( wndTag ) ) {
 
87
        return false;
 
88
    }
 
89
    QByteArray wnd = QByteArray::fromBase64( ioSec.keys.value( wndTag ).toAscii() );
 
90
    ret.window = qUncompress( wnd );
 
91
    assert( GZipIndex::WINSIZE == ret.window.size() );
 
92
    return true;
 
93
}
 
94
 
 
95
IOAdapter* GetDocumentFromIndexTask::getOpenedIOAdapter(const UIndex::ItemSection& itemSec, const UIndex::IOSection& ioSec) {
 
96
    IOAdapterId adId = ioSec.ioAdapterId;
 
97
    IOAdapterFactory* factory = AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById( adId );
 
98
    
 
99
    if( NULL == factory ) {
 
100
        setError(tr( "Can't find IO adapter: %1" ).arg( adId ));
 
101
        return NULL;
 
102
    }
 
103
    IOAdapter* ret = factory->createIOAdapter();
 
104
    bool ok = ret->open( ioSec.url, IOAdapterMode_Read );
 
105
    if( !ok ) {
 
106
        delete ret;
 
107
        setError(tr( "Can't open file for read: '%1'" ).arg(ioSec.url));
 
108
        return NULL;
 
109
    }
 
110
    
 
111
    if( BaseIOAdapters::LOCAL_FILE == adId ) {
 
112
        bool ok = ret->skip( itemSec.startOff );
 
113
        if( !ok ) {
 
114
            delete ret;
 
115
            setError(tr( "Error positioning in indexed file" ));
 
116
            return NULL;
 
117
        }
 
118
    }
 
119
    else if( BaseIOAdapters::GZIPPED_LOCAL_FILE == adId ) {
 
120
        ZlibAdapter* gzAdapter = qobject_cast< ZlibAdapter* >( ret );
 
121
        assert( NULL != gzAdapter );
 
122
        
 
123
        GZipIndexAccessPoint accessPoint;
 
124
        bool ok = getGzipIndexAccessPoint( accessPoint, ioSec, itemSec.startOff );
 
125
        if( !ok ) {
 
126
            setError(tr( "GZIP index is corrupted" ));
 
127
            delete ret;
 
128
            return NULL;
 
129
        }
 
130
        ok = gzAdapter->skip( accessPoint, itemSec.startOff );
 
131
        if( !ok ) {
 
132
            setError(tr( "Error positioning in indexed file" ));
 
133
            delete ret;
 
134
            return NULL;
 
135
        }
 
136
    }
 
137
    else { // others not supported
 
138
        return NULL;
 
139
    }
 
140
    return ret;
 
141
}
 
142
 
 
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" ));
 
148
        return;
 
149
    }
 
150
    if( 0 > docNum || docNum >= index.items.size() ) {
 
151
        stateInfo.setError(tr( "Invalid document number: %1, max: %2").arg(docNum).arg(index.items.size()));
 
152
        return;
 
153
    }
 
154
}
 
155
 
 
156
GetDocumentFromIndexTask::~GetDocumentFromIndexTask() {
 
157
    cleanup();
 
158
}
 
159
 
 
160
void GetDocumentFromIndexTask::run() {
 
161
    if( stateInfo.hasErrors() ) {
 
162
        return;
 
163
    }
 
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" ));
 
168
        return;
 
169
    }
 
170
    
 
171
    IOAdapter* ioAdapt = getOpenedIOAdapter( itemSec, ioSec);
 
172
    if( NULL == ioAdapt ) {
 
173
        assert( stateInfo.hasErrors() );
 
174
        return;
 
175
    }
 
176
    assert( ioAdapt->isOpen() );
 
177
    DocumentFormat* df = AppContext::getDocumentFormatRegistry()->getFormatById( itemSec.docFormat );
 
178
    if( NULL == df ) {
 
179
        delete ioAdapt;
 
180
        stateInfo.setError(tr( "Unknown document format: %1" ).arg(itemSec.docFormat));
 
181
        return;
 
182
    }
 
183
    
 
184
    doc = df->loadExistingDocument( ioAdapt, stateInfo, QVariantMap() );
 
185
    assert( isCanceled() || NULL != doc || hasErrors() );
 
186
    assert( NULL == doc || doc->isLoaded() );
 
187
    delete ioAdapt;
 
188
}
 
189
 
 
190
Task::ReportResult GetDocumentFromIndexTask::report() {
 
191
    if ( stateInfo.hasErrors() || isCanceled() ) {
 
192
        return ReportResult_Finished;
 
193
    }
 
194
    assert( NULL != doc );
 
195
    return ReportResult_Finished;
 
196
}
 
197
 
 
198
void GetDocumentFromIndexTask::cleanup() {
 
199
    if( NULL != doc ) {
 
200
        delete doc;
 
201
        doc = NULL;
 
202
    }
 
203
}
 
204
 
 
205
Document* GetDocumentFromIndexTask::getDocument() const {
 
206
    return doc;
 
207
}
 
208
 
 
209
Document* GetDocumentFromIndexTask::takeDocument() {
 
210
    Document* ret = doc;
 
211
    doc = NULL;
 
212
    return ret;
 
213
}
 
214
 
 
215
} // GB2