| 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 | |
package net.sourceforge.pebble.index; |
| 33 | |
|
| 34 | |
import net.sourceforge.pebble.domain.Blog; |
| 35 | |
import net.sourceforge.pebble.domain.BlogEntry; |
| 36 | |
import net.sourceforge.pebble.domain.Tag; |
| 37 | |
import org.apache.commons.logging.Log; |
| 38 | |
import org.apache.commons.logging.LogFactory; |
| 39 | |
|
| 40 | |
import java.io.*; |
| 41 | |
import java.util.*; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public class TagIndex { |
| 49 | |
|
| 50 | 4 | private static final Log log = LogFactory.getLog(TagIndex.class); |
| 51 | |
|
| 52 | |
private Blog blog; |
| 53 | |
|
| 54 | |
|
| 55 | 2728 | private Map<String,IndexedTag> tags = new HashMap<String,IndexedTag>(); |
| 56 | |
|
| 57 | |
|
| 58 | 2728 | private List<Tag> orderedTags = new ArrayList<Tag>(); |
| 59 | |
|
| 60 | 2728 | public TagIndex(Blog blog) { |
| 61 | 2728 | this.blog = blog; |
| 62 | |
|
| 63 | 2728 | readIndex(); |
| 64 | 2728 | recalculateTagRankings(); |
| 65 | 2728 | } |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public void clear() { |
| 71 | 36 | tags = new HashMap<String,IndexedTag>(); |
| 72 | 36 | writeIndex(); |
| 73 | 36 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public synchronized void index(Collection<BlogEntry> blogEntries) { |
| 81 | 36 | for (BlogEntry blogEntry : blogEntries) { |
| 82 | 36 | if (blogEntry.isPublished()) { |
| 83 | 36 | for (Tag tag : blogEntry.getAllTags()) { |
| 84 | 0 | IndexedTag t = getTag(tag.getName()); |
| 85 | 0 | t.addBlogEntry(blogEntry.getId()); |
| 86 | 0 | } |
| 87 | |
} |
| 88 | |
} |
| 89 | |
|
| 90 | 36 | writeIndex(); |
| 91 | 36 | recalculateTagRankings(); |
| 92 | 36 | } |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public synchronized void index(BlogEntry blogEntry) { |
| 100 | 200 | if (blogEntry.isPublished()) { |
| 101 | 196 | for (Tag tag : blogEntry.getAllTags()) { |
| 102 | 4 | IndexedTag t = getTag(tag.getName()); |
| 103 | 4 | t.addBlogEntry(blogEntry.getId()); |
| 104 | 4 | } |
| 105 | |
|
| 106 | 196 | writeIndex(); |
| 107 | 196 | recalculateTagRankings(); |
| 108 | |
} |
| 109 | 200 | } |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
public synchronized void unindex(BlogEntry blogEntry) { |
| 117 | 56 | for (Tag tag : tags.values()) { |
| 118 | 0 | IndexedTag t = getTag(tag.getName()); |
| 119 | 0 | t.removeBlogEntry(blogEntry.getId()); |
| 120 | 0 | } |
| 121 | |
|
| 122 | 56 | writeIndex(); |
| 123 | 56 | recalculateTagRankings(); |
| 124 | 56 | } |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
private void readIndex() { |
| 130 | 2728 | File indexFile = new File(blog.getIndexesDirectory(), "tags.index"); |
| 131 | 2728 | if (indexFile.exists()) { |
| 132 | |
try { |
| 133 | 0 | BufferedReader reader = new BufferedReader(new FileReader(indexFile)); |
| 134 | 0 | String indexEntry = reader.readLine(); |
| 135 | 0 | while (indexEntry != null) { |
| 136 | 0 | String[] tuple = indexEntry.split("="); |
| 137 | 0 | IndexedTag tag = getTag(tuple[0]); |
| 138 | |
|
| 139 | 0 | if (tuple.length > 1 && tuple[1] != null) { |
| 140 | 0 | String[] blogEntries = tuple[1].split(","); |
| 141 | 0 | for (String blogEntry : blogEntries) { |
| 142 | 0 | tag.addBlogEntry(blogEntry); |
| 143 | |
} |
| 144 | |
} |
| 145 | |
|
| 146 | 0 | indexEntry = reader.readLine(); |
| 147 | 0 | } |
| 148 | |
|
| 149 | 0 | reader.close(); |
| 150 | 0 | } catch (Exception e) { |
| 151 | 0 | log.error("Error while reading index", e); |
| 152 | 0 | } |
| 153 | |
} |
| 154 | 2728 | } |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
private void writeIndex() { |
| 160 | |
try { |
| 161 | 324 | File indexFile = new File(blog.getIndexesDirectory(), "tags.index"); |
| 162 | 324 | BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile)); |
| 163 | |
|
| 164 | 324 | for (IndexedTag tag : tags.values()) { |
| 165 | 4 | writer.write(tag.getName()); |
| 166 | 4 | writer.write("="); |
| 167 | 4 | for (String blogEntry : tag.getBlogEntries()) { |
| 168 | 4 | writer.write(blogEntry); |
| 169 | 4 | writer.write(","); |
| 170 | |
} |
| 171 | 4 | writer.newLine(); |
| 172 | |
} |
| 173 | |
|
| 174 | 324 | writer.flush(); |
| 175 | 324 | writer.close(); |
| 176 | 0 | } catch (Exception e) { |
| 177 | 0 | log.error("Error while writing index", e); |
| 178 | 324 | } |
| 179 | 324 | } |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
synchronized IndexedTag getTag(String name) { |
| 188 | 12 | String encodedName = Tag.encode(name); |
| 189 | 12 | IndexedTag tag = tags.get(encodedName); |
| 190 | 12 | if (tag == null) { |
| 191 | 8 | tag = new IndexedTag(name, blog); |
| 192 | 8 | tags.put(encodedName, tag); |
| 193 | |
} |
| 194 | 12 | return tag; |
| 195 | |
} |
| 196 | |
|
| 197 | |
private synchronized void recalculateTagRankings() { |
| 198 | 3016 | if (tags.size() > 0) { |
| 199 | |
|
| 200 | 4 | int maxBlogEntries = 0; |
| 201 | 4 | for (IndexedTag tag : tags.values()) { |
| 202 | 4 | if (tag.getNumberOfBlogEntries() > maxBlogEntries) { |
| 203 | 4 | maxBlogEntries = tag.getNumberOfBlogEntries(); |
| 204 | |
} |
| 205 | |
} |
| 206 | |
|
| 207 | 4 | int[] thresholds = new int[10]; |
| 208 | 44 | for (int i = 0; i < 10; i++) { |
| 209 | 40 | thresholds[i] = (int)Math.round((maxBlogEntries/10.0) * (i+1)); |
| 210 | |
} |
| 211 | |
|
| 212 | 4 | orderedTags = new ArrayList<Tag>(); |
| 213 | |
|
| 214 | |
|
| 215 | 4 | for (IndexedTag tag : tags.values()) { |
| 216 | 4 | tag.calculateRank(thresholds); |
| 217 | |
|
| 218 | 4 | if (tag.getNumberOfBlogEntries() > 0) { |
| 219 | 4 | orderedTags.add(tag); |
| 220 | |
} |
| 221 | |
} |
| 222 | |
|
| 223 | 4 | Collections.sort(orderedTags); |
| 224 | |
} |
| 225 | |
|
| 226 | 3016 | } |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
public List<Tag> getTags() { |
| 232 | 12 | return new ArrayList<Tag>(orderedTags); |
| 233 | |
} |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
public List<String> getRecentBlogEntries(Tag tag) { |
| 242 | 0 | return new ArrayList<String>(getTag(tag.getName()).getBlogEntries()); |
| 243 | |
} |
| 244 | |
|
| 245 | |
} |