00001 /* 00002 * Copyright (C) 2009, Edmundo Albuquerque de Souza e Silva. 00003 * 00004 * This file may be distributed under the terms of the Q Public License 00005 * as defined by Trolltech AS of Norway and appearing in the file 00006 * LICENSE.QPL included in the packaging of this file. 00007 * 00008 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING 00009 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00010 * PURPOSE. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, 00011 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 00012 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00013 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00014 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00015 * 00016 * Thanks: Jose Renato Santos 00017 * 00018 */ 00019 00020 /////////////////////////////////////////////////////////////////////////////// 00021 // ObjMapMgr.h 00022 /////////////////////////////////////////////////////////////////////////////// 00023 00024 #ifndef __OBJMAPMGR_H_ 00025 #define __OBJMAPMGR_H_ 00026 00027 //Start - GCC 3 00028 //#include <fstream.h> 00029 #include <fstream> 00030 //#include <ostream> 00031 #include <iostream> 00032 using namespace std; 00033 //End - GCC 3 00034 00035 #include <sys/types.h> 00036 #include <dirent.h> 00037 00038 #include "RioTypes.h" 00039 #include "ObjectInfo.h" 00040 #include "ServerTypes.h" 00041 00042 /////////////////////////////////////////////////////////////////////////////// 00043 00044 class ObjMapMgr; 00045 class DiskMgr; 00046 00047 // Identificador para o cabecalho do arquivo de metadado. 00048 00049 const char ObjectSignatureValue[8] = 00050 {(char)0x99, (char)0x5a, (char)0x50, (char)0x8b, (char)0xe7, (char)0xea, (char)0x5b, (char)0x1b}; 00051 00052 00053 struct ObjectHeader 00054 { 00055 char Signature[8]; 00056 short Type; 00057 time_t LastWrite; 00058 int nRep; 00059 unsigned int BlockSize; 00060 RioBlock nBlocks; 00061 RioObjectSize Size; 00062 00063 unsigned int VideoRate; // 4 bytes 00064 bool nullmd5; 00065 unsigned char md5sum[16]; 00066 char pad[7]; // pad to 64 byte boundary. 00067 }; 00068 00069 /////////////////////////////////////////////////////////////////////////////// 00070 // Only built by ObjMapMgr 00071 class RioObject 00072 { 00073 private: 00074 ObjMapMgr *o_mgr; 00075 char *o_Name; 00076 int o_FileHandle; 00077 bool o_DeleteOnClose; 00078 bool o_MapChanged; 00079 bool o_MapValid; 00080 RioBlock o_nMapBlocks; 00081 RioDiskBlock *o_Map; 00082 00083 bool o_Locked; // I/O (MapBlock) fails if set 00084 bool o_HdrValid; 00085 ObjectHeader o_Hdr; 00086 00087 RioObject( ObjMapMgr *mgr ); 00088 RioObject( const RioObject &x ); // not implemented 00089 RioObject &operator = ( const RioObject &x );// not implemented 00090 00091 int SaveObject(); 00092 void PrintHdr(); 00093 // O novo parametro e um vetor de bits que indica quais servidores de 00094 // armazenamento nao podem ser usados (cada bit esta associado a um 00095 // servidor e um valor 1 neste bit indica que o servidor nao pode ser 00096 // usado). 00097 int Extend( RioBlock nBlocks, unsigned long long int ExcludeStorages ); 00098 int Truncate( RioBlock nBlocks ); 00099 00100 friend class ObjMapMgr; 00101 00102 public: 00103 ~RioObject(); 00104 00105 int Close(); 00106 int GetSize( RioObjectSize *Size ); 00107 int GetType( short *Type ); 00108 int GetnBlocks( RioBlock *nBlocks ); 00109 int GetBlockMap( RioBlock first, 00110 RioBlock nBlocks, 00111 RioDiskBlock *blocks); 00112 // O novo parametro e um vetor de bits que indica quais servidores de 00113 // armazenamento nao podem ser usados (cada bit esta associado a um 00114 // servidor e um valor 1 neste bit indica que o servidor nao pode ser 00115 // usado). 00116 int SetSize( RioObjectSize Size, char *md5sum, 00117 unsigned long long int ExcludeStorages ); 00118 int MarkDelete(); 00119 int MapBlock( RioBlock Block, int RepBits, int *numreps, 00120 RioDiskBlock *reps ); 00121 00122 /** 00123 * Funcao GetObjectName: retorna o nome do arquivo associado ao objeto 00124 * RioObject. O nome retornado inclui o caminho relativo do arquivo no 00125 * sistema de arquivos do sistema operacional em que o Servidor RIO esta 00126 * executando, em relacao ao diretorio do servidor.(esta funcao e usada 00127 * pela implementacao da cache). 00128 * @param ObjectName Ponteiro para uma string que ira armazenar o nome 00129 * do arquivo com o seu caminho. A funcao supoe que existe espaco 00130 * suficiente na string para copiar o nome no endereco apontado por 00131 * ObjectName (contando o terminador). 00132 * @return S_OK (a funcao nao gera erros). 00133 */ 00134 int GetObjectName( char *ObjectName ); 00135 00136 // Novas funcoes usadas pelo controle de fluxo. 00137 00138 /** 00139 * GetVideoRate retorna a taxa de transmissao (em Kbps) do video. 00140 * @param VideoRate ponteiro para o inteiro nao sinalizado que 00141 * armazenara a taxa de transmissao do video. 00142 * @return S_OK. 00143 */ 00144 int GetVideoRate( unsigned int *VideoRate ); 00145 00146 /** 00147 * SetVideoRate altera a taxa de transmissao (em Kbps) do video. 00148 * @param VideoRate nova taxa de transmissao do video. 00149 * @return o valor retornado pela funcao SaveObject() 00150 */ 00151 int SetVideoRate( unsigned int VideoRate ); 00152 00153 // Nova funcao usada pela implementacao que permite o reinicio dos 00154 // servidores. 00155 00156 /** 00157 * Nova funcao usada para realocar os blocos associados ao ultimo bloco 00158 * do arquivo associado ao objeto. Este funcao e usada pelo objeto da 00159 * classe Router ao reenviarmos um evento de escrita de um bloco no 00160 * disco 00161 * @param Block numero do bloco a ser realocado. 00162 * @param ExcludeStorages vetor de bits com os servidores de 00163 * armazenamento que nao devem ser usados. Cada bit i esta associado ao 00164 * servidor com a ID i e um valor 1 neste bit indica que este servidor 00165 * nao pode ser usado. 00166 * @return S_OK se a realocacao dos blocos foi feita com sucesso e um 00167 * valor diferente de S_OK se algum erro ocorreu. 00168 */ 00169 int ReallocBlocks( RioBlock Block, 00170 unsigned long long int ExcludeStorages ); 00171 }; 00172 00173 /////////////////////////////////////////////////////////////////////////////// 00174 class ObjMapMgr 00175 { 00176 private: 00177 int m_used; 00178 int m_n; 00179 unsigned int m_BlockSize; 00180 int m_MaxReplications; 00181 int m_UseReplications; 00182 DiskMgr *m_DiskMgr; 00183 00184 // m_mutexmeta locks metadata on disk + m_used 00185 // (metadata is data in file, not file directory entry 00186 pthread_mutex_t m_mutexmeta; 00187 00188 // m_mutexfast locks in storage data (including RioObject(s) 00189 // include o_Map and o_Locked 00190 pthread_mutex_t m_mutexfast; 00191 00192 ostream *m_log; 00193 00194 friend class RioObject; 00195 00196 public: 00197 ObjMapMgr(); 00198 ~ObjMapMgr(); 00199 int Initialize( int Size, unsigned int BlockSize, 00200 int MaxReplications, int UseReplications, 00201 DiskMgr *DiskMgr, 00202 ostream *LogStream ); 00203 int Start(); 00204 int Stop(); 00205 00206 int Open( char* Name, RioAccess Access, RioObject **Object ); 00207 00208 void CleanUpDelete(); 00209 int GetObjectInfo( char* Name, ObjectInfo* ObjectInfo ); 00210 00211 int createobject( char *name ); 00212 00213 #ifdef RIO_64 00214 static const int OBJHDRSIZE = 80; 00215 #else 00216 static const int OBJHDRSIZE = 64; 00217 #endif 00218 }; 00219 00220 #endif // __OBJMAPMGR_H_