aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/fo/properties/PropertyCache.java
blob: 9201257960d1e0bf33e7d0ac3925093c71531d88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * 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.
 */

/* $Id$ */

package org.apache.fop.fo.properties;

import org.apache.fop.fo.flow.Marker;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 *  Dedicated cache, meant for storing canonical instances
 *  of property-related classes.
 *  The public access points are overloaded <code>fetch()</code> methods
 *  that each correspond to a cached type.
 *  It is designed especially to be used concurrently by multiple threads,
 *  drawing heavily upon the principles behind Java 1.5's
 *  <code>ConcurrentHashMap</code>.
 */
public final class PropertyCache {

    private static final int SEGMENT_COUNT = 32; //0x20
    private static final int INITIAL_BUCKET_COUNT = SEGMENT_COUNT;

    /** bitmask to apply to the hash to get to the
     *  corresponding cache segment */
    private static final int SEGMENT_MASK = SEGMENT_COUNT - 1; //0x1F
    /**
     * Indicates whether the cache should be used at all
     * Can be controlled by the system property:
     *   org.apache.fop.fo.properties.use-cache
     */
    private final boolean useCache;

    /** the segments array (length = 32) */
    private CacheSegment[] segments = new CacheSegment[SEGMENT_COUNT];
    /** the table of hash-buckets */
    private CacheEntry[] table = new CacheEntry[INITIAL_BUCKET_COUNT];

    private Class runtimeType;

    private final boolean[] votesForRehash = new boolean[SEGMENT_COUNT];

    /* same hash function as used by java.util.HashMap */
    private static int hash(Object x) {
        return hash(x.hashCode());
    }

    private static int hash(int hashCode) {
        int h = hashCode;
        h += ~(h << 9);
        h ^= (h >>> 14);
        h += (h << 4);
        h ^= (h >>> 10);
        return h;
    }

    /* shortcut function */
    private static boolean eq(Object p, Object q) {
        return (p == q || (p != null && p.equals(q)));
    }

    /* Class modeling a cached entry */
    private static class CacheEntry extends WeakReference {
        private volatile CacheEntry nextEntry;
        private final int hash;

        /* main constructor */
        public CacheEntry(Object p, CacheEntry nextEntry, ReferenceQueue refQueue) {
            super(p, refQueue);
            this.nextEntry = nextEntry;
            this.hash = hash(p);
        }

        /* main constructor */
        public CacheEntry(Object p, CacheEntry nextEntry) {
            super(p);
            this.nextEntry = nextEntry;
            this.hash = hash(p);
        }

    }

    /* Wrapper objects to synchronize on */
    private static final class CacheSegment {
        CacheSegment() {
        }
        private int count = 0;
        int getCount() {
            return count;
        }
    }

    private void cleanSegment(int segmentIndex) {
        CacheSegment segment = segments[segmentIndex];

        int oldCount = segment.count;

        /* clean all buckets in this segment */
        for (int bucketIndex = segmentIndex;
                    bucketIndex < table.length;
                    bucketIndex += SEGMENT_COUNT) {
            CacheEntry prev = null;
            CacheEntry entry = table[bucketIndex];
            if (entry == null) {
                continue;
            }
            do {
                if (entry.get() == null) {
                    if (prev == null) {
                        table[bucketIndex] = entry.nextEntry;
                    } else {
                        prev.nextEntry = entry.nextEntry;
                    }
                    segment.count--;
                    assert segment.count >= 0;
                } else {
                    prev = entry;
                }
                entry = entry.nextEntry;
            } while (entry != null);
        }

        synchronized (votesForRehash) {
            if (oldCount > segment.count) {
                votesForRehash[segmentIndex] = false;
                return;
            }
            /* cleanup had no effect */
            if (!votesForRehash[segmentIndex]) {
                /* first time for this segment */
                votesForRehash[segmentIndex] = true;
                int voteCount = 0;
                for (int i = SEGMENT_MASK + 1; --i >= 0;) {
                    if (votesForRehash[i]) {
                        voteCount++;
                    }
                }
                if (voteCount > SEGMENT_MASK / 4) {
                    rehash(SEGMENT_MASK);
                    /* reset votes */
                    for (int i = SEGMENT_MASK + 1; --i >= 0;) {
                        votesForRehash[i] = false;
                    }

                }
            }
        }
    }

    /*
     * Puts a new instance in the cache.
     * If the total number of entries for the corresponding
     * segment exceeds twice the amount of hash-buckets, a
     * cleanup will be performed to try and remove obsolete
     * entries.
     */
    private void put(Object o) {

        int hash = hash(o);
        int segmentIndex = hash & SEGMENT_MASK;
        CacheSegment segment = segments[segmentIndex];

        synchronized (segment) {
            int index = hash & (table.length - 1);
            CacheEntry entry = table[index];

            if (entry == null) {
                entry = new CacheEntry(o, null);
                table[index] = entry;
                segment.count++;
            } else {
                Object p = entry.get();
                if (eq(p, o)) {
                    return;
                } else {
                    CacheEntry newEntry = new CacheEntry(o, entry);
                    table[index] = newEntry;
                    segment.count++;
                }
            }

            if (segment.count > (2 * table.length)) {
                  cleanSegment(segmentIndex);
            }
        }
    }


    /* Gets a cached instance. Returns null if not found */
    private Object get(Object o) {

        int hash = hash(o);
        int index = hash & (table.length - 1);

        CacheEntry entry = table[index];
        Object q;

        /* try non-synched first */
        for (CacheEntry e = entry; e != null; e = e.nextEntry) {
            if ( e.hash == hash ) {
                q = e.get();
                if ( ( q != null ) &&  eq ( q, o ) ) {
                    return q;
                }
            }
        }

        /* retry synched, only if the above attempt did not succeed,
         * as another thread may, in the meantime, have added a
         * corresponding entry */
        CacheSegment segment = segments[hash & SEGMENT_MASK];
        synchronized (segment) {
            entry = table[index];
            for (CacheEntry e = entry; e != null; e = e.nextEntry) {
                if ( e.hash == hash ) {
                    q = e.get();
                    if ( ( q != null ) &&  eq ( q, o ) ) {
                        return q;
                    }
                }
            }
        }
        return null;
    }

    /*
     * Recursively acquires locks on all 32 segments,
     * extends the cache and redistributes the entries.
     *
     */
    private void rehash(int index) {

        CacheSegment seg = segments[index];
        synchronized (seg) {
            if (index > 0) {
                /* need to recursively acquire locks on all segments */
                rehash(index - 1);
            } else {
                /* double the amount of buckets */
                int newLength = table.length << 1;
                if (newLength > 0) { //no overflow?
                    /* reset segment counts */
                    for (int i = segments.length; --i >= 0;) {
                        segments[i].count = 0;
                    }

                    CacheEntry[] newTable = new CacheEntry[newLength];

                    int hash, idx;
                    Object o;
                    newLength--;
                    for (int i = table.length; --i >= 0;) {
                        for (CacheEntry c = table[i]; c != null; c = c.nextEntry) {
                            o = c.get();
                            if (o != null) {
                                hash = c.hash;
                                idx = hash & newLength;
                                newTable[idx] = new CacheEntry(o, newTable[idx]);
                                segments[hash & SEGMENT_MASK].count++;
                            }
                        }
                    }
                    table = newTable;
                }
            }
        }
    }

    /**
     *  Default constructor.
     *
     *  @param c    Runtime type of the objects that will be stored in the cache
     */
    public PropertyCache(Class c) {
        this.useCache = Boolean.valueOf(System.getProperty(
                            "org.apache.fop.fo.properties.use-cache", "true")
                        ).booleanValue();
        if (useCache) {
            for (int i = SEGMENT_MASK + 1; --i >= 0;) {
                segments[i] = new CacheSegment();
            }
        }
        this.runtimeType = c;
    }

    /**
     *  Generic fetch() method.
     *  Checks if the given <code>Object</code> is present in the cache -
     *  if so, returns a reference to the cached instance.
     *  Otherwise the given object is added to the cache and returned.
     *
     *  @param obj   the Object to check for
     *  @return  the cached instance
     */
    private Object fetch(Object obj) {
        if (!this.useCache) {
            return obj;
        }

        if (obj == null) {
            return null;
        }

        Object cacheEntry = get(obj);
        if (cacheEntry != null) {
            return cacheEntry;
        }
        put(obj);
        return obj;
    }

    /**
     *  Checks if the given {@link Property} is present in the cache -
     *  if so, returns a reference to the cached instance.
     *  Otherwise the given object is added to the cache and returned.
     *
     *  @param prop the Property instance to check for
     *  @return the cached instance
     */
    public Property fetch(Property prop) {

        return (Property) fetch((Object) prop);
    }

    /**
     *  Checks if the given {@link CommonHyphenation} is present in the cache -
     *  if so, returns a reference to the cached instance.
     *  Otherwise the given object is added to the cache and returned.
     *
     *  @param chy the CommonHyphenation instance to check for
     *  @return the cached instance
     */
    public CommonHyphenation fetch(CommonHyphenation chy) {

        return (CommonHyphenation) fetch((Object) chy);
    }

    /**
     *  Checks if the given {@link CommonFont} is present in the cache -
     *  if so, returns a reference to the cached instance.
     *  Otherwise the given object is added to the cache and returned.
     *
     *  @param cf the CommonFont instance to check for
     *  @return the cached instance
     */
    public CommonFont fetch(CommonFont cf) {

        return (CommonFont) fetch((Object) cf);
    }

    /**
     *  Checks if the given {@link CommonBorderPaddingBackground} is present in the cache -
     *  if so, returns a reference to the cached instance.
     *  Otherwise the given object is added to the cache and returned.
     *
     *  @param cbpb the CommonBorderPaddingBackground instance to check for
     *  @return the cached instance
     */
    public CommonBorderPaddingBackground fetch(CommonBorderPaddingBackground cbpb) {

        return (CommonBorderPaddingBackground) fetch((Object) cbpb);
    }

    /**
     *  Checks if the given {@link CommonBorderPaddingBackground.BorderInfo} is present
     *  in the cache - if so, returns a reference to the cached instance.
     *  Otherwise the given object is added to the cache and returned.
     *
     *  @param bi the BorderInfo instance to check for
     *  @return the cached instance
     */
    public CommonBorderPaddingBackground.BorderInfo fetch(
            CommonBorderPaddingBackground.BorderInfo bi) {
        return (CommonBorderPaddingBackground.BorderInfo) fetch((Object) bi);
    }

    /**
     *  Checks if the given {@link org.apache.fop.fo.flow.Marker.MarkerAttribute} is present
     *  in the cache - if so, returns a reference to the cached instance.
     *  Otherwise the given object is added to the cache and returned.
     *
     *  @param ma the MarkerAttribute instance to check for
     *  @return the cached instance
     */
    public Marker.MarkerAttribute fetch(
            Marker.MarkerAttribute ma) {
        return (Marker.MarkerAttribute) fetch((Object) ma);
    }

    /** {@inheritDoc} */
    public String toString() {
        return super.toString() + "[runtimeType=" + this.runtimeType + "]";
    }


}