1 25 26 package org.snipsnap.snip; 27 28 import org.apache.lucene.search.Hits; 29 import org.codehaus.nanning.Aspects; 30 import org.radeox.util.logging.Logger; 31 import org.snipsnap.app.Application; 32 import org.snipsnap.app.ApplicationManager; 33 import org.snipsnap.app.ApplicationStorage; 34 import org.snipsnap.container.Components; 35 import org.snipsnap.notification.Message; 36 import org.snipsnap.notification.MessageService; 37 import org.snipsnap.snip.storage.CacheSnipStorage; 38 import org.snipsnap.snip.storage.CacheStorage; 39 import org.snipsnap.snip.storage.CacheableStorage; 40 import org.snipsnap.snip.storage.MemorySnipStorage; 41 import org.snipsnap.snip.storage.QuerySnipStorage; 42 import org.snipsnap.snip.storage.SnipStorage; 43 import org.snipsnap.user.Digest; 44 import org.snipsnap.util.ApplicationAwareMap; 45 import org.snipsnap.util.Queue; 46 import org.snipsnap.util.mail.PostDaemon; 47 import org.snipsnap.versioning.VersionManager; 48 49 import java.io.IOException ; 50 import java.sql.Timestamp ; 51 import java.util.ArrayList ; 52 import java.util.HashMap ; 53 import java.util.Iterator ; 54 import java.util.List ; 55 import java.util.ListIterator ; 56 import java.util.Map ; 57 import java.util.Timer ; 58 import java.util.TimerTask ; 59 60 71 72 public class SnipSpaceImpl implements SnipSpace { 73 private ApplicationAwareMap changed; 75 private List delayed; 76 77 private SnipIndexer indexer; 79 private Timer timer; 80 private String eTag; 81 private SnipStorage storage; 82 private VersionManager versionManager; 83 84 private ApplicationAwareMap blogs; 85 86 public SnipSpaceImpl(SnipStorage storage, 87 ApplicationManager manager, 88 VersionManager versionManager 89 ) { 90 this.storage = storage; 91 this.versionManager = versionManager; 92 93 changed = new ApplicationAwareMap(HashMap .class, Queue.class); 94 blogs = new ApplicationAwareMap(HashMap .class, HashMap .class); 95 96 if ("full".equals(Application.get().getConfiguration().getCache())) { 99 Logger.debug("Cache strategy is: keep full, using MemorySnipStorage and QuerySnipStorage"); 100 this.storage = new QuerySnipStorage(new MemorySnipStorage(storage, manager)); 104 } else if ("cache".equals(Application.get().getConfiguration().getCache()) 105 && storage instanceof CacheableStorage) { 106 Logger.debug("Cache strategy is: cache, using CacheSnipStorage"); 107 CacheableStorage old = (CacheableStorage) storage; 110 this.storage = new CacheSnipStorage(storage); 111 old.setCache(((CacheStorage) storage).getCache()); 114 } 115 116 indexer = new SnipIndexer(); 117 118 Iterator iterator = manager.getApplications().iterator(); 122 while (iterator.hasNext()) { 124 Map app = (Map) iterator.next(); 125 String applicationOid = (String ) app.get(ApplicationStorage.OID); 126 ((Queue) changed.getObject(applicationOid)).fill(this.storage.storageByRecent(applicationOid, 50)); 127 } 128 129 delayed = new ArrayList (); 132 133 setETag(); 134 timer = new Timer (); 135 timer.schedule(new TimerTask () { 136 137 public void run() { 138 List toStoreList = delayed; 139 List temp = new ArrayList (); 143 delayed = temp; 144 145 ListIterator iterator = toStoreList.listIterator(); 146 while (iterator.hasNext()) { 147 Snip snip = (Snip) iterator.next(); 148 Application.get().storeObject(Application.OID, snip.getApplication()); 150 systemStore(snip); 151 iterator.remove(); 152 } 153 } 154 }, 5 * 60 * 1000, 5 * 60 * 1000); 157 158 PostDaemon.getInstance(); 160 } 161 162 public void init() { 163 } 164 165 public String getETag() { 166 return "\"" + eTag + "\""; 167 } 168 169 public Blog getBlog() { 170 return getBlog(Application.get().getConfiguration().getStartSnip()); 171 } 172 173 public Blog getBlog(String name) { 175 Blog blog; 176 if (blogs.getMap().containsKey(name)) { 178 blog = (Blog) blogs.getMap().get(name); 179 } else { 180 blog = (Blog) org.snipsnap.interceptor.Aspects.newInstance( 183 new BlogImpl((SnipSpace) Aspects.getThis(), name), 184 Blog.class); 185 blogs.getMap().put(name, blog); 186 } 187 return blog; 188 } 189 190 public void changed(Snip snip) { 192 changed.getQueue().add(snip); 193 setETag(); 194 } 195 196 public void setETag() { 197 eTag = Digest.getDigest(new java.util.Date ().toString()); 198 } 199 200 public int getSnipCount() { 201 return storage.storageCount(); 202 } 203 204 public List getChanged() { 205 return getChanged(15); 206 } 207 208 public List getChanged(int count) { 209 return changed.getQueue().get(count); 210 } 211 212 public List getAll() { 213 return storage.storageAll(); 214 } 215 216 public List getSince(Timestamp date) { 217 return storage.storageByDateSince(date); 218 } 219 220 public List getByDate(String nameSpace, String start, String end) { 221 return storage.storageByDateInName(nameSpace, start, end); 222 } 223 224 231 public List getHot(int count) { 232 return storage.storageByHotness(count); 233 } 234 235 public List getComments(Snip snip) { 236 return storage.storageByComments(snip); 237 } 238 239 public List getByUser(String login) { 240 return storage.storageByUser(login); 241 } 242 243 public List getChildren(Snip snip) { 244 return storage.storageByParent(snip); 245 } 246 247 public List getChildrenDateOrder(Snip snip, int count) { 248 return storage.storageByParentNameOrder(snip, count); 249 } 250 251 public List getChildrenModifiedOrder(Snip snip, int count) { 252 return storage.storageByParentModifiedOrder(snip, count); 253 } 254 255 public void reIndex() { 256 try { 257 indexer.deleteIndex(); 258 List snips = getAll(); 259 Iterator iterator = snips.iterator(); 260 while (iterator.hasNext()) { 261 Snip snip = (Snip) iterator.next(); 262 indexer.reIndex(snip); 263 } 264 } catch (IOException e) { 265 Logger.fatal("unable to re-index SnipSpace: ", e); 266 e.printStackTrace(); 267 } 268 } 269 270 public Hits search(String queryString) { 271 return indexer.search(queryString); 272 } 273 274 public String getContent(String title, String content) { 275 return content = "1 " + title + " {anchor:" + title + "}\n" + content; 276 } 277 278 public boolean exists(String name) { 279 if (null == load(name)) { 280 return false; 281 } else { 282 return true; 283 } 284 } 285 286 public Snip[] match(String pattern) { 287 return storage.match(pattern); 288 } 289 290 public Snip[] match(String start, String end) { 291 return storage.match(start, end); 292 } 293 294 public Snip load(String name) { 295 return storage.storageLoad(name); 296 } 297 298 public void store(Snip snip) { 299 Application app = Application.get(); 300 changed(snip); 301 snip.setMUser(app.getUser()); 302 snip.setMTime(new Timestamp (new java.util.Date ().getTime())); 303 synchronized (snip) { 304 snip.setVersion(snip.getVersion() + 1); 305 } 306 versionManager.storeVersion(snip); 307 systemStore(snip); 308 MessageService service = (MessageService) Components.getComponent(MessageService.class); 309 if (null != service) { 310 service.send(new Message(Message.SNIP_MODIFIED, snip)); 311 } 312 return; 313 } 314 315 322 public void systemStore(Snip snip) { 323 Application app = Application.get(); 325 long start = app.start(); 326 storage.storageStore(snip); 327 indexer.reIndex(snip); 328 app.stop(start, "systemStore - " + snip.getName()); 329 return; 330 } 331 332 333 342 public void delayedStore(Snip snip) { 343 Logger.debug("delayedStore"); 345 synchronized (delayed) { 346 if (!delayed.contains(snip)) { 347 delayed.add(snip); 348 } 349 } 350 } 351 352 public Snip create(String name, String content) { 353 name = name.trim(); 354 Snip snip = storage.storageCreate(name, content); 355 versionManager.storeVersion(snip); 356 changed(snip); 357 indexer.index(snip); 358 MessageService service = (MessageService) Components.getComponent(MessageService.class); 359 if (null != service) { 360 service.send(new Message(Message.SNIP_CREATE, snip)); 361 } 362 return snip; 363 } 364 365 public void remove(Snip snip) { 366 synchronized (delayed) { 367 delayed.remove(snip); 368 } 369 changed.getQueue().remove(snip); 370 storage.storageRemove(snip); 371 indexer.removeIndex(snip); 372 return; 373 } 374 } | Popular Tags |