| 1 | package be.nikiroo.fanfix.library; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.net.URL; |
| 6 | import java.util.List; |
| 7 | |
| 8 | import be.nikiroo.fanfix.Instance; |
| 9 | import be.nikiroo.fanfix.bundles.UiConfig; |
| 10 | import be.nikiroo.fanfix.data.MetaData; |
| 11 | import be.nikiroo.fanfix.data.Story; |
| 12 | import be.nikiroo.utils.Image; |
| 13 | import be.nikiroo.utils.Progress; |
| 14 | |
| 15 | /** |
| 16 | * This library will cache another pre-existing {@link BasicLibrary}. |
| 17 | * |
| 18 | * @author niki |
| 19 | */ |
| 20 | public class CacheLibrary extends BasicLibrary { |
| 21 | private List<MetaData> metas; |
| 22 | private BasicLibrary lib; |
| 23 | private LocalLibrary cacheLib; |
| 24 | |
| 25 | /** |
| 26 | * Create a cache library around the given one. |
| 27 | * <p> |
| 28 | * It will return the same result, but those will be saved to disk at the |
| 29 | * same time to be fetched quicker the next time. |
| 30 | * |
| 31 | * @param cacheDir |
| 32 | * the cache directory where to save the files to disk |
| 33 | * @param lib |
| 34 | * the original library to wrap |
| 35 | */ |
| 36 | public CacheLibrary(File cacheDir, BasicLibrary lib) { |
| 37 | this.cacheLib = new LocalLibrary(cacheDir, Instance.getUiConfig() |
| 38 | .getString(UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE), Instance |
| 39 | .getUiConfig().getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE), |
| 40 | true); |
| 41 | this.lib = lib; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public String getLibraryName() { |
| 46 | return lib.getLibraryName(); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public Status getStatus() { |
| 51 | return lib.getStatus(); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | protected List<MetaData> getMetas(Progress pg) { |
| 56 | if (pg == null) { |
| 57 | pg = new Progress(); |
| 58 | } |
| 59 | |
| 60 | if (metas == null) { |
| 61 | metas = lib.getMetas(pg); |
| 62 | } |
| 63 | |
| 64 | pg.done(); |
| 65 | return metas; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public synchronized File getFile(final String luid, Progress pg) { |
| 70 | if (pg == null) { |
| 71 | pg = new Progress(); |
| 72 | } |
| 73 | |
| 74 | Progress pgImport = new Progress(); |
| 75 | Progress pgGet = new Progress(); |
| 76 | Progress pgRecall = new Progress(); |
| 77 | |
| 78 | pg.setMinMax(0, 5); |
| 79 | pg.addProgress(pgImport, 3); |
| 80 | pg.addProgress(pgGet, 1); |
| 81 | pg.addProgress(pgRecall, 1); |
| 82 | |
| 83 | if (!isCached(luid)) { |
| 84 | try { |
| 85 | cacheLib.imprt(lib, luid, pgImport); |
| 86 | updateInfo(cacheLib.getInfo(luid)); |
| 87 | pgImport.done(); |
| 88 | } catch (IOException e) { |
| 89 | Instance.getTraceHandler().error(e); |
| 90 | } |
| 91 | |
| 92 | pgImport.done(); |
| 93 | pgGet.done(); |
| 94 | } |
| 95 | |
| 96 | File file = cacheLib.getFile(luid, pgRecall); |
| 97 | pgRecall.done(); |
| 98 | |
| 99 | pg.done(); |
| 100 | return file; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Image getCover(final String luid) { |
| 105 | if (isCached(luid)) { |
| 106 | return cacheLib.getCover(luid); |
| 107 | } |
| 108 | |
| 109 | // We could update the cache here, but it's not easy |
| 110 | return lib.getCover(luid); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Image getSourceCover(String source) { |
| 115 | Image custom = getCustomSourceCover(source); |
| 116 | if (custom != null) { |
| 117 | return custom; |
| 118 | } |
| 119 | |
| 120 | return cacheLib.getSourceCover(source); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Image getCustomSourceCover(String source) { |
| 125 | Image custom = cacheLib.getCustomSourceCover(source); |
| 126 | if (custom == null) { |
| 127 | custom = lib.getCustomSourceCover(source); |
| 128 | if (custom != null) { |
| 129 | cacheLib.setSourceCover(source, custom); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return custom; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void setSourceCover(String source, String luid) { |
| 138 | lib.setSourceCover(source, luid); |
| 139 | cacheLib.setSourceCover(source, getCover(luid)); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | protected void updateInfo(MetaData meta) { |
| 144 | if (meta != null && metas != null) { |
| 145 | for (int i = 0; i < metas.size(); i++) { |
| 146 | if (metas.get(i).getLuid().equals(meta.getLuid())) { |
| 147 | metas.set(i, meta); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | cacheLib.updateInfo(meta); |
| 153 | lib.updateInfo(meta); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | protected void deleteInfo(String luid) { |
| 158 | if (luid == null) { |
| 159 | metas = null; |
| 160 | } else if (metas != null) { |
| 161 | for (int i = 0; i < metas.size(); i++) { |
| 162 | if (metas.get(i).getLuid().equals(luid)) { |
| 163 | metas.remove(i--); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | cacheLib.deleteInfo(luid); |
| 169 | lib.deleteInfo(luid); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public synchronized Story save(Story story, String luid, Progress pg) |
| 174 | throws IOException { |
| 175 | Progress pgLib = new Progress(); |
| 176 | Progress pgCacheLib = new Progress(); |
| 177 | |
| 178 | if (pg == null) { |
| 179 | pg = new Progress(); |
| 180 | } |
| 181 | |
| 182 | pg.setMinMax(0, 2); |
| 183 | pg.addProgress(pgLib, 1); |
| 184 | pg.addProgress(pgCacheLib, 1); |
| 185 | |
| 186 | story = lib.save(story, luid, pgLib); |
| 187 | story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib); |
| 188 | |
| 189 | updateInfo(story.getMeta()); |
| 190 | |
| 191 | return story; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public synchronized void delete(String luid) throws IOException { |
| 196 | if (isCached(luid)) { |
| 197 | cacheLib.delete(luid); |
| 198 | } |
| 199 | lib.delete(luid); |
| 200 | |
| 201 | MetaData meta = getInfo(luid); |
| 202 | if (meta != null) { |
| 203 | metas.remove(meta); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public synchronized void changeSource(String luid, String newSource, |
| 209 | Progress pg) throws IOException { |
| 210 | if (pg == null) { |
| 211 | pg = new Progress(); |
| 212 | } |
| 213 | |
| 214 | Progress pgCache = new Progress(); |
| 215 | Progress pgOrig = new Progress(); |
| 216 | pg.setMinMax(0, 2); |
| 217 | pg.addProgress(pgCache, 1); |
| 218 | pg.addProgress(pgOrig, 1); |
| 219 | |
| 220 | MetaData meta = getInfo(luid); |
| 221 | if (meta == null) { |
| 222 | throw new IOException("Story not found: " + luid); |
| 223 | } |
| 224 | |
| 225 | if (isCached(luid)) { |
| 226 | cacheLib.changeSource(luid, newSource, pgCache); |
| 227 | } |
| 228 | pgCache.done(); |
| 229 | |
| 230 | lib.changeSource(luid, newSource, pgOrig); |
| 231 | pgOrig.done(); |
| 232 | |
| 233 | meta.setSource(newSource); |
| 234 | pg.done(); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Check if the {@link Story} denoted by this Library UID is present in the |
| 239 | * cache. |
| 240 | * |
| 241 | * @param luid |
| 242 | * the Library UID |
| 243 | * |
| 244 | * @return TRUE if it is |
| 245 | */ |
| 246 | public boolean isCached(String luid) { |
| 247 | return cacheLib.getInfo(luid) != null; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Clear the {@link Story} from the cache. |
| 252 | * |
| 253 | * @param luid |
| 254 | * the story to clear |
| 255 | * |
| 256 | * @throws IOException |
| 257 | * in case of I/O error |
| 258 | */ |
| 259 | public void clearFromCache(String luid) throws IOException { |
| 260 | if (isCached(luid)) { |
| 261 | cacheLib.delete(luid); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public Story imprt(URL url, Progress pg) throws IOException { |
| 267 | if (pg == null) { |
| 268 | pg = new Progress(); |
| 269 | } |
| 270 | |
| 271 | Progress pgImprt = new Progress(); |
| 272 | Progress pgCache = new Progress(); |
| 273 | pg.setMinMax(0, 10); |
| 274 | pg.addProgress(pgImprt, 7); |
| 275 | pg.addProgress(pgCache, 3); |
| 276 | |
| 277 | Story story = lib.imprt(url, pgImprt); |
| 278 | cacheLib.save(story, story.getMeta().getLuid(), pgCache); |
| 279 | |
| 280 | updateInfo(story.getMeta()); |
| 281 | |
| 282 | pg.done(); |
| 283 | return story; |
| 284 | } |
| 285 | |
| 286 | // All the following methods are only used by Save and Delete in |
| 287 | // BasicLibrary: |
| 288 | |
| 289 | @Override |
| 290 | protected int getNextId() { |
| 291 | throw new java.lang.InternalError("Should not have been called"); |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | protected void doDelete(String luid) throws IOException { |
| 296 | throw new java.lang.InternalError("Should not have been called"); |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | protected Story doSave(Story story, Progress pg) throws IOException { |
| 301 | throw new java.lang.InternalError("Should not have been called"); |
| 302 | } |
| 303 | } |