casacore
ColumnCache.h
Go to the documentation of this file.
1 //# ColumnCache.h: A caching object for a table column
2 //# Copyright (C) 1997
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef TABLES_COLUMNCACHE_H
29 #define TABLES_COLUMNCACHE_H
30 
31 
32 //# Includes
33 #include <cassert>
34 #include <limits>
35 
36 #include <casacore/casa/aips.h>
37 
38 
39 namespace casacore { //# NAMESPACE CASACORE - BEGIN
40 
41 // <summary>
42 // A caching object for a table column.
43 // </summary>
44 
45 // <use visibility=local>
46 
47 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
48 // </reviewed>
49 
50 // <prerequisite>
51 //# Classes you should understand before using this one.
52 // <li> <linkto class=ScalarColumn>ScalarColumn</linkto>
53 // </prerequisite>
54 
55 // <synopsis>
56 // ColumnCache acts as a cache for a table column.
57 // It contains a pointer to data and the start and end row number
58 // for which these data are valid. An increment is part of the object
59 // and is usually 0 or 1. The value 0 is used for data which is
60 // valid for multiple rows (as used in
61 // <linkto class=IncrementalStMan>IncrementalStMan</linkto>).
62 // The value 1 is used for data stored consecutevily in a buffer for
63 // each row (as used in <linkto class=StManAipsIO>StManAipsIO</linkto>).
64 // <p>
65 // The ColumnCache object is created and updated by the data manager.
66 // The top level <linkto class=ScalarColumn>ScalarColumn</linkto> object
67 // contains a pointer to the cache object. In this way the
68 // <src>ScalarColumn::get</src> can often be executed by a few inlined
69 // statements which improves performance considerably.
70 // <p>
71 // The <src>invalidate</src> function can be used to invalidate the
72 // cache. This is for instance needed when a table lock is acquired
73 // or released to be sure that the cache gets refreshed.
74 // </synopsis>
75 
76 // <motivation>
77 // This class was developed to improve the performance for getting a scalar.
78 // </motivation>
79 
80 // <todo asof="$DATE:$">
81 // <li>For ConcatColumn add the ability to have other ColumnCache objects
82 // using this one and invalidate them as well.
83 // </todo>
84 
85 
87 {
88 public:
89  // Constructor.
90  // It sets the increment to 1 and calls invalidate.
92 
93  // Set the increment to the given value.
94  void setIncrement (rownr_t increment);
95 
96  // Set the start and end row number for which the given data pointer
97  // is valid.
98  void set (rownr_t startRow, rownr_t endRow, const void* dataPtr);
99 
100  // Invalidate the cache.
101  // This clears the data pointer and sets startRow>endRow.
102  void invalidate();
103 
104  // Calculate the offset in the cached data for the given row.
105  // -1 is returned if the row is not within the cached rows.
106  Int64 offset (rownr_t rownr) const;
107 
108  // Give a pointer to the data.
109  // The calling function has to do a proper cast after which the
110  // calculated offset can be added to get the proper data.
111  const void* dataPtr() const;
112 
113  // Give the start, end (including), and increment row number
114  // of the cached column values.
115  rownr_t start() const
116  { return itsStart; }
117  rownr_t end() const
118  { return itsEnd; }
119  rownr_t incr() const
120  { return itsIncr; }
121 
122 private:
126  const void* itsData;
127 };
128 
129 
130 inline void ColumnCache::setIncrement (rownr_t increment)
131 {
132  itsIncr = increment;
133 }
134 
136 {
137  set (1, 0, 0);
138 }
139 
140 inline Int64 ColumnCache::offset (rownr_t rownr) const
141 {
142  if (rownr < itsStart || rownr > itsEnd) {
143  return -1;
144  }
145  const rownr_t offset = (rownr - itsStart) * itsIncr;
146  assert(offset <= static_cast<rownr_t>(std::numeric_limits<Int64>::max()));
147  return Int64(offset);
148 }
149 
150 inline const void* ColumnCache::dataPtr() const
151 {
152  return itsData;
153 }
154 
155 
156 } //# NAMESPACE CASACORE - END
157 
158 #endif
Int64 offset(rownr_t rownr) const
Calculate the offset in the cached data for the given row.
Definition: ColumnCache.h:140
rownr_t end() const
Definition: ColumnCache.h:117
void setIncrement(rownr_t increment)
Set the increment to the given value.
Definition: ColumnCache.h:130
const void * dataPtr() const
Give a pointer to the data.
Definition: ColumnCache.h:150
rownr_t start() const
Give the start, end (including), and increment row number of the cached column values.
Definition: ColumnCache.h:115
rownr_t incr() const
Definition: ColumnCache.h:119
void invalidate()
Invalidate the cache.
Definition: ColumnCache.h:135
void set(rownr_t startRow, rownr_t endRow, const void *dataPtr)
Set the start and end row number for which the given data pointer is valid.
ColumnCache()
Constructor.
const void * itsData
Definition: ColumnCache.h:126
this file contains all the compiler specific defines
Definition: mainpage.dox:28
LatticeExprNode max(const LatticeExprNode &left, const LatticeExprNode &right)
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
uInt64 rownr_t
Define the type of a row number in a table.
Definition: aipsxtype.h:46