use snappy in java implementation

This commit is contained in:
Maxim.Mossienko
2014-04-08 20:42:06 +02:00
parent 9e4e295dff
commit a0dd4c52be
11 changed files with 19 additions and 100 deletions

View File

@@ -1,11 +1,11 @@
<component name="libraryTable">
<library name="Snappy-Java">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/snappy-java-1.0.5.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/snappy-in-java-0.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$PROJECT_DIR$/lib/src/snappy-java-1.0.5.zip!/snappy-java-1.0.5/src/main/java" />
<root url="jar://$PROJECT_DIR$/lib/src/snappy-in-java-0.3-src.jar!/snappy-snappy-0.3/src/main/java" />
</SOURCES>
</library>
</component>

View File

@@ -795,7 +795,7 @@ def layout_core(String home, String target) {
include(name: "asm.jar")
include(name: "asm-commons.jar")
include(name: "cli-parser-1.1.jar")
include(name: "snappy-java-1.0.5.jar")
include(name: "snappy-in-java-0.3.jar")
include(name: "jayatana-1.2.4.jar")
}
}

View File

@@ -253,7 +253,7 @@ libraryLicense(name: "YourKit Java Profiler", libraryName: "yjp-controller-api-r
libraryLicense(name: "protobuf", version: "2.5.0", license: "New BSD", url: "http://code.google.com/p/protobuf/", licenseUrl: "http://code.google.com/p/protobuf/source/browse/trunk/COPYING.txt?r=367")
libraryLicense(name: "Netty", libraryName: "Netty", version: "5.0.0.Alpha1", license: "Apache 2.0", url: "http://netty.io", licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0")
libraryLicense(name: "Kryo", libraryName: "Kryo", version: "2.22", license: "New BSD License", url: "https://github.com/EsotericSoftware/kryo", licenseUrl: "https://github.com/EsotericSoftware/kryo/blob/master/license.txt")
libraryLicense(name: "Snappy-Java", libraryName: "Snappy-Java", version: "1.0.5", license: "Apache 2.0", url: "http://code.google.com/p/snappy-java/", licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0")
libraryLicense(name: "Snappy-Java", libraryName: "Snappy-Java", version: "0.3", license: "Apache 2.0", url: "https://github.com/dain/snappy", licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0")
libraryLicense(name: "Cucumber-Java", libraryName: "cucumber-java", version: "1.0.14", license: "MIT License", url: "https://github.com/cucumber/cucumber-jvm/", licenseUrl: "http://www.opensource.org/licenses/mit-license.html")
libraryLicense(name: "Cucumber-JVM", libraryName: "cucumber-jvm", version: "1.0.14", license: "MIT License", url: "https://github.com/cucumber/cucumber-jvm/", licenseUrl: "http://www.opensource.org/licenses/mit-license.html")
libraryLicense(name: "Cucumber-Groovy", libraryName: "cucumber-groovy", version: "1.0.14", license: "MIT License", url: "https://github.com/cucumber/cucumber-jvm/", licenseUrl: "http://www.opensource.org/licenses/mit-license.html")

View File

@@ -58,7 +58,7 @@ resolver.jar
rhino-js-1_7R4.jar
sanselan-0.98-snapshot.jar
serviceMessages.jar
snappy-java-1.0.5.jar
snappy-in-java-0.3.jar
swingx-core-1.6.2.jar
trove4j.jar
velocity.jar

BIN
lib/snappy-in-java-0.3.jar Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -15,14 +15,15 @@
*/
package com.intellij.util;
import com.intellij.openapi.util.ThreadLocalCachedValue;
import com.intellij.util.io.DataInputOutputUtil;
import org.xerial.snappy.Snappy;
import org.iq80.snappy.CorruptionException;
import org.iq80.snappy.Snappy;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
@@ -30,35 +31,20 @@ import java.nio.charset.Charset;
* @author Maxim.Mossienko
*/
public class CompressionUtil {
private static final boolean ourCanUseSnappy;
static {
boolean canUseSnappy = false;
try {
if (!SnappyInitializer.NO_SNAPPY) {
Field impl = Snappy.class.getDeclaredField("impl");
impl.setAccessible(true);
canUseSnappy = impl.get(null) != null;
}
}
catch (Throwable ignored) { }
ourCanUseSnappy = canUseSnappy;
}
private static final int COMPRESSION_THRESHOLD = 64;
private static final ThreadLocal<SoftReference<byte[]>> spareBufferLocal = new ThreadLocal<SoftReference<byte[]>>();
public static int writeCompressed(DataOutput out, byte[] bytes, int length) throws IOException {
if (length > COMPRESSION_THRESHOLD && ourCanUseSnappy) {
if (length > COMPRESSION_THRESHOLD) {
SoftReference<byte[]> reference = spareBufferLocal.get();
byte[] compressedOutputBuffer = com.intellij.reference.SoftReference.dereference(reference);
int maxCompressedSize = 32 + length + length / 6; // snappy.cc#MaxCompressedLength
int maxCompressedSize = Snappy.maxCompressedLength(length);
if (compressedOutputBuffer == null || compressedOutputBuffer.length < maxCompressedSize) {
compressedOutputBuffer = new byte[maxCompressedSize];
spareBufferLocal.set(new SoftReference<byte[]>(compressedOutputBuffer));
}
int compressedSize = Snappy.rawCompress(bytes, 0, length, compressedOutputBuffer, 0);
int compressedSize = Snappy.compress(bytes, 0, length, compressedOutputBuffer, 0);
DataInputOutputUtil.writeINT(out, -compressedSize);
out.write(compressedOutputBuffer, 0, compressedSize);
return compressedSize;
@@ -76,8 +62,7 @@ public class CompressionUtil {
if (size >= 0) {
return bytes;
} else {
if (!ourCanUseSnappy) throw new IOException("Can not read compressed data");
return Snappy.uncompress(bytes);
return Snappy.uncompress(bytes, 0, bytes.length);
}
}
@@ -87,22 +72,23 @@ public class CompressionUtil {
if (string instanceof CharSequence) return (CharSequence)string;
byte[] b = (byte[])string;
try {
return Snappy.uncompressString(b, charset);
} catch (Exception ex) {
byte[] bytes = Snappy.uncompress(b, 0, b.length);
return new String(bytes, charset);
} catch (CorruptionException ex) {
throw new RuntimeException(ex);
}
}
public static Object compressCharSequence(CharSequence string, Charset charset) {
if (!ourCanUseSnappy || string.length() < STRING_COMPRESSION_THRESHOLD) {
if (string.length() < STRING_COMPRESSION_THRESHOLD) {
if (string instanceof CharBuffer && ((CharBuffer)string).capacity() > STRING_COMPRESSION_THRESHOLD) {
string = string.toString(); // shrink to size
}
return string;
}
try {
return Snappy.compress(string.toString(), charset);
} catch (IOException ex) {
return Snappy.compress(string.toString().getBytes(charset));
} catch (CorruptionException ex) {
ex.printStackTrace();
return string;
}

View File

@@ -1,64 +0,0 @@
package com.intellij.util;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import org.xerial.snappy.Snappy;
import org.xerial.snappy.SnappyLoader;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Method;
public class SnappyInitializer {
public static final boolean NO_SNAPPY = SystemProperties.getBooleanProperty("idea.no.snappy", false);
public static void initializeSnappy(Logger log, File ideaTempDir) {
if (!NO_SNAPPY) {
if (System.getProperty(SnappyLoader.KEY_SNAPPY_TEMPDIR) == null) {
System.setProperty(SnappyLoader.KEY_SNAPPY_TEMPDIR, ideaTempDir.getPath());
}
try {
final long t = System.currentTimeMillis();
loadSnappyForJRockit();
log.info("Snappy library loaded (" + Snappy.getNativeLibraryVersion() + ") in " + (System.currentTimeMillis() - t) + " ms");
}
catch (Throwable t) {
log.error("Unable to load Snappy library" + " (OS: " + SystemInfo.OS_NAME + " " + SystemInfo.OS_VERSION + ")", t);
}
}
}
// todo[r.sh] drop after migration on Java 7
private static void loadSnappyForJRockit() throws Exception {
String vmName = System.getProperty("java.vm.name");
if (vmName == null || !vmName.toLowerCase().contains("jrockit")) {
return;
}
byte[] bytes;
InputStream in = SnappyInitializer.class.getResourceAsStream("/org/xerial/snappy/SnappyNativeLoader.bytecode");
try {
bytes = FileUtil.loadBytes(in);
}
finally {
in.close();
}
ClassLoader classLoader = SnappyInitializer.class.getClassLoader();
Method defineClass = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
defineClass.setAccessible(true);
Class<?> loaderClass = (Class<?>)defineClass.invoke(classLoader, "org.xerial.snappy.SnappyNativeLoader", bytes, 0, bytes.length);
loaderClass = classLoader.loadClass(loaderClass.getName());
String[] classes = {"org.xerial.snappy.SnappyNativeAPI", "org.xerial.snappy.SnappyNative", "org.xerial.snappy.SnappyErrorCode"};
for (String aClass : classes) {
classLoader.loadClass(aClass);
}
Method loadNativeLibrary = SnappyLoader.class.getDeclaredMethod("loadNativeLibrary", Class.class);
loadNativeLibrary.setAccessible(true);
loadNativeLibrary.invoke(null, loaderClass);
}
}

View File

@@ -30,7 +30,6 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.AppUIUtil;
import com.intellij.util.Consumer;
import com.intellij.util.EnvironmentUtil;
import com.intellij.util.SnappyInitializer;
import com.intellij.util.lang.UrlClassLoader;
import com.sun.jna.Native;
import org.jetbrains.annotations.NonNls;
@@ -278,8 +277,6 @@ public class StartupUtil {
System.setProperty(JAVA_IO_TEMP_DIR, javaTempDir);
}
SnappyInitializer.initializeSnappy(log, ideTempDir);
if (SystemInfo.isWin2kOrNewer) {
IdeaWin32.isAvailable(); // logging is done there
}