Add optional lz4 compression for snapshots
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / io / LZ4InputOutputStreamSupport.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.io;
9
10 import com.google.common.io.ByteSource;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.OutputStream;
17 import net.jpountz.lz4.LZ4Factory;
18 import net.jpountz.lz4.LZ4FrameInputStream;
19 import net.jpountz.lz4.LZ4FrameOutputStream;
20 import net.jpountz.lz4.LZ4FrameOutputStream.FLG.Bits;
21 import net.jpountz.xxhash.XXHashFactory;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 final class LZ4InputOutputStreamSupport extends InputOutputStreamFactory {
26     private static final Logger LOG = LoggerFactory.getLogger(LZ4InputOutputStreamSupport.class);
27     private static final LZ4Factory LZ4_FACTORY = LZ4Factory.fastestInstance();
28     private static final XXHashFactory HASH_FACTORY = XXHashFactory.fastestInstance();
29
30     private final LZ4FrameOutputStream.BLOCKSIZE blocksize;
31
32     LZ4InputOutputStreamSupport(final LZ4FrameOutputStream.BLOCKSIZE blocksize) {
33         this.blocksize = blocksize;
34     }
35
36     @Override
37     public InputStream createInputStream(final ByteSource input) throws IOException {
38         final InputStream stream = input.openStream();
39         try {
40             return new LZ4FrameInputStream(stream, LZ4_FACTORY.safeDecompressor(), HASH_FACTORY.hash32());
41         } catch (IOException e) {
42             stream.close();
43             LOG.warn("Error loading with lz4 decompression, using default one", e);
44             return input.openBufferedStream();
45         }
46     }
47
48     @Override
49     public InputStream createInputStream(final File file) throws IOException {
50         final FileInputStream fileInput = new FileInputStream(file);
51         try {
52             return new LZ4FrameInputStream(fileInput, LZ4_FACTORY.safeDecompressor(), HASH_FACTORY.hash32());
53         } catch (IOException e) {
54             fileInput.close();
55             LOG.warn("Error loading file with lz4 decompression, using default one", e);
56             return defaultCreateInputStream(file);
57         }
58     }
59
60     @Override
61     public OutputStream createOutputStream(final File file) throws IOException {
62         return new LZ4FrameOutputStream(new FileOutputStream(file), blocksize, -1, LZ4_FACTORY.fastCompressor(),
63             HASH_FACTORY.hash32(), Bits.BLOCK_INDEPENDENCE);
64     }
65
66     @Override
67     public OutputStream wrapOutputStream(final OutputStream output) throws IOException {
68         return new LZ4FrameOutputStream(output, blocksize, -1, LZ4_FACTORY.fastCompressor(), HASH_FACTORY.hash32(),
69             Bits.BLOCK_INDEPENDENCE);
70     }
71 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.