alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.mdr.persistence.jdbcimpl;

import org.netbeans.mdr.persistence.btreeimpl.btreestorage.*;
import org.netbeans.mdr.persistence.*;

import java.io.*;
import java.text.*;
import java.util.*;

/**
 * JdbcPrimaryIndex implements primary index storage using JDBC.
 *
 * @author John V. Sichi
 * @version $Id: JdbcPrimaryIndex.java,v 1.2 2004/02/05 04:23:32 jsichi Exp $
 */
class JdbcPrimaryIndex
    extends JdbcSinglevaluedIndex implements MDRCache.OverflowHandler
{
    private final MDRCache cache;

    // NOTE:  most of this class was ripped from BtreeDatabase
    static final int MDR_CACHE_SIZE = 2048;
    static final int MDR_CACHE_THRESHHOLD = 1000;
    
    JdbcPrimaryIndex()
    {
        cache = new MDRCache(MDR_CACHE_SIZE, this, MDR_CACHE_THRESHHOLD);
    }

    // implement MDRCache.OverflowHandler
	public void cacheThreshholdReached(MDRCache cache, int size) 
        throws StorageException
    {
        flushChanges();
    }

    void flushChanges()
        throws StorageException
    {
        // TODO:  optimize with batched JDBC calls where supported
        Iterator iter = cache.iterateDeleted();
        while (iter.hasNext()) {
            super.removeImpl(iter.next());
        }
        iter = cache.getDirty().iterator();
        while (iter.hasNext()) {
            Object key = iter.next();
            super.replaceImpl(key,cache.get(key));
        }
        iter = cache.iterateNew();
        while (iter.hasNext()) {
            Object key = iter.next();
            super.addImpl(key,cache.get(key));
        }
        cache.clearLists();
    }

    private MOFID makeMOFID(Object key) throws StorageException
    {
        if (key instanceof MOFID) {
            return (MOFID) key;
        } else {
            throw new IllegalArgumentException(
                "Argument must be of org.netbeans.mdr.persistence.MOFID type");
        }
    }

    void objectStateChanged(Object key) throws StorageException
    {
        cache.setDirty(makeMOFID(key));
    }

    private boolean exists(MOFID key) throws StorageException
    {
        if (cache.get(key) != null) {   
            return true;
        } else if (cache.isDeleted(key)) {
            return false;
        } else {
            return super.getIfExists(key) != null;
        }
    }
    
    private void noSuchRecord(Object key) throws StorageException
    {
        throw new StorageBadRequestException(
            MessageFormat.format("No record exists with key {0}",
                new Object[] {key} ) );
    }
    
    private void addToCache(MOFID key, Object value) throws StorageException
    {
        cache.put(key, value);
        cache.setNew(key);
    }
    
    private void replaceInCache(MOFID key, Object value)
        throws StorageException
    {
        boolean isNew = cache.isNew(key);
        
        cache.replace(key, value);
        
        if (isNew) {
            cache.setNew(key);
        } else {
            cache.setDirty(key);
        }
    }
    
    // override JdbcSinglevaluedIndex
    public synchronized boolean put(
        Object key,Object value) throws StorageException
    {
        MOFID mKey = makeMOFID(key);
        if (!exists(mKey)) {
            addToCache(mKey,value);
            return false;
        } else {
            replaceInCache(mKey,value);
            return true;
        }
    }
    
    // override JdbcSinglevaluedIndex
    public synchronized void replace(Object key, Object value)
        throws StorageException, StorageBadRequestException
    {
        MOFID mKey = makeMOFID(key);
        
        if (!exists(mKey)) {
            noSuchRecord(mKey);
        }

        replaceInCache(mKey, value);
    }
    
    // override JdbcSinglevaluedIndex
    public Object get(Object key)
        throws StorageException, StorageBadRequestException
    {
        Object retval = getIfExists(key);
        if (retval == null) {
            noSuchRecord(key);
        }

        return retval;
    }

    // override JdbcSinglevaluedIndex
    public Object getObject(Object key, SinglevaluedIndex repos)
        throws StorageException
    {
        return get(key);
    }

    // override JdbcSinglevaluedIndex
    public synchronized Object getIfExists(Object key) throws StorageException
    {
        MOFID mKey = makeMOFID(key);
        Object retval;
        retval = cache.get(mKey);
        if (retval == null) {
            if (cache.isDeleted(mKey)) {
                return null;
            }
            retval = super.getIfExists(mKey);
            if (retval != null) {
                cache.put(mKey, retval);
            }
        }

        return retval;
    }
    
    // override JdbcSinglevaluedIndex
    public Object getObjectIfExists(Object key, SinglevaluedIndex repos)
        throws StorageException
    {
        return getIfExists(key);
    }

    // NOTE:  it's a pain to implement keySet() and values() correctly,
    // and as far as I can tell they're never used for primary indexes
    
    // override JdbcSinglevaluedIndex
    public Collection values()
        throws StorageException
    {
        throw new RuntimeException("oops, not yet implemented");
    }
    
    // override JdbcIndex
    public Set keySet() throws StorageException
    {
        throw new RuntimeException("oops, not yet implemented");
    }

    // override JdbcIndex
    public synchronized void add(
        Object key, Object value) throws StorageException
    {
        MOFID mKey = makeMOFID(key);
        if (exists(mKey)) {
            throw new StorageBadRequestException(
                MessageFormat.format("Record with key {0} already exists",
                    new Object[] {mKey} ) );
        }
        addToCache(mKey, value);
    }
    
    // override JdbcIndex
    public synchronized boolean remove(Object key) throws StorageException
    {
        MOFID mKey = makeMOFID(key);
        if (!exists(mKey)) {
            return false;
        } else {
            cache.remove(mKey);
            return true;
        }
    }
}

// End JdbcPrimaryIndex.java
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.