📄 storagequery.java
字号:
*/ public BaseEntry singleEntryQuery(final String entryId, final String feedId, final ProvidedService config) throws IOException, ParseException { StorageEntryWrapper wrapper = this.buffer.getEntry(entryId, feedId); if (wrapper == null) { Hits hits = storageQuery(entryId); if (hits.length() <= 0) return null; Document doc = hits.doc(0); return buildEntryFromLuceneDocument(doc, config); } /* * ServerBaseEntry enables the dynamic element of the entry like the * links to be dynamic. BufferedEntries will be reused until they are * written. */ return wrapper.getEntry(); } /** * Fetches the requested entries from the storage. The given list contains * entry id's to be looked up in the storage. First the {@link StorageBuffer} * will be queried for the entry id's. If not all of the entries remain in * the buffer the underlying lucene index will be searched. The entries are * not guaranteed to be in the same order as they are in the given id list. * Entry ID's not found in the index or the buffer will be omitted. * <p> * The entries will be searched in a feed context specified by the given * feed ID * </p> * * @param entryIds - * the entry id's to fetch. * @param feedId - * the feed id e.g. feed context. * @param config - * the FeedInstanceConfiguration containing extension profile used * to create the entry instances * * @return - the list of entries corresponding to the given entry id list. * @throws IOException - * if the index could not be queries or the entries could not be * build * @throws ParseException - * if an entry could not be parsed while building it from the * Lucene Document. */ public List<BaseEntry> entryQuery(List<String> entryIds, final String feedId, final ProvidedService config) throws IOException, ParseException { List<BaseEntry> resultList = new ArrayList<BaseEntry>(entryIds.size()); List<String> searchList = new ArrayList<String>(entryIds.size()); for (String entry : entryIds) { StorageEntryWrapper bufferedEntry = this.buffer.getEntry(entry, feedId); if (bufferedEntry != null) { resultList.add(bufferedEntry.getEntry()); } else searchList.add(entry); } if (searchList.isEmpty()) return resultList; Hits hits = storageQuery(searchList); Iterator hitIterator = hits.iterator(); while (hitIterator.hasNext()) { Hit hit = (Hit) hitIterator.next(); Document doc = hit.getDocument(); BaseEntry entry = buildEntryFromLuceneDocument(doc, config); resultList.add(entry); } return resultList; } private BaseEntry buildEntryFromLuceneDocument(final Document doc, final ProvidedService config) throws ParseException, IOException { Reader reader = new StringReader(doc.getField(StorageEntryWrapper.FIELD_CONTENT).stringValue()); BaseEntry entry = GDataEntityBuilder.buildEntry(reader, config); entry.setVersionId(doc.getField(StorageEntryWrapper.FIELD_VERSION).stringValue()); return entry; } private BaseFeed buildFeedFromLuceneDocument(final Document doc, final ProvidedService config) throws ParseException, IOException { Reader reader = new StringReader(doc.getField(StorageFeedWrapper.FIELD_CONTENT).stringValue()); return GDataEntityBuilder.buildFeed(reader, config); } /** * Queries the storage for an user instance * * @param username - * the username (primary key) * @return - the user instance if found or <code>null</code> if not exists * @throws IOException - * if the storage can not be accessed. */ public GDataAccount getUser(final String username) throws IOException { if (username == null) return null; TermQuery query = new TermQuery(new Term( StorageAccountWrapper.FIELD_ACCOUNTNAME, username)); Hits h = this.searcher.search(query); if (h.length() == 0) return null; return StorageAccountWrapper.buildEntity(h.doc(0)); } /** * Closes all resources used in the {@link StorageQuery}. The instance can * not be reused after invoking this method. * * @throws IOException - * if the resources can not be closed */ public void close() throws IOException { this.searcher.close(); this.buffer.close(); } /** * Checks whether a feed for the given feedID is stored * * @param feedId - * the feed ID * @return <code>true</code> if and only if a feed is stored for the * provided feed ID, <code>false</code> if no feed for the given * id is stored * @throws IOException */ public boolean isFeedStored(String feedId) throws IOException { Hits h = storageFeedQuery(feedId); return (h.length() > 0); } /** * Looks up the feed type for the given feed ID * * @param feedID - * the feed ID * @return - the feed type * @throws IOException - * if the storage can not be accessed */ public String getService(String feedID) throws IOException { Hits hits = storageFeedQuery(feedID); if (hits.length() <= 0) return null; Document doc = hits.doc(0); String feedType = doc.get(StorageFeedWrapper.FIELD_SERVICE_ID); return feedType; } private Hits storageFeedQuery(String feedId) throws IOException { TermQuery query = new TermQuery(new Term( StorageFeedWrapper.FIELD_FEED_ID, feedId)); return this.searcher.search(query); } /** * Looks up the account reference for the given feed id * * @param feedId - * id of the feed * @return - the name of the account associated with the feed for the given * feed id, or <code>null</code> if the feed is not stored * @throws IOException - * if the storage can not be accessed */ public String getAccountNameForFeedId(String feedId) throws IOException { Hits h = storageFeedQuery(feedId); if (h.length() == 0) return null; Document doc = h.doc(0); return doc.get(StorageFeedWrapper.FIELD_ACCOUNTREFERENCE); } protected long getEntryLastModified(final String entryId, final String feedId) throws IOException, StorageException { StorageEntryWrapper wrapper = this.buffer.getEntry(entryId, feedId); if (wrapper != null) return wrapper.getTimestamp(); Hits h = storageQuery(entryId); if (h.length() > 0) try { return Long.parseLong(h.doc(0).get( StorageEntryWrapper.FIELD_TIMESTAMP)); } catch (Exception e) { LOG.warn("Can not parse timestamp from entry -- " + h.doc(0).get(StorageEntryWrapper.FIELD_TIMESTAMP)); } else throw new StorageException("Entry not found"); return 0; } protected long getFeedLastModified(final String feedId) throws IOException { Long bufferedTime = this.buffer.getFeedLastModified(feedId); if (bufferedTime != null) return bufferedTime; Hits entryHits = storageFeedQuery(feedId, this.timeStampSort); if (entryHits.length() > 0) { try { return Long.parseLong(entryHits.doc(0).getField( StorageEntryWrapper.FIELD_TIMESTAMP).stringValue()); } catch (Exception e) { LOG.warn("Can not parse timestamp from entry -- " + entryHits.doc(0).get( StorageEntryWrapper.FIELD_TIMESTAMP)); } } return 0; } protected boolean isEntryStored(String entryId, String feedId) throws IOException { if (LOG.isDebugEnabled()) LOG.debug("Checking isEntryStored for entryid " + entryId + " feedid: " + feedId); if (this.buffer.getEntry(entryId, feedId) != null) return true; Hits h = storageQuery(entryId); if (h.length() > 0) return true; return false; } protected boolean checkEntryVersion(String id, String feedId, int version) throws IOException { if (LOG.isDebugEnabled()) LOG.debug("Checking entry version for entryid " + id + " feedid: " + feedId + " version: " + version); StorageEntryWrapper wrapper = this.buffer.getEntry(id, feedId); if (wrapper != null) return wrapper.getVersion() == version; Hits h = storageQuery(id); if (h.length() < 1) return false; Document doc = h.doc(0); String fieldValue = null; try { fieldValue = doc.getField(StorageEntryWrapper.FIELD_VERSION) .stringValue(); int storedVersion = Integer.parseInt(fieldValue); if(LOG.isDebugEnabled()) LOG.debug("StoredEntry has version "+storedVersion +" return compare result"); return storedVersion == version; } catch (Exception e) { LOG.error("Entry has no parable Version id or field is not set -- " + fieldValue); } return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -