📄 db4ostorage.java
字号:
} catch (Exception e) { LOG .error("Error occured on persisting changes -- rollback changes"); this.container.rollback(); throw new StorageException("Can not persist changes -- " + e.getMessage(), e); } } /** * @see org.apache.lucene.gdata.storage.Storage#updateFeed(org.apache.lucene.gdata.data.ServerBaseFeed, * java.lang.String) */ @SuppressWarnings("unchecked") public void updateFeed(ServerBaseFeed feed, String accountname) throws StorageException { if (feed == null) throw new StorageException("Can not update feed -- is null"); if (feed.getId() == null) throw new StorageException("Can not update feed -- id is null"); if(feed.getServiceType() == null) throw new StorageException("Can not update feed -- service type is null"); if(accountname == null) throw new StorageException("Account name is null"); GDataAccount account = getAccount(accountname); Query query = this.container.query(); query.constrain(ServerBaseFeed.class); query.descend("feed").descend("id").constrain(feed.getId()); ObjectSet<ServerBaseFeed> set= query.execute(); if (set.size() > 1) throw new StorageException("Query for feed id " + feed.getId() + " returns more than one result"); if (set.size() < 1) throw new StorageException("can not find feed for given feed id -- " + feed.getId()); ServerBaseFeed result = set.next(); refreshPersistentObject(result); BaseFeed oldFeed = result.getFeed(); result.setAccount(account); result.setFeed(feed.getFeed()); try { this.container.delete(oldFeed); this.container.set(result); this.container.commit(); } catch (Exception e) { LOG .error("Error occurred on persisting changes -- rollback changes"); this.container.rollback(); throw new StorageException("Can not persist changes -- " + e.getMessage(), e); } } /** * @see org.apache.lucene.gdata.storage.Storage#getServiceForFeed(java.lang.String) */ @SuppressWarnings("unchecked") public String getServiceForFeed(final String feedId) throws StorageException { if(feedId == null) throw new StorageException("can not get Service for feed -- feed id is null"); if(LOG.isInfoEnabled()) LOG.info("Retrieving Service for feed -- feed id: "+feedId); Query query = this.container.query(); query.constrain(ServerBaseFeed.class); query.descend("feed").descend("id").constrain(feedId); ObjectSet<ServerBaseFeed> feed = query.execute(); if (feed.size() > 1) throw new StorageException("Query for feed id " + feedId + " returns more than one result"); if (feed.size() < 1) throw new StorageException("can not find feed for given feed id -- " + feedId); ServerBaseFeed result = feed.next(); if(LOG.isInfoEnabled()) LOG.info("Retrieved Service for feed -- serviceType: "+result.getServiceType()); return result.getServiceType(); } /** * @see org.apache.lucene.gdata.storage.Storage#getAccount(java.lang.String) */ public GDataAccount getAccount(String accountName) throws StorageException { if (accountName == null) throw new StorageException( "Can not get account -- account name is null"); if (LOG.isInfoEnabled()) LOG.info("Retrieving account for account name: " + accountName); Query query = this.container.query(); query.constrain(GDataAccount.class); query.descend("name").constrain(accountName).equal(); ObjectSet set = query.execute(); if (set.size() > 1) throw new StorageException( "Account query returned not a unique result -- account name: " + accountName); if (!set.hasNext()) throw new ResourceNotFoundException( "No such account stored -- query returned not result for account name: " + accountName); return (GDataAccount) set.next(); } /** * @see org.apache.lucene.gdata.storage.Storage#close() */ public void close() { releaseAllSemaphore(); } /** * @see org.apache.lucene.gdata.storage.Storage#getAccountNameForFeedId(java.lang.String) */ public String getAccountNameForFeedId(String feedId) throws StorageException { if(feedId == null) throw new StorageException("feed id is null"); GDataAccount account = getServerBaseFeed(feedId).getAccount(); if(account == null) throw new IllegalStateException("No account stored with feed -- feedID: "+feedId); return account.getName(); } /** * @see org.apache.lucene.gdata.storage.Storage#getEntryLastModified(java.lang.String, java.lang.String) */ public Long getEntryLastModified(String entryId, final String feedId) throws StorageException { if(entryId == null) throw new StorageException("Entry ID is null"); return new Long(getInternalEntry(entryId).getUpdateTime()); } @SuppressWarnings("unchecked") private ServerBaseFeed getServerBaseFeed(String feedId)throws StorageException{ Query query = this.container.query(); query.constrain(ServerBaseFeed.class); query.descend("feed").descend("id").constrain(feedId); ObjectSet<ServerBaseFeed> feed = query.execute(); if (feed.size() > 1) throw new StorageException("Query for feed id " + feedId + " returns more than one result"); if (feed.size() < 1) throw new StorageException("can not find feed for given feed id -- " + feedId); return feed.next(); } /* * !Caution! -- could instantiate a lot of objects if used with certain classes!! * Refresh a persisted object with a depth of 100 * */ private void refreshPersistentObject(Object o){ this.container.ext().refresh(o,100); } /** * @see org.apache.lucene.gdata.storage.Storage#getFeedLastModified(java.lang.String) */ public Long getFeedLastModified(String feedId) throws StorageException { if(feedId == null) throw new StorageException("can not get last modified -- id is null"); ServerBaseFeed feed = getServerBaseFeed(feedId); return new Long(feed.getUpdated().getValue()); } private BaseEntry clearDynamicElements(BaseEntry entry){ this.container.ext().refresh(entry.getLinks(), 2); return entry; } private BaseFeed clearDynamicElements(BaseFeed feed){ this.container.ext().refresh(feed.getLinks(), 2); feed.getEntries().clear(); return feed; } ObjectContainer getContainer(){ return this.container; } static class DB4oEntry { private BaseEntry entry; private int version; private String feedId; private long updateTime; /** * @return Returns the entry. */ protected BaseEntry getEntry() { return this.entry; } /** * @param entry * The entry to set. */ protected void setEntry(BaseEntry entry) { this.entry = entry; } /** * @return Returns the feed. */ protected String getFeedId() { return this.feedId; } /** * @param feed * The feed to set. */ protected void setFeedId(String feed) { this.feedId = feed; } /** * @return Returns the updateTime. */ protected long getUpdateTime() { return this.updateTime; } /** * @param updateTime * The updateTime to set. */ protected void setUpdateTime(long updateTime) { this.updateTime = updateTime; } /** * @return Returns the version. */ public int getVersion() { return this.version; } /** * @param version The version to set. */ public void setVersion(int version) { this.version = version; if(this.entry != null) this.entry.setVersionId(""+this.version); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -