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 * Changes are Copyright(C) 2007, 2008 by Nokia Corporation and/or its subsidiary(-ies), all rights reserved.
8*/
9#ifndef _lucene_store_IndexInput_
10#define _lucene_store_IndexInput_
11
12#if defined(_LUCENE_PRAGMA_ONCE)
13# pragma once
14#endif
15
16#include <QtCore/QString>
17
18#include "CLucene/util/bufferedstream.h"
19#include "IndexOutput.h"
20
21CL_NS_DEF(store)
22
23 /** Abstract base class for input from a file in a {@link Directory}. A
24 * random-access input stream. Used for all Lucene index input operations.
25 * @see Directory
26 * @see IndexOutput
27 */
28 class IndexInput: LUCENE_BASE {
29 private:
30 void skipChars( const int32_t count);
31 protected:
32 IndexInput();
33 IndexInput(const IndexInput& clone);
34 public:
35 virtual ~IndexInput(){}
36 virtual IndexInput* clone() const =0;
37
38 DEFINE_MUTEX(THIS_LOCK)
39
40 /** Reads and returns a single byte.
41 * @see IndexOutput#writeByte(byte)
42 */
43 virtual uint8_t readByte() =0;
44
45 /** Reads a specified number of bytes into an array at the specified offset.
46 * @param b the array to read bytes into
47 * @param offset the offset in the array to start storing bytes
48 * @param len the number of bytes to read
49 * @see IndexOutput#writeBytes(byte[],int32_t)
50 */
51 virtual void readBytes(uint8_t* b, const int32_t len) =0;
52
53 /** Reads four bytes and returns an int.
54 * @see IndexOutput#writeInt(int32_t)
55 */
56 int32_t readInt();
57
58 /** Reads an int stored in variable-length format. Reads between one and
59 * five bytes. Smaller values take fewer bytes. Negative numbers are not
60 * supported.
61 * @see IndexOutput#writeVInt(int32_t)
62 */
63 virtual int32_t readVInt();
64
65 /** Reads eight bytes and returns a long.
66 * @see IndexOutput#writeLong(long)
67 */
68 int64_t readLong();
69
70 /** Reads a long stored in variable-length format. Reads between one and
71 * nine bytes. Smaller values take fewer bytes. Negative numbers are not
72 * supported. */
73 int64_t readVLong();
74
75 /** Reads a string.
76 * @see IndexOutput#writeString(String)
77 * maxLength is the amount read into the buffer, the whole string is still read from the stream
78 * returns the amount read
79 */
80 int32_t readString(TCHAR* buffer, const int32_t maxlength);
81
82 /** Reads a string.
83 * @see IndexOutput#writeString(String)
84 * If unique is true (default) the string will be duplicated.
85 * If false and the length is zero, LUCENE_BLANK_STRING is returned
86 */
87 TCHAR* readString(const bool unique=true);
88
89
90 /** Reads UTF-8 encoded characters into an array.
91 * @param buffer the array to read characters into
92 * @param start the offset in the array to start storing characters
93 * @param length the number of characters to read
94 * @see IndexOutput#writeChars(String,int32_t,int32_t)
95 */
96 void readChars( TCHAR* buffer, const int32_t start, const int32_t len);
97
98 /** Closes the stream to futher operations. */
99 virtual void close() =0;
100
101 /** Returns the current position in this file, where the next read will
102 * occur.
103 * @see #seek(long)
104 */
105 virtual int64_t getFilePointer() const =0;
106
107 /** Sets current position in this file, where the next read will occur.
108 * @see #getFilePointer()
109 */
110 virtual void seek(const int64_t pos) =0;
111
112 /** The number of bytes in the file. */
113 virtual int64_t length() = 0;
114
115 virtual QString getDirectoryType() const = 0;
116 };
117
118 /** Abstract base class for input from a file in a {@link Directory}. A
119 * random-access input stream. Used for all Lucene index input operations.
120 * @see Directory
121 * @see IndexOutput
122 */
123 class BufferedIndexInput: public IndexInput{
124 private:
125 uint8_t* buffer; //array of bytes
126 void refill();
127 protected:
128 int32_t bufferSize; //size of the buffer
129 int64_t bufferStart; // position in file of buffer
130 int32_t bufferLength; // end of valid l_byte_ts
131 int32_t bufferPosition; // next uint8_t to read
132
133 /** Returns a clone of this stream.
134 *
135 * <p>Clones of a stream access the same data, and are positioned at the same
136 * point as the stream they were cloned from.
137 *
138 * <p>Expert: Subclasses must ensure that clones may be positioned at
139 * different points in the input from each other and from the stream they
140 * were cloned from.
141 */
142 BufferedIndexInput(const BufferedIndexInput& clone);
143 BufferedIndexInput(int32_t bufferSize = CL_NS(store)::BufferedIndexOutput::BUFFER_SIZE);
144 public:
145
146 virtual ~BufferedIndexInput();
147 virtual IndexInput* clone() const = 0;
148 void close();
149 inline uint8_t readByte(){
150 if (bufferPosition >= bufferLength)
151 refill();
152
153 return buffer[bufferPosition++];
154 }
155 void readBytes(uint8_t* b, const int32_t len);
156 int64_t getFilePointer() const;
157 void seek(const int64_t pos);
158
159 protected:
160 /** Expert: implements buffer refill. Reads bytes from the current position
161 * in the input.
162 * @param b the array to read bytes into
163 * @param offset the offset in the array to start storing bytes
164 * @param length the number of bytes to read
165 */
166 virtual void readInternal(uint8_t* b, const int32_t len) = 0;
167
168 /** Expert: implements seek. Sets current position in this file, where the
169 * next {@link #readInternal(byte[],int32_t,int32_t)} will occur.
170 * @see #readInternal(byte[],int32_t,int32_t)
171 */
172 virtual void seekInternal(const int64_t pos) = 0;
173 };
174
175 /**
176 * JStream InputStream which reads from an IndexInput. This class is
177 * used by the FieldReader to create binary fields. You can then use
178 * a GZipInputStream to read compressed data or any of the other
179 * JStream stream types.
180 *
181 */
182 class IndexInputStream: public jstreams::BufferedInputStream<char>{
183 IndexInput* input;
184 public:
185 IndexInputStream(IndexInput* input);
186 ~IndexInputStream();
187 int32_t fillBuffer(char* start, int32_t space);
188 };
189CL_NS_END
190#endif
191