Coverage Report - net.sourceforge.pebble.index.BlogEntryIndex
 
Classes in this File Line Coverage Branch Coverage Complexity
BlogEntryIndex
75%
75/99
72%
16/22
2
 
 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  
 
 33  
 package net.sourceforge.pebble.index;
 34  
 
 35  
 import net.sourceforge.pebble.comparator.ReverseBlogEntryIdComparator;
 36  
 import net.sourceforge.pebble.domain.Blog;
 37  
 import net.sourceforge.pebble.domain.BlogEntry;
 38  
 import net.sourceforge.pebble.domain.Day;
 39  
 import org.apache.commons.logging.Log;
 40  
 import org.apache.commons.logging.LogFactory;
 41  
 
 42  
 import java.io.*;
 43  
 import java.util.*;
 44  
 
 45  
 /**
 46  
  * Keeps an index of all blog entries, allowing efficient access at runtime.
 47  
  *
 48  
  * @author    Simon Brown
 49  
  */
 50  
 public class BlogEntryIndex {
 51  
 
 52  4
   private static final Log log = LogFactory.getLog(BlogEntryIndex.class);
 53  
 
 54  
   private Blog blog;
 55  
 
 56  2720
   private List<String> indexEntries = new ArrayList<String>();
 57  2720
   private List<String> publishedIndexEntries = new ArrayList<String>();
 58  2720
   private List<String> unpublishedIndexEntries = new ArrayList<String>();
 59  
 
 60  2720
   public BlogEntryIndex(Blog blog) {
 61  2720
     this.blog = blog;
 62  
 
 63  2720
     readIndex(true);
 64  2720
     readIndex(false);
 65  2720
   }
 66  
 
 67  
   /**
 68  
    * Clears the index.
 69  
    */
 70  
   public void clear() {
 71  36
     indexEntries = new ArrayList<String>();
 72  36
     publishedIndexEntries = new ArrayList<String>();
 73  36
     unpublishedIndexEntries = new ArrayList<String>();
 74  36
     writeIndex(true);
 75  36
     writeIndex(false);
 76  36
   }
 77  
 
 78  
   /**
 79  
    * Indexes one or more blog entries.
 80  
    *
 81  
    * @param blogEntries   a List of BlogEntry instances
 82  
    */
 83  
   public synchronized void index(Collection<BlogEntry> blogEntries) {
 84  36
     for (BlogEntry blogEntry : blogEntries) {
 85  36
       Day day = blog.getBlogForDay(blogEntry.getDate());
 86  36
       if (blogEntry.isPublished()) {
 87  36
         publishedIndexEntries.add(blogEntry.getId());
 88  36
         day.addPublishedBlogEntry(blogEntry.getId());
 89  
       } else {
 90  0
         unpublishedIndexEntries.add(blogEntry.getId());
 91  0
         day.addUnpublishedBlogEntry(blogEntry.getId());
 92  
       }
 93  36
       indexEntries.add(blogEntry.getId());
 94  36
     }
 95  
 
 96  36
     Collections.sort(indexEntries, new ReverseBlogEntryIdComparator());
 97  36
     Collections.sort(publishedIndexEntries, new ReverseBlogEntryIdComparator());
 98  36
     Collections.sort(unpublishedIndexEntries, new ReverseBlogEntryIdComparator());
 99  
 
 100  36
     writeIndex(true);
 101  36
     writeIndex(false);
 102  36
   }
 103  
 
 104  
   /**
 105  
    * Indexes a single blog entry.
 106  
    *
 107  
    * @param blogEntry   a BlogEntry instance
 108  
    */
 109  
   public synchronized void index(BlogEntry blogEntry) {
 110  628
     Day day = blog.getBlogForDay(blogEntry.getDate());
 111  628
     if (blogEntry.isPublished()) {
 112  184
       publishedIndexEntries.add(blogEntry.getId());
 113  184
       day.addPublishedBlogEntry(blogEntry.getId());
 114  184
       writeIndex(true);
 115  
     } else {
 116  444
       unpublishedIndexEntries.add(blogEntry.getId());
 117  444
       day.addUnpublishedBlogEntry(blogEntry.getId());
 118  444
       writeIndex(false);
 119  
     }
 120  628
     indexEntries.add(blogEntry.getId());
 121  
 
 122  628
     Collections.sort(indexEntries, new ReverseBlogEntryIdComparator());
 123  628
     Collections.sort(publishedIndexEntries, new ReverseBlogEntryIdComparator());
 124  628
     Collections.sort(unpublishedIndexEntries, new ReverseBlogEntryIdComparator());
 125  628
   }
 126  
 
 127  
   /**
 128  
    * Unindexes a single blog entry.
 129  
    *
 130  
    * @param blogEntry   a BlogEntry instance
 131  
    */
 132  
   public synchronized void unindex(BlogEntry blogEntry) {
 133  72
     Day day = blog.getBlogForDay(blogEntry.getDate());
 134  72
     day.removeBlogEntry(blogEntry);
 135  
 
 136  72
     indexEntries.remove(blogEntry.getId());
 137  72
     publishedIndexEntries.remove(blogEntry.getId());
 138  72
     unpublishedIndexEntries.remove(blogEntry.getId());
 139  
 
 140  72
     writeIndex(true);
 141  72
     writeIndex(false);
 142  72
   }
 143  
 
 144  
   /**
 145  
    * Helper method to load the index.
 146  
    */
 147  
   private void readIndex(boolean published) {
 148  
     File indexFile;
 149  5440
     if (published) {
 150  2720
       indexFile = new File(blog.getIndexesDirectory(), "blogentries-published.index");
 151  
     } else {
 152  2720
       indexFile = new File(blog.getIndexesDirectory(), "blogentries-unpublished.index");
 153  
     }
 154  
 
 155  5440
     if (indexFile.exists()) {
 156  
       try {
 157  0
         BufferedReader reader = new BufferedReader(new FileReader(indexFile));
 158  0
         String indexEntry = reader.readLine();
 159  0
         while (indexEntry != null) {
 160  0
           indexEntries.add(indexEntry);
 161  
 
 162  
           // and add it to the internal memory structures
 163  0
           Date date = new Date(Long.parseLong(indexEntry));
 164  0
           Day day = blog.getBlogForDay(date);
 165  
 
 166  0
           if (published) {
 167  0
             publishedIndexEntries.add(indexEntry);
 168  0
             day.addPublishedBlogEntry(indexEntry);
 169  
           } else {
 170  0
             unpublishedIndexEntries.add(indexEntry);
 171  0
             day.addUnpublishedBlogEntry(indexEntry);
 172  
           }
 173  
 
 174  0
           indexEntry = reader.readLine();
 175  0
         }
 176  
 
 177  0
         reader.close();
 178  0
       } catch (Exception e) {
 179  0
         log.error("Error while reading index", e);
 180  0
       }
 181  
     }
 182  
 
 183  5440
     Collections.sort(indexEntries, new ReverseBlogEntryIdComparator());
 184  5440
     Collections.sort(publishedIndexEntries, new ReverseBlogEntryIdComparator());
 185  5440
     Collections.sort(unpublishedIndexEntries, new ReverseBlogEntryIdComparator());
 186  5440
   }
 187  
 
 188  
   /**
 189  
    * Helper method to write out the index to disk.
 190  
    */
 191  
   private void writeIndex(boolean published) {
 192  
     try {
 193  
       File indexFile;
 194  916
       if (published) {
 195  328
         indexFile = new File(blog.getIndexesDirectory(), "blogentries-published.index");
 196  
       } else {
 197  588
         indexFile = new File(blog.getIndexesDirectory(), "blogentries-unpublished.index");
 198  
       }
 199  916
       BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile));
 200  
 
 201  916
       if (published) {
 202  328
         for (String indexEntry : publishedIndexEntries) {
 203  248
           writer.write(indexEntry);
 204  248
           writer.newLine();
 205  
         }
 206  
       } else {
 207  588
         for (String indexEntry : unpublishedIndexEntries) {
 208  772
           writer.write(indexEntry);
 209  772
           writer.newLine();
 210  
         }
 211  
       }
 212  
 
 213  916
       writer.flush();
 214  916
       writer.close();
 215  0
     } catch (Exception e) {
 216  0
       log.error("Error while writing index", e);
 217  916
     }
 218  916
   }
 219  
 
 220  
   /**
 221  
    * Gets the number of blog entries for this blog.
 222  
    *
 223  
    * @return  an int
 224  
    */
 225  
   public int getNumberOfBlogEntries() {
 226  32
     return indexEntries.size();
 227  
   }
 228  
 
 229  
   /**
 230  
    * Gets the number of published blog entries for this blog.
 231  
    *
 232  
    * @return  an int
 233  
    */
 234  
   public int getNumberOfPublishedBlogEntries() {
 235  0
     return publishedIndexEntries.size();
 236  
   }
 237  
 
 238  
   /**
 239  
    * Gets the number of unpublished blog entries for this blog.
 240  
    *
 241  
    * @return  an int
 242  
    */
 243  
   public int getNumberOfUnpublishedBlogEntries() {
 244  0
     return unpublishedIndexEntries.size();
 245  
   }
 246  
 
 247  
   /**
 248  
    * Gets the full list of blog entries.
 249  
    *
 250  
    * @return  a List of blog entry IDs
 251  
    */
 252  
   public List<String> getBlogEntries() {
 253  88
     return new ArrayList<String>(indexEntries);
 254  
   }
 255  
 
 256  
   /**
 257  
    * Gets the full list of published blog entries.
 258  
    *
 259  
    * @return  a List of blog entry IDs
 260  
    */
 261  
   public List<String> getPublishedBlogEntries() {
 262  84
     return new ArrayList<String>(publishedIndexEntries);
 263  
   }
 264  
 
 265  
   /**
 266  
    * Gets the full list of unpublished blog entries.
 267  
    *
 268  
    * @return  a List of blog entry IDs
 269  
    */
 270  
   public List<String> getUnpublishedBlogEntries() {
 271  0
     return new ArrayList<String>(unpublishedIndexEntries);
 272  
   }
 273  
 
 274  
 }