~ubuntu-branches/ubuntu/raring/clucene-core/raring-proposed

« back to all changes in this revision

Viewing changes to src/core/CLucene/store/IndexInput.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-08-11 09:33:38 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120811093338-fgrx41ftqew3qt6a
Tags: 2.3.3.4-1
* New upstream release (Closes: #661703).
* Convert package to multiarch.
* Drop obsolete patches:
  - 01_add_missing_include_bug505667.diff
  - 02_posixness_fix_bug530308.diff
* Add patches:
  - Fixing_ZLIB_configuration_in_shared_CMakeLists.patch
  - Fix-pkgconfig-file-by-adding-clucene-shared-library.patch
  - Install-contribs-lib.patch
  - multiarch.patch
* Update debian/compat: bump to 8.
* Update debian/control:
  - update build dependencies (add cmake, libboost-dev and libz-dev).
  - bump Standards-Version to 3.9.3.
  - rename packages due to ABI bump: libclucene0ldbl -> libclucene-core1.
  - add libclucene-contribs1 package.
* Update debian/rules:
  - rewrite to use CMake.
  - add multiarch support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 /*------------------------------------------------------------------------------
 
2
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
 
3
*
 
4
* Distributable under the terms of either the Apache License (Version 2.0) or
 
5
* the GNU Lesser General Public License, as specified in the COPYING file.
 
6
------------------------------------------------------------------------------*/
 
7
#include "CLucene/_ApiHeader.h"
 
8
#include "IndexInput.h"
 
9
#include "IndexOutput.h"
 
10
#include "CLucene/util/Misc.h"
 
11
 
 
12
CL_NS_DEF(store)
 
13
CL_NS_USE(util)
 
14
 
 
15
        IndexInput::IndexInput():
 
16
                NamedObject()
 
17
        {
 
18
        }
 
19
        IndexInput::~IndexInput()
 
20
        {
 
21
        }
 
22
        IndexInput::IndexInput(const IndexInput& /*other*/)
 
23
        {
 
24
        }
 
25
 
 
26
  int32_t IndexInput::readInt() {
 
27
    int32_t b = (readByte() << 24);
 
28
    b |= (readByte() << 16);
 
29
    b |= (readByte() <<  8);
 
30
    return (b | readByte());
 
31
  }
 
32
 
 
33
  int32_t IndexInput::readVInt() {
 
34
    uint8_t b = readByte();
 
35
    int32_t i = b & 0x7F;
 
36
    for (int32_t shift = 7; (b & 0x80) != 0; shift += 7) {
 
37
      b = readByte();
 
38
      i |= (b & 0x7F) << shift;
 
39
    }
 
40
    return i;
 
41
  }
 
42
 
 
43
  int64_t IndexInput::readLong() {
 
44
    int64_t i = ((int64_t)readInt() << 32);
 
45
    return (i | ((int64_t)readInt() & 0xFFFFFFFFL));
 
46
  }
 
47
 
 
48
  int64_t IndexInput::readVLong() {
 
49
    uint8_t b = readByte();
 
50
    int64_t i = b & 0x7F;
 
51
    for (int32_t shift = 7; (b & 0x80) != 0; shift += 7) {
 
52
      b = readByte();
 
53
      i |= (((int64_t)b) & 0x7FL) << shift;
 
54
    }
 
55
    return i;
 
56
  }
 
57
 
 
58
  void IndexInput::skipChars( const int32_t count) {
 
59
        for (int32_t i = 0; i < count; i++) {
 
60
                TCHAR b = readByte();
 
61
                if ((b & 0x80) == 0) {
 
62
                        // Do Nothing.
 
63
                } else if ((b & 0xE0) != 0xE0) {
 
64
                        readByte();
 
65
                } else {
 
66
                        readByte();
 
67
                        readByte();
 
68
                }
 
69
        }
 
70
}
 
71
 
 
72
        #ifdef _UCS2
 
73
  int32_t IndexInput::readString(char* buffer, const int32_t maxLength){
 
74
        TCHAR* buf = _CL_NEWARRAY(TCHAR,maxLength);
 
75
    int32_t ret = -1;
 
76
        try{
 
77
                ret = readString(buf,maxLength);
 
78
                STRCPY_TtoA(buffer,buf,ret+1);
 
79
        }_CLFINALLY ( _CLDELETE_CARRAY(buf); )
 
80
        return ret;
 
81
  }
 
82
  #endif
 
83
 
 
84
  int32_t IndexInput::readString(TCHAR* buffer, const int32_t maxLength){
 
85
    int32_t len = readVInt();
 
86
                int32_t ml=maxLength-1;
 
87
    if ( len >= ml ){
 
88
      readChars(buffer, 0, ml);
 
89
      buffer[ml] = 0;
 
90
      //we have to finish reading all the data for this string!
 
91
      if ( len-ml > 0 ){
 
92
                                //seek(getFilePointer()+(len-ml)); <- that was the wrong way to "finish reading"
 
93
                                skipChars(len-ml);
 
94
                  }
 
95
      return ml;
 
96
    }else{
 
97
      readChars(buffer, 0, len);
 
98
      buffer[len] = 0;
 
99
      return len;
 
100
    }
 
101
  }
 
102
 
 
103
   TCHAR* IndexInput::readString(){
 
104
    int32_t len = readVInt();
 
105
 
 
106
    if ( len == 0){
 
107
      return stringDuplicate(LUCENE_BLANK_STRING);
 
108
    }
 
109
 
 
110
    TCHAR* ret = _CL_NEWARRAY(TCHAR,len+1);
 
111
    readChars(ret, 0, len);
 
112
    ret[len] = 0;
 
113
 
 
114
    return ret;
 
115
  }
 
116
 
 
117
  void IndexInput::readBytes( uint8_t* b, const int32_t len, bool /*useBuffer*/) {
 
118
    // Default to ignoring useBuffer entirely
 
119
    readBytes(b, len);
 
120
  }
 
121
 
 
122
  void IndexInput::readChars( TCHAR* buffer, const int32_t start, const int32_t len) {
 
123
    const int32_t end = start + len;
 
124
    TCHAR b;
 
125
    for (int32_t i = start; i < end; ++i) {
 
126
      b = readByte();
 
127
      if ((b & 0x80) == 0) {
 
128
        b = (b & 0x7F);
 
129
      } else if ((b & 0xE0) != 0xE0) {
 
130
        b = (((b & 0x1F) << 6)
 
131
          | (readByte() & 0x3F));
 
132
      } else {
 
133
                  b = ((b & 0x0F) << 12) | ((readByte() & 0x3F) << 6);
 
134
                  b |= (readByte() & 0x3F);
 
135
      }
 
136
      buffer[i] = b;
 
137
        }
 
138
  }
 
139
 
 
140
 
 
141
 
 
142
 
 
143
 
 
144
 
 
145
BufferedIndexInput::BufferedIndexInput(int32_t _bufferSize):
 
146
                buffer(NULL),
 
147
                bufferSize(_bufferSize>=0?_bufferSize:CL_NS(store)::BufferedIndexOutput::BUFFER_SIZE),
 
148
                bufferStart(0),
 
149
                bufferLength(0),
 
150
                bufferPosition(0)
 
151
  {
 
152
  }
 
153
 
 
154
  BufferedIndexInput::BufferedIndexInput(const BufferedIndexInput& other):
 
155
        IndexInput(other),
 
156
    buffer(NULL),
 
157
    bufferSize(other.bufferSize),
 
158
    bufferStart(other.bufferStart),
 
159
    bufferLength(other.bufferLength),
 
160
    bufferPosition(other.bufferPosition)
 
161
  {
 
162
    /* DSR: Does the fact that sometime clone.buffer is not NULL even when
 
163
    ** clone.bufferLength is zero indicate memory corruption/leakage?
 
164
    **   if ( clone.buffer != NULL) { */
 
165
    if (other.bufferLength != 0 && other.buffer != NULL) {
 
166
      buffer = _CL_NEWARRAY(uint8_t,bufferLength);
 
167
      memcpy(buffer,other.buffer,bufferLength * sizeof(uint8_t));
 
168
    }
 
169
  }
 
170
 
 
171
        const char* BufferedIndexInput::getObjectName(){ return getClassName(); }
 
172
        const char* BufferedIndexInput::getClassName(){ return "BufferedIndexInput"; }
 
173
                
 
174
  void BufferedIndexInput::readBytes(uint8_t* b, const int32_t len){
 
175
    readBytes(b, len, true);
 
176
  }
 
177
  void BufferedIndexInput::readBytes(uint8_t* _b, const int32_t _len, bool useBuffer){
 
178
    int32_t len = _len;
 
179
    uint8_t* b = _b;
 
180
 
 
181
    if(len <= (bufferLength-bufferPosition)){
 
182
      // the buffer contains enough data to satisfy this request
 
183
      if(len>0) // to allow b to be null if len is 0...
 
184
        memcpy(b, buffer + bufferPosition, len);
 
185
      bufferPosition+=len;
 
186
    } else {
 
187
      // the buffer does not have enough data. First serve all we've got.
 
188
      int32_t available = bufferLength - bufferPosition;
 
189
      if(available > 0){
 
190
        memcpy(b, buffer + bufferPosition, available);
 
191
        b += available;
 
192
        len -= available;
 
193
        bufferPosition += available;
 
194
      }
 
195
      // and now, read the remaining 'len' bytes:
 
196
      if (useBuffer && len<bufferSize){
 
197
        // If the amount left to read is small enough, and
 
198
        // we are allowed to use our buffer, do it in the usual
 
199
        // buffered way: fill the buffer and copy from it:
 
200
        refill();
 
201
        if(bufferLength<len){
 
202
          // Throw an exception when refill() could not read len bytes:
 
203
          memcpy(b, buffer, bufferLength);
 
204
          _CLTHROWA(CL_ERR_IO, "read past EOF");
 
205
        } else {
 
206
          memcpy(b, buffer, len);
 
207
          bufferPosition=len;
 
208
        }
 
209
      } else {
 
210
        // The amount left to read is larger than the buffer
 
211
        // or we've been asked to not use our buffer -
 
212
        // there's no performance reason not to read it all
 
213
        // at once. Note that unlike the previous code of
 
214
        // this function, there is no need to do a seek
 
215
        // here, because there's no need to reread what we
 
216
        // had in the buffer.
 
217
        int64_t after = bufferStart+bufferPosition+len;
 
218
        if(after > length())
 
219
          _CLTHROWA(CL_ERR_IO, "read past EOF");
 
220
        readInternal(b, len);
 
221
        bufferStart = after;
 
222
        bufferPosition = 0;
 
223
        bufferLength = 0;                    // trigger refill() on read
 
224
      }
 
225
    }
 
226
  }
 
227
 
 
228
  int64_t BufferedIndexInput::getFilePointer() const{
 
229
    return bufferStart + bufferPosition;
 
230
  }
 
231
 
 
232
  void BufferedIndexInput::seek(const int64_t pos) {
 
233
    if ( pos < 0 )
 
234
      _CLTHROWA(CL_ERR_IO, "IO Argument Error. Value must be a positive value.");
 
235
    if (pos >= bufferStart && pos < (bufferStart + bufferLength))
 
236
      bufferPosition = (int32_t)(pos - bufferStart);  // seek within buffer
 
237
    else {
 
238
      bufferStart = pos;
 
239
      bufferPosition = 0;
 
240
      bufferLength = 0;                           // trigger refill() on read()
 
241
      seekInternal(pos);
 
242
    }
 
243
  }
 
244
  void BufferedIndexInput::close(){
 
245
    _CLDELETE_ARRAY(buffer);
 
246
    bufferLength = 0;
 
247
    bufferPosition = 0;
 
248
    bufferStart = 0;
 
249
  }
 
250
 
 
251
 
 
252
  BufferedIndexInput::~BufferedIndexInput(){
 
253
    BufferedIndexInput::close();
 
254
  }
 
255
 
 
256
  void BufferedIndexInput::refill() {
 
257
    int64_t start = bufferStart + bufferPosition;
 
258
    int64_t end = start + bufferSize;
 
259
    if (end > length())                           // don't read past EOF
 
260
      end = length();
 
261
    bufferLength = (int32_t)(end - start);
 
262
    if (bufferLength <= 0)
 
263
      _CLTHROWA(CL_ERR_IO, "IndexInput read past EOF");
 
264
 
 
265
    if (buffer == NULL){
 
266
      buffer = _CL_NEWARRAY(uint8_t,bufferSize);                  // allocate buffer lazily
 
267
    }
 
268
    readInternal(buffer, bufferLength);
 
269
 
 
270
 
 
271
    bufferStart = start;
 
272
    bufferPosition = 0;
 
273
  }
 
274
 
 
275
  void BufferedIndexInput::setBufferSize( int32_t newSize ) {
 
276
 
 
277
          if ( newSize != bufferSize ) {
 
278
                  bufferSize = newSize;
 
279
                  if ( buffer != NULL ) {
 
280
 
 
281
                          uint8_t* newBuffer = _CL_NEWARRAY( uint8_t, newSize );
 
282
                          int32_t leftInBuffer = bufferLength - bufferPosition;
 
283
                          int32_t numToCopy;
 
284
 
 
285
                          if ( leftInBuffer > newSize ) {
 
286
                                  numToCopy = newSize;
 
287
                          } else {
 
288
                                  numToCopy = leftInBuffer;
 
289
                          }
 
290
 
 
291
                          memcpy( (void*)newBuffer, (void*)(buffer + bufferPosition), numToCopy );
 
292
 
 
293
                          bufferStart += bufferPosition;
 
294
                          bufferPosition = 0;
 
295
                          bufferLength = numToCopy;
 
296
 
 
297
                          _CLDELETE_ARRAY( buffer );
 
298
                          buffer = newBuffer;
 
299
 
 
300
                  }
 
301
          }
 
302
 
 
303
  }
 
304
 
 
305
CL_NS_END
 
306