lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yo...@apache.org
Subject svn commit: r417265 [4/6] - in /lucene/java/trunk/contrib/gdata-server: ./ lib/ src/java/ src/java/org/apache/lucene/gdata/server/ src/java/org/apache/lucene/gdata/server/registry/ src/java/org/apache/lucene/gdata/servlet/ src/java/org/apache/lucene/gd...
Date Mon, 26 Jun 2006 18:14:55 GMT
Modified: lucene/java/trunk/contrib/gdata-server/src/java/org/apache/lucene/gdata/storage/lucenestorage/StorageCoreController.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/gdata-server/src/java/org/apache/lucene/gdata/storage/lucenestorage/StorageCoreController.java?rev=417265&r1=417264&r2=417265&view=diff
==============================================================================
--- lucene/java/trunk/contrib/gdata-server/src/java/org/apache/lucene/gdata/storage/lucenestorage/StorageCoreController.java (original)
+++ lucene/java/trunk/contrib/gdata-server/src/java/org/apache/lucene/gdata/storage/lucenestorage/StorageCoreController.java Mon Jun 26 11:14:53 2006
@@ -1,286 +1,398 @@
-package org.apache.lucene.gdata.storage.lucenestorage; 
- 
-import java.io.File; 
-import java.io.IOException; 
- 
-import org.apache.commons.logging.Log; 
-import org.apache.commons.logging.LogFactory; 
-import org.apache.lucene.analysis.standard.StandardAnalyzer; 
-import org.apache.lucene.gdata.server.registry.GDataServerRegistry; 
-import org.apache.lucene.gdata.storage.IDGenerator; 
-import org.apache.lucene.gdata.storage.StorageController; 
-import org.apache.lucene.gdata.storage.StorageException; 
-import org.apache.lucene.gdata.storage.lucenestorage.configuration.StorageConfigurator; 
-import org.apache.lucene.gdata.storage.lucenestorage.util.ReferenceCounter; 
-import org.apache.lucene.index.IndexModifier; 
-import org.apache.lucene.search.IndexSearcher; 
-import org.apache.lucene.store.Directory; 
-import org.apache.lucene.store.FSDirectory; 
- 
-/** 
- * TODO document this 
- * @author Simon Willnauer 
+package org.apache.lucene.gdata.storage.lucenestorage;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.gdata.data.GDataAccount;
+import org.apache.lucene.gdata.server.registry.Component;
+import org.apache.lucene.gdata.server.registry.ComponentType;
+import org.apache.lucene.gdata.storage.IDGenerator;
+import org.apache.lucene.gdata.storage.Storage;
+import org.apache.lucene.gdata.storage.StorageController;
+import org.apache.lucene.gdata.storage.StorageException;
+import org.apache.lucene.gdata.storage.lucenestorage.configuration.StorageConfigurator;
+import org.apache.lucene.gdata.storage.lucenestorage.util.ReferenceCounter;
+import org.apache.lucene.index.IndexModifier;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+import org.apache.lucene.store.RAMDirectory;
+
+/**
+ *  
  * 
- */ 
-public class StorageCoreController implements StorageController{ 
-    protected static final Log LOG = LogFactory.getLog(StorageCoreController.class); 
-    private IndexSearcher searcher; 
-    private static StorageCoreController coreController; 
-    private final Directory storageDir; 
-    private final StorageModifier modifier; 
-    private ReferenceCounter<StorageQuery> storageQuery; 
-    private StorageBuffer currentBuffer; 
-    private Object storageControllerLock = new Object(); 
-    private static final int DEFAULT_STORAGE_BUFFER_SIZE = 10; 
-    private static final int DEFAULT_STORAGE_PERSIST_FACTOR = 10; 
-    private static final String STORAGELOG = ".lucenestorage"; 
-    private int storageBufferSize; 
-    private int storagePersistFactor; 
-    private StorageConfigurator configurator; 
-    private IDGenerator idGenerator; 
-    private int indexOptimizeInterval; 
-     
-   private StorageCoreController()throws IOException, StorageException{ 
-       this(null); 
-   } 
-    
-    
-    
+ * @author Simon Willnauer
+ * 
+ */
+@Component(componentType = ComponentType.STORAGECONTROLLER)
+public class StorageCoreController implements StorageController {
+    protected static final Log LOG = LogFactory
+            .getLog(StorageCoreController.class);
+
+    private IndexSearcher searcher;
+
+    private final Directory storageDir;
+
+    private final StorageModifier modifier;
+
+    private ReferenceCounter<StorageQuery> storageQuery;
+
+    private StorageBuffer currentBuffer;
+
+    private Object storageControllerLock = new Object();
+
+    private static final int DEFAULT_STORAGE_BUFFER_SIZE = 10;
+
+    private static final int DEFAULT_STORAGE_PERSIST_FACTOR = 10;
+
+    private static final String STORAGELOG = ".lucenestorage";
+
+    private int storageBufferSize;
+
+    private int storagePersistFactor;
+
+    private StorageConfigurator configurator;
+
+    private IDGenerator idGenerator;
+
+    private int indexOptimizeInterval;
     
-    private StorageCoreController(final Directory dir) throws IOException, StorageException { 
-        synchronized (StorageCoreController.class) { 
-            try{ 
-                this.idGenerator = new IDGenerator(10); 
-            }catch (Exception e) { 
-                throw new StorageException("Can't create ID Generator",e); 
-            } 
-             
-            boolean createNewStorage = false; 
-             
-            if(dir == null){ 
-            this.configurator = StorageConfigurator.getStorageConfigurator(); 
-            String storageDirPath = this.configurator.getStorageDirectory(); 
-            File storeDir = new File(storageDirPath); 
-            File storageLog = new File(storeDir.getAbsolutePath()+System.getProperty("file.separator")+STORAGELOG); 
-            try{ 
-            if(storeDir.isDirectory() && !storageLog.exists()){ 
-                 
-                if(createLuceneStorageLog(storeDir)){ 
-                    this.storageDir = FSDirectory.getDirectory(storeDir,true); 
-                    createNewStorage = true; 
-                } 
-                else 
-                    throw new StorageException("could not create storage log file in "+storageDirPath); 
-                 
-            }else 
-                this.storageDir = FSDirectory.getDirectory(storeDir,false); 
-            }catch (IOException e) { 
-                storageLog.delete(); 
-                throw e; 
-            } 
-            this.indexOptimizeInterval = this.configurator.getIndexOptimizeInterval(); 
-            this.storageBufferSize = this.configurator.getStorageBufferSize() < DEFAULT_STORAGE_BUFFER_SIZE?DEFAULT_STORAGE_BUFFER_SIZE:this.configurator.getStorageBufferSize(); 
-            this.storagePersistFactor = this.configurator.getStoragepersistFactor() < DEFAULT_STORAGE_PERSIST_FACTOR? DEFAULT_STORAGE_PERSIST_FACTOR:this.configurator.getStoragepersistFactor(); 
-             
-            } 
-            else 
-                this.storageDir = dir; 
-             
-            this.currentBuffer = new StorageBuffer(this.storageBufferSize); 
-            this.modifier = createStorageModifier(createNewStorage); 
-            this.searcher = new IndexSearcher(this.storageDir); 
-             
- 
-            GDataServerRegistry.getRegistry().registerStorage(this);// TODO reverse dependency here 
-            
-                 
-             
-     } 
-         
-    } 
-    private StorageModifier createStorageModifier(boolean create) throws IOException{ 
-        IndexModifier indexModifier = new IndexModifier(this.storageDir,new StandardAnalyzer(),create); 
-        return new StorageModifier(indexModifier,this.currentBuffer,this.storagePersistFactor,this.indexOptimizeInterval); 
-    } 
-    /**TODO document this 
-     * @return 
-     */ 
-    public StorageModifier getStorageModifier(){ 
-        return this.modifier; 
-    } 
-     
-    /**TODO document this 
-     * @return 
-     * @throws IOException 
-     * @throws StorageException 
-     */ 
-    public static StorageCoreController getStorageCoreController() throws IOException, StorageException{ 
-        synchronized (StorageCoreController.class) { 
-            if(coreController == null) 
-            coreController = new StorageCoreController(); 
-            return coreController; 
-        } 
-    } 
-    /**TODO document this 
-     * @param dir 
-     * @return 
-     * @throws IOException 
-     * @throws StorageException 
-     */ 
-    protected static StorageCoreController getStorageCoreController(final Directory dir) throws IOException, StorageException{ 
-        synchronized (StorageCoreController.class) { 
-            if(coreController == null) 
-            coreController = new StorageCoreController(dir); 
-            return coreController; 
-        } 
-    } 
- 
-    /**TODO document this 
-     * @return 
-     * @throws IOException 
-     */ 
-    public  ReferenceCounter<StorageQuery> getStorageQuery() throws IOException { 
-        synchronized (this.storageControllerLock) { 
- 
-        if(this.storageQuery == null){ 
-            this.storageQuery = getNewStorageQueryHolder(new StorageQuery(this.currentBuffer,this.searcher)); 
-            if(LOG.isInfoEnabled()) 
-                LOG.info("Relese new StorageQuery"); 
-        } 
-        this.storageQuery.increamentReference(); 
-        return this.storageQuery; 
-        } 
-    } 
-     
-    private ReferenceCounter<StorageQuery> getNewStorageQueryHolder(final StorageQuery query){ 
-        ReferenceCounter<StorageQuery> holder = new ReferenceCounter<StorageQuery>(query){ 
-            public void close(){ 
-                try{ 
-                    if(LOG.isInfoEnabled()) 
-                        LOG.info("close StorageQuery -- zero references remaining"); 
-                    this.resource.close(); 
-                }catch (IOException e) { 
-                    LOG.warn("Error during close call on StorageQuery"+e.getMessage(),e); 
-                } 
-            } 
-        }; 
-        holder.increamentReference(); 
-        return holder; 
-    } 
-     
-   
-     
-    protected void registerNewStorageQuery() throws IOException{ 
-        if(LOG.isInfoEnabled()) 
-            LOG.info("new StorageQuery requested -- create new storage buffer"); 
-        synchronized (this.storageControllerLock) { 
-            if(this.storageQuery != null) 
-                this.storageQuery.decrementRef(); 
-            this.searcher = new IndexSearcher(this.storageDir); 
-            this.storageQuery = null; 
-            this.currentBuffer = new StorageBuffer(this.storageBufferSize); 
-             
-        } 
-         
-    } 
- 
-     
-    protected StorageBuffer releaseNewStorageBuffer() { 
-        synchronized (this.storageControllerLock) { 
-            return this.currentBuffer; 
-        } 
-    } 
- 
-    /**TODO document this 
-     * @return 
-     * @throws IOException 
-     */ 
-    public IndexModifier createIndexModifier() throws IOException { 
-        if(LOG.isInfoEnabled()) 
-            LOG.info("new IndexModifier created - release to StorageModifier"); 
-        synchronized (this.storageControllerLock) { 
-           return new IndexModifier(this.storageDir,new StandardAnalyzer(),false); 
-        } 
-    } 
-     
-    private void close() throws IOException{ 
-        synchronized (this.storageControllerLock) { 
-        if(LOG.isInfoEnabled()) 
-            LOG.info("StorageController has been closed -- server is shutting down -- release all resources"); 
-        if(this.storageQuery != null) 
-            this.storageQuery.decrementRef(); 
-        coreController = null; 
-        this.modifier.close(); 
-        //TODO make sure all resources will be released 
-        } 
-    } 
-    /**TODO document this 
-     * @return 
-     */ 
-    public int getStorageBufferSize() { 
-        return this.storageBufferSize; 
-    } 
-    /** 
-     * @param storageBufferSize 
-     */ 
-    public void setStorageBufferSize(int storageBufferSize) { 
-        this.storageBufferSize = storageBufferSize; 
-    } 
-    /**TODO document this 
-     * @return 
-     */ 
-    public int getStoragePersistFactor() { 
-        return this.storagePersistFactor; 
-    } 
-    /** 
-     * @param storagePersistFactor 
-     */ 
-    public void setStoragePersistFactor(int storagePersistFactor) { 
-        this.storagePersistFactor = storagePersistFactor; 
-    } 
-    /** 
-     * @throws IOException 
-     * @throws StorageException 
-     */ 
-    public void forceWrite()throws IOException, StorageException{ 
-        this.modifier.forceWrite(); 
-    } 
-     
-     
-    private boolean createLuceneStorageLog(File storageDirectory) throws IOException{ 
-        if(storageDirectory.isDirectory() && !storageDirectory.exists()){ 
-            storageDirectory.createNewFile(); 
-        } 
-        File file = new File(storageDirectory.getAbsolutePath()+System.getProperty("file.separator")+STORAGELOG); 
-        return file.createNewFile(); 
-         
-         
-    }    
-     
+//    private RecoverController recoverController;
+
+    /**
+     * Creates a new <tt>StoragCoreController</tt> and sets up the storage
+     * environment reading the configuration file.
+     * 
+     * 
+     * 
+     * @throws IOException -
+     *             if an IOException occures
+     * @throws StorageException -
+     *             if the storage lock can not be created or the
+     *             {@link IDGenerator} can not be loaded
+     */
+    public StorageCoreController() throws IOException, StorageException {
+        synchronized (StorageCoreController.class) {
+            try {
+                this.idGenerator = new IDGenerator(10);
+            } catch (Exception e) {
+                throw new StorageException("Can't create ID Generator", e);
+            }
+
+            boolean createNewStorage = false;
+            this.configurator = StorageConfigurator.getStorageConfigurator();
+            if (!this.configurator.isRamDirectory()) {
+
+                String storageDirPath = this.configurator.getStorageDirectory();
+                File storeDir = new File(storageDirPath);
+                File storageLog = new File(storeDir.getAbsolutePath()
+                        + System.getProperty("file.separator") + STORAGELOG);
+                try {
+                    if (storeDir.isDirectory() && !storageLog.exists()) {
+
+                        if (createLuceneStorageLog(storeDir)) {
+                            this.storageDir = FSDirectory.getDirectory(
+                                    storeDir, true);
+                            createNewStorage = true;
+                        } else
+                            throw new StorageException(
+                                    "could not create storage lock file in "
+                                            + storageDirPath);
+
+                    } else
+                        this.storageDir = FSDirectory.getDirectory(storeDir,
+                                false);
+                } catch (IOException e) {
+                    storageLog.delete();
+                    throw e;
+                }
+                this.indexOptimizeInterval = this.configurator
+                        .getIndexOptimizeInterval();
+                this.storageBufferSize = this.configurator
+                        .getStorageBufferSize() < DEFAULT_STORAGE_BUFFER_SIZE ? DEFAULT_STORAGE_BUFFER_SIZE
+                        : this.configurator.getStorageBufferSize();
+                this.storagePersistFactor = this.configurator
+                        .getStoragepersistFactor() < DEFAULT_STORAGE_PERSIST_FACTOR ? DEFAULT_STORAGE_PERSIST_FACTOR
+                        : this.configurator.getStoragepersistFactor();
+
+            } else
+                this.storageDir = getRamDirectory();
+
+            this.currentBuffer = new StorageBuffer(this.storageBufferSize);
+            this.modifier = createStorageModifier(createNewStorage);
+            this.searcher = new IndexSearcher(this.storageDir);
+//            this.recoverController = new RecoverController(null,this.configurator.isRecover(),this.configurator.isKeepRecoveredFiles());
+            if(createNewStorage)
+                createAdminAccount();
+
+        }
+
+    }
+
+    private StorageModifier createStorageModifier(boolean create)
+            throws IOException {
+        IndexModifier indexModifier = new IndexModifier(this.storageDir,
+                new StandardAnalyzer(), create);
+        return new StorageModifier(this, indexModifier, this.currentBuffer,
+                this.storagePersistFactor, this.indexOptimizeInterval);
+    }
+
+    /**
+     * returns the current storage modifier
+     * 
+     * @return - the current storage modifier
+     */
+    protected StorageModifier getStorageModifier() {
+        return this.modifier;
+    }
+
+    /**
+     * returns a <tt>StorageQuery</tt> to query the storage index. The
+     * returned object is a reference counter to keep track of the references to
+     * the <tt>StorageQuery</tt>. The reference is already incremented before
+     * returned from this method.
+     * <p>
+     * if the reference counter has no remaining references the resource e.g.
+     * the <tt>StorageQuery</tt> will be closed. This ensures that a
+     * <tt>StorageQuery</tt> instance will be arround as long as needed and
+     * the resources will be released. The reference counter should be
+     * decremented by clients after finished using the query instance.
+     * </p>
+     * 
+     * @return a {@link ReferenceCounter} instance holding the StorageQuery as a
+     *         resource.
+     * 
+     */
+    protected ReferenceCounter<StorageQuery> getStorageQuery() {
+        synchronized (this.storageControllerLock) {
+
+            if (this.storageQuery == null) {
+                this.storageQuery = getNewStorageQueryHolder(new StorageQuery(
+                        this.currentBuffer, this.searcher));
+                if (LOG.isInfoEnabled())
+                    LOG.info("Relese new StorageQuery");
+            }
+            this.storageQuery.increamentReference();
+            return this.storageQuery;
+        }
+    }
+
+    private ReferenceCounter<StorageQuery> getNewStorageQueryHolder(
+            final StorageQuery query) {
+        ReferenceCounter<StorageQuery> holder = new ReferenceCounter<StorageQuery>(
+                query) {
+            public void close() {
+                try {
+                    if (LOG.isInfoEnabled())
+                        LOG
+                                .info("close StorageQuery -- zero references remaining");
+                    this.resource.close();
+                } catch (IOException e) {
+                    LOG.warn("Error during close call on StorageQuery"
+                            + e.getMessage(), e);
+                }
+            }
+        };
+        holder.increamentReference();
+        return holder;
+    }
+
+    /**
+     * Forces the controller to register a new <tt>StorageQuery</tt> instance.
+     * This method will be called after an index has been modified to make the
+     * changes available for searching.
+     * 
+     * @throws IOException -
+     *             if an IO exception occures
+     */
+    protected void registerNewStorageQuery() throws IOException {
+        if (LOG.isInfoEnabled())
+            LOG.info("new StorageQuery requested -- create new storage buffer");
+        synchronized (this.storageControllerLock) {
+            if (this.storageQuery != null)
+                this.storageQuery.decrementRef();
+            this.searcher = new IndexSearcher(this.storageDir);
+            this.storageQuery = null;
+            this.currentBuffer = new StorageBuffer(this.storageBufferSize);
+
+        }
+
+    }
+
+    /**
+     * Creates a new StorageBuffer
+     * 
+     * @return the new StorageBuffer
+     */
+    protected StorageBuffer releaseNewStorageBuffer() {
+        synchronized (this.storageControllerLock) {
+            return this.currentBuffer;
+        }
+    }
+
+    /**
+     * Creates a new IndexModifier on the storage index
+     * 
+     * @return - a new modifier
+     * @throws IOException -
+     *             if an IO exception occures
+     */
+    protected IndexModifier createIndexModifier() throws IOException {
+        if (LOG.isInfoEnabled())
+            LOG.info("new IndexModifier created - release to StorageModifier");
+        synchronized (this.storageControllerLock) {
+            return new IndexModifier(this.storageDir, new StandardAnalyzer(),
+                    false);
+        }
+    }
+
+    private void close() throws IOException {
+        synchronized (this.storageControllerLock) {
+            if (LOG.isInfoEnabled())
+                LOG
+                        .info("StorageController has been closed -- server is shutting down -- release all resources");
+            if (this.storageQuery != null)
+                this.storageQuery.decrementRef();
+            this.modifier.close();
+            // TODO make sure all resources will be released
+        }
+    }
+
+    /**
+     * The size of the <tt>StorageBuffer</tt>.
+     * 
+     * @return - storage buffer size
+     */
+    public int getStorageBufferSize() {
+        return this.storageBufferSize;
+    }
+
+    /**
+     * The size of the <tt>StorageBuffer</tt>. This size should be at least
+     * as big as the persist factor to prevent the <tt>StorageBuffer</tt> from
+     * resizing
+     * 
+     * @param storageBufferSize
+     */
+    public void setStorageBufferSize(int storageBufferSize) {
+        this.storageBufferSize = storageBufferSize;
+    }
+
+    /**
+     * An integer value after how many changes to the StorageModifier the
+     * buffered changes will be persisted / wirtten to the index
+     * 
+     * @return - the persist factor
+     */
+    public int getStoragePersistFactor() {
+        return this.storagePersistFactor;
+    }
+
+    /**
+     * @param storagePersistFactor
+     */
+    public void setStoragePersistFactor(int storagePersistFactor) {
+        this.storagePersistFactor = storagePersistFactor;
+    }
+
+    /**
+     * Forces the StorageModifier to write all buffered changes.
+     * 
+     * @throws IOException -
+     *             if an IO exception occures
+     * 
+     */
+    public void forceWrite() throws IOException {
+        this.modifier.forceWrite();
+    }
+
+    private boolean createLuceneStorageLog(File storageDirectory)
+            throws IOException {
+        if (storageDirectory.isDirectory() && !storageDirectory.exists()) {
+            storageDirectory.createNewFile();
+        }
+        File file = new File(storageDirectory.getAbsolutePath()
+                + System.getProperty("file.separator") + STORAGELOG);
+        return file.createNewFile();
+
+    }
+
+    /**
+     * Creates a unique ID to store as an id for
+     * {@link org.apache.lucene.gdata.data.ServerBaseEntry} instances
+     * 
+     * @return - a unique id
+     * @throws StorageException -
+     *             if no id can be released
+     */
+    public synchronized String releaseID() throws StorageException {
+        try {
+            return this.idGenerator.getUID();
+        } catch (InterruptedException e) {
+            throw new StorageException("Can't release new ID", e);
+        }
+
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.StorageController#destroy()
+     */
+    public void destroy() {
+        try {
+            close();
+        } catch (Exception e) {
+            LOG.error("Closing StorageCoreController failed -- "
+                    + e.getMessage(), e);
+        }
+    }
+
+    /**
+     * 
+     * @return - the lucene directory used as a storage
+     */
+    protected Directory getDirectory() {
+        return this.storageDir;
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.StorageController#getStorage()
+     */
+    public Storage getStorage() throws StorageException {
+        try {
+            return new StorageImplementation();
+        } catch (StorageException e) {
+            StorageException ex = new StorageException(
+                    "Can't create Storage instance -- " + e.getMessage(), e);
+            ex.setStackTrace(e.getStackTrace());
+            throw ex;
+
+        }
+    }
+
+    // TODO Try to remove this --> testcases
+    private RAMDirectory getRamDirectory() throws IOException {
+        IndexWriter writer;
+        RAMDirectory retVal = new RAMDirectory();
+        writer = new IndexWriter(retVal, new StandardAnalyzer(), true);
+        writer.close();
+        return retVal;
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.server.registry.ServerComponent#initialize()
+     */
+    public void initialize() {
+        //
+    }
     
-    /**TODO document this 
-     * @return 
-     * @throws StorageException 
-     */ 
-    public synchronized String releaseID() throws StorageException{ 
-        try{ 
-        return this.idGenerator.getUID(); 
-        }catch (InterruptedException e) { 
-            throw new StorageException("Can't release new ID",e); 
-        } 
-         
-    } 
- 
- 
- 
-    /** 
-     * @see org.apache.lucene.gdata.storage.StorageController#destroy() 
-     */ 
-    public void destroy() { 
-        try{ 
-        close(); 
-        }catch (Exception e) { 
-            LOG.error("Closing StorageCoreController failed -- "+e.getMessage(),e); 
-        } 
-    } 
-} 
+    private void createAdminAccount() throws StorageException{
+        GDataAccount adminAccount = GDataAccount.createAdminAccount();
+        StorageAccountWrapper wrapper = new StorageAccountWrapper(adminAccount);
+        this.getStorageModifier().createAccount(wrapper);
+    }
+
+}

Modified: lucene/java/trunk/contrib/gdata-server/src/java/org/apache/lucene/gdata/storage/lucenestorage/StorageEntryWrapper.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/gdata-server/src/java/org/apache/lucene/gdata/storage/lucenestorage/StorageEntryWrapper.java?rev=417265&r1=417264&r2=417265&view=diff
==============================================================================
--- lucene/java/trunk/contrib/gdata-server/src/java/org/apache/lucene/gdata/storage/lucenestorage/StorageEntryWrapper.java (original)
+++ lucene/java/trunk/contrib/gdata-server/src/java/org/apache/lucene/gdata/storage/lucenestorage/StorageEntryWrapper.java Mon Jun 26 11:14:53 2006
@@ -12,177 +12,215 @@
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  * See the License for the specific language governing permissions and 
  * limitations under the License. 
- */ 
- 
-package org.apache.lucene.gdata.storage.lucenestorage; 
- 
-import java.io.IOException; 
-import java.io.StringWriter; 
- 
-import org.apache.lucene.document.Document; 
-import org.apache.lucene.document.Field; 
- 
-import com.google.gdata.data.BaseEntry; 
-import com.google.gdata.data.ExtensionProfile; 
-import com.google.gdata.util.common.xml.XmlWriter; 
- 
-/** 
- * This immutable class wrapps Entries for an internal Storage representation of 
- * an entry. This class also acts as a Documentfactory for lucene documents to 
- * be stored inside the index. 
- *  
- * @author Simon Willnauer 
- *  
- */ 
-public class StorageEntryWrapper implements Comparable<StorageEntryWrapper> { 
-    private static final String INTERNAL_ENCODING = "UTF-8"; 
- 
-    /** 
-     * lucene field name Entry id 
-     */ 
-    public final static String FIELD_ENTRY_ID = "entryId"; 
- 
-    /** 
-     * lucene field name feed id 
-     */ 
-    public final static String FIELD_FEED_ID = "feedId"; 
- 
-    /** 
-     * lucene field name entry content 
-     */ 
-    public final static String FIELD_CONTENT = "content"; 
- 
-    /** 
-     * lucene field name creating timestamp 
-     */ 
-    public final static String FIELD_TIMESTAMP = "timestamp"; 
- 
-    private final String entryId; 
- 
-    private final String feedId; 
- 
-    private final String content; 
- 
-    private final transient BaseEntry entry; 
- 
-    private final Long timestamp; 
- 
-    private transient Document document; 
- 
-    private StorageOperation operation; 
- 
-    private final ExtensionProfile profile; 
- 
-    /** 
-     * Creates a new StorageEntryWrapper. 
-     *  
-     * @param entry - 
-     *            the entry to wrap 
-     * @param feedId - 
-     *            the feed id 
-     * @param operation - 
-     *            the StorageOperation 
-     * @param profile - 
-     *            the ExtensionProfil for the given entry 
-     * @throws IOException - 
-     *             if the entry content can not be generated 
-     */ 
-    protected StorageEntryWrapper(final BaseEntry entry, final String feedId, 
-            StorageOperation operation, final ExtensionProfile profile) 
-            throws IOException { 
-        this.entry = entry; 
-        this.operation = operation; 
-        this.entryId = entry.getId(); 
-        this.feedId = feedId; 
-        this.profile = profile; 
-        this.content = buildContent(); 
-        this.timestamp = new Long(System.currentTimeMillis()); 
- 
-    } 
- 
-    private String buildContent() throws IOException { 
-        StringWriter writer = new StringWriter(); 
-        XmlWriter xmlWriter = new XmlWriter(writer, INTERNAL_ENCODING); 
-        this.entry.generateAtom(xmlWriter, this.profile); 
-        return writer.toString(); 
- 
-    } 
- 
-    /** 
-     * @return - the lucene document representing the entry 
-     */ 
-    public Document getLuceneDocument() { 
-        if (this.document != null) 
-            return this.document; 
-        this.document = new Document(); 
-        this.document.add(new Field("entryId", this.entryId, Field.Store.YES, 
-                Field.Index.UN_TOKENIZED)); 
-        this.document.add(new Field("feedId", this.feedId, Field.Store.YES, 
-                Field.Index.UN_TOKENIZED)); 
-        this.document.add(new Field("content", this.content, 
-                Field.Store.COMPRESS, Field.Index.UN_TOKENIZED)); 
-        this.document.add(new Field("timestamp", this.timestamp.toString(), 
-                Field.Store.YES, Field.Index.UN_TOKENIZED)); 
- 
-        return this.document; 
- 
-    } 
- 
-    /** 
-     * @return - the wrapped entry 
-     */ 
-    public BaseEntry getEntry() { 
-        return this.entry; 
-    } 
- 
-    /** 
-     * @return - the entry id 
-     */ 
-    public String getEntryId() { 
-        return this.entryId; 
-    } 
- 
-    /** 
-     * @return - the feed id 
-     */ 
-    public String getFeedId() { 
-        return this.feedId; 
-    } 
- 
-    /** 
-     * Storage operations 
-     *  
-     * @author Simon Willnauer 
-     *  
-     */ 
-    public static enum StorageOperation { 
-        /** 
-         * delete 
-         */ 
-        DELETE, 
-        /** 
-         * update 
-         */ 
-        UPDATE, 
-        /** 
-         * insert 
-         */ 
-        INSERT 
-    } 
- 
-    /** 
-     * @return the specified storage operation 
-     */ 
-    public StorageOperation getOperation() { 
-        return this.operation; 
-    } 
- 
-    /** 
-     * @see java.lang.Comparable#compareTo(T) 
-     */ 
-    public int compareTo(StorageEntryWrapper arg0) { 
-        return arg0.timestamp == this.timestamp ? 0 
-                : (arg0.timestamp > this.timestamp ? 1 : -1); 
-    } 
- 
-} 
+ */
+
+package org.apache.lucene.gdata.storage.lucenestorage;
+
+import java.io.IOException;
+import java.io.StringWriter;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.gdata.data.ServerBaseEntry;
+import org.apache.lucene.gdata.server.registry.ProvidedService;
+import org.apache.lucene.gdata.server.registry.ProvidedServiceConfig;
+import org.apache.lucene.gdata.storage.lucenestorage.StorageBuffer.BufferableEntry;
+
+import com.google.gdata.data.BaseEntry;
+import com.google.gdata.util.common.xml.XmlWriter;
+
+/**
+ * This immutable class wrapps <tt>ServerBaseEntry</tt> for an internal
+ * Storage representation of an entry. This class also acts as a Documentfactory
+ * for lucene documents to be stored inside the index.
+ * 
+ * @author Simon Willnauer
+ * 
+ */
+public class StorageEntryWrapper implements Comparable<StorageEntryWrapper>,
+        StorageWrapper {
+
+    private static final long serialVersionUID = -4619985652059888526L;
+
+    private static final String INTERNAL_ENCODING = "UTF-8";
+
+    /**
+     * lucene field name Entry id
+     */
+    public final static String FIELD_ENTRY_ID = "entryId";
+
+    /**
+     * lucene field name feed id
+     */
+    public final static String FIELD_FEED_REFERENCE = "feedReference";
+
+    /**
+     * lucene field name entry content
+     */
+    public final static String FIELD_CONTENT = "content";
+
+    /**
+     * lucene field name creating timestamp
+     */
+    public final static String FIELD_TIMESTAMP = "timestamp";
+
+    private final String entryId;
+
+    private final String feedId;
+
+    private  String content;
+
+    private final ServerBaseEntry entry;
+
+    private Long timestamp;
+
+    private transient Document document;
+
+    private StorageOperation operation;
+
+    private ProvidedService config;
+
+    /**
+     * Creates a new StorageEntryWrapper.
+     * 
+     * @param entry -
+     *            the entry to wrap
+     * 
+     * @param operation -
+     *            the StorageOperation
+     * 
+     * @throws IOException -
+     *             if the entry content can not be generated
+     */
+    public StorageEntryWrapper(final ServerBaseEntry entry,
+            StorageOperation operation) throws IOException {
+
+        this.entry = entry;
+        this.operation = operation;
+        this.entryId = entry.getId();
+        this.feedId = entry.getFeedId();
+        if (operation != StorageOperation.DELETE) {
+            this.config = entry.getServiceConfig();
+            this.content = buildContent();
+         
+        }
+        this.timestamp = new Long(
+                this.entry.getUpdated() != null ? this.entry.getUpdated()
+                        .getValue() : System.currentTimeMillis());
+            
+
+    }
+
+    private String buildContent() throws IOException {
+        StringWriter writer = new StringWriter();
+        XmlWriter xmlWriter = new XmlWriter(writer, INTERNAL_ENCODING);
+        this.entry.generateAtom(xmlWriter, this.config.getExtensionProfile());
+        return writer.toString();
+
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.lucenestorage.StorageWrapper#getLuceneDocument()
+     */
+    public Document getLuceneDocument() {
+        if(this.operation == StorageOperation.DELETE)
+            return null;
+        if (this.document != null)
+            return this.document;
+        this.document = new Document();
+        this.document.add(new Field(FIELD_ENTRY_ID, this.entryId,
+                Field.Store.YES, Field.Index.UN_TOKENIZED));
+        this.document.add(new Field(FIELD_FEED_REFERENCE, this.feedId,
+                Field.Store.YES, Field.Index.UN_TOKENIZED));
+        this.document.add(new Field(FIELD_CONTENT, this.content,
+                Field.Store.COMPRESS, Field.Index.NO));
+        this.document.add(new Field(FIELD_TIMESTAMP, this.timestamp.toString(),
+                Field.Store.YES, Field.Index.UN_TOKENIZED));
+
+        return this.document;
+
+    }
+
+    /**
+     * @return - the wrapped entry
+     */
+    public BaseEntry getEntry() {
+        /*
+         * this wrapps the entry again. BufferableEntry creates a new instance
+         * for the dynamic element like links.
+         */
+        return new BufferableEntry(this.entry.getEntry());
+    }
+
+    /**
+     * @return - the entry id
+     */
+    public String getEntryId() {
+        return this.entryId;
+    }
+
+    /**
+     * @return - the feed id
+     */
+    public String getFeedId() {
+        return this.feedId;
+    }
+
+    /**
+     * Storage operations
+     * 
+     * @author Simon Willnauer
+     * 
+     */
+    public static enum StorageOperation {
+        /**
+         * delete
+         */
+        DELETE,
+        /**
+         * update
+         */
+        UPDATE,
+        /**
+         * insert
+         */
+        INSERT
+    }
+
+    /**
+     * @return the specified storage operation
+     */
+    public StorageOperation getOperation() {
+        return this.operation;
+    }
+
+    /**
+     * This compare method compares the timestamps of the wrapper instances.
+     * 
+     * @param arg0 -
+     *            the wrapper to compare
+     * @par
+     * @return - 0 if the wrappers timestamp are the same, an integer > 0 if the
+     *         given wrapper is after this wrapper
+     * 
+     */
+    public int compareTo(StorageEntryWrapper arg0) {
+        return arg0.timestamp == this.timestamp ? 0
+                : (arg0.timestamp > this.timestamp ? 1 : -1);
+    }
+
+    /**
+     * @return - the specified {@link ProvidedServiceConfig}
+     */
+    public ProvidedService getConfigurator() {
+        return this.config;
+    }
+
+    /**
+     * @return Returns the timestamp.
+     */
+    public Long getTimestamp() {
+        return this.timestamp;
+    }
+
+}

Modified: lucene/java/trunk/contrib/gdata-server/src/java/org/apache/lucene/gdata/storage/lucenestorage/StorageImplementation.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/gdata-server/src/java/org/apache/lucene/gdata/storage/lucenestorage/StorageImplementation.java?rev=417265&r1=417264&r2=417265&view=diff
==============================================================================
--- lucene/java/trunk/contrib/gdata-server/src/java/org/apache/lucene/gdata/storage/lucenestorage/StorageImplementation.java (original)
+++ lucene/java/trunk/contrib/gdata-server/src/java/org/apache/lucene/gdata/storage/lucenestorage/StorageImplementation.java Mon Jun 26 11:14:53 2006
@@ -1,259 +1,526 @@
-/** 
- * Copyright 2004 The Apache Software Foundation 
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.gdata.storage.lucenestorage;
+
+import java.io.IOException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.lucene.gdata.data.GDataAccount;
+import org.apache.lucene.gdata.data.ServerBaseEntry;
+import org.apache.lucene.gdata.data.ServerBaseFeed;
+import org.apache.lucene.gdata.server.registry.ComponentType;
+import org.apache.lucene.gdata.server.registry.GDataServerRegistry;
+import org.apache.lucene.gdata.storage.ResourceNotFoundException;
+import org.apache.lucene.gdata.storage.Storage;
+import org.apache.lucene.gdata.storage.StorageController;
+import org.apache.lucene.gdata.storage.StorageException;
+import org.apache.lucene.gdata.storage.lucenestorage.StorageEntryWrapper.StorageOperation;
+import org.apache.lucene.gdata.storage.lucenestorage.util.ReferenceCounter;
+
+import com.google.gdata.data.BaseEntry;
+import com.google.gdata.data.BaseFeed;
+
+/**
+ * This is an implementation of the
+ * {@link org.apache.lucene.gdata.storage.Storage} interface. The
+ * StorageImplementation provides access to the
+ * {@link org.apache.lucene.gdata.storage.lucenestorage.StorageQuery} and the
+ * {@link org.apache.lucene.gdata.storage.lucenestorage.StorageModifier}. This
+ * class will be instanciated per client request.
  * 
- * Licensed under the Apache License, Version 2.0 (the "License"); 
- * you may not use this file except in compliance with the License. 
- * You may obtain a copy of the License at 
  * 
- *     http://www.apache.org/licenses/LICENSE-2.0 
  * 
- * Unless required by applicable law or agreed to in writing, software 
- * distributed under the License is distributed on an "AS IS" BASIS, 
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
- * See the License for the specific language governing permissions and 
- * limitations under the License. 
- */ 
- 
-package org.apache.lucene.gdata.storage.lucenestorage; 
- 
-import java.io.IOException; 
-import java.util.List; 
- 
-import org.apache.commons.logging.Log; 
-import org.apache.commons.logging.LogFactory; 
-import org.apache.lucene.gdata.storage.Storage; 
-import org.apache.lucene.gdata.storage.StorageException; 
-import org.apache.lucene.gdata.storage.lucenestorage.StorageEntryWrapper.StorageOperation; 
-import org.apache.lucene.gdata.storage.lucenestorage.util.ReferenceCounter; 
- 
-import com.google.gdata.data.BaseEntry; 
-import com.google.gdata.data.BaseFeed; 
-import com.google.gdata.data.ExtensionProfile; 
-import com.google.gdata.data.Feed; 
- 
-/** 
- * This is an implementation of the 
- * {@link org.apache.lucene.gdata.storage.Storage} interface. The 
- * StorageImplementation provides access to the 
- * {@link org.apache.lucene.gdata.storage.lucenestorage.StorageQuery} and the 
- * {@link org.apache.lucene.gdata.storage.lucenestorage.StorageModifier}. This 
- * class will be instanciated per client request. 
- *  
- *  
- *  
- * @author Simon Willnauer 
- *  
- */ 
-public class StorageImplementation implements Storage { 
-    private final StorageCoreController controller; 
- 
-    private ExtensionProfile profile; 
- 
-    private static final Log LOG = LogFactory 
-            .getLog(StorageImplementation.class); 
- 
-    /** 
-     * Creates a new StorageImplementation 
-     *  
-     * @throws StorageException - 
-     *             if the 
-     *             {@link org.apache.lucene.gdata.storage.StorageController} can 
-     *             not be created 
-     * @throws IOException - 
-     *             if the 
-     *             {@link org.apache.lucene.gdata.storage.StorageController} can 
-     *             not be created 
-     * @see StorageCoreController#getStorageCoreController() 
-     *  
-     */ 
-    public StorageImplementation() throws IOException, StorageException { 
-        this.controller = StorageCoreController.getStorageCoreController(); 
-    } 
- 
-    /** 
-     * @see org.apache.lucene.gdata.storage.Storage#storeEntry(com.google.gdata.data.BaseEntry, 
-     *      java.lang.String) 
-     */ 
-    public BaseEntry storeEntry(BaseEntry entry, String feedId) 
-            throws StorageException { 
-        if (this.profile == null) 
-            throw new StorageException( 
-                    "Can process ExtensionProfile not set -- is null"); 
-        if (feedId == null) 
-            throw new StorageException("No feed ID specified -- is null"); 
-        StorageModifier modifier = this.controller.getStorageModifier(); 
-        String id = this.controller.releaseID(); 
-        entry.setId(feedId + id); 
-        if (LOG.isInfoEnabled()) 
-            LOG.info("Store entry " + id + " -- feed: " + feedId); 
- 
-        try { 
-            StorageEntryWrapper wrapper = new StorageEntryWrapper(entry, 
-                    feedId, StorageOperation.INSERT, this.profile); 
-            modifier.insertEntry(wrapper); 
-        } catch (IOException e) { 
-            StorageException ex = new StorageException("Can't create Entry -- " 
-                    + e.getMessage(), e); 
-            ex.setStackTrace(e.getStackTrace()); 
-            throw ex; 
- 
-        } 
- 
-        return entry; 
-    } 
- 
-    /** 
-     * @see org.apache.lucene.gdata.storage.Storage#deleteEntry(java.lang.String, 
-     *      java.lang.String) 
-     */ 
-    public void deleteEntry(String entryId, String feedId) 
-            throws StorageException { 
-        if (this.profile == null) 
-            throw new StorageException( 
-                    "Can process ExtensionProfile not set -- is null"); 
-        if (feedId == null) 
-            throw new StorageException("No feed ID specified -- is null"); 
-        if (entryId == null) 
-            throw new StorageException("No entry ID specified -- is null"); 
-        if (LOG.isInfoEnabled()) 
-            LOG.info("delete entry " + entryId + " -- feed: " + feedId); 
-        StorageModifier modifier = this.controller.getStorageModifier(); 
-        modifier.deleteEntry(entryId, feedId); 
-    } 
- 
-    /** 
-     * @see org.apache.lucene.gdata.storage.Storage#updateEntry(com.google.gdata.data.BaseEntry, 
-     *      java.lang.String) 
-     */ 
-    public BaseEntry updateEntry(BaseEntry entry, String feedId) 
-            throws StorageException { 
-        if (this.profile == null) 
-            throw new StorageException( 
-                    "Can process ExtensionProfile not set -- is null"); 
-        if (feedId == null) 
-            throw new StorageException("No feed ID specified -- is null"); 
-        if (entry == null) 
-            throw new StorageException("enrty is null"); 
-        if (entry.getId() == null) 
-            throw new StorageException("No entry ID specified -- is null"); 
-        if (LOG.isInfoEnabled()) 
-            LOG.info("update entry " + entry.getId() + " -- feed: " + feedId); 
-        StorageModifier modifier = this.controller.getStorageModifier(); 
- 
-        try { 
-            StorageEntryWrapper wrapper = new StorageEntryWrapper(entry, 
-                    feedId, StorageOperation.UPDATE, this.profile); 
-            modifier.updateEntry(wrapper); 
-        } catch (IOException e) { 
-            LOG.error("Can't update entry for feedID: " + feedId 
-                    + "; entryId: " + entry.getId() + " -- " + e.getMessage(), 
-                    e); 
-            StorageException ex = new StorageException("Can't create Entry -- " 
-                    + e.getMessage(), e); 
-            ex.setStackTrace(e.getStackTrace()); 
-            throw ex; 
- 
-        } 
- 
-        return entry; 
- 
-    } 
- 
-    /** 
-     * @see org.apache.lucene.gdata.storage.Storage#getFeed(java.lang.String, 
-     *      int, int) 
-     */ 
-    @SuppressWarnings("unchecked") 
-    public BaseFeed getFeed(String feedId, int startIndex, int resultCount) 
-            throws StorageException { 
-        if (this.profile == null) 
-            throw new StorageException( 
-                    "Can process ExtensionProfile not set -- is null"); 
-        if (feedId == null) 
-            throw new StorageException("No feed ID specified -- is null"); 
-        if (LOG.isInfoEnabled()) 
-            LOG.info("get feed: " + feedId + " startindex: " + startIndex 
-                    + " resultCount: " + resultCount); 
-        ReferenceCounter<StorageQuery> query = null; 
-        try { 
-            query = this.controller.getStorageQuery(); 
-            List<BaseEntry> resultList = query.get().getLatestFeedQuery(feedId, 
-                    resultCount, startIndex, this.profile); 
-            BaseFeed feed = new Feed(); 
-            feed.getEntries().addAll(resultList); 
-            return feed; 
-        } catch (Exception e) { 
-            LOG.error("Can't get latest feed for feedID: " + feedId + " -- " 
-                    + e.getMessage(), e); 
-            StorageException ex = new StorageException("Can't create Entry -- " 
-                    + e.getMessage(), e); 
-            ex.setStackTrace(e.getStackTrace()); 
-            throw ex; 
- 
-        } finally { 
-            if (query != null) 
-                query.decrementRef(); 
-        } 
- 
-    } 
- 
-    /** 
-     * @see org.apache.lucene.gdata.storage.Storage#getEntry(java.lang.String, 
-     *      java.lang.String) 
-     */ 
-    public BaseEntry getEntry(String entryId, String feedId) 
-            throws StorageException { 
-        if (this.profile == null) 
-            throw new StorageException( 
-                    "Can process ExtensionProfile not set -- is null"); 
-        if (feedId == null) 
-            throw new StorageException("No feed ID specified -- is null"); 
-        if (entryId == null) 
-            throw new StorageException("No entry ID specified -- is null"); 
-        if (LOG.isInfoEnabled()) 
-            LOG.info("get entry " + entryId + " -- feed: " + feedId); 
-        ReferenceCounter<StorageQuery> query = null; 
-        try { 
-            query = this.controller.getStorageQuery(); 
-            return query.get().singleEntryQuery(entryId, feedId, this.profile); 
-        } catch (Exception e) { 
-            LOG.error("Can't get entry for feedID: " + feedId + "; entryId: " 
-                    + entryId + " -- " + e.getMessage(), e); 
-            StorageException ex = new StorageException("Can't create Entry -- " 
-                    + e.getMessage(), e); 
-            ex.setStackTrace(e.getStackTrace()); 
-            throw ex; 
- 
-        } finally { 
-            if (query != null) 
-                query.decrementRef(); 
-        } 
- 
-    } 
- 
-    /** 
-     * @see org.apache.lucene.gdata.storage.Storage#getEntries(java.util.List, 
-     *      java.lang.String) 
-     */ 
-    public List<BaseEntry> getEntries(List<String> entryIdList, String feedId) 
-            throws StorageException { 
-        throw new StorageException("not implemented yet"); 
-        // TODO implement this 
-     
-    } 
- 
-    /** 
-     * @see org.apache.lucene.gdata.storage.Storage#close() 
-     */ 
-    public void close() { 
-        // 
-    } 
- 
-    /** 
-     * @see org.apache.lucene.gdata.storage.Storage#setExtensionProfile(com.google.gdata.data.ExtensionProfile) 
-     */ 
-    public void setExtensionProfile(ExtensionProfile profile) { 
-        this.profile = profile; 
-    } 
- 
-} 
+ * @author Simon Willnauer
+ * 
+ */
+public class StorageImplementation implements Storage {
+    private final StorageCoreController controller;
+
+    private static final Log LOG = LogFactory
+            .getLog(StorageImplementation.class);
+
+    /**
+     * Creates a new StorageImplementation
+     * 
+     * @throws StorageException -
+     *             if the storage controller can not be obtained
+     * 
+     * 
+     * 
+     */
+    public StorageImplementation() throws StorageException {
+        this.controller = (StorageCoreController) GDataServerRegistry
+                .getRegistry().lookup(StorageController.class,
+                        ComponentType.STORAGECONTROLLER);
+        if (this.controller == null)
+            throw new StorageException("Can't get registered StorageController");
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.Storage#storeEntry(org.apache.lucene.gdata.data.ServerBaseEntry)
+     */
+    public BaseEntry storeEntry(final ServerBaseEntry entry)
+            throws StorageException {
+
+        if (entry == null)
+            throw new StorageException("entry is null");
+        StorageModifier modifier = this.controller.getStorageModifier();
+        String id = this.controller.releaseID();
+        entry.setId(entry.getFeedId() + id);
+        if (LOG.isInfoEnabled())
+            LOG.info("Store entry " + id + " -- feed: " + entry.getFeedId());
+
+        try {
+            StorageEntryWrapper wrapper = new StorageEntryWrapper(entry,
+                    StorageOperation.INSERT);
+            modifier.insertEntry(wrapper);
+        } catch (IOException e) {
+            StorageException ex = new StorageException("Can't create Entry -- "
+                    + e.getMessage(), e);
+            ex.setStackTrace(e.getStackTrace());
+            throw ex;
+
+        }
+
+        return entry.getEntry();
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.Storage#deleteEntry(org.apache.lucene.gdata.data.ServerBaseEntry)
+     */
+    public void deleteEntry(final ServerBaseEntry entry)
+            throws StorageException {
+
+        if (entry == null)
+            throw new StorageException("Entry is null");
+
+        if (LOG.isInfoEnabled())
+            LOG.info("delete entry " + entry.getId() + " -- feed: "
+                    + entry.getFeedId());
+        StorageModifier modifier = this.controller.getStorageModifier();
+        ReferenceCounter<StorageQuery> query = this.controller.getStorageQuery();
+        try{
+        if(query.get().isEntryStored(entry.getId(),entry.getFeedId())){
+            
+            modifier.deleteEntry(new StorageEntryWrapper(entry,StorageOperation.DELETE));
+        }
+        else
+            throw new ResourceNotFoundException("Entry for entry id: "+entry.getId()+" is not stored");
+        }catch (IOException e) {
+            throw new StorageException("Can not access storage");
+        }finally{
+            query.decrementRef();
+        }
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.Storage#updateEntry(org.apache.lucene.gdata.data.ServerBaseEntry)
+     */
+    public BaseEntry updateEntry(ServerBaseEntry entry) throws StorageException {
+
+        if (entry == null)
+            throw new StorageException("entry is null");
+        if(entry.getId() == null)
+            throw new StorageException("entry id is null");
+        if(entry.getFeedId() == null)
+            throw new StorageException("feed id is null");
+        if (LOG.isInfoEnabled())
+            LOG.info("update entry " + entry.getId() + " -- feed: "
+                    + entry.getFeedId());
+        StorageModifier modifier = this.controller.getStorageModifier();
+        ReferenceCounter<StorageQuery> query = this.controller.getStorageQuery();
+        try {
+            StorageEntryWrapper wrapper = new StorageEntryWrapper(entry,
+                    StorageOperation.UPDATE);
+            if(query.get().isEntryStored(entry.getId(),entry.getFeedId()))
+                modifier.updateEntry(wrapper);
+            else
+                throw new ResourceNotFoundException("Entry for entry id: "+entry.getId()+" is not stored");
+            
+        } catch (IOException e) {
+            LOG.error("Can't update entry for feedID: " + entry.getFeedId()
+                    + "; entryId: " + entry.getId() + " -- " + e.getMessage(),
+                    e);
+            StorageException ex = new StorageException("Can't create Entry -- "
+                    + e.getMessage(), e);
+            ex.setStackTrace(e.getStackTrace());
+            throw ex;
+
+        }
+
+        return entry.getEntry();
+
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.Storage#getFeed(org.apache.lucene.gdata.data.ServerBaseFeed)
+     */
+    @SuppressWarnings("unchecked")
+    public BaseFeed getFeed(final ServerBaseFeed feed) throws StorageException {
+
+        if (feed == null)
+            throw new StorageException("feed is null");
+        if (LOG.isInfoEnabled())
+            LOG.info("get feed: " + feed.getId() + " startindex: "
+                    + feed.getStartIndex() + " resultCount: "
+                    + feed.getItemsPerPage());
+        ReferenceCounter<StorageQuery> query = null;
+        try {
+            query = this.controller.getStorageQuery();
+            BaseFeed retVal = query.get().getLatestFeedQuery(feed.getId(),
+                    feed.getItemsPerPage(), feed.getStartIndex(),
+                    feed.getServiceConfig());
+            return retVal;
+        } catch (Exception e) {
+            LOG.error("Can't get latest feed for feedID: " + feed.getId()
+                    + " -- " + e.getMessage(), e);
+            StorageException ex = new StorageException("Can't create Entry -- "
+                    + e.getMessage(), e);
+            ex.setStackTrace(e.getStackTrace());
+            throw ex;
+
+        } finally {
+            if (query != null)
+                query.decrementRef();
+        }
+
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.Storage#getEntry(org.apache.lucene.gdata.data.ServerBaseEntry)
+     */
+    public BaseEntry getEntry(final ServerBaseEntry entry)
+            throws StorageException {
+
+        if (entry == null)
+            throw new StorageException("No entry  specified -- is null");
+        if (LOG.isInfoEnabled())
+            LOG.info("get entry " + entry.getId() + " -- feed: "
+                    + entry.getFeedId());
+        ReferenceCounter<StorageQuery> query = null;
+        try {
+            query = this.controller.getStorageQuery();
+            BaseEntry retVal = query.get().singleEntryQuery(entry.getId(),
+                    entry.getFeedId(), entry.getServiceConfig());
+            if(retVal == null)
+                throw new ResourceNotFoundException("can not get entry for entry ID "+entry.getId());
+            return retVal;
+        } catch (Exception e) {
+            LOG.error("Can't get entry for feedID: " + entry.getFeedId()
+                    + "; entryId: " + entry.getId() + " -- " + e.getMessage(),
+                    e);
+            StorageException ex = new StorageException("Can't create Entry -- "
+                    + e.getMessage(), e);
+            ex.setStackTrace(e.getStackTrace());
+            throw ex;
+
+        } finally {
+            if (query != null)
+                query.decrementRef();
+        }
+
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.Storage#close()
+     */
+    public void close() {
+        //
+    }
+
+    /**
+     * @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 save null Account");
+        ReferenceCounter<StorageQuery> query = null;
+        try {
+            query = this.controller.getStorageQuery();
+            if (query.get().getUser(Account.getName()) != null)
+                throw new StorageException("Account already exists");
+            StorageModifier modifier = this.controller.getStorageModifier();
+            StorageAccountWrapper wrapper = new StorageAccountWrapper(Account);
+            modifier.createAccount(wrapper);
+        } catch (Exception e) {
+            LOG.error("Can't save Account -- " + e.getMessage(), e);
+            StorageException ex = new StorageException("Can't save Account -- "
+                    + e.getMessage(), e);
+            ex.setStackTrace(e.getStackTrace());
+            throw ex;
+
+        } finally {
+            if (query != null)
+                query.decrementRef();
+        }
+    }
+
+    /**
+     * @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 null Account");
+        ReferenceCounter<StorageQuery> query = null;
+        try {
+            query = this.controller.getStorageQuery();
+            if (query.get().getUser(Account.getName()) == null)
+                throw new StorageException("Account does not exist");
+            StorageModifier modifier = this.controller.getStorageModifier();
+            StorageAccountWrapper wrapper = new StorageAccountWrapper(Account);
+            modifier.updateAccount(wrapper);
+        } catch (Exception e) {
+            LOG.error("Can't update Account -- " + e.getMessage(), e);
+            StorageException ex = new StorageException(
+                    "Can't update Account -- " + e.getMessage(), e);
+            ex.setStackTrace(e.getStackTrace());
+            throw ex;
+
+        } finally {
+            if (query != null)
+                query.decrementRef();
+        }
+
+    }
+
+    /**
+     * @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 null Account");
+        ReferenceCounter<StorageQuery> query = null;
+        try {
+            query = this.controller.getStorageQuery();
+            if (query.get().getUser(Accountname) == null)
+                throw new StorageException("Account does not exist");
+            StorageModifier modifier = this.controller.getStorageModifier();
+            modifier.deleteAccount(Accountname);
+        } catch (Exception e) {
+            LOG.error("Can't update Account -- " + e.getMessage(), e);
+            StorageException ex = new StorageException(
+                    "Can't update Account -- " + e.getMessage(), e);
+            ex.setStackTrace(e.getStackTrace());
+            throw ex;
+
+        } finally {
+            if (query != null)
+                query.decrementRef();
+        }
+    }
+
+    /**
+     * @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 insert null feed");
+        if (accountName == null)
+            throw new StorageException("accountName must not be null");
+        ReferenceCounter<StorageQuery> query = null;
+        try {
+            query = this.controller.getStorageQuery();
+            if (query.get().isFeedStored(feed.getId()))
+                throw new StorageException("feed with feedID " + feed.getId()
+                        + " is already stored");
+            StorageModifier modifier = this.controller.getStorageModifier();
+            StorageFeedWrapper wrapper = new StorageFeedWrapper(feed,
+                    accountName);
+            modifier.createFeed(wrapper);
+
+        } catch (Exception e) {
+            LOG.error("Can't create feed -- " + e.getMessage(), e);
+            StorageException ex = new StorageException("Can't create feed -- "
+                    + e.getMessage(), e);
+            ex.setStackTrace(e.getStackTrace());
+            throw ex;
+
+        } finally {
+            if (query != null)
+                query.decrementRef();
+        }
+
+    }
+
+    /**
+     * @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 id is null ");
+        ReferenceCounter<StorageQuery> query = null;
+        try {
+            query = this.controller.getStorageQuery();
+            if (!query.get().isFeedStored(feedId))
+                throw new StorageException("Account does not exist");
+            StorageModifier modifier = this.controller.getStorageModifier();
+
+            modifier.deleteFeed(feedId);
+
+        } catch (Exception e) {
+            LOG.error("Can't delete feed -- " + e.getMessage(), e);
+            StorageException ex = new StorageException("Can't create feed -- "
+                    + e.getMessage(), e);
+            ex.setStackTrace(e.getStackTrace());
+            throw ex;
+
+        } finally {
+            if (query == null)
+                query.decrementRef();
+        }
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.Storage#updateFeed(org.apache.lucene.gdata.data.ServerBaseFeed,
+     *      java.lang.String)
+     */
+    public void updateFeed(ServerBaseFeed feed, String accountName)
+            throws StorageException {
+        if (feed == null)
+            throw new StorageException("can not update null feed");
+        if (accountName == null)
+            throw new StorageException("accountName must not be null");
+        ReferenceCounter<StorageQuery> query = null;
+        try {
+            query = this.controller.getStorageQuery();
+            if (!query.get().isFeedStored(feed.getId()))
+                throw new StorageException("Account does not exist");
+            StorageModifier modifier = this.controller.getStorageModifier();
+            StorageFeedWrapper wrapper = new StorageFeedWrapper(feed,
+                    accountName);
+            modifier.updateFeed(wrapper);
+
+        } catch (Exception e) {
+            LOG.error("Can't create feed -- " + e.getMessage(), e);
+            StorageException ex = new StorageException("Can't create feed -- "
+                    + e.getMessage(), e);
+            ex.setStackTrace(e.getStackTrace());
+            throw ex;
+
+        } finally {
+            if (query == null)
+                query.decrementRef();
+        }
+
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.Storage#getServiceForFeed(java.lang.String)
+     */
+    public String getServiceForFeed(String feedId) throws StorageException {
+        if (feedId == null)
+            throw new StorageException("no feed for the feedID == null");
+        ReferenceCounter<StorageQuery> query = null;
+        try {
+            query = this.controller.getStorageQuery();
+            String type = query.get().getService(feedId);
+            if (type == null)
+                throw new StorageException("no feed for the feedID == "
+                        + feedId + " found");
+            return type;
+        } catch (Exception e) {
+            throw new StorageException("Can not access storage", e);
+        } finally {
+            if (query != null)
+                query.decrementRef();
+        }
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.Storage#getAccount(java.lang.String)
+     */
+    public GDataAccount getAccount(String accountName) throws StorageException {
+        if (accountName == null)
+            throw new StorageException("account name must not be null");
+        ReferenceCounter<StorageQuery> query = null;
+        try {
+            query = this.controller.getStorageQuery();
+            return query.get().getUser(accountName);
+        } catch (Exception e) {
+            throw new StorageException("Can not access storage", e);
+        } finally {
+            if (query != null)
+                query.decrementRef();
+        }
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.Storage#getAccountNameForFeedId(java.lang.String)
+     */
+    public String getAccountNameForFeedId(String feedId)
+            throws StorageException {
+        if (feedId == null)
+            throw new StorageException("feedid must not be null");
+        ReferenceCounter<StorageQuery> query = null;
+        try {
+            query = this.controller.getStorageQuery();
+            String accountName = query.get().getAccountNameForFeedId(feedId);
+            if (accountName == null)
+                throw new StorageException("no feed for feedId " + feedId
+                        + " found");
+            return accountName;
+        } catch (IOException e) {
+            throw new StorageException("Can not access storage - "
+                    + e.getMessage(), e);
+        } finally {
+            if (query != null)
+                query.decrementRef();
+        }
+
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.Storage#getEntryLastModified(java.lang.String, java.lang.String)
+     */
+    public Long getEntryLastModified(String entryId,String feedId) throws StorageException {
+        ReferenceCounter<StorageQuery> query = null;
+        try {
+            query = this.controller.getStorageQuery();
+            return new Long(query.get().getEntryLastModified(entryId,feedId));
+        } catch (IOException e) {
+            throw new StorageException("Can not access storage - "
+                    + e.getMessage(), e);
+        } finally {
+            if (query != null)
+                query.decrementRef();
+        }
+
+        
+    }
+
+    /**
+     * @see org.apache.lucene.gdata.storage.Storage#getFeedLastModified(java.lang.String)
+     */
+    public Long getFeedLastModified(String feedId) throws StorageException {
+        ReferenceCounter<StorageQuery> query = null;
+        try {
+            query = this.controller.getStorageQuery();
+            return new Long(query.get().getFeedLastModified(feedId));
+        } catch (IOException e) {
+            throw new StorageException("Can not access storage - "
+                    + e.getMessage(), e);
+        } finally {
+            if (query != null)
+                query.decrementRef();
+        }
+
+            }
+
+}



Mime
View raw message