#include "CLucene/StdHeader.h" #ifndef CLUCENE_LITE #include "FieldsWriter.h" #include "CLucene/util/VoidMap.h" #include "CLucene/util/Reader.h" #include "CLucene/store/Directory.h" #include "CLucene/store/OutputStream.h" #include "CLucene/document/Document.h" #include "CLucene/document/Field.h" #include "FieldInfos.h" namespace lucene{ namespace index { //static const char_t* FieldsWriter::segmentname( const char_t* segment, const char_t* ext ){ char_t* buf = new char_t[MAX_PATH]; stringPrintF(buf,_T("%s%s"), segment,ext ); return buf; } FieldsWriter::FieldsWriter(Directory& d, const char_t* segment, FieldInfos& fn): fieldInfos(fn) { const char_t* buf = segmentname(segment,_T(".fdt")); fieldsStream = &d.createFile ( buf ); delete[] buf; buf = segmentname(segment,_T(".fdx")); indexStream = &d.createFile( buf ); delete[] buf; } FieldsWriter::~FieldsWriter(){ } void FieldsWriter::close() { fieldsStream->close(); indexStream->close(); _DELETE( fieldsStream ); _DELETE( indexStream ); } void FieldsWriter::addDocument(lucene::document::Document& doc) { indexStream->writeLong(fieldsStream->getFilePointer()); int_t storedCount = 0; lucene::document::DocumentFieldEnumeration* fields = doc.fields(); while (fields->hasMoreElements()) { lucene::document::Field* field = fields->nextElement(); if (field->IsStored()) storedCount++; } delete fields; fieldsStream->writeVInt(storedCount); fields = doc.fields(); while (fields->hasMoreElements()) { lucene::document::Field* field = fields->nextElement(); if (field->IsStored()) { fieldsStream->writeVInt(fieldInfos.fieldNumber(field->Name())); l_byte_t bits = 0; if (field->IsTokenized()) bits |= 1; //FEATURE: can this data be compressed? A bit could be set to compress or not compress??? fieldsStream->writeByte(bits); //FEATURE: this problem in Java Lucene too, if using Reader, data is not stored. if ( field->StringValue() == NULL ){ lucene::util::Reader* r = field->ReaderValue(); int_t rp = r->position(); r->seek(0); int_t rl = r->available(); char_t* rv = new char_t[rl]; r->read(rv,0,rl); r->seek(rp); //reset position //simulate writeString fieldsStream->writeVInt(rl); fieldsStream->writeChars(rv,0,rl); delete[] rv; }else fieldsStream->writeString(field->StringValue()); } } delete fields; } }} #endif