FirteX-高性能全文索引和检索平台API Documentation |
00001 // 00002 // Copyright(C) 2005--2006 Institute of Computing Tech, Chinese Academy of Sciences. 00003 // All rights reserved. 00004 // This file is part of FirteX (www.firtex.org) 00005 // 00006 // Use of the FirteX is subject to the terms of the software license set forth in 00007 // the LICENSE file included with this software, and also available at 00008 // http://www.firtex.org/license.html 00009 // 00010 // Author : 郭瑞杰(GuoRuijie) 00011 // Email : ruijieguo@software.ict.ac.cn,ruijieguo@gmail.com 00012 // Created : 2005/11/9 00013 // 00014 #ifndef __FILECACHEREADER_H 00015 #define __FILECACHEREADER_H 00016 00017 #if _MSC_VER > 1000 00018 #pragma once 00019 #endif // _MSC_VER > 1000 00020 00021 00022 #include <fstream> 00023 #include <string> 00024 #include "IndexInput.h" 00025 #include "utility/Config.h" 00026 00027 using namespace std; 00028 00029 namespace firtex 00030 { 00031 namespace store 00032 { 00033 class CFSIndexInput : public CIndexInput 00034 { 00035 public: 00036 CFSIndexInput(const tchar* filename) 00037 { 00038 m_fileHandle = ::fileOpen(filename, O_BINARY | O_RDONLY | O_RANDOM, _S_IREAD ); 00039 00040 if (m_fileHandle < 0) 00041 { 00042 int err = errno; 00043 tstring sFileName = filename; 00044 if ( err == ENOENT ) 00045 FIRTEX_THROW2(FILEIO_ERROR,"File does not exist: " + sFileName); 00046 else if ( err == EACCES ) 00047 FIRTEX_THROW2(FILEIO_ERROR,"File Access denied: " + sFileName); 00048 else if ( err == EMFILE ) 00049 FIRTEX_THROW2(FILEIO_ERROR,"Too many open files: " + sFileName); 00050 } 00051 m_length = fileSize(m_fileHandle); 00052 m_filename = filename; 00053 } 00054 CFSIndexInput(const tchar* filename,char* buffer,size_t buffsize):CIndexInput(buffer,buffsize) 00055 { 00056 m_fileHandle = ::fileOpen(filename, O_BINARY | O_RDONLY | O_RANDOM, _S_IREAD ); 00057 00058 if (m_fileHandle < 0) 00059 { 00060 int err = errno; 00061 tstring sFileName = filename; 00062 if ( err == ENOENT ) 00063 FIRTEX_THROW2(FILEIO_ERROR,"File does not exist: " + sFileName); 00064 else if ( err == EACCES ) 00065 FIRTEX_THROW2(FILEIO_ERROR,"File Access denied: " + sFileName); 00066 else if ( err == EMFILE ) 00067 FIRTEX_THROW2(FILEIO_ERROR,"Too many open files: " + sFileName); 00068 } 00069 m_length = fileSize(m_fileHandle); 00070 m_filename = filename; 00071 } 00072 CFSIndexInput(const tchar* filename,size_t buffsize):CIndexInput(buffsize) 00073 { 00074 m_fileHandle = ::fileOpen(filename, O_BINARY | O_RDONLY | O_RANDOM, _S_IREAD ); 00075 00076 if (m_fileHandle < 0) 00077 { 00078 int err = errno; 00079 tstring sFileName = filename; 00080 if ( err == ENOENT ) 00081 FIRTEX_THROW2(FILEIO_ERROR,"File does not exist: " + sFileName); 00082 else if ( err == EACCES ) 00083 FIRTEX_THROW2(FILEIO_ERROR,"File Access denied: " + sFileName); 00084 else if ( err == EMFILE ) 00085 FIRTEX_THROW2(FILEIO_ERROR,"Too many open files: " + sFileName); 00086 } 00087 m_length = fileSize(m_fileHandle); 00088 m_filename = filename; 00089 } 00090 00091 virtual ~CFSIndexInput() 00092 { 00093 close(); 00094 } 00095 public: 00096 void readInternal(char* b, size_t offset, size_t len) 00097 { 00098 int64_t position = getFilePointer(); 00099 if (position != fileTell(m_fileHandle)) 00100 { 00101 if(fileSeek(m_fileHandle,position,SEEK_SET) != position) 00102 FIRTEX_THROW2(FILEIO_ERROR,"CFSIndexInput::readInternal():file IO seek error: " + m_filename); 00103 } 00104 size_t ret = ::fileRead(m_fileHandle,b+offset,(unsigned int)len); 00105 if(ret == 0) 00106 FIRTEX_THROW2(FILEIO_ERROR,"CFSIndexInput::readInternal():read past EOF:" + m_filename); 00107 else if (ret == -1) 00108 { 00109 int err = errno; 00110 cout << err; 00111 FIRTEX_THROW2(FILEIO_ERROR,"CFSIndexInput::readInternal():file IO read error:" + m_filename); 00112 } 00113 } 00114 00115 void close() 00116 { 00117 if(m_fileHandle >= 0) 00118 { 00119 ::fileClose(m_fileHandle); 00120 m_fileHandle = -1; 00121 } 00122 } 00123 00124 00125 CIndexInput* clone(char* buffer,size_t buffsize) 00126 { 00127 CFSIndexInput* pClone = new CFSIndexInput(m_filename.c_str(),buffer,buffsize); 00128 return pClone; 00129 } 00130 00131 CIndexInput* clone() 00132 { 00133 CFSIndexInput* pClone = new CFSIndexInput(m_filename.c_str(),m_bufferSize); 00134 return pClone; 00135 } 00136 protected: 00137 void seekInternal(int64_t position) 00138 { 00139 if(fileSeek(m_fileHandle,position,SEEK_SET) != position) 00140 FIRTEX_THROW2(FILEIO_ERROR,"CFSIndexInput::seekInternal():file IO seek error: " + m_filename); 00141 } 00142 protected: 00143 int m_fileHandle; 00144 tstring m_filename; 00145 }; 00146 } 00147 } 00148 00149 00150 #endif
http://www.firtex.org http://www.sourceforge.net/projects/firtex