NCBI C++ ToolKit
lds2_handlers.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef LDS2_HANDLERS_HPP__
2 #define LDS2_HANDLERS_HPP__
3 /* $Id: lds2_handlers.hpp 85614 2019-02-26 19:29:39Z grichenk $
4  * ===========================================================================
5  *
6  * PUBLIC DOMAIN NOTICE
7  * National Center for Biotechnology Information
8  *
9  * This software/database is a "United States Government Work" under the
10  * terms of the United States Copyright Act. It was written as part of
11  * the author's official duties as a United States Government employee and
12  * thus cannot be copyrighted. This software/database is freely available
13  * to the public for use. The National Library of Medicine and the U.S.
14  * Government have not placed any restriction on its use or reproduction.
15  *
16  * Although all reasonable efforts have been taken to ensure the accuracy
17  * and reliability of the software and data, the NLM and the U.S.
18  * Government do not and cannot warrant the performance or results that
19  * may be obtained by using this software or data. The NLM and the U.S.
20  * Government disclaim all warranties, express or implied, including
21  * warranties of performance, merchantability or fitness for any particular
22  * purpose.
23  *
24  * Please cite the author in any work or product based on this material.
25  *
26  * ===========================================================================
27  *
28  * Author: Aleksey Grichenko
29  *
30  * File Description: LDS v.2 URL handlers.
31  *
32  */
33 
34 #include <corelib/ncbiobj.hpp>
36 #include <deque>
37 
40 
41 
42 /// Base class for URL handler.
44 {
45 public:
46  /// Create a handler with the given name.
47  CLDS2_UrlHandler_Base(const string& handler_name)
48  : m_Name(handler_name) {}
49  virtual ~CLDS2_UrlHandler_Base(void) {}
50 
51  /// Get handler name
52  const string& GetHandlerName(void) const { return m_Name; }
53 
54  /// Fill file/url information - crc, size, format etc.
55  /// 'size' member must be set to >=0 if the file exists,
56  /// negative values indicate non-existent files/urls which
57  /// should be removed from the database.
58  /// The default implementation uses GetXXXX methods to fill
59  /// the information.
60  virtual void FillInfo(SLDS2_File& info);
61 
62  /// Save information about chunks for the URL in the database.
63  /// The default implementation does nothing.
64  virtual void SaveChunks(const SLDS2_File& /*file_info*/,
65  CLDS2_Database& /*db*/) {}
66 
67  /// Open input stream for the URL at the specified position.
68  /// The stream will be deleted by the caller.
69  /// Database is provided so that the handler can fetch
70  /// information about chunks.
71  /// Return NULL on error (e.g. the file does not exist).
72  virtual shared_ptr<CNcbiIstream> OpenStream(const SLDS2_File& /*file_info*/,
73  Int8 /*stream_pos*/,
74  CLDS2_Database* /*db*/) = 0;
75 
76 protected:
77  /// Allow to change handler name by derived classes.
78  void SetHandlerName(const string& new_name) { m_Name = new_name; }
79 
80  /// Methods for getting file information. Most of this information
81  /// (except format) is not critical and is used only to detect
82  /// modifications and reindex file in the database if at least
83  /// one field changes.
84 
85  /// Guess file format. The default implementation opens the
86  /// stream (see OpenStream) and uses CFormatGuess on it.
87  virtual SLDS2_File::TFormat GetFileFormat(const SLDS2_File& file_info);
88  /// Get file size - returns 0 by default. Negative values are used
89  /// to indicate non-existing files.
90  virtual Int8 GetFileSize(const SLDS2_File& /*file_info*/) { return 0; }
91  /// Get file CRC - returns 0 by default.
92  virtual Uint4 GetFileCRC(const SLDS2_File& /*file_info*/) { return 0; }
93  /// Get file timestamp - returns 0 by default.
94  virtual Int8 GetFileTime(const SLDS2_File& /*file_info*/) { return 0; }
95 
96 private:
97  string m_Name;
98 };
99 
100 
101 /// Default handler for local files - registered automatically
102 /// by LDS2 manager and data loader.
104 {
105 public:
106  CLDS2_UrlHandler_File(void);
107  ~CLDS2_UrlHandler_File(void) override {}
108 
109  /// Open input stream for the URL at the specified position.
110  /// The stream will be deleted by the caller.
111  shared_ptr<CNcbiIstream> OpenStream(const SLDS2_File& file_info,
112  Int8 stream_pos,
113  CLDS2_Database* db) override;
114 
115  static const string s_GetHandlerName(void);
116 
117 protected:
118  Int8 GetFileSize(const SLDS2_File& file_info) override;
119  Uint4 GetFileCRC(const SLDS2_File& file_info) override;
120  Int8 GetFileTime(const SLDS2_File& file_info) override;
121 
122  // Return cached stream if possible, or open a new one and cache it.
123  shared_ptr<CNcbiIstream> OpenOrGetStream(const SLDS2_File& file_info);
124 
125 private:
126  typedef shared_ptr<CNcbiIstream> TStream;
127  typedef pair<string, TStream> TNamedStream;
128  typedef deque<TNamedStream> TStreamCache;
129 
130  TStreamCache& x_GetStreamCache(void);
131 
133 };
134 
135 
136 /// Handler for GZip local files. Not registered by default.
138 {
139 public:
140  /// Create GZip file handler.
142  ~CLDS2_UrlHandler_GZipFile(void) override {}
143 
144  /// Save information about chunks for the URL in the database.
145  void SaveChunks(const SLDS2_File& file_info,
146  CLDS2_Database& db) override;
147 
148  /// Open input stream for the URL at the specified position.
149  /// The stream will be deleted by the caller.
150  shared_ptr<CNcbiIstream> OpenStream(const SLDS2_File& file_info,
151  Int8 stream_pos,
152  CLDS2_Database* db) override;
153 
154  static const string s_GetHandlerName(void);
155 };
156 
157 
160 
161 #endif // LDS2_HPP__
EFormat
The formats are checked in the same order as declared here.
Base class for URL handler.
CLDS2_UrlHandler_Base(const string &handler_name)
Create a handler with the given name.
virtual Uint4 GetFileCRC(const SLDS2_File &)
Get file CRC - returns 0 by default.
virtual ~CLDS2_UrlHandler_Base(void)
virtual Int8 GetFileTime(const SLDS2_File &)
Get file timestamp - returns 0 by default.
virtual void SaveChunks(const SLDS2_File &, CLDS2_Database &)
Save information about chunks for the URL in the database.
const string & GetHandlerName(void) const
Get handler name.
virtual Int8 GetFileSize(const SLDS2_File &)
Get file size - returns 0 by default.
virtual shared_ptr< CNcbiIstream > OpenStream(const SLDS2_File &, Int8, CLDS2_Database *)=0
Open input stream for the URL at the specified position.
void SetHandlerName(const string &new_name)
Allow to change handler name by derived classes.
Default handler for local files - registered automatically by LDS2 manager and data loader.
pair< string, TStream > TNamedStream
shared_ptr< CNcbiIstream > TStream
deque< TNamedStream > TStreamCache
~CLDS2_UrlHandler_File(void) override
CRef< CTls< TStreamCache > > m_StreamCache
Handler for GZip local files. Not registered by default.
~CLDS2_UrlHandler_GZipFile(void) override
CObject –.
Definition: ncbiobj.hpp:180
CRef –.
Definition: ncbiobj.hpp:618
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
#define NCBI_LDS2_EXPORT
Definition: ncbi_export.h:584
static MDB_envinfo info
Definition: mdb_load.c:37
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
LDS2 database.
Definition: lds2_db.hpp:57
Modified on Mon May 27 04:35:25 2024 by modify_doxy.py rev. 669887