comparison src/org/eclipse/jetty/io/BufferCache.java @ 836:e161dca40760

remove StringMap
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 16 Sep 2016 12:35:17 -0600
parents 3428c60d7cfc
children 3ff59e08a1b7
comparison
equal deleted inserted replaced
835:88b70b8dab9c 836:e161dca40760
18 18
19 package org.eclipse.jetty.io; 19 package org.eclipse.jetty.io;
20 20
21 import java.util.ArrayList; 21 import java.util.ArrayList;
22 import java.util.HashMap; 22 import java.util.HashMap;
23 import java.util.Map.Entry; 23 import java.util.Map;
24 import java.util.TreeMap;
24 25
25 import org.eclipse.jetty.util.StringMap;
26 26
27 /* ------------------------------------------------------------------------------- */ 27 /* ------------------------------------------------------------------------------- */
28 /** 28 /**
29 * Stores a collection of {@link Buffer} objects. 29 * Stores a collection of {@link Buffer} objects.
30 * Buffers are stored in an ordered collection and can retreived by index or value 30 * Buffers are stored in an ordered collection and can retreived by index or value
31 * 31 *
32 */ 32 */
33 public class BufferCache 33 public class BufferCache
34 { 34 {
35 private final HashMap _bufferMap=new HashMap(); 35 private final HashMap _bufferMap=new HashMap();
36 private final StringMap _stringMap=new StringMap(StringMap.CASE_INSENSTIVE); 36 private final TreeMap _stringMap = new TreeMap();
37 private final ArrayList _index= new ArrayList(); 37 private final ArrayList _index= new ArrayList();
38 38
39 /* ------------------------------------------------------------------------------- */ 39 /* ------------------------------------------------------------------------------- */
40 /** Add a buffer to the cache at the specified index. 40 /** Add a buffer to the cache at the specified index.
41 * @param value The content of the buffer. 41 * @param value The content of the buffer.
42 */ 42 */
43 public CachedBuffer add(String value, int ordinal) 43 public CachedBuffer add(String value, int ordinal)
44 { 44 {
45 CachedBuffer buffer= new CachedBuffer(value, ordinal); 45 CachedBuffer buffer= new CachedBuffer(value, ordinal);
46 _bufferMap.put(buffer, buffer); 46 _bufferMap.put(buffer, buffer);
47 _stringMap.put(value, buffer); 47 _stringMap.put(value.toLowerCase(), buffer);
48 while ((ordinal - _index.size()) >= 0) 48 while ((ordinal - _index.size()) >= 0)
49 _index.add(null); 49 _index.add(null);
50 if (_index.get(ordinal)==null) 50 if (_index.get(ordinal)==null)
51 _index.add(ordinal, buffer); 51 _index.add(ordinal, buffer);
52 return buffer; 52 return buffer;
53 } 53 }
54 54
55 public CachedBuffer get(int ordinal) 55 public CachedBuffer get(int ordinal)
56 { 56 {
57 if (ordinal < 0 || ordinal >= _index.size()) 57 if (ordinal < 0 || ordinal >= _index.size())
58 return null; 58 return null;
59 return (CachedBuffer)_index.get(ordinal); 59 return (CachedBuffer)_index.get(ordinal);
60 } 60 }
61 61
62 public CachedBuffer get(Buffer buffer) 62 public CachedBuffer get(Buffer buffer)
63 { 63 {
64 return (CachedBuffer)_bufferMap.get(buffer); 64 return (CachedBuffer)_bufferMap.get(buffer);
65 } 65 }
66 66
67 public CachedBuffer get(String value) 67 public CachedBuffer get(String value)
68 { 68 {
69 return (CachedBuffer)_stringMap.get(value); 69 return (CachedBuffer)_stringMap.get(value.toLowerCase());
70 } 70 }
71 71
72 public Buffer lookup(Buffer buffer) 72 public Buffer lookup(Buffer buffer)
73 { 73 {
74 if (buffer instanceof CachedBuffer) 74 if (buffer instanceof CachedBuffer)
75 return buffer; 75 return buffer;
76 76
77 Buffer b= get(buffer); 77 Buffer b= get(buffer);
78 if (b == null) 78 if (b == null)
79 { 79 {
80 if (buffer instanceof Buffer.CaseInsensitve) 80 if (buffer instanceof Buffer.CaseInsensitve)
81 return buffer; 81 return buffer;
82 return new ByteArrayBuffer.CaseInsensitive(buffer.asArray(),0,buffer.length(),Buffer.IMMUTABLE); 82 return new ByteArrayBuffer.CaseInsensitive(buffer.asArray(),0,buffer.length(),Buffer.IMMUTABLE);
83 } 83 }
84 84
85 return b; 85 return b;
86 } 86 }
87 87
88 public CachedBuffer getBest(byte[] value, int offset, int maxLength) 88 public CachedBuffer getBest(byte[] value, int offset, int maxLength)
89 { 89 {
90 Entry entry = _stringMap.getBestEntry(value, offset, maxLength); 90 String key = new String(value,offset,maxLength).toLowerCase();
91 if (entry!=null) 91 CachedBuffer buffer = (CachedBuffer)_stringMap.get(key);
92 return (CachedBuffer)entry.getValue(); 92 if( buffer != null )
93 return null; 93 return buffer;
94 } 94 Map.Entry floor = _stringMap.floorEntry(key);
95 Map.Entry ceiling = _stringMap.ceilingEntry(key);
96 if( floor==null ) {
97 if( ceiling==null )
98 return null;
99 String ceilingKey = (String)ceiling.getKey();
100 return key.charAt(0) == ceilingKey.charAt(0) ? (CachedBuffer)ceiling.getValue() : null;
101 } else {
102 String floorKey = (String)floor.getKey();
103 if( ceiling==null )
104 return key.charAt(0) == floorKey.charAt(0) ? (CachedBuffer)floor.getValue() : null;
105 String ceilingKey = (String)ceiling.getKey();
106 int n = Math.min( key.length(), Math.min( floorKey.length(), ceilingKey.length() ) );
107 int i = 0;
108 while( ++i <= n && key.regionMatches(0,floorKey,0,i) && key.regionMatches(0,ceilingKey,0,i) );
109 i--;
110 if( i==0 )
111 return null;
112 return i==0 ? null : key.regionMatches(0,floorKey,0,i) ? (CachedBuffer)floor.getValue() : (CachedBuffer)ceiling.getValue();
113 }
114 /*
115 Entry entry = _stringMap.getBestEntry(value, offset, maxLength);
116 if (entry!=null)
117 return (CachedBuffer)entry.getValue();
118 return null;
119 */
120 }
95 121
96 public Buffer lookup(String value) 122 public Buffer lookup(String value)
97 { 123 {
98 Buffer b= get(value); 124 Buffer b= get(value);
99 if (b == null) 125 if (b == null)
100 { 126 {
101 return new CachedBuffer(value,-1); 127 return new CachedBuffer(value,-1);
102 } 128 }
103 return b; 129 return b;
104 } 130 }
105 131
106 public String toString(Buffer buffer) 132 public String toString(Buffer buffer)
107 { 133 {
108 return lookup(buffer).toString(); 134 return lookup(buffer).toString();
109 } 135 }
110 136
111 public int getOrdinal(String value) 137 public int getOrdinal(String value)
112 { 138 {
113 CachedBuffer buffer = (CachedBuffer)_stringMap.get(value); 139 CachedBuffer buffer = (CachedBuffer)_stringMap.get(value.toLowerCase());
114 return buffer==null?-1:buffer.getOrdinal(); 140 return buffer==null?-1:buffer.getOrdinal();
115 } 141 }
116 142
117 public int getOrdinal(Buffer buffer) 143 public int getOrdinal(Buffer buffer)
118 { 144 {
119 if (buffer instanceof CachedBuffer) 145 if (buffer instanceof CachedBuffer)
120 return ((CachedBuffer)buffer).getOrdinal(); 146 return ((CachedBuffer)buffer).getOrdinal();
121 buffer=lookup(buffer); 147 buffer=lookup(buffer);
122 if (buffer!=null && buffer instanceof CachedBuffer) 148 if (buffer!=null && buffer instanceof CachedBuffer)
123 return ((CachedBuffer)buffer).getOrdinal(); 149 return ((CachedBuffer)buffer).getOrdinal();
124 return -1; 150 return -1;
125 } 151 }
126 152
127 public static class CachedBuffer extends ByteArrayBuffer.CaseInsensitive 153 public static class CachedBuffer extends ByteArrayBuffer.CaseInsensitive
128 { 154 {
129 private final int _ordinal; 155 private final int _ordinal;
130 private HashMap _associateMap=null; 156 private HashMap _associateMap=null;
131 157
132 public CachedBuffer(String value, int ordinal) 158 public CachedBuffer(String value, int ordinal)
133 { 159 {
134 super(value); 160 super(value);
135 _ordinal= ordinal; 161 _ordinal= ordinal;
136 } 162 }
137 163
138 public int getOrdinal() 164 public int getOrdinal()
139 { 165 {
140 return _ordinal; 166 return _ordinal;
141 } 167 }
142 168
143 public CachedBuffer getAssociate(Object key) 169 public CachedBuffer getAssociate(Object key)
144 { 170 {
145 if (_associateMap==null) 171 if (_associateMap==null)
146 return null; 172 return null;
147 return (CachedBuffer)_associateMap.get(key); 173 return (CachedBuffer)_associateMap.get(key);
148 } 174 }
149 175
150 // TODO Replace Associate with a mime encoding specific solution 176 // TODO Replace Associate with a mime encoding specific solution
151 public void setAssociate(Object key, CachedBuffer associate) 177 public void setAssociate(Object key, CachedBuffer associate)
152 { 178 {
153 if (_associateMap==null) 179 if (_associateMap==null)
154 _associateMap=new HashMap(); 180 _associateMap=new HashMap();
155 _associateMap.put(key,associate); 181 _associateMap.put(key,associate);
156 } 182 }
157 } 183 }
158 184
159 185
160 @Override 186 @Override
161 public String toString() 187 public String toString()
162 { 188 {
163 return "CACHE["+ 189 return "CACHE["+
164 "bufferMap="+_bufferMap+ 190 "bufferMap="+_bufferMap+
165 ",stringMap="+_stringMap+ 191 ",stringMap="+_stringMap+
166 ",index="+_index+ 192 ",index="+_index+
167 "]"; 193 "]";
168 } 194 }
169 } 195 }