1 package org.kit.furia.index;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.PriorityQueue;
10
11 import org.ajmm.obsearch.Index;
12 import org.ajmm.obsearch.index.IndexShort;
13 import org.ajmm.obsearch.ob.OBShort;
14 import org.ajmm.obsearch.result.OBPriorityQueueShort;
15 import org.ajmm.obsearch.result.OBResultShort;
16 import org.apache.log4j.Logger;
17 import org.kit.furia.Document;
18 import org.kit.furia.ResultCandidate;
19 import org.kit.furia.Document.DocumentElement;
20 import org.kit.furia.exceptions.IRException;
21
22 import com.sleepycat.je.DatabaseException;
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 public class FIRIndexShort < O extends OBShort >
49 extends AbstractIRIndex < O > implements
50 org.kit.furia.IRIndexShort < O > {
51
52 private static final Logger logger = Logger.getLogger(FIRIndexShort.class.getSimpleName());
53
54
55
56
57 private IndexShort < O > index;
58
59
60
61
62
63
64
65
66
67 public FIRIndexShort(IndexShort < O > index, File dbFolder)
68 throws IOException {
69 super(dbFolder);
70 this.index = index;
71 }
72
73 public final List < ResultCandidate > search(Document < O > document, byte k,
74 short r, short n) throws IRException{
75 Iterator < Document < O >.DocumentElement < O >> it = document
76 .iterator();
77
78
79
80
81 Map<Integer, Integer> documentInTermsOfTheDatabase = new HashMap<Integer, Integer>(document.size() * k);
82
83 while (it.hasNext()) {
84 Document < O >.DocumentElement < O > elem = it.next();
85 O toMatch = elem.getObject();
86 OBPriorityQueueShort < O > result = new OBPriorityQueueShort < O >(
87 k);
88 try{
89
90 index.searchOB(toMatch, r, result);
91
92
93
94 Iterator<OBResultShort<O>> itO = result.iterator();
95 while(itO.hasNext()){
96 OBResultShort<O> match = itO.next();
97 Integer exists = documentInTermsOfTheDatabase.get(match.getId());
98 if(exists == null){
99 documentInTermsOfTheDatabase.put(match.getId(), elem.getCount());
100 }else{
101 documentInTermsOfTheDatabase.put(match.getId(), elem.getCount() + exists);
102 }
103 }
104
105
106
107 }catch(Exception e){
108 logger.fatal("Fatal error while searching" , e);
109 throw new IRException(e);
110 }
111
112 }
113 return processQueryResults(documentInTermsOfTheDatabase,n, document);
114 }
115
116
117 public Index < O > getIndex() {
118 return index;
119 }
120
121 public int getWordsSize() throws DatabaseException{
122 return this.index.databaseSize();
123 }
124
125 }