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
62   212   23   6,2
20   113   0,37   10
10     2,3  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  AuthorIndex       Line # 48 62 0% 23 23 75% 0.75
 
  (186)
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * - Redistributions of source code must retain the above copyright
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * without specific prior written permission.
19    *
20    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    * POSSIBILITY OF SUCH DAMAGE.
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    * Keeps an index of all blog entries from a specific author, allowing efficient access at runtime.
45    *
46    * @author Simon Brown
47    */
 
48    public class AuthorIndex {
49   
50    private static final Log log = LogFactory.getLog(AuthorIndex.class);
51   
52    private Blog blog;
53   
54    /** the map containing the tags */
55    private Map<String,List<String>> authors = new HashMap<String,List<String>>();
56   
 
57  1340 toggle public AuthorIndex(Blog blog) {
58  1340 this.blog = blog;
59   
60  1340 readIndex();
61    }
62   
63    /**
64    * Clears the index.
65    */
 
66  18 toggle public void clear() {
67  18 authors = new HashMap<String,List<String>>();
68  18 writeIndex();
69    }
70   
71    /**
72    * Indexes one or more blog entries.
73    *
74    * @param blogEntries a List of BlogEntry instances
75    */
 
76  18 toggle public synchronized void index(Collection<BlogEntry> blogEntries) {
77  18 for (BlogEntry blogEntry : blogEntries) {
78  18 if (blogEntry.isPublished()) {
79  18 List<String> blogEntryIds = getBlogEntries(blogEntry.getAuthor());
80  18 blogEntryIds.add(blogEntry.getId());
81  18 Collections.sort(blogEntryIds, new ReverseBlogEntryIdComparator());
82    }
83    }
84   
85  18 writeIndex();
86    }
87   
88    /**
89    * Indexes a single blog entry.
90    *
91    * @param blogEntry a BlogEntry instance
92    */
 
93  300 toggle public synchronized void index(BlogEntry blogEntry) {
94  300 if (blogEntry.isPublished()) {
95  96 List<String> blogEntryIds = getBlogEntries(blogEntry.getAuthor());
96  96 blogEntryIds.add(blogEntry.getId());
97  96 Collections.sort(blogEntryIds, new ReverseBlogEntryIdComparator());
98   
99  96 writeIndex();
100    }
101    }
102   
103    /**
104    * Unindexes a single blog entry.
105    *
106    * @param blogEntry a BlogEntry instance
107    */
 
108  38 toggle public synchronized void unindex(BlogEntry blogEntry) {
109  38 List<String> blogEntries = authors.get(blogEntry.getAuthor());
110  38 if (blogEntries != null) {
111  10 blogEntries.remove(blogEntry.getId());
112   
113  10 if (blogEntries.isEmpty()) {
114  10 authors.remove(blogEntry.getAuthor());
115    }
116    }
117   
118  38 writeIndex();
119    }
120   
121    /**
122    * Helper method to load the index.
123    */
 
124  1340 toggle private void readIndex() {
125  1340 File indexFile = new File(blog.getIndexesDirectory(), "authors.index");
126  1340 if (indexFile.exists()) {
127  0 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    }
144   
145  0 reader.close();
146    } catch (Exception e) {
147  0 log.error("Error while reading index", e);
148    }
149    }
150    }
151   
152    /**
153    * Helper method to write out the index to disk.
154    */
 
155  170 toggle private void writeIndex() {
156  170 try {
157  170 File indexFile = new File(blog.getIndexesDirectory(), "authors.index");
158  170 BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile));
159   
160  170 for (String author : authors.keySet()) {
161  114 writer.write(author);
162  114 writer.write("=");
163  114 List<String> blogEntries = authors.get(author);
164  114 if (blogEntries != null) {
165  114 for (String blogEntry : blogEntries) {
166  128 writer.write(blogEntry);
167  128 writer.write(",");
168    }
169    }
170  114 writer.newLine();
171    }
172   
173  170 writer.flush();
174  170 writer.close();
175    } catch (Exception e) {
176  0 log.error("Error while writing index", e);
177    }
178    }
179   
 
180  114 toggle private synchronized List<String> getBlogEntries(String author) {
181  114 List<String> blogEntries = authors.get(author);
182  114 if (blogEntries == null) {
183  104 blogEntries = new LinkedList<String>();
184  104 authors.put(author, blogEntries);
185    }
186   
187  114 return blogEntries;
188    }
189   
190    /**
191    * Gets the list of authors associated with this blog.
192    */
 
193  8 toggle public List<String> getAuthors() {
194  8 return new LinkedList<String>(authors.keySet());
195    }
196   
197    /**
198    * Gets the blog entries for a given author.
199    *
200    * @param username a username (String)
201    * @return a List of blog entry IDs
202    */
 
203  12 toggle public List<String> getRecentBlogEntries(String username) {
204  12 List<String> blogEntries = authors.get(username);
205  12 if (blogEntries == null) {
206  4 return new LinkedList<String>();
207    } else {
208  8 return new LinkedList<String>(blogEntries);
209    }
210    }
211   
212    }