broke out the object cache functionality; generalized ReadOnlyVector;

finally implemented background sweeps of unreferenced cache objects;
anonymous user, when viewing profiles, now sees them as if all "privacy"
flags were switched on
This commit is contained in:
Eric J. Bowersox
2001-03-26 06:11:11 +00:00
parent 15a7fa56d2
commit 7e72407b84
29 changed files with 601 additions and 233 deletions

View File

@@ -0,0 +1,72 @@
/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.util.collections;
import java.util.*;
public class ReadOnlyVector extends AbstractList
{
/*--------------------------------------------------------------------------------
* Attributes
*--------------------------------------------------------------------------------
*/
private Vector my_vec; // local vector
/*--------------------------------------------------------------------------------
* Constructor
*--------------------------------------------------------------------------------
*/
public ReadOnlyVector(Vector vec)
{
my_vec = vec;
my_vec.trimToSize();
} // end constructor
/*--------------------------------------------------------------------------------
* finalize() method
*--------------------------------------------------------------------------------
*/
protected void finalize() throws Throwable
{
my_vec = null;
super.finalize();
} // end finalize
/*--------------------------------------------------------------------------------
* Implementations from superclass AbstractList
*--------------------------------------------------------------------------------
*/
public Object get(int index)
{
return my_vec.elementAt(index);
} // end get
public int size()
{
return my_vec.size();
} // end size
} // end class ReadOnlyVector

View File

@@ -0,0 +1,162 @@
/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.util.rcache;
import java.util.*;
import com.silverwrist.util.collections.*;
public class ReferenceCache
{
/*--------------------------------------------------------------------------------
* Attributes
*--------------------------------------------------------------------------------
*/
private HashMap the_data;
/*--------------------------------------------------------------------------------
* Constructor
*--------------------------------------------------------------------------------
*/
public ReferenceCache()
{
the_data = new HashMap();
} // end constructor
/*--------------------------------------------------------------------------------
* External operations
*--------------------------------------------------------------------------------
*/
public synchronized ReferencedData get(Object key)
{
ReferencedData rc = (ReferencedData)(the_data.get(key));
if (rc!=null)
rc.rd_addRef();
return rc;
} // end get
public synchronized ReferencedData getOrCreate(Object key, ReferencedDataBuilder builder)
throws ReferencedDataBuilderException
{
ReferencedData rc = (ReferencedData)(the_data.get(key));
if (rc==null)
{ // use the builder to build one
rc = builder.build(key);
if (rc!=null)
the_data.put(key,rc);
} // end if
else // just add a reference
rc.rd_addRef();
return rc;
} // end getOrCreate
public void register(ReferencedData data)
{
Object key = data.rd_getKey();
synchronized (this)
{ // see if it's in the cache, if not, add it
if (the_data.get(key)!=null)
throw new ReferenceCacheException("object already in cache",key);
// dump it in the object cache
the_data.put(key,data);
} // end synchronized block
} // end register
public synchronized void detach(Object key)
{
the_data.remove(key);
} // end detach
public void sweep()
{
int count = 0;
synchronized (this)
{ // bail out early if possible
if (the_data.size()==0)
return;
// provide a storage bin for all keys we decide to 86
Object keyzap[] = new Object[the_data.size()];
Iterator it = the_data.values().iterator();
while (it.hasNext())
{ // check each value we contain in turn
ReferencedData rd = (ReferencedData)(it.next());
if (rd.rd_unreferenced())
keyzap[count++] = rd.rd_getKey();
} // end while
for (int i=0; i<count; i++)
the_data.remove(keyzap[i]);
} // end synchronized block
} // end sweep
public List sweepReturn()
{
Vector rc = new Vector();
int count = 0;
synchronized (this)
{ // bail out early if possible
if (the_data.size()>0)
{ // provide a storage bin for all keys we decide to 86
Object keyzap[] = new Object[the_data.size()];
Iterator it = the_data.values().iterator();
while (it.hasNext())
{ // check each value we contain in turn
ReferencedData rd = (ReferencedData)(it.next());
if (rd.rd_unreferenced())
keyzap[count++] = rd.rd_getKey();
else
{ // add another reference and tack it onto the list
rd.rd_addRef();
rc.add(rd);
} // end else
} // end while
for (int i=0; i<count; i++)
the_data.remove(keyzap[i]);
} // end if
} // end synchronized block
return new ReadOnlyVector(rc);
} // end sweepReturn
} // end ReferenceCache

View File

@@ -0,0 +1,52 @@
/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.util.rcache;
public class ReferenceCacheException extends RuntimeException
{
/*--------------------------------------------------------------------------------
* Attributes
*--------------------------------------------------------------------------------
*/
private Object keyval;
/*--------------------------------------------------------------------------------
* Constructor
*--------------------------------------------------------------------------------
*/
public ReferenceCacheException(String msg, Object keyval)
{
super("[keyval " + keyval.toString() + "]: " + msg);
this.keyval = keyval;
} // end constructor
/*--------------------------------------------------------------------------------
* External operations
*--------------------------------------------------------------------------------
*/
public Object getKeyVal()
{
return keyval;
} // end getKeyVal
} // end class ReferenceCacheException

View File

@@ -0,0 +1,30 @@
/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.util.rcache;
public interface ReferencedData
{
public abstract int rd_addRef();
public abstract int rd_release();
public abstract boolean rd_unreferenced();
public abstract Object rd_getKey();
} // end interface ReferencedData

View File

@@ -0,0 +1,24 @@
/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.util.rcache;
public interface ReferencedDataBuilder
{
public abstract ReferencedData build(Object key) throws ReferencedDataBuilderException;
} // end interface ReferencedDataBuilder

View File

@@ -0,0 +1,52 @@
/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.util.rcache;
public class ReferencedDataBuilderException extends Exception
{
/*--------------------------------------------------------------------------------
* Attributes
*--------------------------------------------------------------------------------
*/
private Exception target;
/*--------------------------------------------------------------------------------
* Constructors
*--------------------------------------------------------------------------------
*/
public ReferencedDataBuilderException(Exception target)
{
super("Error building new object: " + target.getMessage());
this.target = target;
} // end constructor
/*--------------------------------------------------------------------------------
* External operations
*--------------------------------------------------------------------------------
*/
public Exception getTarget()
{
return target;
} // end getTarget
} // end class ReferencedDataBuilderException