Coverage Report - net.sourceforge.pebble.index.AuthorIndex
 
Classes in this File Line Coverage Branch Coverage Complexity
AuthorIndex
76%
59/77
60%
18/30
2.8
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, 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  4
   private static final Log log = LogFactory.getLog(AuthorIndex.class);
 51  
 
 52  
   private Blog blog;
 53  
 
 54  
   /** the map containing the tags */
 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  
    * Clears the index.
 65  
    */
 66  
   public void clear() {
 67  36
     authors = new HashMap<String,List<String>>();
 68  36
     writeIndex();
 69  36
   }
 70  
 
 71  
   /**
 72  
    * Indexes one or more blog entries.
 73  
    *
 74  
    * @param blogEntries   a List of BlogEntry instances
 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  
    * Indexes a single blog entry.
 90  
    *
 91  
    * @param blogEntry   a BlogEntry instance
 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  
    * Unindexes a single blog entry.
 105  
    *
 106  
    * @param blogEntry   a BlogEntry instance
 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  
    * Helper method to load the index.
 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  
    * Helper method to write out the index to disk.
 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  
    * Gets the list of authors associated with this blog.
 192  
    */
 193  
   public List<String> getAuthors() {
 194  16
     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  
   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  
 }