casacore
SSMIndex.h
Go to the documentation of this file.
1 //# SSMIndex.h: The bucket index for a group of columns in the SSM
2 //# Copyright (C) 2000
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 //# $Id$
26 
27 #ifndef TABLES_SSMINDEX_H
28 #define TABLES_SSMINDEX_H
29 
30 //# Includes
31 #include <casacore/casa/aips.h>
32 #include <casacore/casa/Containers/Block.h>
33 #include <casacore/casa/Arrays/Vector.h>
34 #include <map>
35 
36 namespace casacore { //# NAMESPACE CASACORE - BEGIN
37 
38 //# Forward Declarations
39 class SSMBase;
40 
41 
42 // <summary>
43 // The bucket index for a group of columns in the Standard Storage Manager.
44 // </summary>
45 
46 // <use visibility=local>
47 
48 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tStandardStMan.cc">
49 // </reviewed>
50 
51 // <prerequisite>
52 //# Classes you should understand before using this one.
53 // <li> <linkto class=SSMBase>SSMBase</linkto>
54 // </prerequisite>
55 
56 // <etymology>
57 // SSMIndex represent the bucket index in the Standard Storage Manager.
58 // </etymology>
59 
60 // <synopsis>
61 // In <linkto class=SSMBase>SSMBase</linkto> it is described that an index
62 // is used to map row number to data bucket in a bucket stream.
63 // This class implements this index. It serves 2 purposes:
64 // <ol>
65 // <li> It keeps a block of row numbers giving the last row number
66 // stored in each data bucket.
67 // <br>Note that each bucket does not need to contain the same number
68 // of rows, because rows might be deleted from it.
69 // When all rows are deleted from a bucket, the bucket is removed
70 // from the index and added to the free bucket list.
71 // <li> When a column is deleted, the bucket will have a hole.
72 // SSMIndex maintains a map to know the size and offset of each hole.
73 // Adjacent holes are combined.
74 // When a new column is added <linkto class=SSMBase>SSMBase</linkto>
75 // will scan the SSMIndex objects to find the hole fitting best.
76 // </ol>
77 // </synopsis>
78 
79 // <todo asof="$DATE:$">
80 //# A List of bugs, limitations, extensions or planned refinements.
81 // <li> recreate should recreate the itsLastRow && itsBucketNr as well
82 // (moving them to the front and rearrange the freespace as one
83 // concatenated block)
84 // </todo>
85 
86 class SSMIndex
87 {
88 public:
89  // Create the object with the given number of rows per bucket.
90  // Note that the default is needed to create the object for existing
91  // tables.
92  explicit SSMIndex (SSMBase* aPtrSSM, uInt rowsPerBucket=0);
93 
95 
96  // Read the bucket index from the AipsIO object.
97  void get (AipsIO& anOs);
98 
99  // Write the bucket index into the AipsIO object.
100  void put (AipsIO& anOs) const;
101 
102  // Recreate the object in case all rows are deleted from the table.
103  void recreate();
104 
105  // Return all the bucketnrs used in this index.
107 
108  // Return the nr of buckets used.
110 
111  // Set nr of columns use this index.
112  void setNrColumns (Int aNrColumns, uInt aSizeUsed);
113 
114  // Add some rows.
115  void addRow (rownr_t aNrRows);
116 
117  // Show Statistics of index.
118  void showStatistics (ostream& anOs) const;
119 
120  // A column is removed.
121  // Set the free space at offset for a field with the given nr of bits.
122  // It returns the nr of columns still used in this index.
123  Int removeColumn (Int anOffset, uInt nbits);
124 
125  // Try to find free space for a field with a given length (best fit).
126  // -1 is returned if no fit is found.
127  // Otherwise it returns the nr of bytes left unused.
128  Int getFree (Int& anOffset, uInt nbits) const;
129 
130  // reuse the space at offset for a field with the given nr of bits.
131  // This is used when column has been added to this bucket.
132  void addColumn (Int anOffset, uInt nbits);
133 
134  // Delete the given row.
135  // It returns the bucket nr if it gets empty, otherwise -1.
136  Int deleteRow (rownr_t aRowNumber);
137 
138  // Get the number of rows that fits in ach bucket.
139  uInt getRowsPerBucket() const;
140 
141  // Find the bucket containing the given row.
142  // An exception is thrown if not found.
143  // It also sets the first and last row number fitting in that bucket.
144  void find (rownr_t aRowNumber, uInt& aBucketNr, rownr_t& aStartRow,
145  rownr_t& anEndRow, const String& colName) const;
146 
147 private:
148  // Get the index of the bucket containing the given row.
149  uInt getIndex (rownr_t aRowNr, const String& colName) const;
150 
151 
152  //# Pointer to specific Storage Manager.
154 
155  //# Nr of entries used in blocks.
157 
158  //# Last row nr indexed together with itsBucketNumber
160 
161  //# Bucketnumbers indexed together with itsLastRow.
162  //# So itsLastRow[0] contains the last rownumber of the bucket
163  //# in itsBucketNumber[0]
165 
166  //# Map that contains length/offset pairs for free size (size in bytes).
167  std::map<Int,Int> itsFreeSpace;
168 
169  //# How many rows fit in a bucket?
171 
172  //# Nr of columns using this index.
174 };
175 
176 
178 {
179  return itsRowsPerBucket;
180 }
181 
182 
183 
184 } //# NAMESPACE CASACORE - END
185 
186 #endif
void showStatistics(ostream &anOs) const
Show Statistics of index.
SSMBase * itsSSMPtr
Definition: SSMIndex.h:153
Block< uInt > itsBucketNumber
Definition: SSMIndex.h:164
uInt getRowsPerBucket() const
Get the number of rows that fits in ach bucket.
Definition: SSMIndex.h:177
void put(AipsIO &anOs) const
Write the bucket index into the AipsIO object.
Int getFree(Int &anOffset, uInt nbits) const
Try to find free space for a field with a given length (best fit).
void recreate()
Recreate the object in case all rows are deleted from the table.
void setNrColumns(Int aNrColumns, uInt aSizeUsed)
Set nr of columns use this index.
void addColumn(Int anOffset, uInt nbits)
reuse the space at offset for a field with the given nr of bits.
void addRow(rownr_t aNrRows)
Add some rows.
std::map< Int, Int > itsFreeSpace
Definition: SSMIndex.h:167
Int removeColumn(Int anOffset, uInt nbits)
A column is removed.
void get(AipsIO &anOs)
Read the bucket index from the AipsIO object.
Block< rownr_t > itsLastRow
Definition: SSMIndex.h:159
Int deleteRow(rownr_t aRowNumber)
Delete the given row.
void find(rownr_t aRowNumber, uInt &aBucketNr, rownr_t &aStartRow, rownr_t &anEndRow, const String &colName) const
Find the bucket containing the given row.
uInt getNrBuckets() const
Return the nr of buckets used.
Vector< uInt > getBuckets() const
Return all the bucketnrs used in this index.
uInt getIndex(rownr_t aRowNr, const String &colName) const
Get the index of the bucket containing the given row.
SSMIndex(SSMBase *aPtrSSM, uInt rowsPerBucket=0)
Create the object with the given number of rows per bucket.
String: the storage and methods of handling collections of characters.
Definition: String.h:225
this file contains all the compiler specific defines
Definition: mainpage.dox:28
unsigned int uInt
Definition: aipstype.h:51
int Int
Definition: aipstype.h:50
uInt64 rownr_t
Define the type of a row number in a table.
Definition: aipsxtype.h:46