Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart8.png 29% of files have more coverage
85   242   23   6,54
16   147   0,27   13
13     1,77  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  BlogEntryIndex       Line # 18 85 0% 23 30 73,7% 0.7368421
 
  (208)
 
1    package net.sourceforge.pebble.index;
2   
3    import net.sourceforge.pebble.comparator.ReverseBlogEntryIdComparator;
4    import net.sourceforge.pebble.domain.Blog;
5    import net.sourceforge.pebble.domain.BlogEntry;
6    import net.sourceforge.pebble.domain.Day;
7    import org.apache.commons.logging.Log;
8    import org.apache.commons.logging.LogFactory;
9   
10    import java.io.*;
11    import java.util.*;
12   
13    /**
14    * Keeps an index of all blog entries, allowing efficient access at runtime.
15    *
16    * @author Simon Brown
17    */
 
18    public class BlogEntryIndex {
19   
20    private static final Log log = LogFactory.getLog(BlogEntryIndex.class);
21   
22    private Blog blog;
23   
24    private List<String> indexEntries = new ArrayList<String>();
25    private List<String> publishedIndexEntries = new ArrayList<String>();
26    private List<String> unpublishedIndexEntries = new ArrayList<String>();
27   
 
28  1334 toggle public BlogEntryIndex(Blog blog) {
29  1334 this.blog = blog;
30   
31  1334 readIndex(true);
32  1334 readIndex(false);
33    }
34   
35    /**
36    * Clears the index.
37    */
 
38  18 toggle public void clear() {
39  18 indexEntries = new ArrayList<String>();
40  18 publishedIndexEntries = new ArrayList<String>();
41  18 unpublishedIndexEntries = new ArrayList<String>();
42  18 writeIndex(true);
43  18 writeIndex(false);
44    }
45   
46    /**
47    * Indexes one or more blog entries.
48    *
49    * @param blogEntries a List of BlogEntry instances
50    */
 
51  18 toggle public synchronized void index(Collection<BlogEntry> blogEntries) {
52  18 for (BlogEntry blogEntry : blogEntries) {
53  18 Day day = blog.getBlogForDay(blogEntry.getDate());
54  18 if (blogEntry.isPublished()) {
55  18 publishedIndexEntries.add(blogEntry.getId());
56  18 day.addPublishedBlogEntry(blogEntry.getId());
57    } else {
58  0 unpublishedIndexEntries.add(blogEntry.getId());
59  0 day.addUnpublishedBlogEntry(blogEntry.getId());
60    }
61  18 indexEntries.add(blogEntry.getId());
62    }
63   
64  18 Collections.sort(indexEntries, new ReverseBlogEntryIdComparator());
65  18 Collections.sort(publishedIndexEntries, new ReverseBlogEntryIdComparator());
66  18 Collections.sort(unpublishedIndexEntries, new ReverseBlogEntryIdComparator());
67   
68  18 writeIndex(true);
69  18 writeIndex(false);
70    }
71   
72    /**
73    * Indexes a single blog entry.
74    *
75    * @param blogEntry a BlogEntry instance
76    */
 
77  294 toggle public synchronized void index(BlogEntry blogEntry) {
78  294 Day day = blog.getBlogForDay(blogEntry.getDate());
79  294 if (blogEntry.isPublished()) {
80  92 publishedIndexEntries.add(blogEntry.getId());
81  92 day.addPublishedBlogEntry(blogEntry.getId());
82  92 writeIndex(true);
83    } else {
84  202 unpublishedIndexEntries.add(blogEntry.getId());
85  202 day.addUnpublishedBlogEntry(blogEntry.getId());
86  202 writeIndex(false);
87    }
88  294 indexEntries.add(blogEntry.getId());
89   
90  294 Collections.sort(indexEntries, new ReverseBlogEntryIdComparator());
91  294 Collections.sort(publishedIndexEntries, new ReverseBlogEntryIdComparator());
92  294 Collections.sort(unpublishedIndexEntries, new ReverseBlogEntryIdComparator());
93    }
94   
95    /**
96    * Unindexes a single blog entry.
97    *
98    * @param blogEntry a BlogEntry instance
99    */
 
100  36 toggle public synchronized void unindex(BlogEntry blogEntry) {
101  36 Day day = blog.getBlogForDay(blogEntry.getDate());
102  36 day.removeBlogEntry(blogEntry);
103   
104  36 indexEntries.remove(blogEntry.getId());
105  36 publishedIndexEntries.remove(blogEntry.getId());
106  36 unpublishedIndexEntries.remove(blogEntry.getId());
107   
108  36 writeIndex(true);
109  36 writeIndex(false);
110    }
111   
112    /**
113    * Helper method to load the index.
114    */
 
115  2668 toggle private void readIndex(boolean published) {
116  2668 File indexFile;
117  2668 if (published) {
118  1334 indexFile = new File(blog.getIndexesDirectory(), "blogentries-published.index");
119    } else {
120  1334 indexFile = new File(blog.getIndexesDirectory(), "blogentries-unpublished.index");
121    }
122   
123  2668 if (indexFile.exists()) {
124  0 try {
125  0 BufferedReader reader = new BufferedReader(new FileReader(indexFile));
126  0 String indexEntry = reader.readLine();
127  0 while (indexEntry != null) {
128  0 indexEntries.add(indexEntry);
129   
130    // and add it to the internal memory structures
131  0 Date date = new Date(Long.parseLong(indexEntry));
132  0 Day day = blog.getBlogForDay(date);
133   
134  0 if (published) {
135  0 publishedIndexEntries.add(indexEntry);
136  0 day.addPublishedBlogEntry(indexEntry);
137    } else {
138  0 unpublishedIndexEntries.add(indexEntry);
139  0 day.addUnpublishedBlogEntry(indexEntry);
140    }
141   
142  0 indexEntry = reader.readLine();
143    }
144   
145  0 reader.close();
146    } catch (Exception e) {
147  0 log.error("Error while reading index", e);
148    }
149    }
150   
151  2668 Collections.sort(indexEntries, new ReverseBlogEntryIdComparator());
152  2668 Collections.sort(publishedIndexEntries, new ReverseBlogEntryIdComparator());
153  2668 Collections.sort(unpublishedIndexEntries, new ReverseBlogEntryIdComparator());
154    }
155   
156    /**
157    * Helper method to write out the index to disk.
158    */
 
159  438 toggle private void writeIndex(boolean published) {
160  438 try {
161  438 File indexFile;
162  438 if (published) {
163  164 indexFile = new File(blog.getIndexesDirectory(), "blogentries-published.index");
164    } else {
165  274 indexFile = new File(blog.getIndexesDirectory(), "blogentries-unpublished.index");
166    }
167  438 BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile));
168   
169  438 if (published) {
170  164 for (String indexEntry : publishedIndexEntries) {
171  124 writer.write(indexEntry);
172  124 writer.newLine();
173    }
174    } else {
175  274 for (String indexEntry : unpublishedIndexEntries) {
176  348 writer.write(indexEntry);
177  348 writer.newLine();
178    }
179    }
180   
181  438 writer.flush();
182  438 writer.close();
183    } catch (Exception e) {
184  0 log.error("Error while writing index", e);
185    }
186    }
187   
188    /**
189    * Gets the number of blog entries for this blog.
190    *
191    * @return an int
192    */
 
193  16 toggle public int getNumberOfBlogEntries() {
194  16 return indexEntries.size();
195    }
196   
197    /**
198    * Gets the number of published blog entries for this blog.
199    *
200    * @return an int
201    */
 
202  0 toggle public int getNumberOfPublishedBlogEntries() {
203  0 return publishedIndexEntries.size();
204    }
205   
206    /**
207    * Gets the number of unpublished blog entries for this blog.
208    *
209    * @return an int
210    */
 
211  0 toggle public int getNumberOfUnpublishedBlogEntries() {
212  0 return unpublishedIndexEntries.size();
213    }
214   
215    /**
216    * Gets the full list of blog entries.
217    *
218    * @return a List of blog entry IDs
219    */
 
220  44 toggle public List<String> getBlogEntries() {
221  44 return new ArrayList<String>(indexEntries);
222    }
223   
224    /**
225    * Gets the full list of published blog entries.
226    *
227    * @return a List of blog entry IDs
228    */
 
229  50 toggle public List<String> getPublishedBlogEntries() {
230  50 return new ArrayList<String>(publishedIndexEntries);
231    }
232   
233    /**
234    * Gets the full list of unpublished blog entries.
235    *
236    * @return a List of blog entry IDs
237    */
 
238  0 toggle public List<String> getUnpublishedBlogEntries() {
239  0 return new ArrayList<String>(unpublishedIndexEntries);
240    }
241   
242    }