OpenZGY/C++ API and Internals (ALPHA)
Access seismic data stored in ZGY format.
file.h
Go to the documentation of this file.
1 // Copyright 2017-2020, Schlumberger
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
49 #include <cstdint>
50 #include <vector>
51 #include <string>
52 #include <memory>
53 #include <functional>
54 
55 #include "../declspec.h"
56 #include "timer.h"
57 
58 namespace OpenZGY {
59  class IOContext;
60 }
61 
62 namespace InternalZGY {
63 #if 0
64 }
65 #endif
66 
67 enum class OpenMode
68 {
69  Closed = 0,
70  ReadOnly,
71  ReadWrite,
72  Truncate,
73 };
74 
75 enum class UsageHint
76 {
77  Unknown = 0x00,
78  TextFile = 0x01,
79  Header = 0x10,
80  Data = 0x20,
81  Compressed = 0x40,
82  Mixed = 0x40,
83 };
84 
85 
97 class Config
98 {
99 public:
100  virtual ~Config();
101  virtual std::string dump() const = 0;
102 
103 protected:
104  static std::string _get_string_env(
105  const std::string& value_from_cf,
106  const std::string& cfname,
107  const std::string& envname,
108  const std::string& dflt);
109 
110  static std::int64_t _get_numeric_env(
111  std::int64_t value_from_cf,
112  const std::string& cfname,
113  const std::string& envname,
114  std::int64_t dflt,
115  std::int64_t min_value,
116  std::int64_t max_value);
117 
118  static std::string _redact(const std::string&);
119 };
120 
121 class FileConfig : public Config
122 {
123 public:
124  explicit FileConfig(const OpenZGY::IOContext *context);
125  virtual std::string dump() const override;
126 };
127 
129 {
130 public:
131  typedef std::function<void(const void*, std::int64_t)> delivery_t;
132  std::int64_t offset;
133  std::int64_t size;
134  delivery_t delivery;
135  ReadRequest(std::int64_t offset_in, std::int64_t size_in, const delivery_t& delivery_in)
136  : offset(offset_in)
137  , size(size_in)
138  , delivery(delivery_in)
139  {
140  }
141 };
142 
143 typedef std::vector<ReadRequest> ReadList;
144 typedef std::vector<ReadList> ReadDoubleList;
145 
146 class FileADT
147 {
148 public:
149  virtual ~FileADT();
150 
158  virtual void xx_read(void *data, std::int64_t offset, std::int64_t size, UsageHint usagehint=UsageHint::Unknown) = 0;
159 
194  virtual void xx_readv(const ReadList& requests, bool parallel_ok=false, bool immutable_ok=false, bool transient_ok=false, UsageHint usagehint=UsageHint::Unknown) = 0;
195 
202  virtual void xx_write(const void* data, std::int64_t offset, std::int64_t size, UsageHint usagehint=UsageHint::Unknown) = 0;
203 
209  virtual void xx_close() = 0;
210 
214  virtual std::int64_t xx_eof() const = 0;
215 
219  virtual bool xx_threadsafe() const = 0;
220 
225  virtual bool xx_iscloud() const = 0;
226 
227 protected:
228  static std::string _nice(std::int64_t n);
229  static void _validate_read(void *data, std::int64_t offset, std::int64_t size, std::int64_t eof, OpenMode mode);
230  static void _validate_write(const void *data, std::int64_t offset, std::int64_t size, OpenMode mode);
231  static void _validate_readv(const ReadList& requests, std::int64_t eof, OpenMode mode);
232 
233 public:
234  static std::shared_ptr<FileADT> factory(const std::string& filename, OpenMode mode, const OpenZGY::IOContext *iocontext);
235 };
236 
237 class OPENZGY_TEST_API FileFactory
238 {
239 public:
240  typedef std::function<std::shared_ptr<FileADT>(const std::string&, OpenMode, const OpenZGY::IOContext*)> factory_t;
241 
242 public:
243  std::shared_ptr<FileADT> create(const std::string& filename, OpenMode mode, const OpenZGY::IOContext *iocontext);
244  void add_factory(const factory_t& factory);
245  static FileFactory& instance();
246 
247 private:
248  FileFactory();
249  FileFactory(const FileFactory&) = delete;
250  FileFactory& operator=(const FileFactory&) = delete;
251 
252 private:
253  std::vector<factory_t> _registry;
254 };
255 
262 class FileCommon : public FileADT
263 {
264 protected:
265  FileConfig _config;
266  OpenMode _mode;
267  std::string _name;
268  std::int64_t _eof;
269  std::shared_ptr<SummaryTimer> _rtimer;
270  std::shared_ptr<SummaryTimer> _wtimer;
271 
272 public:
273  FileCommon(const std::string& filename, OpenMode mode, const OpenZGY::IOContext *iocontext);
274  // NEW functions
275  virtual std::int64_t _real_eof() const;
276  virtual void _check_short_read(std::int64_t offset, std::int64_t size, std::int64_t got) const;
277 };
278 
279 }
InternalZGY::FileADT
Definition: file.h:146
OpenZGY::IOContext
Base class for backend specific context.
Definition: iocontext.h:49
InternalZGY::Config
Definition: file.h:97
OpenZGY
The entire public API is in this namespace.
Definition: api.cpp:68
InternalZGY::FileFactory
Definition: file.h:237
InternalZGY::ReadRequest
Definition: file.h:128
timer.h
Easy to use performance measurement.
InternalZGY::FileConfig
Definition: file.h:121
InternalZGY
Implementation not visible to clients.
InternalZGY::FileCommon
Implementation of some methods that might be shared.
Definition: file.h:262