| 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.comparator.ReverseBlogEntryIdComparator; |
| 35 | |
import net.sourceforge.pebble.domain.Blog; |
| 36 | |
import net.sourceforge.pebble.domain.BlogEntry; |
| 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 AuthorIndex { |
| 49 | |
|
| 50 | 4 | private static final Log log = LogFactory.getLog(AuthorIndex.class); |
| 51 | |
|
| 52 | |
private Blog blog; |
| 53 | |
|
| 54 | |
|
| 55 | 2732 | private Map<String,List<String>> authors = new HashMap<String,List<String>>(); |
| 56 | |
|
| 57 | 2732 | public AuthorIndex(Blog blog) { |
| 58 | 2732 | this.blog = blog; |
| 59 | |
|
| 60 | 2732 | readIndex(); |
| 61 | 2732 | } |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public void clear() { |
| 67 | 36 | authors = new HashMap<String,List<String>>(); |
| 68 | 36 | writeIndex(); |
| 69 | 36 | } |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public synchronized void index(Collection<BlogEntry> blogEntries) { |
| 77 | 36 | for (BlogEntry blogEntry : blogEntries) { |
| 78 | 36 | if (blogEntry.isPublished()) { |
| 79 | 36 | List<String> blogEntryIds = getBlogEntries(blogEntry.getAuthor()); |
| 80 | 36 | blogEntryIds.add(blogEntry.getId()); |
| 81 | 36 | Collections.sort(blogEntryIds, new ReverseBlogEntryIdComparator()); |
| 82 | 36 | } |
| 83 | |
} |
| 84 | |
|
| 85 | 36 | writeIndex(); |
| 86 | 36 | } |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
public synchronized void index(BlogEntry blogEntry) { |
| 94 | 640 | if (blogEntry.isPublished()) { |
| 95 | 192 | List<String> blogEntryIds = getBlogEntries(blogEntry.getAuthor()); |
| 96 | 192 | blogEntryIds.add(blogEntry.getId()); |
| 97 | 192 | Collections.sort(blogEntryIds, new ReverseBlogEntryIdComparator()); |
| 98 | |
|
| 99 | 192 | writeIndex(); |
| 100 | |
} |
| 101 | 640 | } |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
public synchronized void unindex(BlogEntry blogEntry) { |
| 109 | 76 | List<String> blogEntries = authors.get(blogEntry.getAuthor()); |
| 110 | 76 | if (blogEntries != null) { |
| 111 | 20 | blogEntries.remove(blogEntry.getId()); |
| 112 | |
|
| 113 | 20 | if (blogEntries.isEmpty()) { |
| 114 | 20 | authors.remove(blogEntry.getAuthor()); |
| 115 | |
} |
| 116 | |
} |
| 117 | |
|
| 118 | 76 | writeIndex(); |
| 119 | 76 | } |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
private void readIndex() { |
| 125 | 2732 | File indexFile = new File(blog.getIndexesDirectory(), "authors.index"); |
| 126 | 2732 | if (indexFile.exists()) { |
| 127 | |
try { |
| 128 | 0 | BufferedReader reader = new BufferedReader(new FileReader(indexFile)); |
| 129 | 0 | String indexEntry = reader.readLine(); |
| 130 | 0 | while (indexEntry != null) { |
| 131 | 0 | String[] tuple = indexEntry.split("="); |
| 132 | 0 | String author = tuple[0]; |
| 133 | 0 | List<String> blogEntries = getBlogEntries(author); |
| 134 | |
|
| 135 | 0 | if (tuple.length > 1 && tuple[1] != null) { |
| 136 | 0 | String[] blogEntryIds = tuple[1].split(","); |
| 137 | 0 | for (String blogEntry : blogEntryIds) { |
| 138 | 0 | blogEntries.add(blogEntry); |
| 139 | |
} |
| 140 | |
} |
| 141 | |
|
| 142 | 0 | indexEntry = reader.readLine(); |
| 143 | 0 | } |
| 144 | |
|
| 145 | 0 | reader.close(); |
| 146 | 0 | } catch (Exception e) { |
| 147 | 0 | log.error("Error while reading index", e); |
| 148 | 0 | } |
| 149 | |
} |
| 150 | 2732 | } |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
private void writeIndex() { |
| 156 | |
try { |
| 157 | 340 | File indexFile = new File(blog.getIndexesDirectory(), "authors.index"); |
| 158 | 340 | BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile)); |
| 159 | |
|
| 160 | 340 | for (String author : authors.keySet()) { |
| 161 | 228 | writer.write(author); |
| 162 | 228 | writer.write("="); |
| 163 | 228 | List<String> blogEntries = authors.get(author); |
| 164 | 228 | if (blogEntries != null) { |
| 165 | 228 | for (String blogEntry : blogEntries) { |
| 166 | 256 | writer.write(blogEntry); |
| 167 | 256 | writer.write(","); |
| 168 | |
} |
| 169 | |
} |
| 170 | 228 | writer.newLine(); |
| 171 | 228 | } |
| 172 | |
|
| 173 | 340 | writer.flush(); |
| 174 | 340 | writer.close(); |
| 175 | 0 | } catch (Exception e) { |
| 176 | 0 | log.error("Error while writing index", e); |
| 177 | 340 | } |
| 178 | 340 | } |
| 179 | |
|
| 180 | |
private synchronized List<String> getBlogEntries(String author) { |
| 181 | 228 | List<String> blogEntries = authors.get(author); |
| 182 | 228 | if (blogEntries == null) { |
| 183 | 208 | blogEntries = new LinkedList<String>(); |
| 184 | 208 | authors.put(author, blogEntries); |
| 185 | |
} |
| 186 | |
|
| 187 | 228 | return blogEntries; |
| 188 | |
} |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
public List<String> getAuthors() { |
| 194 | 16 | return new LinkedList<String>(authors.keySet()); |
| 195 | |
} |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
public List<String> getRecentBlogEntries(String username) { |
| 204 | 24 | List<String> blogEntries = authors.get(username); |
| 205 | 24 | if (blogEntries == null) { |
| 206 | 8 | return new LinkedList<String>(); |
| 207 | |
} else { |
| 208 | 16 | return new LinkedList<String>(blogEntries); |
| 209 | |
} |
| 210 | |
} |
| 211 | |
|
| 212 | |
} |