int32_t SegmentMerger::mergeFields()
{
    //Func - Merge the fields of all segments 
    //Pre  - true
    //Post - The field infos and field values of all segments have been merged.

    //Create a new FieldInfos
    fieldInfos = _CLNEW FieldInfos();		  // merge field names

    //Condition check to see if fieldInfos points to a valid instance
    CND_CONDITION(fieldInfos != NULL, "Memory allocation for fieldInfos failed");

    IndexReader* reader = NULL;

    int32_t docCount = 0;

    //Iterate through all readers
    for (uint32_t i = 0; i < readers.size(); i++) {
        //get the i-th reader
        reader = readers[i];
        //Condition check to see if reader points to a valid instance
        CND_CONDITION(reader != NULL,"No IndexReader found");

        StringArrayWithDeletor tmp;

        tmp.clear();
        reader->getFieldNames(IndexReader::TERMVECTOR_WITH_POSITION_OFFSET, tmp);
        addIndexed(reader, fieldInfos, tmp, true, true, true);

        tmp.clear();
        reader->getFieldNames(IndexReader::TERMVECTOR_WITH_POSITION, tmp);
        addIndexed(reader, fieldInfos, tmp, true, true, false);

        tmp.clear();
        reader->getFieldNames(IndexReader::TERMVECTOR_WITH_OFFSET, tmp);
        addIndexed(reader, fieldInfos, tmp, true, false, true);

        tmp.clear();
        reader->getFieldNames(IndexReader::TERMVECTOR, tmp);
        addIndexed(reader, fieldInfos, tmp, true, false, false);

        tmp.clear();
        reader->getFieldNames(IndexReader::INDEXED, tmp);
        addIndexed(reader, fieldInfos, tmp, false, false, false);

        tmp.clear();
        reader->getFieldNames(IndexReader::UNINDEXED, tmp);
        if (tmp.size() > 0) {
            TCHAR** arr = _CL_NEWARRAY(TCHAR*,tmp.size()+1);
            tmp.toArray(arr);
            fieldInfos->add((const TCHAR**)arr, false);
            _CLDELETE_ARRAY(arr);
            //no need to delete the contents, since tmp is responsible for it
        }
    }
int32_t SegmentMerger::mergeFields() {
//Func - Merge the fields of all segments
//Pre  - true
//Post - The field infos and field values of all segments have been merged.

  if (!mergeDocStores) {
    // When we are not merging by doc stores, that means
    // all segments were written as part of a single
    // autoCommit=false IndexWriter session, so their field
    // name -> number mapping are the same.  So, we start
    // with the fieldInfos of the last segment in this
    // case, to keep that numbering.
    assert(readers[readers.size()-1]->instanceOf(SegmentReader::getClassName()));
    assert(false);//check last...and remove if correct...
    SegmentReader* sr = (SegmentReader*)readers[readers.size()-1];
    fieldInfos = sr->fieldInfos()->clone();
  } else {
    //Create a new FieldInfos
    fieldInfos = _CLNEW FieldInfos();		  // merge field names
  }
	//Condition check to see if fieldInfos points to a valid instance
	CND_CONDITION(fieldInfos != NULL,"Memory allocation for fieldInfos failed");

	IndexReader* reader = NULL;

  //Iterate through all readers
  for (uint32_t i = 0; i < readers.size(); i++){
	  //get the i-th reader
	  reader = readers[i];
	  //Condition check to see if reader points to a valid instance
	  CND_CONDITION(reader != NULL,"No IndexReader found");

    if (reader->instanceOf(SegmentReader::getClassName())) {
      SegmentReader* segmentReader = (SegmentReader*) reader;
      for (size_t j = 0; j < segmentReader->getFieldInfos()->size(); j++) {
        FieldInfo* fi = segmentReader->getFieldInfos()->fieldInfo(j);
        fieldInfos->add(fi->name, fi->isIndexed, fi->storeTermVector,
          fi->storePositionWithTermVector, fi->storeOffsetWithTermVector,
          !reader->hasNorms(fi->name), fi->storePayloads);
      }
    } else {
	    StringArrayWithDeletor tmp;

	    tmp.clear(); reader->getFieldNames(IndexReader::TERMVECTOR_WITH_POSITION_OFFSET, tmp);
	    addIndexed(reader, fieldInfos, tmp, true, true, true, false);

	    tmp.clear(); reader->getFieldNames(IndexReader::TERMVECTOR_WITH_POSITION, tmp);
	    addIndexed(reader, fieldInfos, tmp, true, true, false, false);

	    tmp.clear(); reader->getFieldNames(IndexReader::TERMVECTOR_WITH_OFFSET, tmp);
	    addIndexed(reader, fieldInfos, tmp, true, false, true, false);

	    tmp.clear(); reader->getFieldNames(IndexReader::TERMVECTOR, tmp);
	    addIndexed(reader, fieldInfos, tmp, true, false, false, false);

	    tmp.clear(); reader->getFieldNames(IndexReader::STORES_PAYLOADS, tmp);
	    addIndexed(reader, fieldInfos, tmp, false, false, false, true);

	    tmp.clear(); reader->getFieldNames(IndexReader::INDEXED, tmp);
	    addIndexed(reader, fieldInfos, tmp, false, false, false, false);

	    tmp.clear(); reader->getFieldNames(IndexReader::UNINDEXED, tmp);
	    if ( tmp.size() > 0 ){
		    TCHAR** arr = _CL_NEWARRAY(TCHAR*,tmp.size()+1);
            tmp.toArray_nullTerminated(arr);
		    fieldInfos->add((const TCHAR**)arr, false);
		    _CLDELETE_ARRAY(arr); //no need to delete the contents, since tmp is responsible for it
	    }
    }
  }