File: SSMStringHandler.h

package info (click to toggle)
casacore 3.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,912 kB
  • sloc: cpp: 471,569; fortran: 16,372; ansic: 7,416; yacc: 4,714; lex: 2,346; sh: 1,865; python: 629; perl: 531; sed: 499; csh: 201; makefile: 32
file content (263 lines) | stat: -rw-r--r-- 10,522 bytes parent folder | download | duplicates (2)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//# SSMStringHandler.h: Store strings in the Standard Storage Manager
//# Copyright (C) 2000
//# 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: casa-feedback@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA
 
#ifndef TABLES_SSMSTRINGHANDLER_H
#define TABLES_SSMSTRINGHANDLER_H
 
 
//# Includes
#include <casacore/casa/aips.h>
#include <casacore/casa/OS/Conversion.h>
#include <casacore/casa/BasicSL/String.h>
#include <casacore/casa/Arrays/Array.h>

namespace casacore { //# NAMESPACE CASACORE - BEGIN

//# Forward Declarations.
class SSMBase;


// <summary>
// Store strings in the Standard Storage Manager.
// </summary>

// <use visibility=local>

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

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

// <etymology>
// SSMStringHandler handles strings for the Standard Storage Manager.
// </etymology>

// <synopsis>
// Variable length strings cannot be stored in the data bucket.
// Only short (<8 characters) strings can be stored directly.
// Class SSMStringhandler is used by the SSM to store strings in
// so-called string buckets.
// A string bucket has the following layout:
// <ul>
//  <li> The first Int is reserved to be used for the free bucket list.
//  <li> <src>itsUsedLength</src> tells how many bytes have been used.
//       Thus it tells the next free byte in the string part.
//       In principle it always increases. Only if data are removed
//       from the last part of the string part, it is decreased, thus
//       the deleted part can be reused again.
//  <li> <src>itsNDeleted</src> tells how many bytes of the string part
//       are deleted (i.e. not used). Initially it is the length of the
//       string part of the bucket (i.e. bucketsize minus 4 Ints).
//       When a string is stored, its length is subtracted from itsNDeleted.
//       When a string is removed, its length is added again.
//       When the string part is deleted, the bucket is added to the
//       free bucket list.
//  <li> <src>itsNextBucket</src> tells the next bucket if the last
//       entry in the bucket is continued in another bucket.
//       Normally this field is -1 (meaning not continued), but long
//       strings or string arrays might be continued in another bucket
//       (and continued from there again).
//  <li> The string part is a sequence of bytes containing the string
//       data. When a value is to be stored, it will replace the current
//       value if the new value is not longer. Otherwise the current
//       value (if any) is deleted and the new value is appended to
//       the end of the string part in the last bucket used.
//       <p>
//       For a scalar string only its characters are stored. Its length
//       (and bucketnr and offset in string bucket) are stored in the data
//       bucket.
//       <br>
//       A fixed length array is stored as an array of bytes. That byte
//       array contains length-value pairs for each element of the array.
//       The total length (and bucketnr and offset) are stored in the data
//       bucket.
//       <br>
//       A variable length array is stored as the shape, a flag, optionally
//       followed by the string array as length-value pairs (as above).
//       The shape consists of the nr of dimensions followed by the
//       length of each dimension. The flag indicates if a string array
//       is actually stored. It is not if only the shape of the array
//       is set, but no data put yet.
// </ul>
// SSMStringHandler keeps a copy of the current bucket in use to reduce
// the number of accesses to the bucket cache.
// <p>
// It also keeps the bucket number of the last bucket where data were
// added to. It tells which bucket to use when new data has to be stored.
// </synopsis>
  
//# <todo asof="$DATE:$">
//# A List of bugs, limitations, extensions or planned refinements.
//# </todo>

class SSMStringHandler
{
public:
  // Default constructor initializes last string bucket to -1.
  SSMStringHandler (SSMBase* aBase);

  ~SSMStringHandler();

  // Forbid copy constructor.
  SSMStringHandler (const SSMStringHandler&) = delete;

  // Forbid assignment.
  SSMStringHandler& operator= (const SSMStringHandler&) = delete;

  // Set or get last string bucketnr.
  // Setting is needed when an existing table is opened.
  // <group>
  void setLastStringBucket (Int lastStringBucket);
  Int lastStringBucket() const;
  // </group>

  // Put a single string or an array of strings into a bucket.
  // If its length does not exceed the given length, it reuses
  // the currently used space (given by bucketnr and offset).
  // Otherwise it adds the data to the last string bucket.
  // It fills the offset and bucketnr where the data are stored and the
  // length occupied in the buckets.
  // An array of strings is flattened first (a la SSMColumn::writeString).
  // <br>
  // If <src>handleShape</src> is True (for variable shaped arrays), the
  // shape will be put first.
  // <group>
  void put (Int& bucketNr, Int& offset, Int& length, 
	    const String& string);
  void put (Int& bucketNr, Int& offset, Int& length, 
	    const Array<String>& string, Bool handleShape);
  // </group>

  // Put a single string or an array of strings into a bucket.
  // If its length does not exceed the given length, it reuses
  // the currently used space (given by bucketnr and offset).
  // Otherwise it adds the data to the last string bucket.
  // It fills the offset and bucketnr where stored and the
  // length occupied in the buckets.
  void putShape (Int& bucketNr, Int& offset, Int& length, 
		 const IPosition& aShape);

  // Get the shape in the given bucket and offset.
  // It sets the offset to the data right after the shape.
  // The IPosition object is resized as needed.
  void getShape (IPosition& aShape, Int bucket, Int& offset, Int length);

  // Remove data with the given length from a bucket.
  // If the data are continued in next bucket(s), they will be
  // removed there as well.
  void remove (Int bucketNr, Int offset, Int length);

  // Get a string or an array of strings.
  // The array must have the correct shape.
  // <src>handleShape</src> will be True for variable shaped arrays
  // indicating that the data are preceeded by the shape.
  // <group>
  void get (String& string, Int bucket, Int offset, Int length);
  void get (Array<String>& string, Int bucket, Int offset, 
	    Int length, Bool handleShape);
  // </group>

  // Flush the currently used string bucket.
  void flush();
  
  // Initialize the StringHandler
  void init();

  // Resynchronize (after a table lock was acquired).
  // It clears the itsCurrentBucket variable to assure that buckets
  // are reread.
  void resync();
  
private:
  // Get the given bucket and make it current.
  // It first writes the current bucket if it has changed.
  // <br>
  // If <src>isNew</src> is True the bucket is new,
  // so the Ints at its beginning do not have to be interpreted.
  void getBucket (uInt bucketNr, Bool isNew=False);

  // Get a new bucket and make it current.
  // If <src>doConcat</src> is True, the new bucket is a continuation,
  // so <src>itsNextBucket</src> in the currently used bucket is filled
  // with the new bucket number.
  void getNewBucket (Bool doConcat);

  // Put the data with the given length at the end of the current bucket.
  // If they do not fit, they are continued in a new bucket.
  void putData (Int length, const Char* data);

  // Get the data with the given length from the curent bucket at the
  // given offset. If sets the offset to the byte after the data read.
  // Continuation buckets are followed (and made current).
  void getData (Int length, Char* data, Int& offset);

  // Replace the current data with the new data.
  // It is used by <src>put</src> after having assured that the
  // new length does not exceed the current one.
  // It follows continuation buckets as needed.
  // <group>
  void replace (Int bucketNr, Int offset, Int length, 
		const String& string);
  void replace (Int bucketNr, Int offset, Int length, Int totalLength, 
		const IPosition& aShape);
  void replace (Int bucketNr, Int offset, Int length, Int totalLength,
		const Array<String>& string, Bool handleShape);
  void replaceData (Int& offset,Int length, const Char* data);
  // </group>


  SSMBase* itsSSMPtr;      // Pointer to SSMBase stucture
  Int   itsCurrentBucket;  // bucketnr of current string bucket (-1 is none)
  Int   itsLength;         // length of bucket in use (only the string part)
  Int   itsNDeleted;       // #bytes deleted from the string part of the bucket
  Int   itsUsedLength;     // #bytes used from the string part of the bucket
  Int   itsNextBucket;     // next bucket for long strings
  char* itsData;           // bucket string data
  char* itsIntBuf;         // buffer for initialisation params
  Bool  isChanged;         // has current bucket been changed?
  uInt  itsIntSize;        // size of integers in this system
  Int   itsLastBucket;     // last string bucket used
  uInt  itsStart;          // Start position of actual data in bucket
};


inline void SSMStringHandler::setLastStringBucket (Int lastStringBucket)
{ 
  itsLastBucket = lastStringBucket;
}  

inline Int SSMStringHandler::lastStringBucket() const
{ 
  return itsLastBucket; 
}  



} //# NAMESPACE CASACORE - END

#endif