You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@locus.apache.org on 2000/11/30 18:34:19 UTC
cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/util/collections MultiMap.java SimpleHashtable.java package.html
costin 00/11/30 09:34:19
Modified: src/share/org/apache/tomcat/request SimpleMapper1.java
src/share/org/apache/tomcat/util PrefixMapper.java
Added: src/share/org/apache/tomcat/util/collections MultiMap.java
SimpleHashtable.java package.html
Removed: src/share/org/apache/tomcat/util SimpleHashtable.java
Log:
Start refactoring of MimeHeaders, params and cookies.
MultiMap is a subset of MimeHeaders ( same data representation, but only
"generic" methods ) and will be the base of all 3 collections.
( we can add a hash and other optimizations, but after the refactoring is done)
Also, moved SimpleHashtable to tomcat.util.collections - reduce the
name polution in tomcat.util
Revision Changes Path
1.26 +1 -0 jakarta-tomcat/src/share/org/apache/tomcat/request/SimpleMapper1.java
Index: SimpleMapper1.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/request/SimpleMapper1.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- SimpleMapper1.java 2000/11/30 04:58:48 1.25
+++ SimpleMapper1.java 2000/11/30 17:34:12 1.26
@@ -63,6 +63,7 @@
import org.apache.tomcat.core.*;
import org.apache.tomcat.helper.*;
import org.apache.tomcat.util.*;
+import org.apache.tomcat.util.collections.*;
import java.util.*;
/**
1.5 +5 -3 jakarta-tomcat/src/share/org/apache/tomcat/util/PrefixMapper.java
Index: PrefixMapper.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/PrefixMapper.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- PrefixMapper.java 2000/11/30 04:58:57 1.4
+++ PrefixMapper.java 2000/11/30 17:34:12 1.5
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/PrefixMapper.java,v 1.4 2000/11/30 04:58:57 costin Exp $
- * $Revision: 1.4 $
- * $Date: 2000/11/30 04:58:57 $
+ * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/PrefixMapper.java,v 1.5 2000/11/30 17:34:12 costin Exp $
+ * $Revision: 1.5 $
+ * $Date: 2000/11/30 17:34:12 $
*
* ====================================================================
*
@@ -63,6 +63,8 @@
package org.apache.tomcat.util;
+
+import org.apache.tomcat.util.collections.*;
import java.net.URL;
import java.io.File;
1.1 jakarta-tomcat/src/share/org/apache/tomcat/util/collections/MultiMap.java
Index: MultiMap.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.tomcat.util.collections;
import org.apache.tomcat.util.MessageBytes;
import java.io.*;
import java.util.*;
import java.text.*;
// Originally MimeHeaders
/**
* An efficient representation for certain type of map. The keys
* can have a single or multi values, but most of the time there are
* single values.
*
* The data is of "MessageBytes" type, meaning bytes[] that can be
* converted to Strings ( if needed, and encoding is lazy-binded ).
*
* This is a base class for MimeHeaders, Parameters and Cookies.
*
* Data structures: each field is a single-valued key/value.
* The fields are allocated when needed, and are recycled.
* The current implementation does linear search, in future we'll
* also use the hashkey.
*
* @author dac@eng.sun.com
* @author James Todd [gonzo@eng.sun.com]
* @author Costin Manolache
*/
public class MultiMap {
protected Field[] fields;
// fields in use
protected int count;
/**
*
*/
public MultiMap(int initial_size) {
fields=new Field[initial_size];
}
/**
* Clears all header fields.
*/
public void recycle() {
for (int i = 0; i < count; i++) {
fields[i].recycle();
}
count = 0;
}
// -------------------- Idx access to headers ----------
// This allows external iterators.
/**
* Returns the current number of header fields.
*/
public int size() {
return count;
}
/**
* Returns the Nth header name
* This may be used to iterate through all header fields.
*
* An exception is thrown if the index is not valid ( <0 or >size )
*/
public MessageBytes getName(int n) {
// n >= 0 && n < count ? headers[n].getName() : null
return fields[n].name;
}
/**
* Returns the Nth header value
* This may be used to iterate through all header fields.
*/
public MessageBytes getValue(int n) {
return fields[n].value;
}
/** Find the index of a field with the given name.
*/
public int find( String name, int starting ) {
// We can use a hash - but it's not clear how much
// benefit you can get - there is an overhead
// and the number of headers is small (4-5 ?)
// Another problem is that we'll pay the overhead
// of constructing the hashtable
// A custom search tree may be better
for (int i = starting; i < count; i++) {
if (fields[i].name.equals(name)) {
return i;
}
}
return -1;
}
/** Find the index of a field with the given name.
*/
public int findIgnoreCase( String name, int starting ) {
// We can use a hash - but it's not clear how much
// benefit you can get - there is an overhead
// and the number of headers is small (4-5 ?)
// Another problem is that we'll pay the overhead
// of constructing the hashtable
// A custom search tree may be better
for (int i = starting; i < count; i++) {
if (fields[i].name.equalsIgnoreCase(name)) {
return i;
}
}
return -1;
}
/**
* Removes the field at the specified position.
*
* MultiMap will preserve the order of field add unless remove()
* is called. This is not thread-safe, and will invalidate all
* iterators.
*
* This is not a frequent operation for Headers and Parameters -
* there are better ways ( like adding a "isValid" field )
*/
public void remove( int i ) {
// reset and swap with last header
Field mh = fields[i];
// reset the field
mh.recycle();
fields[i] = fields[count - 1];
fields[count - 1] = mh;
count--;
}
/** Create a new, unitialized entry.
*/
public int addField() {
int len = fields.length;
int pos=count;
if (count >= len) {
// expand header list array
Field tmp[] = new Field[pos * 2];
System.arraycopy(fields, 0, tmp, 0, len);
fields = tmp;
}
if (fields[pos] == null) {
fields[pos] = new Field();
}
count++;
return pos;
}
public MessageBytes get( String name) {
for (int i = 0; i < count; i++) {
if (fields[i].name.equals(name)) {
return fields[i].value;
}
}
return null;
}
public int findFirst( String name ) {
for (int i = 0; i < count; i++) {
if (fields[i].name.equals(name)) {
return i;
}
}
return -1;
}
public int findNext( int startPos ) {
int next= fields[startPos].nextPos;
if( next != Field.NEED_NEXT ) {
return next;
}
// next==NEED_NEXT, we never searched for this header
MessageBytes name=fields[startPos].name;
for (int i = startPos; i < count; i++) {
if (fields[i].name.equals(name)) {
// cache the search result
fields[startPos].nextPos=i;
return i;
}
}
fields[startPos].nextPos= Field.LAST;
return -1;
}
// -------------------- Internal representation --------------------
final class Field {
MessageBytes name;
MessageBytes value;
// Extra info for speed
// multiple fields with same name - a linked list will
// speed up multiple name enumerations and search.
static final int NEED_NEXT=-2;
static final int LAST=-1;
int nextPos;
// hashkey
int hash;
Field nextSameHash;
Field() {
nextPos=NEED_NEXT;
}
void recycle() {
name.recycle();
value.recycle();
nextPos=NEED_NEXT;
}
}
}
1.1 jakarta-tomcat/src/share/org/apache/tomcat/util/collections/SimpleHashtable.java
Index: SimpleHashtable.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.tomcat.util.collections;
import java.util.*;
/* **************************************** Stolen from Crimson ******************** */
/* From Crimson/Parser - in a perfect world we'll just have a common set of
utilities, and all apache project will just use those.
*/
// can't be replaced using a Java 2 "Collections" API
// since this package must also run on JDK 1.1
/**
* This class implements a special purpose hashtable. It works like a
* normal <code>java.util.Hashtable</code> except that: <OL>
*
* <LI> Keys to "get" are strings which are known to be interned,
* so that "==" is used instead of "String.equals". (Interning
* could be document-relative instead of global.)
*
* <LI> It's not synchronized, since it's to be used only by
* one thread at a time.
*
* <LI> The keys () enumerator allocates no memory, with live
* updates to the data disallowed.
*
* <LI> It's got fewer bells and whistles: fixed threshold and
* load factor, no JDK 1.2 collection support, only keys can be
* enumerated, things can't be removed, simpler inheritance; more.
*
* </OL>
*
* <P> The overall result is that it's less expensive to use these in
* performance-critical locations, in terms both of CPU and memory,
* than <code>java.util.Hashtable</code> instances. In this package
* it makes a significant difference when normalizing attributes,
* which is done for each start-element construct.
*
* @version $Revision: 1.1 $
*/
public final class SimpleHashtable implements Enumeration
{
// entries ...
private Entry table[];
// currently enumerated key
private Entry current = null;
private int currentBucket = 0;
private int count;
private int threshold;
private static final float loadFactor = 0.75f;
/**
* Constructs a new, empty hashtable with the specified initial
* capacity.
*
* @param initialCapacity the initial capacity of the hashtable.
*/
public SimpleHashtable(int initialCapacity) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
if (initialCapacity==0)
initialCapacity = 1;
table = new Entry[initialCapacity];
threshold = (int)(initialCapacity * loadFactor);
}
/**
* Constructs a new, empty hashtable with a default capacity.
*/
public SimpleHashtable() {
this(11);
}
/**
*/
public void clear ()
{
count = 0;
currentBucket = 0;
current = null;
for (int i = 0; i < table.length; i++)
table [i] = null;
}
/**
* Returns the number of keys in this hashtable.
*
* @return the number of keys in this hashtable.
*/
public int size() {
return count;
}
/**
* Returns an enumeration of the keys in this hashtable.
*
* @return an enumeration of the keys in this hashtable.
* @see Enumeration
*/
public Enumeration keys() {
currentBucket = 0;
current = null;
return this;
}
/**
* Used to view this as an enumeration; returns true if there
* are more keys to be enumerated.
*/
public boolean hasMoreElements ()
{
if (current != null)
return true;
while (currentBucket < table.length) {
current = table [currentBucket++];
if (current != null)
return true;
}
return false;
}
/**
* Used to view this as an enumeration; returns the next key
* in the enumeration.
*/
public Object nextElement ()
{
Object retval;
if (current == null)
throw new IllegalStateException ();
retval = current.key;
current = current.next;
return retval;
}
/**
* Returns the value to which the specified key is mapped in this hashtable.
*/
public Object getInterned (String key) {
Entry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && (e.key == key))
return e.value;
}
return null;
}
/**
* Returns the value to which the specified key is mapped in this
* hashtable ... the key isn't necessarily interned, though.
*/
public Object get(String key) {
Entry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key))
return e.value;
}
return null;
}
/**
* Increases the capacity of and internally reorganizes this
* hashtable, in order to accommodate and access its entries more
* efficiently. This method is called automatically when the
* number of keys in the hashtable exceeds this hashtable's capacity
* and load factor.
*/
private void rehash() {
int oldCapacity = table.length;
Entry oldMap[] = table;
int newCapacity = oldCapacity * 2 + 1;
Entry newMap[] = new Entry[newCapacity];
threshold = (int)(newCapacity * loadFactor);
table = newMap;
/*
System.out.pr intln("rehash old=" + oldCapacity
+ ", new=" + newCapacity
+ ", thresh=" + threshold
+ ", count=" + count);
*/
for (int i = oldCapacity ; i-- > 0 ;) {
for (Entry old = oldMap[i] ; old != null ; ) {
Entry e = old;
old = old.next;
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
e.next = newMap[index];
newMap[index] = e;
}
}
}
/**
* Maps the specified <code>key</code> to the specified
* <code>value</code> in this hashtable. Neither the key nor the
* value can be <code>null</code>.
*
* <P>The value can be retrieved by calling the <code>get</code> method
* with a key that is equal to the original key.
*/
public Object put(Object key, Object value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry e = tab[index] ; e != null ; e = e.next) {
// if ((e.hash == hash) && e.key.equals(key)) {
if ((e.hash == hash) && (e.key == key)) {
Object old = e.value;
e.value = value;
return old;
}
}
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
rehash();
tab = table;
index = (hash & 0x7FFFFFFF) % tab.length;
}
// Creates the new entry.
Entry e = new Entry(hash, key, value, tab[index]);
tab[index] = e;
count++;
return null;
}
public void remove(Object o) {
}
/**
* Hashtable collision list.
*/
private static class Entry {
int hash;
Object key;
Object value;
Entry next;
protected Entry(int hash, Object key, Object value, Entry next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
}
}
1.1 jakarta-tomcat/src/share/org/apache/tomcat/util/collections/package.html
Index: package.html
===================================================================
<html>
<head>
<title>util.collections</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<h1>Specialized collections</h1>
This package includes a number of special collections, tunned for server-side
applications.
The utils are not tomcat specific, but use MessageBytes and few other
top-level utils.
</body>
</html>