casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SSMIndex.h
Go to the documentation of this file.
00001 //# SSMIndex.h: The bucket index for a group of columns in the SSM
00002 //# Copyright (C) 2000
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //# $Id: SSMIndex.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $   
00026 
00027 #ifndef TABLES_SSMINDEX_H
00028 #define TABLES_SSMINDEX_H
00029 
00030 //# Includes
00031 #include <casa/aips.h>
00032 #include <casa/Containers/Block.h>
00033 #include <casa/Containers/SimOrdMap.h>
00034 #include <casa/Arrays/Vector.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 //# Forward Declarations
00039 class SSMBase;
00040 
00041 
00042 // <summary>
00043 // The bucket index for a group of columns in the Standard Storage Manager.
00044 // </summary>
00045 
00046 // <use visibility=local>
00047 
00048 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tStandardStMan.cc">
00049 // </reviewed>
00050 
00051 // <prerequisite>
00052 //# Classes you should understand before using this one.
00053 //   <li> <linkto class=SSMBase>SSMBase</linkto>
00054 // </prerequisite>
00055 
00056 // <etymology>
00057 // SSMIndex represent the bucket index in the Standard Storage Manager.
00058 // </etymology>
00059 
00060 // <synopsis>
00061 // In <linkto class=SSMBase>SSMBase</linkto> it is described that an index
00062 // is used to map row number to data bucket in a bucket stream.
00063 // This class implements this index. It serves 2 purposes:
00064 // <ol>
00065 //  <li> It keeps a block of row numbers giving the last row number
00066 //       stored in each data bucket.
00067 //       <br>Note that each bucket does not need to contain the same number
00068 //       of rows, because rows might be deleted from it.
00069 //       When all rows are deleted from a bucket, the bucket is removed
00070 //       from the index and added to the free bucket list.
00071 //  <li> When a column is deleted, the bucket will have a hole.
00072 //       SSMIndex maintains a map to know the size and offset of each hole.
00073 //       Adjacent holes are combined.
00074 //       When a new column is added <linkto class=SSMBase>SSMBase</linkto>
00075 //       will scan the SSMIndex objects to find the hole fitting best.
00076 // </ol>
00077 // </synopsis>
00078   
00079 // <todo asof="$DATE:$">
00080 //# A List of bugs, limitations, extensions or planned refinements.
00081 //   <li> recreate should recreate the itsLastRow && itsBucketNr as well
00082 //        (moving them to the front and rearrange the freespace as one
00083 //        concatenated block) 
00084 // </todo>
00085 
00086 class SSMIndex 
00087 {
00088 public:
00089   // Create the object with the given number of rows per bucket.
00090   // Note that the default is needed to create the object for existing
00091   // tables.
00092   explicit SSMIndex (SSMBase* aPtrSSM, uInt rowsPerBucket=0);
00093 
00094   ~SSMIndex();
00095 
00096   // Read the bucket index from the AipsIO object.
00097   void get (AipsIO& anOs);
00098 
00099   // Write the bucket index into the AipsIO object.
00100   void put (AipsIO& anOs) const;
00101 
00102   // Recreate the object in case all rows are deleted from the table.
00103   void recreate();
00104   
00105   // Return all the bucketnrs used in this index.
00106   Vector<uInt> getBuckets() const;
00107 
00108   // Return the nr of buckets used.
00109   uInt getNrBuckets() const;
00110 
00111   // Set nr of columns use this index.
00112   void setNrColumns (Int aNrColumns, uInt aSizeUsed);
00113 
00114   // Add some rows.
00115   void addRow (uInt aNrRows);
00116 
00117   // Show Statistics of index.
00118   void showStatistics (ostream& anOs) const;
00119 
00120   // A column is removed.
00121   // Set the free space at offset for a field with the given nr of bits.
00122   // It returns the nr of columns still used in this index.
00123   Int removeColumn (Int anOffset, uInt nbits);
00124 
00125   // Try to find free space for a field with a given length (best fit).
00126   // -1 is returned if no fit is found.
00127   // Otherwise it returns the nr of bytes left unused.
00128   Int getFree (Int& anOffset, uInt nbits) const;
00129 
00130   // reuse the space at offset for a field with the given nr of bits.
00131   // This is used when column has been added to this bucket.
00132   void addColumn (Int anOffset, uInt nbits);
00133 
00134   // Delete the given row.
00135   Int deleteRow (uInt aRowNumber);
00136 
00137   // Get the number of rows that fits in ach bucket.
00138   uInt getRowsPerBucket() const;
00139 
00140   // Find the bucket containing the given row.
00141   // An exception is thrown if not found.
00142   // It also sets the first and last row number fitting in that bucket.
00143   void find (uInt aRowNumber, uInt& aBucketNr, uInt& aStartRow,
00144              uInt& anEndRow) const;
00145 
00146 private:
00147   // Get the index of the bucket containing the given row.
00148   uInt getIndex (uInt aRowNr) const;
00149 
00150 
00151   //# Pointer to specific Storage Manager.    
00152   SSMBase* itsSSMPtr;
00153     
00154   //# Nr of entries used in blocks.
00155   uInt itsNUsed;
00156 
00157   //# Last row nr indexed together with itsBucketNumber
00158   Block<uInt> itsLastRow;
00159 
00160   //# Bucketnumbers indexed together with itsLastRow.
00161   //# So itsLastRow[0] contains the last rownumber of the bucket
00162   //# in itsBucketNumber[0]
00163   Block<uInt> itsBucketNumber;
00164 
00165   //# Map that contains length/offset pairs for free size (size in bytes).
00166   SimpleOrderedMap<Int,Int> itsFreeSpace;
00167 
00168   //# How many rows fit in a bucket?
00169   uInt itsRowsPerBucket;
00170 
00171   //# Nr of columns using this index.
00172   Int itsNrColumns;
00173 };
00174 
00175 
00176 inline uInt SSMIndex::getRowsPerBucket() const
00177 {
00178   return itsRowsPerBucket;
00179 }
00180 
00181 
00182 
00183 } //# NAMESPACE CASA - END
00184 
00185 #endif