📄 db4ostorage.java
字号:
if (feed.getStartIndex() < 1) feed.setStartIndex(1); if (feed.getItemsPerPage() < 0) feed.setItemsPerPage(25); if (LOG.isInfoEnabled()) LOG.info("Fetching feed for feedID: " + feed.getId() + "; start-index: " + feed.getStartIndex() + "; items per page: " + feed.getItemsPerPage()); BaseFeed<BaseFeed, BaseEntry> persistentFeed = getFeedOnly(feed.getId(),feed.getServiceType()); /* * prevent previously added entries in long running storage instances */ clearDynamicElements(persistentFeed); Query query = this.container.query(); query.constrain(DB4oEntry.class); query.descend("feedId").constrain(feed.getId()).equal(); query.descend("updateTime").orderDescending(); ObjectSet<DB4oEntry> set = query.execute(); int size = set.size(); if (size < feed.getStartIndex()) { if (LOG.isDebugEnabled()) LOG.debug("no entries found for feed constrain -- feedID: " + feed.getId() + "; start-index: " + feed.getStartIndex() + "; items per page: " + feed.getItemsPerPage()); return persistentFeed; } int start = feed.getStartIndex() - 1; int items = start + feed.getItemsPerPage(); if (items > size) items = size; List<DB4oEntry> sublist = set.subList(start, items); for (DB4oEntry entry : sublist) { persistentFeed.getEntries().add(clearDynamicElements(entry.getEntry())); } this.container.activate(persistentFeed,RENDER_ACTIVATION_DEPTH); return persistentFeed; } @SuppressWarnings("unchecked") private BaseFeed<BaseFeed, BaseEntry> getFeedOnly(final String feedId, final String serviceId) throws StorageException { if(!checkService(feedId,serviceId)) throw new StorageException(); Query query = this.container.query(); query.constrain(ServerBaseFeed.class); query.constrain(BaseFeed.class); query.descend("id").constrain(feedId).equal(); ObjectSet set = query.execute(); if (set.size() > 1) throw new StorageException("Query for feed id " + feedId + " returns more than one result"); if (set.hasNext()) return (BaseFeed<BaseFeed, BaseEntry>) set.next(); throw new ResourceNotFoundException("can not find feed for given feed id -- " + feedId); } private boolean checkService(String feedId,String serviceId){ Query query = this.container.query(); query.constrain(ServerBaseFeed.class); query.descend("feed").descend("id").constrain(feedId).equal(); query.descend("serviceType").constrain(serviceId).equal(); return query.execute().size() == 1; } private ObjectSet getEnriesForFeedID(String feedId) { Query query = this.container.query(); query.constrain(DB4oEntry.class); query.descend("feedId").constrain(feedId).equal(); return query.execute(); } /** * @see org.apache.lucene.gdata.storage.Storage#getEntry(org.apache.lucene.gdata.data.ServerBaseEntry) */ public BaseEntry getEntry(ServerBaseEntry entry) throws StorageException { if (entry == null) throw new StorageException("can not retrieve entry -- is null"); if (entry.getId() == null) throw new StorageException("can not retrieve entry -- id is null"); if (LOG.isInfoEnabled()) LOG.info("Retrieving entry for entryID: " + entry.getId()); DB4oEntry retval = getInternalEntry(entry.getId()); this.container.activate(retval.getEntry(),RENDER_ACTIVATION_DEPTH); return clearDynamicElements(retval.getEntry()); } @SuppressWarnings("unchecked") private DB4oEntry getInternalEntry(String id) throws StorageException { Query query = this.container.query(); query.constrain(DB4oEntry.class); query.descend("entry").descend("id").constrain(id).equal(); ObjectSet<DB4oEntry> resultSet = query.execute(); if (resultSet.size() > 1) throw new StorageException( "Entry query returned not a unique result"); if (resultSet.hasNext()) return resultSet.next(); throw new ResourceNotFoundException("no entry with entryID: " + id + " stored -- query returned no result"); } /** * @see org.apache.lucene.gdata.storage.Storage#storeAccount(org.apache.lucene.gdata.data.GDataAccount) */ public void storeAccount(GDataAccount account) throws StorageException { if (account == null) throw new StorageException("can not store account -- is null"); if (account.getName() == null) throw new StorageException("can not store account -- name is null"); if (account.getPassword() == null) throw new StorageException( "can not store account -- password is null"); try { getAccount(account.getName()); throw new IllegalStateException("account with accountname: " + account.getName() + " already exists"); } catch (IllegalStateException e) { throw new StorageException("Account already exists"); } catch (StorageException e) { if (LOG.isDebugEnabled()) LOG .debug("checked account for existence -- does not exist -- store account"); } try { this.container.set(account); this.container.commit(); } 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); } if (LOG.isInfoEnabled()) LOG.info("Stored account: " + account); } /** * @see org.apache.lucene.gdata.storage.Storage#updateAccount(org.apache.lucene.gdata.data.GDataAccount) */ public void updateAccount(GDataAccount account) throws StorageException { if (account == null) throw new StorageException("can not update account -- is null"); if (account.getName() == null) throw new StorageException("can not update account -- name is null"); if (account.getPassword() == null) throw new StorageException( "can not update account -- password is null"); GDataAccount persitentAccount = getAccount(account.getName()); refreshPersistentObject(persitentAccount); try { this.container.set(account); this.container.delete(persitentAccount); this.container.commit(); } 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#deleteAccount(java.lang.String) */ public void deleteAccount(String accountname) throws StorageException { if (accountname == null) throw new StorageException( "can not delete account -- account name is null"); GDataAccount account = this.getAccount(accountname); refreshPersistentObject(account); if (LOG.isInfoEnabled()) LOG.info("delete account -- account name: " + accountname); try { this.container.delete(account); this.container.commit(); } 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#storeFeed(org.apache.lucene.gdata.data.ServerBaseFeed, * java.lang.String) */ public void storeFeed(ServerBaseFeed feed, String accountname) throws StorageException { if (feed == null) throw new StorageException("Can not store feed -- is null"); if (feed.getId() == null) throw new StorageException("Can not store feed -- id is null"); if(feed.getServiceType() == null) throw new StorageException("Can not store feed -- service type is null"); if(accountname == null) throw new StorageException("Account name is null"); Query query = this.container.query(); query.constrain(ServerBaseFeed.class); query.descend("feed").descend("id").constrain(feed.getId()).equal(); ObjectSet set = query.execute(); if (set.hasNext()) throw new StorageException("feed with feedID " + feed.getId() + " is already stored"); GDataAccount account = getAccount(accountname); refreshPersistentObject(account); feed.setAccount(account); /* * service config not required in db4o storage. * Entries/Feeds don't have to be build from xml */ feed.setServiceConfig(null); try { this.container.set(feed); this.container.commit(); } 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#deleteFeed(java.lang.String) */ public void deleteFeed(String feedId) throws StorageException { if (feedId == null) throw new StorageException("can not delete feed -- feed id is null"); Query query = this.container.query(); query.constrain(ServerBaseFeed.class); query.descend("feed").descend("id").constrain(feedId).equal(); ObjectSet set = query.execute(); if (set.size() > 1) throw new StorageException( "Feed query returned not a unique result"); if (set.size() == 0) throw new StorageException("no feed with feedID: " + feedId + " stored -- query returned no result"); ServerBaseFeed feed = (ServerBaseFeed) set.next(); refreshPersistentObject(feed); ObjectSet entrySet = getEnriesForFeedID(feed.getId()); try { this.container.delete(feed); this.container.delete(feed.getFeed()); for (Object object : entrySet) { refreshPersistentObject(object); this.container.delete(object); } this.container.commit();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -