FileDocCategorySizeDatePackage
DB4oStorage.javaAPI DocApache Lucene 2.1.031397Wed Feb 14 10:46:04 GMT 2007org.apache.lucene.gdata.storage.db4o

DB4oStorage

public class DB4oStorage extends Object implements org.apache.lucene.gdata.storage.Storage
Storage implementation for the DB4o storage component
author
Simon Willnauer

Fields Summary
private static final Log
LOG
private static final int
RENDER_ACTIVATION_DEPTH
private final com.db4o.ObjectContainer
container
private final org.apache.lucene.gdata.storage.StorageController
controller
private final List
semaphore
Constructors Summary
protected DB4oStorage(com.db4o.ObjectContainer container, org.apache.lucene.gdata.storage.StorageController controller)


    
       
              
        this.container = container;
        this.controller = controller;
    
Methods Summary
private booleancheckService(java.lang.String feedId, java.lang.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 com.google.gdata.data.BaseEntryclearDynamicElements(com.google.gdata.data.BaseEntry entry)

        this.container.ext().refresh(entry.getLinks(), 2);
        return entry;
    
private com.google.gdata.data.BaseFeedclearDynamicElements(com.google.gdata.data.BaseFeed feed)

        this.container.ext().refresh(feed.getLinks(), 2);
        feed.getEntries().clear();
        return feed;
    
public voidclose()

see
org.apache.lucene.gdata.storage.Storage#close()

        releaseAllSemaphore();
        
    
private voidcreateSemaphore(java.lang.String key)

        this.semaphore.add(key);
        if (this.container.ext().setSemaphore(key, 0))
            return;
        throw new ModificationConflictException(
                "can not create semaphore for key -- " + key);
    
public voiddeleteAccount(java.lang.String accountname)

see
org.apache.lucene.gdata.storage.Storage#deleteAccount(java.lang.String)

        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);
        }
    
public voiddeleteEntry(org.apache.lucene.gdata.data.ServerBaseEntry entry)

see
org.apache.lucene.gdata.storage.Storage#deleteEntry(org.apache.lucene.gdata.data.ServerBaseEntry)

        if (entry == null)
            throw new StorageException("Can not delete entry -- is null");
        if (entry.getFeedId() == null)
            throw new StorageException(
                    "can not delete entry -- feed id is null");
        if (entry.getId() == null)
            throw new StorageException("Can not delete entry -- id is null");
        if (LOG.isDebugEnabled())
            LOG.debug("delete entry for feed: " + entry.getFeedId()
                    + " entry ID: " + entry.getId());
        DB4oEntry persistentEntry = getInternalEntry(entry.getId());
        // lock the entry to prevent concurrent access
        createSemaphore(entry.getId());
        refreshPersistentObject(persistentEntry);
        if(persistentEntry.getVersion() != entry.getVersion())
            throw new ModificationConflictException(
                    "Current version does not match given version  -- currentVersion: "+persistentEntry.getVersion()+"; given Version: "+entry.getVersion() );
        BaseFeed<BaseFeed, BaseEntry> feed = getFeedOnly(entry.getFeedId(),entry.getServiceType());
        refreshPersistentObject(feed);
        DateTime time = DateTime.now();
        if (persistentEntry.getEntry().getUpdated() != null)
            time.setTzShift(persistentEntry.getEntry().getUpdated().getTzShift());
        feed.setUpdated(time);
        try {
            //delete the entry
            this.container.delete(persistentEntry.getEntry());
            this.container.delete(persistentEntry);
            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);
        } finally {
            releaseSemaphore(entry.getId());
        }
    
public voiddeleteFeed(java.lang.String feedId)

see
org.apache.lucene.gdata.storage.Storage#deleteFeed(java.lang.String)

        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();
        } 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);
        }

    
public org.apache.lucene.gdata.data.GDataAccountgetAccount(java.lang.String accountName)

see
org.apache.lucene.gdata.storage.Storage#getAccount(java.lang.String)

        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();
    
public java.lang.StringgetAccountNameForFeedId(java.lang.String feedId)

see
org.apache.lucene.gdata.storage.Storage#getAccountNameForFeedId(java.lang.String)

        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();
    
com.db4o.ObjectContainergetContainer()

        return this.container;
    
private com.db4o.ObjectSetgetEnriesForFeedID(java.lang.String feedId)

        Query query = this.container.query();
        query.constrain(DB4oEntry.class);
        query.descend("feedId").constrain(feedId).equal();

        return query.execute();
    
public com.google.gdata.data.BaseEntrygetEntry(org.apache.lucene.gdata.data.ServerBaseEntry entry)

see
org.apache.lucene.gdata.storage.Storage#getEntry(org.apache.lucene.gdata.data.ServerBaseEntry)

        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());
    
public java.lang.LonggetEntryLastModified(java.lang.String entryId, java.lang.String feedId)

see
org.apache.lucene.gdata.storage.Storage#getEntryLastModified(java.lang.String, java.lang.String)

        if(entryId == null)
            throw new StorageException("Entry ID is null");
        return new Long(getInternalEntry(entryId).getUpdateTime());
    
public com.google.gdata.data.BaseFeedgetFeed(org.apache.lucene.gdata.data.ServerBaseFeed feed)

see
org.apache.lucene.gdata.storage.Storage#getFeed(org.apache.lucene.gdata.data.ServerBaseFeed)

        if (feed.getId() == null)
            throw new StorageException("can not get feed -- feed id is null");
        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;

    
public java.lang.LonggetFeedLastModified(java.lang.String feedId)

see
org.apache.lucene.gdata.storage.Storage#getFeedLastModified(java.lang.String)

        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 com.google.gdata.data.BaseFeedgetFeedOnly(java.lang.String feedId, java.lang.String serviceId)

        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 org.apache.lucene.gdata.storage.db4o.DB4oStorage$DB4oEntrygetInternalEntry(java.lang.String id)

        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");
    
private org.apache.lucene.gdata.data.ServerBaseFeedgetServerBaseFeed(java.lang.String 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);
        return feed.next();
    
public java.lang.StringgetServiceForFeed(java.lang.String feedId)

see
org.apache.lucene.gdata.storage.Storage#getServiceForFeed(java.lang.String)

        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();
    
private voidrefreshPersistentObject(java.lang.Object o)

        this.container.ext().refresh(o,100);
    
private voidreleaseAllSemaphore()

        for (String key : this.semaphore) {
            this.container.ext().releaseSemaphore(key);
        }
        this.semaphore.clear();
    
private voidreleaseSemaphore(java.lang.String key)

        if (this.semaphore.contains(key)) {
            this.container.ext().releaseSemaphore(key);
            this.semaphore.remove(key);
        }
    
private voidsetUpdated(org.apache.lucene.gdata.data.ServerBaseEntry entry, org.apache.lucene.gdata.storage.db4o.DB4oStorage$DB4oEntry intEntry)

        if (entry.getUpdated().compareTo(intEntry.getEntry().getUpdated()) <= 0) {
            if (LOG.isDebugEnabled())
                LOG
                        .debug("Set new UpdateTime to entry new entry time is less or equal the time of the stored entry -- old Entry: "
                                + intEntry.getEntry().getUpdated()
                                + "; new Entry: " + entry.getUpdated());
            entry.setUpdated(new DateTime(System.currentTimeMillis(), entry
                    .getUpdated().getTzShift()));
        }

    
private voidsetUpdated(org.apache.lucene.gdata.data.ServerBaseEntry entry, com.google.gdata.data.BaseFeed feed)

        if (entry.getUpdated() != null){
            long timeInMilli = entry.getUpdated().getValue();
            int tzShift = entry.getUpdated().getTzShift();
            feed.setUpdated(new DateTime(timeInMilli, tzShift));
        }
        else{
            int timezone = 0;
            if(feed.getUpdated() != null){
                 timezone = feed.getUpdated().getTzShift();
            }
            long timeInMilli = System.currentTimeMillis();
            entry.setUpdated(new DateTime(timeInMilli,timezone));
            feed.setUpdated(new DateTime(timeInMilli,timezone));
        }
    
public voidstoreAccount(org.apache.lucene.gdata.data.GDataAccount account)

see
org.apache.lucene.gdata.storage.Storage#storeAccount(org.apache.lucene.gdata.data.GDataAccount)

        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);
    
public com.google.gdata.data.BaseEntrystoreEntry(org.apache.lucene.gdata.data.ServerBaseEntry entry)

see
org.apache.lucene.gdata.storage.Storage#storeEntry(org.apache.lucene.gdata.data.ServerBaseEntry)

        if (entry == null)
            throw new StorageException("Can not store entry -- is null");

        if (entry.getFeedId() == null)
            throw new StorageException("can not store entry -- feed id is null");
        if (LOG.isDebugEnabled())
            LOG.debug("Storing entry for feed: " + entry.getFeedId());
        BaseFeed<BaseFeed, BaseEntry> feed = getFeedOnly(entry.getFeedId(),entry.getServiceType());
       refreshPersistentObject(feed);
        try {
            StringBuilder idBuilder = new StringBuilder(entry.getFeedId());
            idBuilder.append(this.controller.releaseId());
            entry.setId(idBuilder.toString());
        } catch (StorageException e) {
            LOG.error("Can not create uid for entry -- " + e.getMessage(), e);
            throw new StorageException("Can not create uid for entry -- "
                    + e.getMessage(), e);

        }
        setUpdated(entry, feed);
        DB4oEntry intEntry = new DB4oEntry();
        intEntry.setEntry(entry.getEntry());
        intEntry.setUpdateTime(entry.getUpdated().getValue());
        intEntry.setFeedId(feed.getId());
        intEntry.setVersion(entry.getVersion());

       
        try {
            this.container.set(feed);
            this.container.set(intEntry);
            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 Entry for entryID: " + entry.getId()
                    + " -- feedID: " + entry.getFeedId());
        return entry.getEntry();
    
public voidstoreFeed(org.apache.lucene.gdata.data.ServerBaseFeed feed, java.lang.String accountname)

see
org.apache.lucene.gdata.storage.Storage#storeFeed(org.apache.lucene.gdata.data.ServerBaseFeed, java.lang.String)

        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);
        }
    
public voidupdateAccount(org.apache.lucene.gdata.data.GDataAccount account)

see
org.apache.lucene.gdata.storage.Storage#updateAccount(org.apache.lucene.gdata.data.GDataAccount)

        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);
        }
    
public com.google.gdata.data.BaseEntryupdateEntry(org.apache.lucene.gdata.data.ServerBaseEntry entry)

see
org.apache.lucene.gdata.storage.Storage#updateEntry(org.apache.lucene.gdata.data.ServerBaseEntry)

        if (entry == null)
            throw new StorageException("Can not update entry -- is null");
        if (entry.getFeedId() == null)
            throw new StorageException(
                    "can not delete entry -- feed id is null");
        if (entry.getId() == null)
            throw new StorageException("Can not delete entry -- id is null");

        DB4oEntry persistentEntry = getInternalEntry(entry.getId());
        // lock the entry to prevent concurrent access
        createSemaphore(entry.getId());
        refreshPersistentObject(persistentEntry);
        if(persistentEntry.getVersion() != entry.getVersion())
            throw new ModificationConflictException(
                    "Current version does not match given version  -- currentVersion: "+persistentEntry.getVersion()+"; given Version: "+entry.getVersion() );
        
        setUpdated(entry, persistentEntry);
        BaseFeed<BaseFeed, BaseEntry> feed = getFeedOnly(entry.getFeedId(),entry.getServiceType());
        refreshPersistentObject(feed);
        BaseEntry retVal = entry.getEntry(); 
        DB4oEntry newEntry = new DB4oEntry();
        newEntry.setEntry(retVal);
        newEntry.setUpdateTime(entry.getUpdated().getValue());
        newEntry.setFeedId(feed.getId());
        // increment Version
        newEntry.setVersion((entry.getVersion())+1);

        setUpdated(entry, feed);
        try {
            this.container.set(feed);
            this.container.set(newEntry);
            this.container.delete(persistentEntry.getEntry());
            this.container.delete(persistentEntry);
            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);
        } finally {
            releaseSemaphore(entry.getId());
        }
        return retVal;

    
public voidupdateFeed(org.apache.lucene.gdata.data.ServerBaseFeed feed, java.lang.String accountname)

see
org.apache.lucene.gdata.storage.Storage#updateFeed(org.apache.lucene.gdata.data.ServerBaseFeed, java.lang.String)

        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);
        }