summaryrefslogtreecommitdiffstats
path: root/3rdparty/clucene/src/CLucene/index/SegmentMergeQueue.cpp
blob: 87978128741cbda59de4f7f911e45d28f463f1c5 (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
/*------------------------------------------------------------------------------
* 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 "SegmentMergeQueue.h"

#include "SegmentMergeInfo.h"
CL_NS_DEF(index)


	SegmentMergeQueue::SegmentMergeQueue(const int32_t size) {
	//Func - Constructor
	//       Creates a queue of length size
	//Pre  - size >= 0
	//Post - The queue has been created of length size

		//BVK: bug. changed condition from size > 0 to size >= 0
		//if size is 0, as it is  when retrieving a TermEnum 
		//from an empty index this should this should not fail.
        CND_PRECONDITION(size >= 0, "size is too small");

		//Call the initialize method of its superclass. The boolean value  passed here
		//indicates that the superclass PriorityQueue takes the responsibility to have its elements deleted
		//The destructor of SegmentMergInfo will make sure that each intstance it will be closed properly 
		//before it is deleted
 		initialize(size,true);
	}

	SegmentMergeQueue::~SegmentMergeQueue(){
	//Func - Destructor
	//       Does nothing as its parent class will clean up everything
	//Pre  - true
	//Post - true
        close();
	}

	void SegmentMergeQueue::close() {
	//Func - Closes and destroyes all SegmentMergeInfo Instances in the queue
    //Pre  - true
	//post - All SegmentMergeInfo Instances in the queue have been closed and  deleted 
	//       The queue is now empty but can still be used

		//call the clear method of the parent class PriorityQueue
        clear();
	}

	bool SegmentMergeQueue::lessThan(SegmentMergeInfo* stiA, SegmentMergeInfo* stiB) {
   //Func - Overloaded method that implements the lessThan operator for the parent class
   //       This method is used by the parent class Priority queue to reorder its internal
   //       data structures. This implementation check if stiA is less than the current term of stiB.
   //Pre  - stiA != NULL
   //		stiB != NULL
   //Post - true is returned if stiA < stiB otherwise false

        CND_PRECONDITION(stiA != NULL, "stiA is NULL");
        CND_PRECONDITION(stiB != NULL, "stiB is NULL");

		//Compare the two terms 
		int32_t comparison = stiA->term->compareTo(stiB->term);
		//Check if they match
		if (comparison == 0){ //todo: can we do an optimized compare here? compare using equals, then compare properly?
			//If the match check if the base of stiA is smaller than the base of stiB
			//Note that different bases means that the terms of stiA an stiB ly in different segments
			return stiA->base < stiB->base; 
		}else{
			//Terms didn't match so return the difference in positions
			return comparison < 0;
		}
	}

CL_NS_END