aboutsummaryrefslogtreecommitdiff
path: root/agent/src/share/classes/sun/jvm/hotspot/utilities/soql/SOQLEngine.java
blob: ef51b7e3c2751650956b5c5e4cf6977baac678c4 (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
/*
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

package sun.jvm.hotspot.utilities.soql;

import java.util.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.memory.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.utilities.*;

/**
 * This is SOQL (Simple Object Query Language) engine. This
 * uses JavaScript engine for the "select" and "where" expression
 * parts.
 */
public class SOQLEngine extends JSJavaScriptEngine {
   public static synchronized SOQLEngine getEngine() {
      if (soleInstance == null) {
         soleInstance = new SOQLEngine();
      }
      return soleInstance;
   }

   /**
      Query is of the form

         select <java script code to select>
         [ from [instanceof] <class name> [<identifier>]
           [ where <java script boolean expression> ]
         ]
   */
   public synchronized void executeQuery(String query, ObjectVisitor visitor)
                                                 throws SOQLException {
      debugPrint("query : " + query);
      StringTokenizer st = new StringTokenizer(query);
      if (st.hasMoreTokens()) {
         String first = st.nextToken();
         if (! first.equals("select") ) {
            throw new SOQLException("query syntax error: no 'select' clause");
         }
      } else {
         throw new SOQLException("query syntax error: no 'select' clause");
      }

      int selectStart = query.indexOf("select");
      int fromStart = query.indexOf("from");

      String selectExpr = null;
      String className = null;
      boolean isInstanceOf = false;
      String whereExpr = null;
      String identifier = null;

      if (fromStart != -1) {
         selectExpr = query.substring(selectStart + "select".length(), fromStart);
         st = new StringTokenizer(query.substring(fromStart + "from".length()));

         if (st.hasMoreTokens()) {
            String tmp = st.nextToken();
            if (tmp.equals("instanceof")) {
               isInstanceOf = true;
               if (! st.hasMoreTokens()) {
                  throw new SOQLException("no class name after 'instanceof'");
               }
               className = st.nextToken();
            } else {
               className = tmp;
            }
         } else {
            throw new SOQLException("query syntax error: class name must follow 'from'");
         }

         if (st.hasMoreTokens()) {
            identifier = st.nextToken();
            if (identifier.equals("where")) {
               throw new SOQLException("query syntax error: identifier should follow class name");
            }
            if (st.hasMoreTokens()) {
               String tmp = st.nextToken();
               if (! tmp.equals("where")) {
                  throw new SOQLException("query syntax error: 'where' clause expected after 'from' clause");
               }
               int whereEnd = query.lastIndexOf("where") + 5; // "where".length
               whereExpr = query.substring(whereEnd);
            }
         } else {
            throw new SOQLException("query syntax error: identifier should follow class name");
         }
      } else { // no from clause
         selectExpr = query.substring(selectStart + "select".length(), query.length());
      }

      executeQuery(new SOQLQuery(selectExpr, isInstanceOf, className, identifier, whereExpr), visitor);
   }

   private void executeQuery(SOQLQuery q, ObjectVisitor visitor) throws SOQLException {
      InstanceKlass kls = null;
      if (q.className != null) {
         kls = SystemDictionaryHelper.findInstanceKlass(q.className);
         if (kls == null) {
            throw new SOQLException(q.className + " is not found!");
         }
      }


      StringBuffer buf = new StringBuffer();
      buf.append("function result(");
      if (q.identifier != null) {
         buf.append(q.identifier);
      }
      buf.append(") { return ");
      buf.append(q.selectExpr.replace('\n', ' '));
      buf.append("; }");

      String selectCode = buf.toString();
      debugPrint(selectCode);
      String whereCode = null;
      if (q.whereExpr != null) {
         buf = new StringBuffer();
         buf.append("function filter(");
         buf.append(q.identifier);
         buf.append(") { return ");
         buf.append(q.whereExpr.replace('\n', ' '));
         buf.append("; }");
         whereCode = buf.toString();
         debugPrint(whereCode);
      } else {
         whereCode = "filter = null;";
      }

      beginQuery();
      // compile select expression and where condition
      evalString(selectCode, "", 1);
      evalString(whereCode,  "", 1);

      // iterate thru heap, if needed
      if (q.className != null) {
         try {
            iterateOops(kls, visitor, q.isInstanceOf);
         } finally {
            endQuery();
         }
      } else {
         // simple "select <expr>" query
         try {
            Object select = call("result", new Object[] {});
            visitor.visit(select);
         } catch (Exception e) {
            e.printStackTrace();
         }
      }
   }

   private void dispatchObject(Oop oop, ObjectVisitor visitor, boolean filterExists) {
      JSJavaObject jsObj = factory.newJSJavaObject(oop);
      Object[] args = new Object[] { jsObj };
      boolean b = true;

      try {
         if (filterExists) {
            Object res = call("filter", args);
            if (res instanceof Boolean) {
               b = ((Boolean)res).booleanValue();
            } else if (res instanceof Number) {
               b = ((Number)res).intValue() != 0;
            } else {
               b = (res != null);
            }
         }

         if (b) {
            Object select = call("result", args);
            visitor.visit(select);
         }
      } catch (Exception e) {
         throw new RuntimeException(e);
      }
   }

   private void iterateOops(final InstanceKlass ik, final ObjectVisitor visitor,
                            boolean includeSubtypes) {
      ObjectHeap oh = VM.getVM().getObjectHeap();
      oh.iterateObjectsOfKlass(new HeapVisitor() {
                    boolean filterExists;
                    public void prologue(long usedSize) {
                        filterExists = getScriptEngine().get("filter") != null;
                    }
                    public boolean doObj(Oop obj) {
                       dispatchObject(obj, visitor, filterExists);
                       return false;
                    }
                    public void epilogue() {}
                 }, ik, includeSubtypes);
   }

   // we create fresh ObjectReader and factory to avoid
   // excessive cache across queries.
   private void beginQuery() {
      objReader = new ObjectReader();
      factory = new JSJavaFactoryImpl();
   }

   // at the end of query we clear object reader cache
   // and factory cache
   private void endQuery() {
      objReader = null;
      factory = null;
   }

   protected ObjectReader getObjectReader() {
      return objReader;
   }

   protected JSJavaFactory getJSJavaFactory() {
      return factory;
   }

   protected boolean isQuitting() {
      return false;
   }

   protected void quit() {
      // do nothing
   }

   private static void debugPrint(String msg) {
      if (debug) System.out.println(msg);
   }

   private static final boolean debug;
   static {
      debug = System.getProperty("sun.jvm.hotspot.utilities.soql.SOQLEngine.debug") != null;
   }

   protected SOQLEngine() {
       super(debug);
       start();
   }

   private ObjectReader objReader;
   private JSJavaFactory factory;
   private static SOQLEngine soleInstance;
}