1 /* |
|
2 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package jdk.jfr.consumer; |
|
27 |
|
28 import jdk.jfr.internal.LongMap; |
|
29 |
|
30 /** |
|
31 * Holds mapping between a set of keys and their corresponding object. |
|
32 * |
|
33 * If the type is a known type, i.e. {@link RecordedThread}, an |
|
34 * {@link ObjectFactory} can be supplied which will instantiate a typed object. |
|
35 */ |
|
36 final class ConstantMap { |
|
37 |
|
38 private static final int RESOLUTION_FINISHED = 0; |
|
39 private static final int RESOLUTION_STARTED = 1; |
|
40 public static final ConstantMap EMPTY = new ConstantMap(); |
|
41 |
|
42 // A temporary placeholder, so objects can |
|
43 // reference themselves (directly, or indirectly), |
|
44 // when making a transition from numeric id references |
|
45 // to normal Java references. |
|
46 private final static class Reference { |
|
47 private final long key; |
|
48 private final ConstantMap pool; |
|
49 |
|
50 Reference(ConstantMap pool, long key) { |
|
51 this.pool = pool; |
|
52 this.key = key; |
|
53 } |
|
54 |
|
55 Object resolve() { |
|
56 return pool.get(key); |
|
57 } |
|
58 |
|
59 public String toString() { |
|
60 return "ref: " + pool.name + "[" + key + "]"; |
|
61 } |
|
62 } |
|
63 |
|
64 final ObjectFactory<?> factory; |
|
65 final String name; |
|
66 |
|
67 private final LongMap<Object> objects; |
|
68 |
|
69 private boolean resolving; |
|
70 private boolean allResolved; |
|
71 |
|
72 private ConstantMap() { |
|
73 this(null, "<empty>"); |
|
74 allResolved = true; |
|
75 } |
|
76 |
|
77 ConstantMap(ObjectFactory<?> factory, String name) { |
|
78 this.name = name; |
|
79 this.objects = new LongMap<>(2); |
|
80 this.factory = factory; |
|
81 } |
|
82 |
|
83 Object get(long id) { |
|
84 // fast path, all objects in pool resolved |
|
85 if (allResolved) { |
|
86 return objects.get(id); |
|
87 } |
|
88 // referenced from a pool, deal with this later |
|
89 if (!resolving) { |
|
90 return new Reference(this, id); |
|
91 } |
|
92 |
|
93 // should always have a value |
|
94 Object value = objects.get(id); |
|
95 if (value == null) { |
|
96 // unless is 0 which is used to represent null |
|
97 if (id == 0) { |
|
98 return null; |
|
99 } |
|
100 throw new InternalError("Missing object id=" + id + " in pool " + name + ". All ids should reference object"); |
|
101 } |
|
102 |
|
103 // id is resolved (but not the whole pool) |
|
104 if (objects.isSetId(id, RESOLUTION_FINISHED)) { |
|
105 return value; |
|
106 } |
|
107 |
|
108 // resolving ourself, abort to avoid infinite recursion |
|
109 if (objects.isSetId(id, RESOLUTION_STARTED)) { |
|
110 return null; |
|
111 } |
|
112 |
|
113 // mark id as STARTED if we should |
|
114 // come back during object resolution |
|
115 objects.setId(id, RESOLUTION_STARTED); |
|
116 |
|
117 // resolve object! |
|
118 Object resolved = resolve(value); |
|
119 |
|
120 // mark id as FINISHED |
|
121 objects.setId(id, RESOLUTION_FINISHED); |
|
122 |
|
123 // if a factory exists, convert to RecordedThread. |
|
124 // RecordedClass etc. and store back results |
|
125 if (factory != null) { |
|
126 Object factorized = factory.createObject(id, resolved); |
|
127 objects.put(id, factorized); |
|
128 return factorized; |
|
129 } else { |
|
130 objects.put(id, resolved); |
|
131 return resolved; |
|
132 } |
|
133 } |
|
134 |
|
135 private static Object resolve(Object o) { |
|
136 if (o instanceof Reference) { |
|
137 return resolve(((Reference) o).resolve()); |
|
138 } |
|
139 if (o != null && o.getClass().isArray()) { |
|
140 final Object[] array = (Object[]) o; |
|
141 for (int i = 0; i < array.length; i++) { |
|
142 Object element = array[i]; |
|
143 array[i] = resolve(element); |
|
144 } |
|
145 return array; |
|
146 } |
|
147 return o; |
|
148 } |
|
149 |
|
150 public void resolve() { |
|
151 objects.forEachKey(k -> get(k)); |
|
152 } |
|
153 |
|
154 public void put(long key, Object value) { |
|
155 objects.put(key, value); |
|
156 } |
|
157 |
|
158 public void setResolving() { |
|
159 resolving = true; |
|
160 allResolved = false; |
|
161 } |
|
162 |
|
163 public void setResolved() { |
|
164 allResolved = true; |
|
165 resolving = false; |
|
166 } |
|
167 |
|
168 public String getName() { |
|
169 return name; |
|
170 } |
|
171 |
|
172 public Object getResolved(long id) { |
|
173 return objects.get(id); |
|
174 } |
|
175 |
|
176 public void putResolved(long id, Object object) { |
|
177 objects.put(id, object); |
|
178 objects.setId(id, RESOLUTION_FINISHED); |
|
179 } |
|
180 |
|
181 public void setAllResolved(boolean allResolved) { |
|
182 this.allResolved = allResolved; |
|
183 } |
|
184 |
|
185 public boolean isResolved(long id) { |
|
186 if (objects.hasKey(id)) { |
|
187 return objects.isSetId(id, RESOLUTION_FINISHED); |
|
188 } |
|
189 return false; |
|
190 } |
|
191 } |
|