Search in sources :

Example 1 with Decompressor

use of com.ms.silverking.compression.Decompressor in project SilverKing by Morgan-Stanley.

the class ValueUtil method verifyChecksum.

public static void verifyChecksum(ByteBuffer storedValue) throws CorruptValueException {
    if (storedValue != null) {
        int baseOffset;
        byte[] storedData;
        byte[] dataToVerify;
        int verifyDataOffset;
        int dataOffset;
        int compressedLength;
        int uncompressedLength;
        Compression compression;
        // don't alter the source buffer
        storedValue = storedValue.duplicate();
        baseOffset = storedValue.position();
        storedData = new byte[storedValue.limit()];
        storedValue.get(storedData);
        compressedLength = MetaDataUtil.getCompressedLength(storedData, baseOffset);
        uncompressedLength = MetaDataUtil.getUncompressedLength(storedData, baseOffset);
        if (debugChecksum) {
            System.out.println("compressedLength: " + compressedLength);
            System.out.println("uncompressedLength: " + uncompressedLength);
        }
        dataOffset = MetaDataUtil.getDataOffset(storedData, baseOffset);
        compression = EnumValues.compression[MetaDataUtil.getCompression(storedData, baseOffset)];
        if (MetaDataUtil.isCompressed(storedData, baseOffset)) {
            byte[] uncompressedData;
            Decompressor decompressor;
            Log.fine("Compressed");
            decompressor = CodecProvider.getDecompressor(compression);
            try {
                // System.out.println(compression +" "+ decompressor);
                uncompressedData = decompressor.decompress(storedData, dataOffset, compressedLength, uncompressedLength);
                dataToVerify = uncompressedData;
                verifyDataOffset = 0;
            } catch (Exception e) {
                throw new CorruptValueException(e);
            }
        } else {
            dataToVerify = storedData;
            verifyDataOffset = dataOffset;
        }
        verifyChecksum(storedData, baseOffset, dataToVerify, verifyDataOffset, uncompressedLength);
        Log.warningAsync("Checksum OK");
    }
}
Also used : Compression(com.ms.silverking.cloud.dht.client.Compression) Decompressor(com.ms.silverking.compression.Decompressor)

Example 2 with Decompressor

use of com.ms.silverking.compression.Decompressor in project SilverKing by Morgan-Stanley.

the class RawRetrievalResult method setStoredValue.

public void setStoredValue(ByteBuffer storedValue, boolean verifyChecksum, boolean filterInvalidated, EncrypterDecrypter encrypterDecrypter) throws CorruptValueException {
    int baseOffset;
    byte[] storedData;
    byte[] dataToVerify;
    int verifyDataOffset;
    int dataOffset;
    int compressedLength;
    int uncompressedLength;
    Compression compression;
    setStoredValue_direct(storedValue);
    dataToVerify = null;
    verifyDataOffset = 0;
    if (result == OpResult.SUCCEEDED) {
        baseOffset = storedValue.position();
        storedData = storedValue.array();
        // System.out.printf("%d\t%d\n", baseOffset, storedData.length);
        compressedLength = MetaDataUtil.getCompressedLength(storedData, baseOffset);
        uncompressedLength = MetaDataUtil.getUncompressedLength(storedData, baseOffset);
        if (debugChecksum) {
            System.out.println("compressedLength: " + compressedLength);
            System.out.println("uncompressedLength: " + uncompressedLength);
        }
        dataOffset = MetaDataUtil.getDataOffset(storedData, baseOffset);
        if (retrievalType.hasValue()) {
            if (encrypterDecrypter != null) {
                byte[] _storedData;
                byte[] plainText;
                int offset;
                int tailLength;
                offset = MetaDataUtil.getDataOffset(storedData, baseOffset);
                plainText = encrypterDecrypter.decrypt(storedData, offset, compressedLength);
                dataToVerify = new byte[compressedLength];
                verifyDataOffset = 0;
                System.arraycopy(storedData, offset, dataToVerify, 0, compressedLength);
                tailLength = storedData.length - offset - compressedLength;
                _storedData = new byte[offset + plainText.length + tailLength];
                System.arraycopy(storedData, 0, _storedData, 0, offset);
                System.arraycopy(plainText, 0, _storedData, offset, plainText.length);
                System.arraycopy(storedData, offset + compressedLength, _storedData, offset + plainText.length, tailLength);
                storedData = _storedData;
            }
            compression = EnumValues.compression[MetaDataUtil.getCompression(storedData, baseOffset)];
            if (MetaDataUtil.isCompressed(storedData, baseOffset)) {
                byte[] uncompressedData;
                Decompressor decompressor;
                Log.fine("Compressed");
                decompressor = CodecProvider.getDecompressor(compression);
                if (decompressor == null) {
                    if (compression == Compression.NONE) {
                        throw new RuntimeException("MetaDataUtil.isCompressed() returning true for uncompressed data");
                    } else {
                        throw new RuntimeException("Can't find compressor for: " + compression);
                    }
                }
                try {
                    // System.out.println(compression +" "+ decompressor);
                    uncompressedData = decompressor.decompress(storedData, dataOffset, compressedLength, uncompressedLength);
                    if (encrypterDecrypter == null) {
                        dataToVerify = uncompressedData;
                        verifyDataOffset = 0;
                    }
                } catch (Exception e) {
                    if (Log.levelMet(Level.INFO)) {
                        Log.logErrorWarning(e);
                    }
                    throw new CorruptValueException(e);
                }
            } else {
                if (encrypterDecrypter == null) {
                    dataToVerify = storedData;
                    verifyDataOffset = dataOffset;
                }
            }
        } else {
            dataToVerify = storedData;
            verifyDataOffset = dataOffset;
        }
        if (filterInvalidated && ValueUtil.isInvalidated(storedData, baseOffset)) {
            result = OpResult.NO_SUCH_VALUE;
        } else {
            if (retrievalType.hasValue()) {
                if (verifyChecksum) {
                    int verifyDataLength;
                    verifyDataLength = dataToVerify.length - verifyDataOffset;
                    ValueUtil.verifyChecksum(storedData, baseOffset, dataToVerify, verifyDataOffset, verifyDataLength);
                }
                cookedValue = checkedWrap(dataToVerify, verifyDataOffset, uncompressedLength);
            // cookedValue = checkedWrap(storedData, dataOffset, uncompressedLength);
            }
        }
    }
}
Also used : Compression(com.ms.silverking.cloud.dht.client.Compression) Decompressor(com.ms.silverking.compression.Decompressor)

Aggregations

Compression (com.ms.silverking.cloud.dht.client.Compression)2 Decompressor (com.ms.silverking.compression.Decompressor)2