TraceDataManager.h 6.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/****************************************************************************
** Copyright 2020 The Open Group
** Copyright 2020 Bluware, Inc.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
****************************************************************************/

#ifndef TRACE_DATA_MANAGER_H
#define TRACE_DATA_MANAGER_H

#include "SEGYFileInfo.h"

// Convenience layer that works with a DataViewManager to access trace data
//
class TraceDataManager
{
public:
Jim King's avatar
Jim King committed
28
  TraceDataManager(std::shared_ptr<DataViewManager> dvm, int64_t tracesPerPage, int64_t traceByteSize, int64_t numTraces) :
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    m_dataViewManager(dvm),
    m_tracesPerPage(tracesPerPage),
    m_traceByteSize(traceByteSize),
    m_numTraces(numTraces),
    m_currentPageTrace(-1)
  {
  }

  void
  addDataRequests(int requestSecondaryKeyStart, int requestSecondaryKeyEnd, const std::vector<SEGYSegmentInfo>::iterator& lower, const std::vector<SEGYSegmentInfo>::iterator& upper)
  {
    std::vector<DataRequestInfo> requests;

    for (auto segment = lower; segment != upper; ++segment)
    {
44
45
46
      const bool
        isSecondaryAscending = segment->m_binInfoStart.m_crosslineNumber <= segment->m_binInfoStop.m_crosslineNumber;

47
      // does this segment have traces within the request secondary key range?
48
49
      if (requestSecondaryKeyEnd < (isSecondaryAscending ? segment->m_binInfoStart.m_crosslineNumber : segment->m_binInfoStop.m_crosslineNumber)
          || requestSecondaryKeyStart > (isSecondaryAscending ? segment->m_binInfoStop.m_crosslineNumber : segment->m_binInfoStart.m_crosslineNumber))
50
51
52
53
54
55
56
57
58
59
60
        continue;

      if (segment->m_traceStart == segment->m_traceStop)
      {
        // The segment is only one trace long, and that trace is in the request range, so add a request
        addTraceRequests(requests, segment->m_traceStart, segment->m_traceStart);
      }
      else
      {
        // axis factors for guessing trace numbers
        const auto
61
          secondaryKeySpanTotal = static_cast<double>(std::abs(segment->m_binInfoStop.m_crosslineNumber - segment->m_binInfoStart.m_crosslineNumber));
62
        const auto
63
64
          secondaryStartSpan = isSecondaryAscending ? requestSecondaryKeyStart - segment->m_binInfoStart.m_crosslineNumber : segment->m_binInfoStart.m_crosslineNumber - requestSecondaryKeyEnd,
          secondaryStopSpan = isSecondaryAscending ? segment->m_binInfoStop.m_crosslineNumber - requestSecondaryKeyEnd : requestSecondaryKeyStart - segment->m_binInfoStop.m_crosslineNumber;
65
66
67
68
69
70
71
72
73
74
75
        const double
          traceStartFactor = secondaryStartSpan <= 0 ? 0.0 : secondaryStartSpan / secondaryKeySpanTotal,
          traceStopFactor = secondaryStopSpan <= 0 ? 1.0 : 1.0 - secondaryStopSpan / secondaryKeySpanTotal;

        // calculate approximate start and end traces
        const auto
          traceDistance = segment->m_traceStop - segment->m_traceStart;
        const auto
          startTrace = static_cast<int64_t>(segment->m_traceStart + traceDistance * traceStartFactor),
          stopTrace = static_cast<int64_t>(segment->m_traceStart + traceDistance * traceStopFactor);

76
77
78
        assert(startTrace >= segment->m_traceStart && startTrace <= segment->m_traceStop);
        assert(stopTrace >= segment->m_traceStart && stopTrace <= segment->m_traceStop);

79
80
        addTraceRequests(requests, startTrace, stopTrace);
      }
81
82
    }

Jim King's avatar
Jim King committed
83
    m_dataViewManager->addDataRequests(requests);
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  }

  const char *
  getTraceData(int64_t traceNumber, OpenVDS::Error & error) const
  {
    // figure out which "page" is associated with the trace
    // request the "page" from the data view manager
    // return trace data pointer within the page

    std::shared_ptr<DataView>
      pageView;

    error = {};

    const auto
      pageTrace = GetPageStartForTrace(traceNumber);
    if (pageTrace == m_currentPageTrace)
    {
      pageView = m_currentPageView;
    }
    else
    {
      auto
        pageRequestInfo = createPageRequestInfo(pageTrace);

Jim King's avatar
Jim King committed
109
      pageView = m_dataViewManager->acquireDataView(pageRequestInfo, true, error);
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

      if (error.code)
      {
        return nullptr;
      }

      if (!pageView)
      {
        error.code = 1;
        error.string = "Failed to acquire DataView";
        return nullptr;
      }
    }

    const char
      * basePtr = static_cast<const char *>(pageView->Pointer(error));
    return basePtr + (traceNumber - pageTrace) * m_traceByteSize;
  }

  void
  retirePagesBefore(int64_t traceNumber) const
  {
    const auto
      pageTrace = GetPageStartForTrace(traceNumber);
    auto
      pageRequestInfo = createPageRequestInfo(pageTrace);
Jim King's avatar
Jim King committed
136
137
138
139
140
141
    m_dataViewManager->retireDataViewsBefore(pageRequestInfo);
  }

  void retireAllPages() const
  {
    m_dataViewManager->retireAllDataViews();
142
143
  }

144
145
146
147
148
149
  int64_t
  fileTraceCount() const
  {
    return m_numTraces;
  }

150
private:
Jim King's avatar
Jim King committed
151
152
  std::shared_ptr<DataViewManager>
    m_dataViewManager;
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  const int64_t
    m_tracesPerPage,
    m_traceByteSize,
    m_numTraces;
  int64_t
    m_currentPageTrace;
  std::shared_ptr<DataView>
    m_currentPageView;

  void
  addTraceRequests(std::vector<DataRequestInfo>& requests, int64_t startTrace, int64_t stopTrace)
  {
    const int64_t
      pageStartStart = GetPageStartForTrace(startTrace),
      pageStartStop = GetPageStartForTrace(stopTrace);

    for (auto pageTrace = pageStartStart; pageTrace <= pageStartStop; pageTrace += m_tracesPerPage)
    {
      requests.emplace_back(createPageRequestInfo(pageTrace));
    }
  }

  DataRequestInfo
  createPageRequestInfo(int64_t pageTrace) const
  {
    const auto
      traceCount = std::min(m_tracesPerPage, m_numTraces - pageTrace);

181
182
    assert(traceCount > 0);

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    DataRequestInfo
      dataRequestInfo;
    dataRequestInfo.offset = SEGY::TextualFileHeaderSize + SEGY::BinaryFileHeaderSize + pageTrace * m_traceByteSize;
    dataRequestInfo.size = traceCount * m_traceByteSize;

    return dataRequestInfo;
  }

  int64_t
  GetPageStartForTrace(int64_t trace) const
  {
    return trace - (trace % m_tracesPerPage);
  }

};

#endif  // TRACE_DATA_MANAGER_H