1 #ifndef DISPATCHER__HPP_INCLUDED
2 #define DISPATCHER__HPP_INCLUDED
3 /* */
4 
5 /*  $Id: dispatcher.hpp 610970 2020-06-26 12:56:46Z grichenk $
6 * ===========================================================================
7 *                            PUBLIC DOMAIN NOTICE
8 *               National Center for Biotechnology Information
9 *
10 *  This software/database is a "United States Government Work" under the
11 *  terms of the United States Copyright Act.  It was written as part of
12 *  the author's official duties as a United States Government employee and
13 *  thus cannot be copyrighted.  This software/database is freely available
14 *  to the public for use. The National Library of Medicine and the U.S.
15 *  Government have not placed any restriction on its use or reproduction.
16 *
17 *  Although all reasonable efforts have been taken to ensure the accuracy
18 *  and reliability of the software and data, the NLM and the U.S.
19 *  Government do not and cannot warrant the performance or results that
20 *  may be obtained by using this software or data. The NLM and the U.S.
21 *  Government disclaim all warranties, express or implied, including
22 *  warranties of performance, merchantability or fitness for any particular
23 *  purpose.
24 *
25 *  Please cite the author in any work or product based on this material.
26 * ===========================================================================
27 *
28 *  Author:  Eugene Vasilchenko
29 *
30 *  File Description: Dispatcher of readers/writers
31 *
32 */
33 
34 #include <corelib/ncbiobj.hpp>
35 #include <corelib/plugin_manager.hpp>
36 #include <objtools/data_loaders/genbank/reader.hpp>
37 #include <objtools/data_loaders/genbank/writer.hpp>
38 #include <objtools/data_loaders/genbank/impl/processor.hpp>
39 #include <objtools/data_loaders/genbank/impl/request_result.hpp>
40 
41 BEGIN_NCBI_SCOPE
42 
43 class CStopWatch;
44 
45 BEGIN_SCOPE(objects)
46 
47 class CBlob_id;
48 class CSeq_id_Handle;
49 class CTSE_Info;
50 class CTSE_Chunk_Info;
51 class CSeq_annot_SNP_Info;
52 
53 class CReaderRequestResult;
54 class CLoadLockBlob;
55 class CReadDispatcherCommand;
56 struct STimeStatistics;
57 
58 class NCBI_XREADER_EXPORT CReadDispatcher : public CObject
59 {
60 public:
61     typedef  CReader::TContentsMask             TContentsMask;
62     typedef  CReader::TBlobState                TBlobState;
63     typedef  CReader::TBlobVersion              TBlobVersion;
64     typedef  CReader::TBlobId                   TBlobId;
65     typedef  CReader::TChunkId                  TChunkId;
66     typedef  CReader::TChunkIds                 TChunkIds;
67     typedef  size_t                             TLevel;
68     typedef  vector<CSeq_id_Handle>             TIds;
69 
70     CReadDispatcher(void);
71     ~CReadDispatcher(void);
72 
73     // insert reader and set its m_Dispatcher
74     void InsertReader   (TLevel       level,  CRef<CReader>    reader);
75     void InsertWriter   (TLevel       level,  CRef<CWriter>    writer);
76     void InsertProcessor(CRef<CProcessor> processor);
77 
78     CWriter* GetWriter(const CReaderRequestResult& result,
79                        CWriter::EType type) const;
80     const CProcessor& GetProcessor(CProcessor::EType type) const;
81 
82     static bool CannotProcess(const CSeq_id_Handle& sih);
83 
84     void LoadSeq_idBlob_ids(CReaderRequestResult& result,
85                             const CSeq_id_Handle& seq_id,
86                             const SAnnotSelector* sel);
87     void LoadSeq_idSeq_ids(CReaderRequestResult& result,
88                            const CSeq_id_Handle& seq_id);
89     void LoadSeq_idGi(CReaderRequestResult& result,
90                       const CSeq_id_Handle& seq_id);
91     void LoadSeq_idAccVer(CReaderRequestResult& result,
92                           const CSeq_id_Handle& seq_id);
93     void LoadSeq_idLabel(CReaderRequestResult& result,
94                          const CSeq_id_Handle& seq_id);
95     void LoadSeq_idTaxId(CReaderRequestResult& result,
96                          const CSeq_id_Handle& seq_id);
97     void LoadSequenceHash(CReaderRequestResult& result,
98                           const CSeq_id_Handle& seq_id);
99     void LoadSequenceLength(CReaderRequestResult& result,
100                             const CSeq_id_Handle& seq_id);
101     void LoadSequenceType(CReaderRequestResult& result,
102                           const CSeq_id_Handle& seq_id);
103 
104     // bulk requests
105     typedef vector<bool> TLoaded;
106     typedef vector<bool> TKnown;
107     typedef vector<TGi> TGis;
108     typedef vector<string> TLabels;
109     typedef vector<TTaxId> TTaxIds;
110     typedef vector<int> THashes;
111     typedef vector<TSeqPos> TLengths;
112     typedef vector<CSeq_inst::EMol> TTypes;
113     typedef vector<TBlobState> TStates;
114     void LoadAccVers(CReaderRequestResult& result,
115                      const TIds& ids, TLoaded& loaded, TIds& ret);
116     void LoadGis(CReaderRequestResult& result,
117                  const TIds& ids, TLoaded& loaded, TGis& ret);
118     void LoadLabels(CReaderRequestResult& result,
119                     const TIds& ids, TLoaded& loaded, TLabels& ret);
120     void LoadTaxIds(CReaderRequestResult& result,
121                     const TIds& ids, TLoaded& loaded, TTaxIds& ret);
122     void LoadHashes(CReaderRequestResult& result,
123                     const TIds& ids, TLoaded& loaded,
124                     THashes& ret, TKnown& known);
125     void LoadLengths(CReaderRequestResult& result,
126                      const TIds& ids, TLoaded& loaded, TLengths& ret);
127     void LoadTypes(CReaderRequestResult& result,
128                    const TIds& ids, TLoaded& loaded, TTypes& ret);
129     void LoadStates(CReaderRequestResult& result,
130                     const TIds& ids, TLoaded& loaded, TStates& ret);
131 
132     static bool SetBlobState(size_t i,
133                              CReaderRequestResult& result,
134                              const TIds& ids, TLoaded& loaded,
135                              TStates& ret);
136 
137     void LoadBlobState(CReaderRequestResult& result,
138                        const TBlobId& blob_id);
139     void LoadBlobVersion(CReaderRequestResult& result,
140                          const TBlobId& blob_id,
141                          const CReader* asking_reader = 0);
142     void LoadBlobs(CReaderRequestResult& result,
143                    const CSeq_id_Handle& seq_id,
144                    TContentsMask mask,
145                    const SAnnotSelector* sel);
146     void LoadBlobs(CReaderRequestResult& result,
147                    const CLoadLockBlobIds& blobs,
148                    TContentsMask mask,
149                    const SAnnotSelector* sel);
150     void LoadBlob(CReaderRequestResult& result,
151                   const CBlob_id& blob_id);
152     void LoadBlob(CReaderRequestResult& result,
153                   const CBlob_Info& blob_info);
154     void LoadChunk(CReaderRequestResult& result,
155                    const TBlobId& blob_id, TChunkId chunk_id);
156     void LoadChunks(CReaderRequestResult& result,
157                     const TBlobId& blob_id,
158                     const TChunkIds& chunk_ids);
159     void LoadBlobSet(CReaderRequestResult& result,
160                      const TIds& seq_ids);
161 
162     void CheckReaders(void) const;
163     void Process(CReadDispatcherCommand& command,
164                  const CReader* asking_reader = 0);
165     void ResetCaches(void);
166 
167     static int CollectStatistics(void); // 0 - no stats, >1 - verbose
168 
169     static void LogStat(CReadDispatcherCommand& command,
170                         CReaderRequestResultRecursion& recursion);
171     static void LogStat(CReadDispatcherCommand& command,
172                         CReaderRequestResultRecursion& recursion, double size);
173 
174 private:
175     typedef map< TLevel,       CRef<CReader> >    TReaders;
176     typedef map< TLevel,       CRef<CWriter> >    TWriters;
177     typedef map< CProcessor::EType, CRef<CProcessor> > TProcessors;
178 
179     TReaders    m_Readers;
180     TWriters    m_Writers;
181     TProcessors m_Processors;
182 };
183 
184 
185 class NCBI_XREADER_EXPORT CReadDispatcherCommand
186 {
187 public:
188     CReadDispatcherCommand(CReaderRequestResult& result);
189     virtual ~CReadDispatcherCommand(void);
190 
191     virtual bool IsDone(void) = 0;
192 
193     // return false if it doesn't make sense to retry
194     virtual bool Execute(CReader& reader) = 0;
195 
196     virtual bool MayBeSkipped(void) const;
197 
198     virtual string GetErrMsg(void) const = 0;
199 
GetResult(void) const200     CReaderRequestResult& GetResult(void) const
201         {
202             return m_Result;
203         }
204 
205     virtual CGBRequestStatistics::EStatType GetStatistics(void) const = 0;
206     virtual string GetStatisticsDescription(void) const = 0;
207     virtual size_t GetStatisticsCount(void) const;
208 
209 private:
210     CReaderRequestResult& m_Result;
211 };
212 
213 
214 END_SCOPE(objects)
215 END_NCBI_SCOPE
216 
217 #endif//DISPATCHER__HPP_INCLUDED
218