File: ColumnCache.h

package info (click to toggle)
casacore 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 51,680 kB
  • sloc: cpp: 462,815; fortran: 16,372; ansic: 7,403; yacc: 4,626; lex: 2,340; sh: 1,786; python: 629; perl: 531; sed: 499; csh: 34; makefile: 31
file content (158 lines) | stat: -rw-r--r-- 4,876 bytes parent folder | download
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
109
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//# ColumnCache.h: A caching object for a table column
//# Copyright (C) 1997
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//#        Internet email: aips2-request@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA
//#
//# $Id$

#ifndef TABLES_COLUMNCACHE_H
#define TABLES_COLUMNCACHE_H


//# Includes
#include <cassert>
#include <limits>

#include <casacore/casa/aips.h>


namespace casacore { //# NAMESPACE CASACORE - BEGIN

// <summary>
// A caching object for a table column.
// </summary>

// <use visibility=local>

// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
// </reviewed>

// <prerequisite>
//# Classes you should understand before using this one.
//   <li> <linkto class=ScalarColumn>ScalarColumn</linkto>
// </prerequisite>

// <synopsis>
// ColumnCache acts as a cache for a table column.
// It contains a pointer to data and the start and end row number
// for which these data are valid. An increment is part of the object
// and is usually 0 or 1. The value 0 is used for data which is
// valid for multiple rows (as used in
// <linkto class=IncrementalStMan>IncrementalStMan</linkto>).
// The value 1 is used for data stored consecutevily in a buffer for
// each row (as used in <linkto class=StManAipsIO>StManAipsIO</linkto>).
// <p>
// The ColumnCache object is created and updated by the data manager.
// The top level <linkto class=ScalarColumn>ScalarColumn</linkto> object
// contains a pointer to the cache object. In this way the
// <src>ScalarColumn::get</src> can often be executed by a few inlined
// statements which improves performance considerably.
// <p>
// The <src>invalidate</src> function can be used to invalidate the
// cache. This is for instance needed when a table lock is acquired
// or released to be sure that the cache gets refreshed.
// </synopsis>

// <motivation>
// This class was developed to improve the performance for getting a scalar.
// </motivation>

// <todo asof="$DATE:$">
//  <li>For ConcatColumn add the ability to have other ColumnCache objects
//      using this one and invalidate them as well.
// </todo>


class ColumnCache
{
public:
    // Constructor.
    // It sets the increment to 1 and calls invalidate.
    ColumnCache();

    // Set the increment to the given value.
    void setIncrement (rownr_t increment);

    // Set the start and end row number for which the given data pointer
    // is valid.
    void set (rownr_t startRow, rownr_t endRow, const void* dataPtr);

    // Invalidate the cache.
    // This clears the data pointer and sets startRow>endRow.
    void invalidate();

    // Calculate the offset in the cached data for the given row.
    // -1 is returned if the row is not within the cached rows.
    Int64 offset (rownr_t rownr) const;

    // Give a pointer to the data.
    // The calling function has to do a proper cast after which the
    // calculated offset can be added to get the proper data.
    const void* dataPtr() const;

    // Give the start, end (including), and increment row number
    // of the cached column values.
    rownr_t start() const
      { return itsStart; }
    rownr_t end() const
      { return itsEnd; }
    rownr_t incr() const
      { return itsIncr; }

private:
    rownr_t  itsStart;
    rownr_t  itsEnd;
    rownr_t  itsIncr;
    const void* itsData;
};


inline void ColumnCache::setIncrement (rownr_t increment)
{
    itsIncr = increment;
}

inline void ColumnCache::invalidate()
{
    set (1, 0, 0);
}

inline Int64 ColumnCache::offset (rownr_t rownr) const
{
    if (rownr < itsStart || rownr > itsEnd) {
        return -1;
    }
    const rownr_t offset = (rownr - itsStart) * itsIncr;
    assert(offset <= static_cast<rownr_t>(std::numeric_limits<Int64>::max()));
    return Int64(offset);
}

inline const void* ColumnCache::dataPtr() const
{
    return itsData;
}


} //# NAMESPACE CASACORE - END

#endif