172 public StoredScript storedScriptFor(final Source source, final String mainClassName,
173 final Map<String, byte[]> classBytes,
174 final Map<Integer, FunctionInitializer> initializers,
175 final Object[] constants, final int compilationId) {
176 for (final Object constant : constants) {
177 // Make sure all constant data is serializable
178 if (!(constant instanceof Serializable)) {
179 getLogger().warning("cannot store ", source, " non serializable constant ", constant);
180 return null;
181 }
182 }
183 return new StoredScript(compilationId, mainClassName, classBytes, initializers, constants);
184 }
185
186 /**
187 * Generate a string representing the function with {@code functionId} and {@code paramTypes}.
188 * @param functionId function id
189 * @param paramTypes parameter types
190 * @return a string representing the function
191 */
192 public static String getCacheKey(final int functionId, final Type[] paramTypes) {
193 final StringBuilder b = new StringBuilder().append(functionId);
194 if(paramTypes != null && paramTypes.length > 0) {
195 b.append('-');
196 for(final Type t: paramTypes) {
197 b.append(Type.getShortSignatureDescriptor(t));
198 }
199 }
200 return b.toString();
201 }
202
203 /**
204 * A store using a file system directory.
205 */
206 public static class DirectoryCodeStore extends CodeStore {
207
208 // Default minimum size for storing a compiled script class
209 private final static int DEFAULT_MIN_SIZE = 1000;
210
211 private final File dir;
212 private final boolean readOnly;
258 }
259 return dir;
260 }
261 });
262 } catch (final PrivilegedActionException e) {
263 throw (IOException) e.getException();
264 }
265 }
266
267 private static String getVersionDir(final ScriptEnvironment env) throws IOException {
268 try {
269 final String versionDir = OptimisticTypesPersistence.getVersionDirName();
270 return env._optimistic_types ? versionDir + "_opt" : versionDir;
271 } catch (final Exception e) {
272 throw new IOException(e);
273 }
274 }
275
276 @Override
277 public StoredScript load(final Source source, final String functionKey) {
278 if (source.getLength() < minSize) {
279 return null;
280 }
281
282 final File file = getCacheFile(source, functionKey);
283
284 try {
285 return AccessController.doPrivileged(new PrivilegedExceptionAction<StoredScript>() {
286 @Override
287 public StoredScript run() throws IOException, ClassNotFoundException {
288 if (!file.exists()) {
289 return null;
290 }
291 try (ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)))) {
292 final StoredScript storedScript = (StoredScript) in.readObject();
293 getLogger().info("loaded ", source, "-", functionKey);
294 return storedScript;
295 }
296 }
297 });
298 } catch (final PrivilegedActionException e) {
|
172 public StoredScript storedScriptFor(final Source source, final String mainClassName,
173 final Map<String, byte[]> classBytes,
174 final Map<Integer, FunctionInitializer> initializers,
175 final Object[] constants, final int compilationId) {
176 for (final Object constant : constants) {
177 // Make sure all constant data is serializable
178 if (!(constant instanceof Serializable)) {
179 getLogger().warning("cannot store ", source, " non serializable constant ", constant);
180 return null;
181 }
182 }
183 return new StoredScript(compilationId, mainClassName, classBytes, initializers, constants);
184 }
185
186 /**
187 * Generate a string representing the function with {@code functionId} and {@code paramTypes}.
188 * @param functionId function id
189 * @param paramTypes parameter types
190 * @return a string representing the function
191 */
192 public static String getCacheKey(final Object functionId, final Type[] paramTypes) {
193 final StringBuilder b = new StringBuilder().append(functionId);
194 if(paramTypes != null && paramTypes.length > 0) {
195 b.append('-');
196 for(final Type t: paramTypes) {
197 b.append(Type.getShortSignatureDescriptor(t));
198 }
199 }
200 return b.toString();
201 }
202
203 /**
204 * A store using a file system directory.
205 */
206 public static class DirectoryCodeStore extends CodeStore {
207
208 // Default minimum size for storing a compiled script class
209 private final static int DEFAULT_MIN_SIZE = 1000;
210
211 private final File dir;
212 private final boolean readOnly;
258 }
259 return dir;
260 }
261 });
262 } catch (final PrivilegedActionException e) {
263 throw (IOException) e.getException();
264 }
265 }
266
267 private static String getVersionDir(final ScriptEnvironment env) throws IOException {
268 try {
269 final String versionDir = OptimisticTypesPersistence.getVersionDirName();
270 return env._optimistic_types ? versionDir + "_opt" : versionDir;
271 } catch (final Exception e) {
272 throw new IOException(e);
273 }
274 }
275
276 @Override
277 public StoredScript load(final Source source, final String functionKey) {
278 if (belowThreshold(source)) {
279 return null;
280 }
281
282 final File file = getCacheFile(source, functionKey);
283
284 try {
285 return AccessController.doPrivileged(new PrivilegedExceptionAction<StoredScript>() {
286 @Override
287 public StoredScript run() throws IOException, ClassNotFoundException {
288 if (!file.exists()) {
289 return null;
290 }
291 try (ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)))) {
292 final StoredScript storedScript = (StoredScript) in.readObject();
293 getLogger().info("loaded ", source, "-", functionKey);
294 return storedScript;
295 }
296 }
297 });
298 } catch (final PrivilegedActionException e) {
|