⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 db4ostorage.java

📁 lucene2.2.0版本
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.db4o;import java.util.ArrayList;import java.util.List;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.storage.ModificationConflictException;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 com.db4o.ObjectContainer;import com.db4o.ObjectSet;import com.db4o.query.Query;import com.google.gdata.data.BaseEntry;import com.google.gdata.data.BaseFeed;import com.google.gdata.data.DateTime;/** *  * Storage implementation for the DB4o storage component * @author Simon Willnauer *  */public class DB4oStorage implements Storage {    private static final Log LOG = LogFactory.getLog(DB4oStorage.class);    private static final int RENDER_ACTIVATION_DEPTH = 100;    private final ObjectContainer container;    private final StorageController controller;    private final List<String> semaphore = new ArrayList<String>();        protected DB4oStorage(final ObjectContainer container,            StorageController controller) {        this.container = container;        this.controller = controller;    }    private void createSemaphore(String key)            throws ModificationConflictException {        this.semaphore.add(key);        if (this.container.ext().setSemaphore(key, 0))            return;        throw new ModificationConflictException(                "can not create semaphore for key -- " + key);    }    private void releaseAllSemaphore() {        for (String key : this.semaphore) {            this.container.ext().releaseSemaphore(key);        }        this.semaphore.clear();    }    private void releaseSemaphore(String key) {        if (this.semaphore.contains(key)) {            this.container.ext().releaseSemaphore(key);            this.semaphore.remove(key);        }    }    /**     * @see org.apache.lucene.gdata.storage.Storage#storeEntry(org.apache.lucene.gdata.data.ServerBaseEntry)     */    public BaseEntry storeEntry(ServerBaseEntry entry) throws StorageException {        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();    }    private void setUpdated(ServerBaseEntry entry, 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 void setUpdated(ServerBaseEntry entry,            BaseFeed<BaseFeed, BaseEntry> 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));        }    }    /**     * @see org.apache.lucene.gdata.storage.Storage#deleteEntry(org.apache.lucene.gdata.data.ServerBaseEntry)     */    public void deleteEntry(ServerBaseEntry entry) throws StorageException {        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());        }    }    /**     * @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("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;    }        /**     * @see org.apache.lucene.gdata.storage.Storage#getFeed(org.apache.lucene.gdata.data.ServerBaseFeed)     */    @SuppressWarnings("unchecked")    public BaseFeed getFeed(ServerBaseFeed feed) throws StorageException {        if (feed.getId() == null)            throw new StorageException("can not get feed -- feed id is null");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -