1 25 26 package org.snipsnap.snip.storage; 27 28 import org.snipsnap.snip.Snip; 29 import org.snipsnap.util.PartialSearcher; 30 import org.snipsnap.util.ApplicationAwareMap; 31 import org.snipsnap.app.Application; 32 import org.snipsnap.app.ApplicationManager; 33 import org.snipsnap.app.ApplicationStorage; 34 35 import java.sql.Timestamp ; 36 import java.util.*; 37 38 47 48 public class MemorySnipStorage implements SnipStorage { 49 public static final String NOT_SUPPORTED_EXCEPTION_MSG = 50 "Method not supported, do not call MemorySnipStorage directly"; 51 52 private SnipStorage storage; 53 54 private ApplicationAwareMap cache; 55 56 private Map allList; 60 61 public MemorySnipStorage(SnipStorage storage, ApplicationManager manager) { 62 this.storage = storage; 63 64 73 cache = new ApplicationAwareMap(HashMap.class, PartialSearcher.class); 74 75 if (storage instanceof CacheableStorage) { 76 ((CacheableStorage) storage).setCache(cache); 77 } 78 79 allList = new HashMap(); 82 83 Iterator iterator = manager.getApplications().iterator(); 85 while (iterator.hasNext()) { 86 String oid = ((Properties)iterator.next()).getProperty(ApplicationStorage.OID); 87 List instanceList = storage.storageAll(oid); 88 allList.put(oid, instanceList); 89 } 90 } 91 92 public Snip[] match(String pattern) { 94 return ((PartialSearcher) cache.getMap()).match(pattern.toUpperCase()); 95 } 96 97 public Snip[] match(String start, String end) { 98 return ((PartialSearcher) cache.getMap()).match(start.toUpperCase(), end.toUpperCase()); 99 } 100 101 public Snip storageLoad(String name) { 102 return (Snip) cache.getMap().get(name.toUpperCase()); 103 } 104 105 public Object loadObject(String name) { 106 return (Snip) cache.getMap().get(name.toUpperCase()); 107 } 108 109 public void storageStore(Snip snip) { 110 storage.storageStore(snip); 111 } 112 113 public Snip storageCreate(String name, String content) { 114 Snip snip = storage.storageCreate(name, content); 115 116 String applicationOid = snip.getApplication(); 118 119 List allSnips = (List) allList.get(applicationOid); 120 if(null == allSnips) { 121 allSnips = storage.storageAll(applicationOid); 122 allList.put(applicationOid, allSnips); 123 } 124 allSnips.add(snip); 125 cache.getMap(applicationOid).put(snip.getName().toUpperCase(), snip); 126 return snip; 127 } 128 129 public void storageRemove(Snip snip) { 130 String applicationOid = snip.getApplication(); 131 storage.storageRemove(snip); 132 List allSnips = (List) allList.get(applicationOid); 133 if (null != allSnips) { 134 allSnips.remove(snip); 135 } else { 136 System.err.println("WARNING: access to unknown oid: "+snip.getApplication()); 137 } 138 cache.getMap(applicationOid).remove(snip.getName().toUpperCase()); 139 } 140 141 public int storageCount() { 142 String application = (String ) Application.get().getObject(Application.OID); 143 List allSnips = (List) allList.get(application); 144 return allSnips != null ? allSnips.size() : 0; 145 } 146 147 public List storageAll(String applicationOid) { 148 List all = (List) allList.get(applicationOid); 149 return all; 150 } 151 152 public List storageAll() { 153 String applicationOid = (String ) Application.get().getObject(Application.OID); 154 return storageAll(applicationOid); 155 } 156 157 public class MethodNotSupportedException extends RuntimeException { 163 public MethodNotSupportedException(String s) { 164 super(s); 165 } 166 }; 167 168 public List storageByHotness(int size) { 169 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 170 } 171 172 public List storageByUser(String login) { 173 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 174 } 175 176 public List storageByDateSince(Timestamp date) { 177 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 178 } 179 180 public List storageByRecent(String applicationOid, int size) { 181 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 182 } 183 184 public List storageByComments(Snip parent) { 185 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 186 } 187 188 public List storageByParent(Snip parent) { 189 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 190 } 191 192 public List storageByParentNameOrder(Snip parent, int count) { 193 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 194 } 195 196 public List storageByParentModifiedOrder(Snip parent, int count) { 197 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 198 } 199 200 public List storageByDateInName(String nameSpace, String start, String end) { 201 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 202 } 203 } 204 | Popular Tags |