summaryrefslogtreecommitdiffstats
path: root/3rdparty/clucene/src/CLucene/index/SegmentMergeInfo.cpp
blob: 85ac784adf23acefc31c82c9c05511161e0382e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*------------------------------------------------------------------------------
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
* 
* Distributable under the terms of either the Apache License (Version 2.0) or 
* the GNU Lesser General Public License, as specified in the COPYING file.
------------------------------------------------------------------------------*/
#include "CLucene/StdHeader.h"
#include "SegmentMergeInfo.h"

#include "SegmentTermEnum.h"
#include "SegmentHeader.h"

CL_NS_DEF(index)

SegmentMergeInfo::SegmentMergeInfo(const int32_t b, TermEnum* te, IndexReader* r):
    reader(r),termEnum(te),base(b), docMap(NULL) {
//Func - Constructor
//Pre  - b >= 0
//       te contains a valid reference to a SegmentTermEnum instance
//       r contains a valid reference to a SegmentReader instance
//Post - The instance has been created

    CND_PRECONDITION(b >= 0, "b is a negative number");

    postings=NULL;
	term   = te->term();
}

SegmentMergeInfo::~SegmentMergeInfo(){
//Func - Destructor
//Pre  - true
//Post - The instance has been destroyed

  close();
}

int32_t* SegmentMergeInfo::getDocMap(){
	if ( docMap == NULL ){
      	// build array which maps document numbers around deletions 
		if (reader->hasDeletions()) {
			//Get the total number of documents managed by the reader including the deleted ones
			int32_t maxDoc = reader->maxDoc();
			//Create a map for all documents
			docMap = _CL_NEWARRAY(int32_t,maxDoc);
			int32_t j = 0;
			//Iterate through all the document numbers
			for (int32_t i = 0; i < maxDoc; i++) {
            //Check if document i is marked deleted
				if (reader->isDeleted(i)){
					//Document i has not been marked deleted so assign -1
					docMap[i] = -1;
				}else{
					docMap[i] = j++;
				}
			}
		}
	}
	return docMap;
}

TermPositions* SegmentMergeInfo::getPositions() {
    if (postings == NULL) {
    	postings = reader->termPositions();
    }
    return postings;
}


bool SegmentMergeInfo::next() {
//Func - Moves the current term of the enumeration termEnum to the next and term
//       points to this new current term
//Pre  - true
//Post - Returns true if the term has been moved to the next otherwise false
	if (termEnum->next()) {
		_CLDECDELETE(term);
		term = termEnum->term();
		return true;
	} else {
		_CLDECDELETE(term); //TODO: test HighFreqTerms errors with this
		term = NULL;
		return false;
	}
}

void SegmentMergeInfo::close() {
//Func - Closes the the resources
//Pre  - true
//Post - The resources have been closed

    //First make sure posting has been closed
    if ( postings != NULL ){
        postings->close();
        _CLVDELETE(postings); //todo: not a clucene object... should be
    }

    if ( termEnum != NULL ){
        termEnum->close();
        _CLDELETE(termEnum);
    }
	_CLDECDELETE(term);
	_CLDELETE_ARRAY(docMap);
}

CL_NS_END