/** * Since skip becomes impossible to calculate in a cached scenario, this * will simply query the underlying reader. */ @Override public List<DatabaseDocument<T>> getDocuments(DatabaseQuery<T> q, int limit, int skip) { List<DatabaseDocument<T>> docs = reader.getDocuments(q, limit, skip); cache.add(docs); return docs; }
/** * If there is any documents in the cache matching the query, those are * returned, otherwise a direct call is made to the underlying reader. * * As such, this may return 1 document (if one is found in the cache) even * though there are thousands of documents in the underlying database that * match. */ @Override public List<DatabaseDocument<T>> getDocuments(DatabaseQuery<T> q, int limit) { List<DatabaseDocument<T>> docs = new ArrayList<DatabaseDocument<T>>(); docs.addAll(cache.getDocument(q, limit)); if (docs.size() == 0) { docs = reader.getDocuments(q, limit); cache.add(docs); } return docs; }
@Override public DatabaseDocument<T> getDocument(DatabaseQuery<T> q) { DatabaseDocument<T> doc = cache.getDocument(q); if (doc == null) { doc = reader.getDocument(q); cache.add(doc); } return getCopy(doc); }
@Override public DatabaseDocument<T> getDocumentById(DocumentID<T> id) { DatabaseDocument<T> doc = cache.getDocumentById(id); if (doc == null) { doc = reader.getDocumentById(id, false); if(doc != null) { cache.add(doc); } else { return null; } } return getCopy(doc); }
@Override public Collection<DatabaseDocument<T>> getAndTag(DatabaseQuery<T> query, int n, String... tags) { Collection<DatabaseDocument<T>> list = new ArrayList<DatabaseDocument<T>>(); Collection<DatabaseDocument<T>> c = cache.getAndTag(query, n, tags); if (c != null) { list.addAll(c); } if (list.size() == 0) { for (String t : tags) { query.requireNotFetchedByStage(t); } list = writer.getAndTag(query, n, addCacheTag(tags)); for (DatabaseDocument<T> d : list) { for (String t : tags) { d.setFetchedBy(t, new Date()); } } cache.add(list); } return list; }
@Override public DatabaseDocument<T> getAndTag(DatabaseQuery<T> query, String... tags) { DatabaseDocument<T> doc = cache.getAndTag(query, tags); if (doc == null) { for (String t : tags) { query.requireNotFetchedByStage(t); } doc = writer.getAndTag(query, addCacheTag(tags)); if (doc != null) { for (String t : tags) { doc.setFetchedBy(t, new Date()); } cache.add(doc); } } return getCopy(doc); }
@Override public boolean update(DatabaseDocument<T> d) { if (!cache.update(d)) { DatabaseDocument<T> doc = reader.getDocumentById(d.getID()); if (doc != null) { cache.add(doc); cache.update(d); } if (cache.getDocumentById(d.getID()) == null) { d.removeFetchedBy(CACHE_TAG); } return writer.update(d); } return true; }