├── settings.gradle ├── gradle.properties ├── src ├── site │ ├── resources │ │ ├── knapsack.jpg │ │ ├── knapsack.png │ │ ├── knapsack-diagram-2.png │ │ ├── knapsack-diagram.png │ │ └── hitchhiker-691581_1280.jpg │ └── site.xml ├── main │ ├── java │ │ └── org │ │ │ └── xbib │ │ │ ├── io │ │ │ ├── compress │ │ │ │ ├── xz │ │ │ │ │ ├── IndexIndicatorException.java │ │ │ │ │ ├── simple │ │ │ │ │ │ ├── SimpleFilter.java │ │ │ │ │ │ ├── ARM.java │ │ │ │ │ │ ├── PowerPC.java │ │ │ │ │ │ ├── ARMThumb.java │ │ │ │ │ │ ├── SPARC.java │ │ │ │ │ │ ├── IA64.java │ │ │ │ │ │ └── X86.java │ │ │ │ │ ├── common │ │ │ │ │ │ ├── StreamFlags.java │ │ │ │ │ │ ├── Util.java │ │ │ │ │ │ └── EncoderUtil.java │ │ │ │ │ ├── FilterCoder.java │ │ │ │ │ ├── FilterDecoder.java │ │ │ │ │ ├── FilterEncoder.java │ │ │ │ │ ├── index │ │ │ │ │ │ ├── IndexRecord.java │ │ │ │ │ │ ├── BlockInfo.java │ │ │ │ │ │ ├── IndexBase.java │ │ │ │ │ │ └── IndexEncoder.java │ │ │ │ │ ├── lz │ │ │ │ │ │ ├── Matches.java │ │ │ │ │ │ ├── CRC32Hash.java │ │ │ │ │ │ └── Hash234.java │ │ │ │ │ ├── check │ │ │ │ │ │ ├── None.java │ │ │ │ │ │ ├── SHA256.java │ │ │ │ │ │ ├── CRC32.java │ │ │ │ │ │ ├── CRC64.java │ │ │ │ │ │ └── Check.java │ │ │ │ │ ├── DeltaCoder.java │ │ │ │ │ ├── LZMA2Coder.java │ │ │ │ │ ├── XZFormatException.java │ │ │ │ │ ├── delta │ │ │ │ │ │ ├── DeltaDecoder.java │ │ │ │ │ │ ├── DeltaEncoder.java │ │ │ │ │ │ └── DeltaCoder.java │ │ │ │ │ ├── XZIOException.java │ │ │ │ │ ├── rangecoder │ │ │ │ │ │ └── RangeCoder.java │ │ │ │ │ ├── DeltaDecoder.java │ │ │ │ │ ├── DeltaEncoder.java │ │ │ │ │ ├── BCJCoder.java │ │ │ │ │ ├── SPARCOptions.java │ │ │ │ │ ├── UnsupportedOptionsException.java │ │ │ │ │ ├── ARMOptions.java │ │ │ │ │ ├── IA64Options.java │ │ │ │ │ ├── X86Options.java │ │ │ │ │ ├── FinishableOutputStream.java │ │ │ │ │ ├── PowerPCOptions.java │ │ │ │ │ ├── ARMThumbOptions.java │ │ │ │ │ ├── CorruptedInputException.java │ │ │ │ │ ├── package-info.java │ │ │ │ │ ├── LZMA2Decoder.java │ │ │ │ │ ├── CountingInputStream.java │ │ │ │ │ ├── RawCoder.java │ │ │ │ │ ├── XZCompressCodec.java │ │ │ │ │ ├── BCJEncoder.java │ │ │ │ │ ├── XZ.java │ │ │ │ │ ├── CountingOutputStream.java │ │ │ │ │ ├── LZMA2Encoder.java │ │ │ │ │ ├── BCJOptions.java │ │ │ │ │ ├── lzma │ │ │ │ │ │ ├── Optimum.java │ │ │ │ │ │ └── State.java │ │ │ │ │ ├── MemoryLimitException.java │ │ │ │ │ ├── FinishableWrapperOutputStream.java │ │ │ │ │ ├── BCJDecoder.java │ │ │ │ │ ├── FilterOptions.java │ │ │ │ │ ├── SeekableInputStream.java │ │ │ │ │ ├── SeekableFileInputStream.java │ │ │ │ │ ├── DeltaOptions.java │ │ │ │ │ └── DeltaOutputStream.java │ │ │ │ ├── bzip2 │ │ │ │ │ ├── BZip2Exception.java │ │ │ │ │ └── BZip2CompressCodec.java │ │ │ │ ├── CompressCodec.java │ │ │ │ ├── lzf │ │ │ │ │ ├── LZFCompressCodec.java │ │ │ │ │ ├── LZFDecoder.java │ │ │ │ │ └── ChunkDecoderFactory.java │ │ │ │ ├── gzip │ │ │ │ │ └── GzipCompressCodec.java │ │ │ │ └── CompressCodecService.java │ │ │ ├── archive │ │ │ │ ├── cpio │ │ │ │ │ ├── CpioSession.java │ │ │ │ │ └── CpioArchiveCodec.java │ │ │ │ ├── zip │ │ │ │ │ ├── ZipSession.java │ │ │ │ │ ├── ZipArchiveInputStream.java │ │ │ │ │ ├── ZipArchiveCodec.java │ │ │ │ │ ├── ZipArchiveEntry.java │ │ │ │ │ └── ZipArchiveOutputStream.java │ │ │ │ ├── tar │ │ │ │ │ ├── TarSession.java │ │ │ │ │ └── TarArchiveCodec.java │ │ │ │ ├── esbulk │ │ │ │ │ ├── EsBulkSession.java │ │ │ │ │ ├── EsBulkArchiveCodec.java │ │ │ │ │ ├── EsBulkArchiveEntry.java │ │ │ │ │ └── EsBulkArchiveOutputStream.java │ │ │ │ ├── ArchiveEntry.java │ │ │ │ ├── ArchiveCodec.java │ │ │ │ ├── ArchiveService.java │ │ │ │ ├── ArchiveInputStream.java │ │ │ │ └── entry │ │ │ │ │ ├── FallbackArchiveEntryEncoding.java │ │ │ │ │ └── ArchiveEntryEncoding.java │ │ │ ├── ConnectionFactory.java │ │ │ ├── Connection.java │ │ │ ├── Packet.java │ │ │ ├── Session.java │ │ │ ├── StringPacket.java │ │ │ └── CountingOutputStream.java │ │ │ └── elasticsearch │ │ │ ├── knapsack │ │ │ ├── KnapsackModule.java │ │ │ ├── KnapsackRequest.java │ │ │ └── KnapsackParameter.java │ │ │ ├── action │ │ │ └── knapsack │ │ │ │ ├── abort │ │ │ │ ├── KnapsackAbortRequestBuilder.java │ │ │ │ ├── KnapsackAbortAction.java │ │ │ │ ├── KnapsackAbortRequest.java │ │ │ │ ├── KnapsackAbortResponse.java │ │ │ │ └── TransportKnapsackAbortAction.java │ │ │ │ ├── state │ │ │ │ ├── KnapsackStateRequestBuilder.java │ │ │ │ ├── KnapsackStateAction.java │ │ │ │ ├── KnapsackStateRequest.java │ │ │ │ └── TransportKnapsackStateAction.java │ │ │ │ ├── pull │ │ │ │ ├── KnapsackPullAction.java │ │ │ │ └── KnapsackPullResponse.java │ │ │ │ ├── imp │ │ │ │ └── KnapsackImportAction.java │ │ │ │ ├── push │ │ │ │ ├── KnapsackPushAction.java │ │ │ │ └── KnapsackPushResponse.java │ │ │ │ └── exp │ │ │ │ ├── KnapsackExportAction.java │ │ │ │ └── KnapsackExportRequestBuilder.java │ │ │ └── rest │ │ │ └── action │ │ │ └── knapsack │ │ │ ├── state │ │ │ └── RestKnapsackStateAction.java │ │ │ └── abort │ │ │ └── RestKnapsackAbortAction.java │ ├── resources │ │ ├── META-INF │ │ │ └── services │ │ │ │ ├── org.xbib.io.archive.ArchiveCodec │ │ │ │ └── org.xbib.io.compress.CompressCodec │ │ └── plugin-descriptor.properties │ ├── templates │ │ └── plugin-descriptor.properties │ └── assemblies │ │ └── plugin.xml └── test │ ├── resources │ ├── macosx-knapsack-tar-test.tar.gz │ └── log4j2.xml │ └── java │ └── org │ ├── elasticsearch │ └── node │ │ └── MockNode.java │ └── xbib │ ├── suites │ ├── ListenerSuite.java │ ├── CompressionTestSuite.java │ ├── KnapsackTestSuite.java │ └── TestListener.java │ ├── io │ └── compress │ │ └── bzip2 │ │ ├── SimpleBZip2Tests.java │ │ ├── BZip2DivSufSortTests.java │ │ └── BZip2HuffmanStageDecoderTests.java │ └── elasticsearch │ └── plugin │ └── knapsack │ ├── KnapsackNodeTest.java │ ├── KnapsackSimpleTests.java │ └── KnapsackExportTests.java ├── .travis.yml ├── gradle ├── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── git.gradle └── publish.gradle ├── .gitignore ├── CREDITS.txt └── gradlew.bat /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'elasticsearch-knapsack' -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | group = org.xbib.elasticsearch.plugin 2 | version = 2.3.4.0 3 | org.gradle.daemon = true 4 | -------------------------------------------------------------------------------- /src/site/resources/knapsack.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jprante/elasticsearch-knapsack/HEAD/src/site/resources/knapsack.jpg -------------------------------------------------------------------------------- /src/site/resources/knapsack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jprante/elasticsearch-knapsack/HEAD/src/site/resources/knapsack.png -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: java 3 | jdk: 4 | - oraclejdk8 5 | 6 | cache: 7 | directories: 8 | - $HOME/.m2 9 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jprante/elasticsearch-knapsack/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /src/site/resources/knapsack-diagram-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jprante/elasticsearch-knapsack/HEAD/src/site/resources/knapsack-diagram-2.png -------------------------------------------------------------------------------- /src/site/resources/knapsack-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jprante/elasticsearch-knapsack/HEAD/src/site/resources/knapsack-diagram.png -------------------------------------------------------------------------------- /src/site/resources/hitchhiker-691581_1280.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jprante/elasticsearch-knapsack/HEAD/src/site/resources/hitchhiker-691581_1280.jpg -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/IndexIndicatorException.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | class IndexIndicatorException extends Exception { 4 | } 5 | -------------------------------------------------------------------------------- /src/test/resources/macosx-knapsack-tar-test.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jprante/elasticsearch-knapsack/HEAD/src/test/resources/macosx-knapsack-tar-test.tar.gz -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /data 2 | /work 3 | /logs 4 | /.idea 5 | /target 6 | .DS_Store 7 | *.iml 8 | /.settings 9 | /.classpath 10 | /.project 11 | /.gradle 12 | /build 13 | /plugins -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/simple/SimpleFilter.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.simple; 2 | 3 | public interface SimpleFilter { 4 | int code(byte[] buf, int off, int len); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/common/StreamFlags.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.common; 2 | 3 | public class StreamFlags { 4 | public int checkType = -1; 5 | public long backwardSize = -1; 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/FilterCoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | interface FilterCoder { 4 | 5 | boolean changesSize(); 6 | 7 | boolean nonLastOK(); 8 | 9 | boolean lastOK(); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/services/org.xbib.io.archive.ArchiveCodec: -------------------------------------------------------------------------------- 1 | org.xbib.io.archive.cpio.CpioArchiveCodec 2 | org.xbib.io.archive.esbulk.EsBulkArchiveCodec 3 | org.xbib.io.archive.tar.TarArchiveCodec 4 | org.xbib.io.archive.zip.ZipArchiveCodec -------------------------------------------------------------------------------- /src/main/resources/META-INF/services/org.xbib.io.compress.CompressCodec: -------------------------------------------------------------------------------- 1 | org.xbib.io.compress.gzip.GzipCompressCodec 2 | org.xbib.io.compress.lzf.LZFCompressCodec 3 | org.xbib.io.compress.xz.XZCompressCodec 4 | org.xbib.io.compress.bzip2.BZip2CompressCodec 5 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Aug 04 18:04:44 CEST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-bin.zip 7 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/FilterDecoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import java.io.InputStream; 4 | 5 | interface FilterDecoder extends FilterCoder { 6 | 7 | int getMemoryUsage(); 8 | 9 | InputStream getInputStream(InputStream in); 10 | } 11 | -------------------------------------------------------------------------------- /gradle/git.gradle: -------------------------------------------------------------------------------- 1 | 2 | task gitRelease(dependsOn: build) << { 3 | grgit.add(patterns: ['.'], update: true) 4 | grgit.commit(message: "release of ${project.version}") 5 | grgit.tag.remove(names: [project.version]) 6 | grgit.tag.add(name: project.version) 7 | grgit.push() 8 | grgit.push(tags: true) 9 | } -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/FilterEncoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | interface FilterEncoder extends FilterCoder { 4 | 5 | long getFilterID(); 6 | 7 | byte[] getFilterProps(); 8 | 9 | boolean supportsFlushing(); 10 | 11 | FinishableOutputStream getOutputStream(FinishableOutputStream out); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/index/IndexRecord.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.index; 2 | 3 | class IndexRecord { 4 | final long unpadded; 5 | final long uncompressed; 6 | 7 | IndexRecord(long unpadded, long uncompressed) { 8 | this.unpadded = unpadded; 9 | this.uncompressed = uncompressed; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/lz/Matches.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.lz; 2 | 3 | public final class Matches { 4 | public final int[] len; 5 | public final int[] dist; 6 | public int count = 0; 7 | 8 | Matches(int countMax) { 9 | len = new int[countMax]; 10 | dist = new int[countMax]; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/resources/plugin-descriptor.properties: -------------------------------------------------------------------------------- 1 | classname=org.xbib.elasticsearch.plugin.knapsack.KnapsackPlugin 2 | name=knapsack 3 | description=Knapsack export/import for Elasticsearch 4 | version=${project.version} 5 | java.version=${java.compiler.version} 6 | elasticsearch.version=${elasticsearch.version} 7 | jvm=true 8 | site=false 9 | isolated=true 10 | -------------------------------------------------------------------------------- /src/main/templates/plugin-descriptor.properties: -------------------------------------------------------------------------------- 1 | classname=${descriptor.classname} 2 | name=${descriptor.name} 3 | description=${descriptor.description} 4 | jvm=${descriptor.jvm} 5 | site=${descriptor.site} 6 | isolated=${descriptor.isolated} 7 | version=${descriptor.version} 8 | java.version=${descriptor.javaVersion} 9 | elasticsearch.version=${descriptor.elasticsearchVersion} 10 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/index/BlockInfo.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.index; 2 | 3 | import org.xbib.io.compress.xz.common.StreamFlags; 4 | 5 | public class BlockInfo { 6 | public StreamFlags streamFlags; 7 | public long compressedOffset; 8 | public long uncompressedOffset; 9 | public long unpaddedSize; 10 | public long uncompressedSize; 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/check/None.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.check; 2 | 3 | public class None extends Check { 4 | public None() { 5 | size = 0; 6 | name = "None"; 7 | } 8 | 9 | public void update(byte[] buf, int off, int len) { 10 | } 11 | 12 | public byte[] finish() { 13 | byte[] empty = new byte[0]; 14 | return empty; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/DeltaCoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | abstract class DeltaCoder implements FilterCoder { 4 | public static final long FILTER_ID = 0x03; 5 | 6 | public boolean changesSize() { 7 | return false; 8 | } 9 | 10 | public boolean nonLastOK() { 11 | return true; 12 | } 13 | 14 | public boolean lastOK() { 15 | return false; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/LZMA2Coder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | abstract class LZMA2Coder implements FilterCoder { 4 | public static final long FILTER_ID = 0x21; 5 | 6 | public boolean changesSize() { 7 | return true; 8 | } 9 | 10 | public boolean nonLastOK() { 11 | return false; 12 | } 13 | 14 | public boolean lastOK() { 15 | return true; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/bzip2/BZip2Exception.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.bzip2; 2 | 3 | import java.io.IOException; 4 | 5 | /** 6 | * Indicates that a data format error was encountered while attempting to decode bzip2 data 7 | */ 8 | public class BZip2Exception extends IOException { 9 | 10 | /** 11 | * @param reason The exception's reason string 12 | */ 13 | public BZip2Exception(String reason) { 14 | 15 | super(reason); 16 | 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/XZFormatException.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | /** 4 | * Thrown when the input data is not in the XZ format. 5 | */ 6 | public class XZFormatException extends XZIOException { 7 | private static final long serialVersionUID = 3L; 8 | 9 | /** 10 | * Creates a new exception with the default error detail message. 11 | */ 12 | public XZFormatException() { 13 | super("Input is not in the XZ format"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/delta/DeltaDecoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.delta; 2 | 3 | public class DeltaDecoder extends DeltaCoder { 4 | public DeltaDecoder(int distance) { 5 | super(distance); 6 | } 7 | 8 | public void decode(byte[] buf, int off, int len) { 9 | int end = off + len; 10 | for (int i = off; i < end; ++i) { 11 | buf[i] += history[(distance + pos) & DISTANCE_MASK]; 12 | history[pos-- & DISTANCE_MASK] = buf[i]; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/org/elasticsearch/node/MockNode.java: -------------------------------------------------------------------------------- 1 | 2 | package org.elasticsearch.node; 3 | 4 | import org.elasticsearch.Version; 5 | import org.elasticsearch.common.settings.Settings; 6 | import org.elasticsearch.env.Environment; 7 | import org.elasticsearch.plugins.Plugin; 8 | 9 | import java.util.Collection; 10 | 11 | public class MockNode extends Node { 12 | 13 | public MockNode(Environment tmpEnv, Collection> classpathPlugins) { 14 | super(tmpEnv, Version.CURRENT, classpathPlugins); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/XZIOException.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | /** 4 | * Generic {@link java.io.IOException IOException} specific to this package. 5 | * The other IOExceptions in this package extend 6 | * from XZIOException. 7 | */ 8 | public class XZIOException extends java.io.IOException { 9 | private static final long serialVersionUID = 3L; 10 | 11 | public XZIOException() { 12 | super(); 13 | } 14 | 15 | public XZIOException(String s) { 16 | super(s); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/cpio/CpioSession.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.archive.cpio; 2 | 3 | import org.xbib.io.BytesProgressWatcher; 4 | import org.xbib.io.archive.ArchiveSession; 5 | 6 | /** 7 | * Cpio Session 8 | */ 9 | public class CpioSession extends ArchiveSession { 10 | 11 | protected CpioSession(BytesProgressWatcher watcher) { 12 | super(watcher); 13 | } 14 | 15 | @Override 16 | protected String getName() { 17 | return CpioArchiveCodec.NAME; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/delta/DeltaEncoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.delta; 2 | 3 | public class DeltaEncoder extends DeltaCoder { 4 | public DeltaEncoder(int distance) { 5 | super(distance); 6 | } 7 | 8 | public void encode(byte[] in, int in_off, int len, byte[] out) { 9 | for (int i = 0; i < len; ++i) { 10 | byte tmp = history[(distance + pos) & DISTANCE_MASK]; 11 | history[pos-- & DISTANCE_MASK] = in[in_off + i]; 12 | out[i] = (byte) (in[in_off + i] - tmp); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/delta/DeltaCoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.delta; 2 | 3 | abstract class DeltaCoder { 4 | static final int DISTANCE_MIN = 1; 5 | static final int DISTANCE_MAX = 256; 6 | static final int DISTANCE_MASK = DISTANCE_MAX - 1; 7 | 8 | final int distance; 9 | final byte[] history = new byte[DISTANCE_MAX]; 10 | int pos = 0; 11 | 12 | DeltaCoder(int distance) { 13 | if (distance < DISTANCE_MIN || distance > DISTANCE_MAX) { 14 | throw new IllegalArgumentException(); 15 | } 16 | 17 | this.distance = distance; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/rangecoder/RangeCoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.rangecoder; 2 | 3 | import java.util.Arrays; 4 | 5 | public abstract class RangeCoder { 6 | static final int SHIFT_BITS = 8; 7 | static final int TOP_MASK = 0xFF000000; 8 | static final int BIT_MODEL_TOTAL_BITS = 11; 9 | static final int BIT_MODEL_TOTAL = 1 << BIT_MODEL_TOTAL_BITS; 10 | static final short PROB_INIT = (short) (BIT_MODEL_TOTAL / 2); 11 | static final int MOVE_BITS = 5; 12 | 13 | public static final void initProbs(short[] probs) { 14 | Arrays.fill(probs, PROB_INIT); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/common/Util.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.common; 2 | 3 | public class Util { 4 | public static final int STREAM_HEADER_SIZE = 12; 5 | public static final long BACKWARD_SIZE_MAX = 1L << 34; 6 | public static final int BLOCK_HEADER_SIZE_MAX = 1024; 7 | public static final long VLI_MAX = Long.MAX_VALUE; 8 | public static final int VLI_SIZE_MAX = 9; 9 | 10 | public static int getVLISize(long num) { 11 | int size = 0; 12 | do { 13 | ++size; 14 | num >>= 7; 15 | } while (num != 0); 16 | 17 | return size; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/check/SHA256.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.check; 2 | 3 | public class SHA256 extends Check { 4 | private final java.security.MessageDigest sha256; 5 | 6 | public SHA256() throws java.security.NoSuchAlgorithmException { 7 | size = 32; 8 | name = "SHA-256"; 9 | sha256 = java.security.MessageDigest.getInstance("SHA-256"); 10 | } 11 | 12 | public void update(byte[] buf, int off, int len) { 13 | sha256.update(buf, off, len); 14 | } 15 | 16 | public byte[] finish() { 17 | byte[] buf = sha256.digest(); 18 | sha256.reset(); 19 | return buf; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/lz/CRC32Hash.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.lz; 2 | 3 | /** 4 | * Provides a CRC32 table using the polynomial from IEEE 802.3. 5 | */ 6 | class CRC32Hash { 7 | private static final int CRC32_POLY = 0xEDB88320; 8 | 9 | static final int[] crcTable = new int[256]; 10 | 11 | static { 12 | for (int i = 0; i < 256; ++i) { 13 | int r = i; 14 | 15 | for (int j = 0; j < 8; ++j) { 16 | if ((r & 1) != 0) { 17 | r = (r >>> 1) ^ CRC32_POLY; 18 | } else { 19 | r >>>= 1; 20 | } 21 | } 22 | 23 | crcTable[i] = r; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/check/CRC32.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.check; 2 | 3 | public class CRC32 extends Check { 4 | private final java.util.zip.CRC32 state = new java.util.zip.CRC32(); 5 | 6 | public CRC32() { 7 | size = 4; 8 | name = "CRC32"; 9 | } 10 | 11 | public void update(byte[] buf, int off, int len) { 12 | state.update(buf, off, len); 13 | } 14 | 15 | public byte[] finish() { 16 | long value = state.getValue(); 17 | byte[] buf = new byte[]{(byte) (value), 18 | (byte) (value >>> 8), 19 | (byte) (value >>> 16), 20 | (byte) (value >>> 24)}; 21 | state.reset(); 22 | return buf; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/DeltaDecoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import java.io.InputStream; 4 | 5 | class DeltaDecoder extends DeltaCoder implements FilterDecoder { 6 | private final int distance; 7 | 8 | DeltaDecoder(byte[] props) throws UnsupportedOptionsException { 9 | if (props.length != 1) { 10 | throw new UnsupportedOptionsException( 11 | "Unsupported Delta filter properties"); 12 | } 13 | 14 | distance = (props[0] & 0xFF) + 1; 15 | } 16 | 17 | public int getMemoryUsage() { 18 | return 1; 19 | } 20 | 21 | public InputStream getInputStream(InputStream in) { 22 | return new DeltaInputStream(in, distance); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/org/xbib/suites/ListenerSuite.java: -------------------------------------------------------------------------------- 1 | package org.xbib.suites; 2 | 3 | import org.junit.runner.Runner; 4 | import org.junit.runner.notification.RunNotifier; 5 | import org.junit.runners.Suite; 6 | import org.junit.runners.model.InitializationError; 7 | import org.junit.runners.model.RunnerBuilder; 8 | 9 | public class ListenerSuite extends Suite { 10 | 11 | private final TestListener listener = new TestListener(); 12 | 13 | public ListenerSuite(Class klass, RunnerBuilder builder) throws InitializationError { 14 | super(klass, builder); 15 | } 16 | 17 | @Override 18 | protected void runChild(Runner runner, RunNotifier notifier) { 19 | notifier.addListener(listener); 20 | runner.run(notifier); 21 | notifier.removeListener(listener); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/ConnectionFactory.java: -------------------------------------------------------------------------------- 1 | 2 | package org.xbib.io; 3 | 4 | import java.io.IOException; 5 | import java.net.URI; 6 | 7 | /** 8 | * A connection factory 9 | */ 10 | public interface ConnectionFactory { 11 | 12 | String getName(); 13 | 14 | /** 15 | * Checks if this connection factory can handle this URI. 16 | * 17 | * @param uri the URI to check 18 | * @return true if the URI can be provided, otherwise false 19 | */ 20 | boolean canOpen(URI uri); 21 | 22 | /** 23 | * Creates a new connection 24 | * 25 | * @param uri the URI for the connection 26 | * @return the connection 27 | * @throws IOException if the connection can not be established 28 | */ 29 | Connection getConnection(URI uri) throws IOException; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/DeltaEncoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | class DeltaEncoder extends DeltaCoder implements FilterEncoder { 4 | private final DeltaOptions options; 5 | private final byte[] props = new byte[1]; 6 | 7 | DeltaEncoder(DeltaOptions options) { 8 | props[0] = (byte) (options.getDistance() - 1); 9 | this.options = (DeltaOptions) options.clone(); 10 | } 11 | 12 | public long getFilterID() { 13 | return FILTER_ID; 14 | } 15 | 16 | public byte[] getFilterProps() { 17 | return props; 18 | } 19 | 20 | public boolean supportsFlushing() { 21 | return true; 22 | } 23 | 24 | public FinishableOutputStream getOutputStream(FinishableOutputStream out) { 25 | return options.getOutputStream(out); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/BCJCoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | abstract class BCJCoder implements FilterCoder { 4 | public static final long X86_FILTER_ID = 0x04; 5 | public static final long POWERPC_FILTER_ID = 0x05; 6 | public static final long IA64_FILTER_ID = 0x06; 7 | public static final long ARM_FILTER_ID = 0x07; 8 | public static final long ARMTHUMB_FILTER_ID = 0x08; 9 | public static final long SPARC_FILTER_ID = 0x09; 10 | 11 | public static boolean isBCJFilterID(long filterID) { 12 | return filterID >= 0x04 && filterID <= 0x09; 13 | } 14 | 15 | public boolean changesSize() { 16 | return false; 17 | } 18 | 19 | public boolean nonLastOK() { 20 | return true; 21 | } 22 | 23 | public boolean lastOK() { 24 | return false; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/SPARCOptions.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import org.xbib.io.compress.xz.simple.SPARC; 4 | 5 | import java.io.InputStream; 6 | 7 | /** 8 | * BCJ filter for SPARC. 9 | */ 10 | public class SPARCOptions extends BCJOptions { 11 | private static final int ALIGNMENT = 4; 12 | 13 | public SPARCOptions() { 14 | super(ALIGNMENT); 15 | } 16 | 17 | public FinishableOutputStream getOutputStream(FinishableOutputStream out) { 18 | return new SimpleOutputStream(out, new SPARC(true, startOffset)); 19 | } 20 | 21 | public InputStream getInputStream(InputStream in) { 22 | return new SimpleInputStream(in, new SPARC(false, startOffset)); 23 | } 24 | 25 | FilterEncoder getFilterEncoder() { 26 | return new BCJEncoder(this, BCJCoder.SPARC_FILTER_ID); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/UnsupportedOptionsException.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | /** 4 | * Thrown when compression options not supported by this implementation 5 | * are detected. Some other implementation might support those options. 6 | */ 7 | public class UnsupportedOptionsException extends XZIOException { 8 | private static final long serialVersionUID = 3L; 9 | 10 | /** 11 | * Creates a new UnsupportedOptionsException with null 12 | * as its error detail message. 13 | */ 14 | public UnsupportedOptionsException() { 15 | } 16 | 17 | /** 18 | * Creates a new UnsupportedOptionsException with the given 19 | * error detail message. 20 | * 21 | * @param s error detail message 22 | */ 23 | public UnsupportedOptionsException(String s) { 24 | super(s); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/ARMOptions.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import org.xbib.io.compress.xz.simple.ARM; 4 | 5 | import java.io.InputStream; 6 | 7 | /** 8 | * BCJ filter for little endian ARM instructions. 9 | */ 10 | public class ARMOptions extends BCJOptions { 11 | private static final int ALIGNMENT = 4; 12 | 13 | public ARMOptions() { 14 | super(ALIGNMENT); 15 | } 16 | 17 | public FinishableOutputStream getOutputStream(FinishableOutputStream out) { 18 | return new SimpleOutputStream(out, new ARM(true, startOffset)); 19 | } 20 | 21 | public InputStream getInputStream(InputStream in) { 22 | return new SimpleInputStream(in, new ARM(false, startOffset)); 23 | } 24 | 25 | FilterEncoder getFilterEncoder() { 26 | return new BCJEncoder(this, BCJCoder.ARM_FILTER_ID); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/IA64Options.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import org.xbib.io.compress.xz.simple.IA64; 4 | 5 | import java.io.InputStream; 6 | 7 | /** 8 | * BCJ filter for Itanium (IA-64) instructions. 9 | */ 10 | public class IA64Options extends BCJOptions { 11 | private static final int ALIGNMENT = 16; 12 | 13 | public IA64Options() { 14 | super(ALIGNMENT); 15 | } 16 | 17 | public FinishableOutputStream getOutputStream(FinishableOutputStream out) { 18 | return new SimpleOutputStream(out, new IA64(true, startOffset)); 19 | } 20 | 21 | public InputStream getInputStream(InputStream in) { 22 | return new SimpleInputStream(in, new IA64(false, startOffset)); 23 | } 24 | 25 | FilterEncoder getFilterEncoder() { 26 | return new BCJEncoder(this, BCJCoder.IA64_FILTER_ID); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/X86Options.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import org.xbib.io.compress.xz.simple.X86; 4 | 5 | import java.io.InputStream; 6 | 7 | /** 8 | * BCJ filter for x86 (32-bit and 64-bit) instructions. 9 | */ 10 | public class X86Options extends BCJOptions { 11 | private static final int ALIGNMENT = 1; 12 | 13 | public X86Options() { 14 | super(ALIGNMENT); 15 | } 16 | 17 | public FinishableOutputStream getOutputStream(FinishableOutputStream out) { 18 | return new SimpleOutputStream(out, new X86(true, startOffset)); 19 | } 20 | 21 | public InputStream getInputStream(InputStream in) { 22 | return new SimpleInputStream(in, new X86(false, startOffset)); 23 | } 24 | 25 | FilterEncoder getFilterEncoder() { 26 | return new BCJEncoder(this, BCJCoder.X86_FILTER_ID); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/common/EncoderUtil.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.common; 2 | 3 | import java.io.IOException; 4 | import java.io.OutputStream; 5 | import java.util.zip.CRC32; 6 | 7 | public class EncoderUtil extends Util { 8 | public static void writeCRC32(OutputStream out, byte[] buf) 9 | throws IOException { 10 | CRC32 crc32 = new CRC32(); 11 | crc32.update(buf); 12 | long value = crc32.getValue(); 13 | 14 | for (int i = 0; i < 4; ++i) { 15 | out.write((byte) (value >>> (i * 8))); 16 | } 17 | } 18 | 19 | public static void encodeVLI(OutputStream out, long num) 20 | throws IOException { 21 | while (num >= 0x80) { 22 | out.write((byte) (num | 0x80)); 23 | num >>>= 7; 24 | } 25 | 26 | out.write((byte) num); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/FinishableOutputStream.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import java.io.IOException; 4 | import java.io.OutputStream; 5 | 6 | /** 7 | * Output stream that supports finishing without closing 8 | * the underlying stream. 9 | */ 10 | public abstract class FinishableOutputStream extends OutputStream { 11 | /** 12 | * Finish the stream without closing the underlying stream. 13 | * No more data may be written to the stream after finishing. 14 | *

15 | * The finish method of FinishableOutputStream 16 | * does nothing. Subclasses should override it if they need finishing 17 | * support, which is the case, for example, with compressors. 18 | * 19 | * @throws java.io.IOException 20 | */ 21 | public void finish() throws IOException { 22 | } 23 | 24 | ; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/PowerPCOptions.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import org.xbib.io.compress.xz.simple.PowerPC; 4 | 5 | import java.io.InputStream; 6 | 7 | /** 8 | * BCJ filter for big endian PowerPC instructions. 9 | */ 10 | public class PowerPCOptions extends BCJOptions { 11 | private static final int ALIGNMENT = 4; 12 | 13 | public PowerPCOptions() { 14 | super(ALIGNMENT); 15 | } 16 | 17 | public FinishableOutputStream getOutputStream(FinishableOutputStream out) { 18 | return new SimpleOutputStream(out, new PowerPC(true, startOffset)); 19 | } 20 | 21 | public InputStream getInputStream(InputStream in) { 22 | return new SimpleInputStream(in, new PowerPC(false, startOffset)); 23 | } 24 | 25 | FilterEncoder getFilterEncoder() { 26 | return new BCJEncoder(this, BCJCoder.POWERPC_FILTER_ID); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/assemblies/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | plugin 4 | 5 | zip 6 | 7 | false 8 | 9 | 10 | / 11 | true 12 | false 13 | compile 14 | 15 | org.xbib.elasticsearch.plugin 16 | 17 | 18 | 19 | 20 | 21 | ${basedir}/src/main/resources/plugin-descriptor.properties 22 | / 23 | true 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/ARMThumbOptions.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import org.xbib.io.compress.xz.simple.ARMThumb; 4 | 5 | import java.io.InputStream; 6 | 7 | /** 8 | * BCJ filter for little endian ARM-Thumb instructions. 9 | */ 10 | public class ARMThumbOptions extends BCJOptions { 11 | private static final int ALIGNMENT = 2; 12 | 13 | public ARMThumbOptions() { 14 | super(ALIGNMENT); 15 | } 16 | 17 | public FinishableOutputStream getOutputStream(FinishableOutputStream out) { 18 | return new SimpleOutputStream(out, new ARMThumb(true, startOffset)); 19 | } 20 | 21 | public InputStream getInputStream(InputStream in) { 22 | return new SimpleInputStream(in, new ARMThumb(false, startOffset)); 23 | } 24 | 25 | FilterEncoder getFilterEncoder() { 26 | return new BCJEncoder(this, BCJCoder.ARMTHUMB_FILTER_ID); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/CorruptedInputException.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | /** 4 | * Thrown when the compressed input data is corrupt. 5 | * However, it is possible that some or all of the data 6 | * already read from the input stream was corrupt too. 7 | */ 8 | public class CorruptedInputException extends XZIOException { 9 | private static final long serialVersionUID = 3L; 10 | 11 | /** 12 | * Creates a new CorruptedInputException with 13 | * the default error detail message. 14 | */ 15 | public CorruptedInputException() { 16 | super("Compressed data is corrupt"); 17 | } 18 | 19 | /** 20 | * Creates a new CorruptedInputException with 21 | * the specified error detail message. 22 | * 23 | * @param s error detail message 24 | */ 25 | public CorruptedInputException(String s) { 26 | super(s); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/Connection.java: -------------------------------------------------------------------------------- 1 | 2 | package org.xbib.io; 3 | 4 | import java.io.IOException; 5 | import java.net.URI; 6 | 7 | /** 8 | * A Connection is an access to a resource via a scheme or a protocol. 9 | * Each connection can serve multiple sessions in parallel. 10 | */ 11 | public interface Connection { 12 | 13 | /** 14 | * Set URI of this connection 15 | * 16 | * @param uri 17 | */ 18 | Connection setURI(URI uri); 19 | 20 | /** 21 | * Get URI of this connection 22 | */ 23 | URI getURI(); 24 | 25 | /** 26 | * Close connection and close all sessions 27 | */ 28 | void close() throws IOException; 29 | 30 | /** 31 | * Create a new session on this connection 32 | * 33 | * @return the session 34 | * @throws IOException if the session can not be created 35 | */ 36 | S createSession() throws IOException; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * XZ data compression 3 | * 4 | * This aims to be a complete implementation of XZ 5 | * data compression in pure Java. Features:

  • Full support for the .xz 6 | * file format specification version 1.0.4
  • Single-threaded streamed 7 | * compression and decompression
  • Single-threaded decompression with 8 | * limited random access support
  • Raw streams (no .xz headers) for 9 | * advanced users, including LZMA2 with preset dictionary

10 | * Threading is planned but it is unknown when it will be implemented. 11 | * 12 | * Start by reading the documentation of 13 | * {@link org.xbib.io.compress.xz.XZOutputStream} and 14 | * {@link org.xbib.io.compress.xz.XZInputStream}. If you use XZ inside another file 15 | * format or protocol, see also {@link org.xbib.io.compress.xz.SingleXZInputStream}. 16 | 17 | */ 18 | package org.xbib.io.compress.xz; 19 | -------------------------------------------------------------------------------- /src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | org.apache.maven.skins 5 | maven-fluido-skin 6 | 1.2.1 7 | 8 | 9 | 10 | true 11 | true 12 | 13 | jprante/elasticsearch-knapsack 14 | right 15 | black 16 | 17 | 18 | xbib 19 | true 20 | true 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 |

29 | 30 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/LZMA2Decoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import java.io.InputStream; 4 | 5 | class LZMA2Decoder extends LZMA2Coder implements FilterDecoder { 6 | private int dictSize; 7 | 8 | LZMA2Decoder(byte[] props) throws UnsupportedOptionsException { 9 | // Up to 1.5 GiB dictionary is supported. The bigger ones 10 | // are too big for int. 11 | if (props.length != 1 || (props[0] & 0xFF) > 37) { 12 | throw new UnsupportedOptionsException( 13 | "Unsupported LZMA2 properties"); 14 | } 15 | 16 | dictSize = 2 | (props[0] & 1); 17 | dictSize <<= (props[0] >>> 1) + 11; 18 | } 19 | 20 | public int getMemoryUsage() { 21 | return LZMA2InputStream.getMemoryUsage(dictSize); 22 | } 23 | 24 | public InputStream getInputStream(InputStream in) { 25 | return new LZMA2InputStream(in, dictSize); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/CountingInputStream.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import java.io.FilterInputStream; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | 7 | /** 8 | * Counts the number of bytes read from an input stream. 9 | */ 10 | class CountingInputStream extends FilterInputStream { 11 | private long size = 0; 12 | 13 | public CountingInputStream(InputStream in) { 14 | super(in); 15 | } 16 | 17 | public int read() throws IOException { 18 | int ret = in.read(); 19 | if (ret != -1 && size >= 0) { 20 | ++size; 21 | } 22 | 23 | return ret; 24 | } 25 | 26 | public int read(byte[] b, int off, int len) throws IOException { 27 | int ret = in.read(b, off, len); 28 | if (ret > 0 && size >= 0) { 29 | size += ret; 30 | } 31 | 32 | return ret; 33 | } 34 | 35 | public long getSize() { 36 | return size; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/Packet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io; 17 | 18 | import java.util.Map; 19 | 20 | /** 21 | * A packet for transporting data chunks with meta data in sessions 22 | */ 23 | public interface Packet

{ 24 | 25 | Map meta(); 26 | 27 | Packet

meta(String key, Object value); 28 | 29 | P payload(); 30 | 31 | Packet

payload(P packet); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/RawCoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | class RawCoder { 4 | static void validate(FilterCoder[] filters) 5 | throws UnsupportedOptionsException { 6 | for (int i = 0; i < filters.length - 1; ++i) { 7 | if (!filters[i].nonLastOK()) { 8 | throw new UnsupportedOptionsException( 9 | "Unsupported XZ filter chain"); 10 | } 11 | } 12 | 13 | if (!filters[filters.length - 1].lastOK()) { 14 | throw new UnsupportedOptionsException( 15 | "Unsupported XZ filter chain"); 16 | } 17 | 18 | int changesSizeCount = 0; 19 | for (int i = 0; i < filters.length; ++i) { 20 | if (filters[i].changesSize()) { 21 | ++changesSizeCount; 22 | } 23 | } 24 | 25 | if (changesSizeCount > 3) { 26 | throw new UnsupportedOptionsException( 27 | "Unsupported XZ filter chain"); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/knapsack/KnapsackModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.knapsack; 17 | 18 | import org.elasticsearch.common.inject.Binder; 19 | import org.elasticsearch.common.inject.Module; 20 | 21 | public class KnapsackModule implements Module { 22 | 23 | @Override 24 | public void configure(Binder binder) { 25 | binder.bind(KnapsackService.class).asEagerSingleton(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/zip/ZipSession.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.zip; 17 | 18 | import org.xbib.io.BytesProgressWatcher; 19 | import org.xbib.io.archive.ArchiveSession; 20 | 21 | public class ZipSession extends ArchiveSession { 22 | 23 | protected ZipSession(BytesProgressWatcher watcher) { 24 | super(watcher); 25 | } 26 | 27 | @Override 28 | public String getName() { 29 | return "zip"; 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/knapsack/KnapsackRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.knapsack; 17 | 18 | import org.elasticsearch.common.unit.TimeValue; 19 | 20 | import java.util.Map; 21 | 22 | public interface KnapsackRequest { 23 | 24 | String getCluster(); 25 | 26 | String getHost(); 27 | 28 | int getPort(); 29 | 30 | boolean getSniff(); 31 | 32 | TimeValue getTimeout(); 33 | 34 | Map getIndexTypeNames(); 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/org/xbib/suites/CompressionTestSuite.java: -------------------------------------------------------------------------------- 1 | package org.xbib.suites; 2 | 3 | import org.junit.runner.RunWith; 4 | import org.junit.runners.Suite; 5 | import org.xbib.io.compress.bzip2.BZip2BitInputStreamTests; 6 | import org.xbib.io.compress.bzip2.BZip2BitOutputStreamTests; 7 | import org.xbib.io.compress.bzip2.BZip2BlockDecompressorTests; 8 | import org.xbib.io.compress.bzip2.BZip2DivSufSortTests; 9 | import org.xbib.io.compress.bzip2.BZip2HuffmanStageDecoderTests; 10 | import org.xbib.io.compress.bzip2.BZip2OutputStreamTests; 11 | import org.xbib.io.compress.bzip2.SimpleBZip2Tests; 12 | import org.xbib.io.compress.bzip2.HuffmanAllocatorTests; 13 | 14 | 15 | @RunWith(Suite.class) 16 | @Suite.SuiteClasses({ 17 | BZip2BitInputStreamTests.class, 18 | BZip2BitOutputStreamTests.class, 19 | BZip2BlockDecompressorTests.class, 20 | BZip2DivSufSortTests.class, 21 | BZip2HuffmanStageDecoderTests.class, 22 | BZip2OutputStreamTests.class, 23 | HuffmanAllocatorTests.class, 24 | SimpleBZip2Tests.class 25 | }) 26 | public class CompressionTestSuite { 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/bzip2/BZip2CompressCodec.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.bzip2; 2 | 3 | import org.xbib.io.compress.CompressCodec; 4 | 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.OutputStream; 8 | 9 | public class BZip2CompressCodec implements CompressCodec { 10 | 11 | @Override 12 | public String getName() { 13 | return "bz2"; 14 | } 15 | 16 | @Override 17 | public BZip2InputStream decode(InputStream in) throws IOException { 18 | return new BZip2InputStream(in); 19 | } 20 | 21 | @Override 22 | public BZip2InputStream decode(InputStream in, int bufsize) throws IOException { 23 | return new BZip2InputStream(in, bufsize); 24 | } 25 | 26 | @Override 27 | public BZip2OutputStream encode(OutputStream out) throws IOException { 28 | return new BZip2OutputStream(out); 29 | } 30 | 31 | @Override 32 | public BZip2OutputStream encode(OutputStream out, int bufsize) throws IOException { 33 | return new BZip2OutputStream(out, bufsize); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/org/xbib/suites/KnapsackTestSuite.java: -------------------------------------------------------------------------------- 1 | package org.xbib.suites; 2 | 3 | import org.junit.runner.RunWith; 4 | import org.junit.runners.Suite; 5 | import org.xbib.elasticsearch.plugin.knapsack.KnapsackExportTests; 6 | import org.xbib.elasticsearch.plugin.knapsack.KnapsackImportTests; 7 | import org.xbib.elasticsearch.plugin.knapsack.KnapsackSimpleTests; 8 | import org.xbib.elasticsearch.plugin.knapsack.KnapsackSplitTests; 9 | import org.xbib.elasticsearch.plugin.knapsack.bulk.KnapsackBulkTests; 10 | import org.xbib.elasticsearch.plugin.knapsack.cpio.KnapsackCpioTests; 11 | import org.xbib.elasticsearch.plugin.knapsack.tar.KnapsackTarTests; 12 | import org.xbib.elasticsearch.plugin.knapsack.zip.KnapsackZipTests; 13 | 14 | @RunWith(ListenerSuite.class) 15 | @Suite.SuiteClasses({ 16 | KnapsackSimpleTests.class, 17 | KnapsackExportTests.class, 18 | KnapsackImportTests.class, 19 | KnapsackTarTests.class, 20 | KnapsackZipTests.class, 21 | KnapsackCpioTests.class, 22 | KnapsackBulkTests.class, 23 | KnapsackSplitTests.class 24 | }) 25 | public class KnapsackTestSuite { 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/XZCompressCodec.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import org.xbib.io.compress.CompressCodec; 4 | 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.OutputStream; 8 | 9 | public class XZCompressCodec implements CompressCodec { 10 | 11 | @Override 12 | public String getName() { 13 | return "xz"; 14 | } 15 | 16 | @Override 17 | public XZInputStream decode(InputStream in) throws IOException { 18 | return new XZInputStream(in); 19 | } 20 | 21 | @Override 22 | public XZInputStream decode(InputStream in, int bufsize) throws IOException { 23 | return new XZInputStream(in, bufsize / 1024); // KB limit 24 | } 25 | 26 | @Override 27 | public XZOutputStream encode(OutputStream out) throws IOException { 28 | return new XZOutputStream(out, new LZMA2Options()); 29 | } 30 | 31 | @Override 32 | public XZOutputStream encode(OutputStream out, int bufsize) throws IOException { 33 | return new XZOutputStream(out, new LZMA2Options()); // ignore bufsize 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/tar/TarSession.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.tar; 17 | 18 | import org.xbib.io.BytesProgressWatcher; 19 | import org.xbib.io.archive.ArchiveSession; 20 | 21 | public class TarSession extends ArchiveSession { 22 | 23 | protected TarSession(BytesProgressWatcher watcher) { 24 | super(watcher); 25 | } 26 | 27 | @Override 28 | public String getName() { 29 | return TarArchiveCodec.NAME; 30 | } 31 | } -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/esbulk/EsBulkSession.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.esbulk; 17 | 18 | import org.xbib.io.BytesProgressWatcher; 19 | import org.xbib.io.archive.ArchiveSession; 20 | 21 | public class EsBulkSession extends ArchiveSession { 22 | 23 | protected EsBulkSession(BytesProgressWatcher watcher) { 24 | super(watcher); 25 | } 26 | 27 | @Override 28 | protected String getName() { 29 | return EsBulkArchiveCodec.NAME; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/BCJEncoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | class BCJEncoder extends BCJCoder implements FilterEncoder { 4 | private final BCJOptions options; 5 | private final long filterID; 6 | private final byte[] props; 7 | 8 | BCJEncoder(BCJOptions options, long filterID) { 9 | assert isBCJFilterID(filterID); 10 | int startOffset = options.getStartOffset(); 11 | 12 | if (startOffset == 0) { 13 | props = new byte[0]; 14 | } else { 15 | props = new byte[4]; 16 | for (int i = 0; i < 4; ++i) { 17 | props[i] = (byte) (startOffset >>> (i * 8)); 18 | } 19 | } 20 | 21 | this.filterID = filterID; 22 | this.options = (BCJOptions) options.clone(); 23 | } 24 | 25 | public long getFilterID() { 26 | return filterID; 27 | } 28 | 29 | public byte[] getFilterProps() { 30 | return props; 31 | } 32 | 33 | public boolean supportsFlushing() { 34 | return false; 35 | } 36 | 37 | public FinishableOutputStream getOutputStream(FinishableOutputStream out) { 38 | return options.getOutputStream(out); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/CompressCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.compress; 17 | 18 | import java.io.IOException; 19 | import java.io.InputStream; 20 | import java.io.OutputStream; 21 | 22 | public interface CompressCodec { 23 | 24 | String getName(); 25 | 26 | I decode(InputStream in) throws IOException; 27 | 28 | I decode(InputStream in, int bufsize) throws IOException; 29 | 30 | O encode(OutputStream out) throws IOException; 31 | 32 | O encode(OutputStream out, int bufsize) throws IOException; 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/Session.java: -------------------------------------------------------------------------------- 1 | 2 | package org.xbib.io; 3 | 4 | import java.io.IOException; 5 | import java.nio.file.Path; 6 | import java.util.EnumSet; 7 | 8 | /** 9 | * The Session interface is used for being opened, receive 10 | * operations, and being closed. Sessions must be opened before the first 11 | * operation and closed after the last operation. 12 | */ 13 | public interface Session

{ 14 | 15 | enum Mode { 16 | 17 | READ, WRITE, READ_WRITE, APPEND, DEFERRED_WRITE, OVERWRITE, URI_ENCODED, NONE; 18 | } 19 | 20 | /** 21 | * Open valve with a given input/output mode 22 | * 23 | * @throws java.io.IOException if valve can not be opened 24 | */ 25 | void open(EnumSet mode, Path path) throws IOException; 26 | 27 | P newPacket(); 28 | 29 | P read() throws IOException; 30 | 31 | void write(P packet) throws IOException; 32 | 33 | /** 34 | * Close valve 35 | * 36 | * @throws java.io.IOException if valve can not be closed 37 | */ 38 | void close() throws IOException; 39 | 40 | /** 41 | * Checks if this session has been successfully opened. 42 | * 43 | * @return true if the session is open 44 | */ 45 | boolean isOpen(); 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/StringPacket.java: -------------------------------------------------------------------------------- 1 | 2 | package org.xbib.io; 3 | 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | public class StringPacket implements Packet { 8 | 9 | private Map meta = new HashMap<>(); 10 | private String name; 11 | private String string; 12 | 13 | public StringPacket() { 14 | } 15 | 16 | public StringPacket name(String name) { 17 | this.name = name; 18 | return this; 19 | } 20 | 21 | public String name() { 22 | return name; 23 | } 24 | 25 | public StringPacket packet(String string) { 26 | this.string = string; 27 | return this; 28 | } 29 | 30 | @Override 31 | public Map meta() { 32 | return meta; 33 | } 34 | 35 | @Override 36 | public Packet meta(String key, Object value) { 37 | meta.put(key, value); 38 | return this; 39 | } 40 | 41 | public String payload() { 42 | return string; 43 | } 44 | 45 | @Override 46 | public Packet payload(String packet) { 47 | this.string = packet; 48 | return this; 49 | } 50 | 51 | @Override 52 | public String toString() { 53 | return string; 54 | } 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/simple/ARM.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.simple; 2 | 3 | public final class ARM implements SimpleFilter { 4 | private final boolean isEncoder; 5 | private int pos; 6 | 7 | public ARM(boolean isEncoder, int startPos) { 8 | this.isEncoder = isEncoder; 9 | pos = startPos + 8; 10 | } 11 | 12 | public int code(byte[] buf, int off, int len) { 13 | int end = off + len - 4; 14 | int i; 15 | 16 | for (i = off; i <= end; i += 4) { 17 | if ((buf[i + 3] & 0xFF) == 0xEB) { 18 | int src = ((buf[i + 2] & 0xFF) << 16) 19 | | ((buf[i + 1] & 0xFF) << 8) 20 | | (buf[i] & 0xFF); 21 | src <<= 2; 22 | 23 | int dest; 24 | if (isEncoder) { 25 | dest = src + (pos + i - off); 26 | } else { 27 | dest = src - (pos + i - off); 28 | } 29 | 30 | dest >>>= 2; 31 | buf[i + 2] = (byte) (dest >>> 16); 32 | buf[i + 1] = (byte) (dest >>> 8); 33 | buf[i] = (byte) dest; 34 | } 35 | } 36 | 37 | i -= off; 38 | pos += i; 39 | return i; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/check/CRC64.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.check; 2 | 3 | public class CRC64 extends Check { 4 | private static final long poly = 0xC96C5795D7870F42L; 5 | private static final long crcTable[] = new long[256]; 6 | 7 | private long crc = -1; 8 | 9 | static { 10 | for (int b = 0; b < crcTable.length; ++b) { 11 | long r = b; 12 | for (int i = 0; i < 8; ++i) { 13 | if ((r & 1) == 1) { 14 | r = (r >>> 1) ^ poly; 15 | } else { 16 | r >>>= 1; 17 | } 18 | } 19 | 20 | crcTable[b] = r; 21 | } 22 | } 23 | 24 | public CRC64() { 25 | size = 8; 26 | name = "CRC64"; 27 | } 28 | 29 | public void update(byte[] buf, int off, int len) { 30 | int end = off + len; 31 | 32 | while (off < end) { 33 | crc = crcTable[(buf[off++] ^ (int) crc) & 0xFF] ^ (crc >>> 8); 34 | } 35 | } 36 | 37 | public byte[] finish() { 38 | long value = ~crc; 39 | crc = -1; 40 | 41 | byte[] buf = new byte[8]; 42 | for (int i = 0; i < buf.length; ++i) { 43 | buf[i] = (byte) (value >> (i * 8)); 44 | } 45 | 46 | return buf; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/XZ.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | /** 4 | * XZ constants. 5 | */ 6 | public class XZ { 7 | /** 8 | * XZ Header Magic Bytes begin a XZ file. 9 | * This can be useful to detect XZ compressed data. 10 | */ 11 | public static final byte[] HEADER_MAGIC = { 12 | (byte) 0xFD, '7', 'z', 'X', 'Z', '\0'}; 13 | 14 | /** 15 | * XZ Footer Magic Bytes are the last bytes of a XZ Stream. 16 | */ 17 | public static final byte[] FOOTER_MAGIC = {'Y', 'Z'}; 18 | 19 | /** 20 | * Integrity check ID indicating that no integrity check is calculated. 21 | *

22 | * Omitting the integrity check is strongly discouraged except when 23 | * the integrity of the data will be verified by other means anyway, 24 | * and calculating the check twice would be useless. 25 | */ 26 | public static final int CHECK_NONE = 0; 27 | 28 | /** 29 | * Integrity check ID for CRC32. 30 | */ 31 | public static final int CHECK_CRC32 = 1; 32 | 33 | /** 34 | * Integrity check ID for CRC64. 35 | */ 36 | public static final int CHECK_CRC64 = 4; 37 | 38 | /** 39 | * Integrity check ID for SHA-256. 40 | */ 41 | public static final int CHECK_SHA256 = 10; 42 | 43 | private XZ() { 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/CountingOutputStream.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import java.io.IOException; 4 | import java.io.OutputStream; 5 | 6 | /** 7 | * Counts the number of bytes written to an output stream. 8 | *

9 | * The finish method does nothing. 10 | * This is FinishableOutputStream instead 11 | * of OutputStream solely because it allows 12 | * using this as the output stream for a chain of raw filters. 13 | */ 14 | class CountingOutputStream extends FinishableOutputStream { 15 | private final OutputStream out; 16 | private long size = 0; 17 | 18 | public CountingOutputStream(OutputStream out) { 19 | this.out = out; 20 | } 21 | 22 | public void write(int b) throws IOException { 23 | out.write(b); 24 | if (size >= 0) { 25 | ++size; 26 | } 27 | } 28 | 29 | public void write(byte[] b, int off, int len) throws IOException { 30 | out.write(b, off, len); 31 | if (size >= 0) { 32 | size += len; 33 | } 34 | } 35 | 36 | public void flush() throws IOException { 37 | out.flush(); 38 | } 39 | 40 | public void close() throws IOException { 41 | out.close(); 42 | } 43 | 44 | public long getSize() { 45 | return size; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/abort/KnapsackAbortRequestBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.abort; 17 | 18 | import org.elasticsearch.action.ActionRequestBuilder; 19 | import org.elasticsearch.client.ElasticsearchClient; 20 | 21 | public class KnapsackAbortRequestBuilder extends ActionRequestBuilder { 22 | 23 | public KnapsackAbortRequestBuilder(ElasticsearchClient client) { 24 | super(client, KnapsackAbortAction.INSTANCE, new KnapsackAbortRequest()); 25 | } 26 | 27 | public KnapsackAbortRequestBuilder setReset(boolean reset) { 28 | request().setReset(reset); 29 | return this; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/check/Check.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.check; 2 | 3 | import org.xbib.io.compress.xz.UnsupportedOptionsException; 4 | import org.xbib.io.compress.xz.XZ; 5 | 6 | public abstract class Check { 7 | int size; 8 | String name; 9 | 10 | public abstract void update(byte[] buf, int off, int len); 11 | 12 | public abstract byte[] finish(); 13 | 14 | public void update(byte[] buf) { 15 | update(buf, 0, buf.length); 16 | } 17 | 18 | public int getSize() { 19 | return size; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public static Check getInstance(int checkType) 27 | throws UnsupportedOptionsException { 28 | switch (checkType) { 29 | case XZ.CHECK_NONE: 30 | return new None(); 31 | 32 | case XZ.CHECK_CRC32: 33 | return new CRC32(); 34 | 35 | case XZ.CHECK_CRC64: 36 | return new CRC64(); 37 | 38 | case XZ.CHECK_SHA256: 39 | try { 40 | return new SHA256(); 41 | } catch (java.security.NoSuchAlgorithmException e) { 42 | } 43 | 44 | break; 45 | } 46 | 47 | throw new UnsupportedOptionsException( 48 | "Unsupported Check ID " + checkType); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/org/xbib/io/compress/bzip2/SimpleBZip2Tests.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.bzip2; 2 | 3 | import org.junit.Test; 4 | 5 | import java.io.ByteArrayInputStream; 6 | import java.io.ByteArrayOutputStream; 7 | import java.io.IOException; 8 | import java.io.ObjectInputStream; 9 | import java.io.ObjectOutputStream; 10 | 11 | import static org.junit.Assert.assertEquals; 12 | 13 | public class SimpleBZip2Tests { 14 | 15 | @Test 16 | public void testBZip2HelloWorld() throws Exception { 17 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 18 | BZip2OutputStream zOut = new BZip2OutputStream(out); 19 | ObjectOutputStream objOut = new ObjectOutputStream(zOut); 20 | String helloWorld = "Hello World!"; 21 | objOut.writeObject(helloWorld); 22 | zOut.close(); 23 | ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 24 | BZip2InputStream zIn = new BZip2InputStream(in); 25 | ObjectInputStream objIn = new ObjectInputStream(zIn); 26 | assertEquals("Hello World!", objIn.readObject()); 27 | } 28 | 29 | @Test 30 | public void testEmptyBZip2() throws IOException { 31 | ByteArrayOutputStream b = new ByteArrayOutputStream(); 32 | try (BZip2OutputStream out = new BZip2OutputStream(b)) { 33 | out.finish(); 34 | } finally { 35 | ; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/state/KnapsackStateRequestBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.state; 17 | 18 | import org.elasticsearch.action.ActionRequestBuilder; 19 | import org.elasticsearch.client.ElasticsearchClient; 20 | 21 | import java.nio.file.Path; 22 | 23 | public class KnapsackStateRequestBuilder extends ActionRequestBuilder { 24 | 25 | public KnapsackStateRequestBuilder(ElasticsearchClient client) { 26 | super(client, KnapsackStateAction.INSTANCE, new KnapsackStateRequest()); 27 | } 28 | 29 | public KnapsackStateRequestBuilder setPath(Path path) { 30 | request.setPath(path); 31 | return this; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/simple/PowerPC.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.simple; 2 | 3 | public final class PowerPC implements SimpleFilter { 4 | private final boolean isEncoder; 5 | private int pos; 6 | 7 | public PowerPC(boolean isEncoder, int startPos) { 8 | this.isEncoder = isEncoder; 9 | pos = startPos; 10 | } 11 | 12 | public int code(byte[] buf, int off, int len) { 13 | int end = off + len - 4; 14 | int i; 15 | 16 | for (i = off; i <= end; i += 4) { 17 | if ((buf[i] & 0xFC) == 0x48 && (buf[i + 3] & 0x03) == 0x01) { 18 | int src = ((buf[i] & 0x03) << 24) 19 | | ((buf[i + 1] & 0xFF) << 16) 20 | | ((buf[i + 2] & 0xFF) << 8) 21 | | (buf[i + 3] & 0xFC); 22 | 23 | int dest; 24 | if (isEncoder) { 25 | dest = src + (pos + i - off); 26 | } else { 27 | dest = src - (pos + i - off); 28 | } 29 | 30 | buf[i] = (byte) (0x48 | ((dest >>> 24) & 0x03)); 31 | buf[i + 1] = (byte) (dest >>> 16); 32 | buf[i + 2] = (byte) (dest >>> 8); 33 | buf[i + 3] = (byte) ((buf[i + 3] & 0x03) | dest); 34 | } 35 | } 36 | 37 | i -= off; 38 | pos += i; 39 | return i; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/org/xbib/elasticsearch/plugin/knapsack/KnapsackNodeTest.java: -------------------------------------------------------------------------------- 1 | package org.xbib.elasticsearch.plugin.knapsack; 2 | 3 | import org.junit.Test; 4 | import org.xbib.elasticsearch.action.knapsack.exp.KnapsackExportRequestBuilder; 5 | import org.xbib.elasticsearch.action.knapsack.exp.KnapsackExportResponse; 6 | import org.xbib.elasticsearch.action.knapsack.imp.KnapsackImportRequestBuilder; 7 | import org.xbib.elasticsearch.action.knapsack.imp.KnapsackImportResponse; 8 | import org.xbib.elasticsearch.util.NodeTestUtils; 9 | 10 | public class KnapsackNodeTest extends NodeTestUtils { 11 | 12 | @Test 13 | public void testNodeExport() throws Exception { 14 | client("1").admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); 15 | KnapsackExportRequestBuilder knapsackExportRequestBuilder = 16 | new KnapsackExportRequestBuilder(client("1")); 17 | KnapsackExportResponse knapsackExportResponse = 18 | knapsackExportRequestBuilder.execute().actionGet(); 19 | Thread.sleep(1000L); // instead we could wait for state 20 | KnapsackImportRequestBuilder knapsackImportRequestBuilder = 21 | new KnapsackImportRequestBuilder(client("1")); 22 | KnapsackImportResponse knapsackImportResponse = knapsackImportRequestBuilder.execute().actionGet(); 23 | knapsackImportResponse.getState().getPath().toFile().delete(); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/LZMA2Encoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import org.xbib.io.compress.xz.lzma.LZMAEncoder; 4 | 5 | class LZMA2Encoder extends LZMA2Coder implements FilterEncoder { 6 | private final LZMA2Options options; 7 | private final byte[] props = new byte[1]; 8 | 9 | LZMA2Encoder(LZMA2Options options) { 10 | if (options.getPresetDict() != null) { 11 | throw new IllegalArgumentException( 12 | "XZ doesn't support a preset dictionary for now"); 13 | } 14 | 15 | if (options.getMode() == LZMA2Options.MODE_UNCOMPRESSED) { 16 | props[0] = (byte) 0; 17 | } else { 18 | int d = Math.max(options.getDictSize(), LZMA2Options.DICT_SIZE_MIN); 19 | props[0] = (byte) (LZMAEncoder.getDistSlot(d - 1) - 23); 20 | } 21 | 22 | // Make a private copy so that the caller is free to change its copy. 23 | this.options = (LZMA2Options) options.clone(); 24 | } 25 | 26 | @Override 27 | public long getFilterID() { 28 | return FILTER_ID; 29 | } 30 | 31 | @Override 32 | public byte[] getFilterProps() { 33 | return props; 34 | } 35 | 36 | @Override 37 | public boolean supportsFlushing() { 38 | return true; 39 | } 40 | 41 | @Override 42 | public FinishableOutputStream getOutputStream(FinishableOutputStream out) { 43 | return options.getOutputStream(out); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/BCJOptions.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | abstract class BCJOptions extends FilterOptions { 4 | private final int alignment; 5 | int startOffset = 0; 6 | 7 | BCJOptions(int alignment) { 8 | this.alignment = alignment; 9 | } 10 | 11 | /** 12 | * Sets the start offset for the address conversions. 13 | * Normally this is useless so you shouldn't use this function. 14 | * The default value is 0. 15 | */ 16 | public void setStartOffset(int startOffset) 17 | throws UnsupportedOptionsException { 18 | if ((startOffset & (alignment - 1)) != 0) { 19 | throw new UnsupportedOptionsException( 20 | "Start offset must be a multiple of " + alignment); 21 | } 22 | 23 | this.startOffset = startOffset; 24 | } 25 | 26 | /** 27 | * Gets the start offset. 28 | */ 29 | public int getStartOffset() { 30 | return startOffset; 31 | } 32 | 33 | public int getEncoderMemoryUsage() { 34 | return SimpleOutputStream.getMemoryUsage(); 35 | } 36 | 37 | public int getDecoderMemoryUsage() { 38 | return SimpleInputStream.getMemoryUsage(); 39 | } 40 | 41 | public Object clone() { 42 | try { 43 | return super.clone(); 44 | } catch (CloneNotSupportedException e) { 45 | assert false; 46 | throw new RuntimeException(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /CREDITS.txt: -------------------------------------------------------------------------------- 1 | 2 | Knapsack contains derived work of Apache Common Compress http://commons.apache.org/proper/commons-compress/ 3 | 4 | The code in this component has many origins: 5 | The bzip2, tar and zip support came from Avalon's Excalibur, but originally 6 | from Ant, as far as life in Apache goes. The tar package is originally Tim Endres' 7 | public domain package. The bzip2 package is based on the work done by Keiron Liddle as well 8 | as Julian Seward's libbzip2. It has migrated via: 9 | Ant -> Avalon-Excalibur -> Commons-IO -> Commons-Compress. 10 | 11 | The cpio package has been contributed by Michael Kuss and the jRPM project. 12 | 13 | Thanks to `nicktgr15 ` for extending Knapsack to support Amazon S3. 14 | 15 | The original URI class loader imlpementation was taken from 16 | Apache Geronimo org/apache/geronimo/kernel/classloader/ (Apache 2.0) 17 | 18 | The original bzip2 implementation was taken from http://code.google.com/p/jbzip2/ (MIT) 19 | 20 | The original LZF implementation was taken from https://github.com/ning/compress (Apache 2.0) 21 | 22 | The original XZ implementation was taken from http://tukaani.org/xz/java.html (Public Domain) 23 | 24 | The original tar implementation was taken from 25 | Apache Commons Compress http://commons.apache.org/proper/commons-compress/tar.html (Apache 2.0) 26 | 27 | The original cpio implementation was taken from the jRPM project (http://jrpm.sourceforge.net) (Apache 2.0) 28 | 29 | The original BytesProgressWatcher is from JetS3t (Apache 2.0) 30 | 31 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/simple/ARMThumb.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.simple; 2 | 3 | public final class ARMThumb implements SimpleFilter { 4 | private final boolean isEncoder; 5 | private int pos; 6 | 7 | public ARMThumb(boolean isEncoder, int startPos) { 8 | this.isEncoder = isEncoder; 9 | pos = startPos + 4; 10 | } 11 | 12 | public int code(byte[] buf, int off, int len) { 13 | int end = off + len - 4; 14 | int i; 15 | 16 | for (i = off; i <= end; i += 2) { 17 | if ((buf[i + 1] & 0xF8) == 0xF0 && (buf[i + 3] & 0xF8) == 0xF8) { 18 | int src = ((buf[i + 1] & 0x07) << 19) 19 | | ((buf[i] & 0xFF) << 11) 20 | | ((buf[i + 3] & 0x07) << 8) 21 | | (buf[i + 2] & 0xFF); 22 | src <<= 1; 23 | 24 | int dest; 25 | if (isEncoder) { 26 | dest = src + (pos + i - off); 27 | } else { 28 | dest = src - (pos + i - off); 29 | } 30 | 31 | dest >>>= 1; 32 | buf[i + 1] = (byte) (0xF0 | ((dest >>> 19) & 0x07)); 33 | buf[i] = (byte) (dest >>> 11); 34 | buf[i + 3] = (byte) (0xF8 | ((dest >>> 8) & 0x07)); 35 | buf[i + 2] = (byte) dest; 36 | i += 2; 37 | } 38 | } 39 | 40 | i -= off; 41 | pos += i; 42 | return i; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/pull/KnapsackPullAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.pull; 17 | 18 | import org.elasticsearch.action.Action; 19 | import org.elasticsearch.client.ElasticsearchClient; 20 | 21 | public class KnapsackPullAction extends Action { 22 | 23 | public final static String NAME = "org.xbib.elasticsearch.knapsack.pull"; 24 | 25 | public final static KnapsackPullAction INSTANCE = new KnapsackPullAction(NAME); 26 | 27 | protected KnapsackPullAction(String name) { 28 | super(name); 29 | } 30 | 31 | @Override 32 | public KnapsackPullResponse newResponse() { 33 | return new KnapsackPullResponse(); 34 | } 35 | 36 | @Override 37 | public KnapsackPullRequestBuilder newRequestBuilder(ElasticsearchClient client) { 38 | return new KnapsackPullRequestBuilder(client); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/zip/ZipArchiveInputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.zip; 17 | 18 | import org.xbib.io.archive.ArchiveInputStream; 19 | 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | import java.util.zip.ZipEntry; 23 | import java.util.zip.ZipInputStream; 24 | 25 | public class ZipArchiveInputStream extends ArchiveInputStream { 26 | 27 | private final ZipInputStream in; 28 | 29 | public ZipArchiveInputStream(InputStream in) { 30 | this.in = new ZipInputStream(in); 31 | } 32 | 33 | @Override 34 | public ZipArchiveEntry getNextEntry() throws IOException { 35 | ZipEntry entry = in.getNextEntry(); 36 | return entry != null ? new ZipArchiveEntry().setEntry(entry) : null; 37 | } 38 | 39 | @Override 40 | public int read(byte[] buffer, int start, int length) throws IOException { 41 | return in.read(buffer, start, length); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/abort/KnapsackAbortAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.abort; 17 | 18 | import org.elasticsearch.action.Action; 19 | import org.elasticsearch.client.ElasticsearchClient; 20 | 21 | public class KnapsackAbortAction extends Action { 22 | 23 | public final static String NAME = "org.xbib.elasticsearch.knapsack.abort"; 24 | 25 | public final static KnapsackAbortAction INSTANCE = new KnapsackAbortAction(NAME); 26 | 27 | protected KnapsackAbortAction(String name) { 28 | super(name); 29 | } 30 | 31 | @Override 32 | public KnapsackAbortResponse newResponse() { 33 | return new KnapsackAbortResponse(); 34 | } 35 | 36 | @Override 37 | public KnapsackAbortRequestBuilder newRequestBuilder(ElasticsearchClient client) { 38 | return new KnapsackAbortRequestBuilder(client); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/state/KnapsackStateAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.state; 17 | 18 | import org.elasticsearch.action.Action; 19 | import org.elasticsearch.client.ElasticsearchClient; 20 | 21 | public class KnapsackStateAction extends Action { 22 | 23 | public final static String NAME = "org.xbib.elasticsearch.knapsack.state"; 24 | 25 | public final static KnapsackStateAction INSTANCE = new KnapsackStateAction(NAME); 26 | 27 | protected KnapsackStateAction(String name) { 28 | super(name); 29 | } 30 | 31 | @Override 32 | public KnapsackStateResponse newResponse() { 33 | return new KnapsackStateResponse(); 34 | } 35 | 36 | @Override 37 | public KnapsackStateRequestBuilder newRequestBuilder(ElasticsearchClient client) { 38 | return new KnapsackStateRequestBuilder(client); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/imp/KnapsackImportAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.imp; 17 | 18 | import org.elasticsearch.action.Action; 19 | import org.elasticsearch.client.ElasticsearchClient; 20 | 21 | public class KnapsackImportAction extends Action { 22 | 23 | public final static String NAME = "org.xbib.elasticsearch.knapsack.import"; 24 | 25 | public final static KnapsackImportAction INSTANCE = new KnapsackImportAction(NAME); 26 | 27 | protected KnapsackImportAction(String name) { 28 | super(name); 29 | } 30 | 31 | @Override 32 | public KnapsackImportResponse newResponse() { 33 | return new KnapsackImportResponse(); 34 | } 35 | 36 | @Override 37 | public KnapsackImportRequestBuilder newRequestBuilder(ElasticsearchClient client) { 38 | return new KnapsackImportRequestBuilder(client); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/simple/SPARC.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.simple; 2 | 3 | public final class SPARC implements SimpleFilter { 4 | private final boolean isEncoder; 5 | private int pos; 6 | 7 | public SPARC(boolean isEncoder, int startPos) { 8 | this.isEncoder = isEncoder; 9 | pos = startPos; 10 | } 11 | 12 | public int code(byte[] buf, int off, int len) { 13 | int end = off + len - 4; 14 | int i; 15 | 16 | for (i = off; i <= end; i += 4) { 17 | if ((buf[i] == 0x40 && (buf[i + 1] & 0xC0) == 0x00) 18 | || (buf[i] == 0x7F && (buf[i + 1] & 0xC0) == 0xC0)) { 19 | int src = ((buf[i] & 0xFF) << 24) 20 | | ((buf[i + 1] & 0xFF) << 16) 21 | | ((buf[i + 2] & 0xFF) << 8) 22 | | (buf[i + 3] & 0xFF); 23 | src <<= 2; 24 | 25 | int dest; 26 | if (isEncoder) { 27 | dest = src + (pos + i - off); 28 | } else { 29 | dest = src - (pos + i - off); 30 | } 31 | 32 | dest >>>= 2; 33 | dest = (((0 - ((dest >>> 22) & 1)) << 22) & 0x3FFFFFFF) 34 | | (dest & 0x3FFFFF) | 0x40000000; 35 | 36 | buf[i] = (byte) (dest >>> 24); 37 | buf[i + 1] = (byte) (dest >>> 16); 38 | buf[i + 2] = (byte) (dest >>> 8); 39 | buf[i + 3] = (byte) dest; 40 | } 41 | } 42 | 43 | i -= off; 44 | pos += i; 45 | return i; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/push/KnapsackPushAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.push; 17 | 18 | import org.elasticsearch.action.Action; 19 | import org.elasticsearch.client.ElasticsearchClient; 20 | 21 | /** 22 | * The knapsack push action exports data to other clusters 23 | */ 24 | public class KnapsackPushAction extends Action { 25 | 26 | public final static String NAME = "org.xbib.elasticsearch.knapsack.push"; 27 | 28 | public final static KnapsackPushAction INSTANCE = new KnapsackPushAction(NAME); 29 | 30 | protected KnapsackPushAction(String name) { 31 | super(name); 32 | } 33 | 34 | @Override 35 | public KnapsackPushResponse newResponse() { 36 | return new KnapsackPushResponse(); 37 | } 38 | 39 | @Override 40 | public KnapsackPushRequestBuilder newRequestBuilder(ElasticsearchClient client) { 41 | return new KnapsackPushRequestBuilder(client); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/org/xbib/suites/TestListener.java: -------------------------------------------------------------------------------- 1 | package org.xbib.suites; 2 | 3 | import org.apache.logging.log4j.LogManager; 4 | import org.apache.logging.log4j.Logger; 5 | import org.junit.runner.Description; 6 | import org.junit.runner.Result; 7 | import org.junit.runner.notification.Failure; 8 | import org.junit.runner.notification.RunListener; 9 | 10 | public class TestListener extends RunListener { 11 | 12 | private final static Logger logger = LogManager.getLogger("test.listener"); 13 | 14 | public void testRunStarted(Description description) throws Exception { 15 | logger.info("number of tests to execute: {}", description.testCount()); 16 | } 17 | 18 | public void testRunFinished(Result result) throws Exception { 19 | logger.info("number of tests executed: {}", result.getRunCount()); 20 | } 21 | 22 | public void testStarted(Description description) throws Exception { 23 | logger.info("starting execution of {} {}", 24 | description.getClassName(), description.getMethodName()); 25 | } 26 | 27 | public void testFinished(Description description) throws Exception { 28 | logger.info("finished execution of {} {}", 29 | description.getClassName(), description.getMethodName()); 30 | } 31 | 32 | public void testFailure(Failure failure) throws Exception { 33 | logger.info("execution of tests failed: {}", 34 | failure.getMessage()); 35 | } 36 | 37 | public void testIgnored(Description description) throws Exception { 38 | logger.info("execution of test ignored: {}", 39 | description.getClassName(), description.getMethodName()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/zip/ZipArchiveCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.zip; 17 | 18 | import org.xbib.io.BytesProgressWatcher; 19 | import org.xbib.io.archive.ArchiveCodec; 20 | 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | import java.io.OutputStream; 24 | 25 | public class ZipArchiveCodec implements ArchiveCodec { 26 | 27 | protected final static String NAME = "zip"; 28 | 29 | public String getName() { 30 | return NAME; 31 | } 32 | 33 | @Override 34 | public ZipSession newSession(BytesProgressWatcher watcher) { 35 | return new ZipSession(watcher); 36 | } 37 | 38 | @Override 39 | public ZipArchiveInputStream createArchiveInputStream(InputStream in) throws IOException { 40 | return new ZipArchiveInputStream(in); 41 | } 42 | 43 | @Override 44 | public ZipArchiveOutputStream createArchiveOutputStream(OutputStream out) { 45 | return new ZipArchiveOutputStream(out); 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/exp/KnapsackExportAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.exp; 17 | 18 | import org.elasticsearch.action.Action; 19 | import org.elasticsearch.client.ElasticsearchClient; 20 | 21 | /** 22 | * The knapsack export action exports data to archive files 23 | */ 24 | public class KnapsackExportAction extends Action { 25 | 26 | public final static String NAME = "org.xbib.elasticsearch.knapsack.export"; 27 | 28 | public final static KnapsackExportAction INSTANCE = new KnapsackExportAction(NAME); 29 | 30 | protected KnapsackExportAction(String name) { 31 | super(name); 32 | } 33 | 34 | @Override 35 | public KnapsackExportResponse newResponse() { 36 | return new KnapsackExportResponse(); 37 | } 38 | 39 | @Override 40 | public KnapsackExportRequestBuilder newRequestBuilder(ElasticsearchClient client) { 41 | return new KnapsackExportRequestBuilder(client); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/index/IndexBase.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.index; 2 | 3 | import org.xbib.io.compress.xz.XZIOException; 4 | import org.xbib.io.compress.xz.common.Util; 5 | 6 | abstract class IndexBase { 7 | private final XZIOException invalidIndexException; 8 | long blocksSum = 0; 9 | long uncompressedSum = 0; 10 | long indexListSize = 0; 11 | long recordCount = 0; 12 | 13 | IndexBase(XZIOException invalidIndexException) { 14 | this.invalidIndexException = invalidIndexException; 15 | } 16 | 17 | private long getUnpaddedIndexSize() { 18 | // Index Indicator + Number of Records + List of Records + CRC32 19 | return 1 + Util.getVLISize(recordCount) + indexListSize + 4; 20 | } 21 | 22 | public long getIndexSize() { 23 | return (getUnpaddedIndexSize() + 3) & ~3; 24 | } 25 | 26 | public long getStreamSize() { 27 | return Util.STREAM_HEADER_SIZE + blocksSum + getIndexSize() 28 | + Util.STREAM_HEADER_SIZE; 29 | } 30 | 31 | int getIndexPaddingSize() { 32 | return (int) ((4 - getUnpaddedIndexSize()) & 3); 33 | } 34 | 35 | void add(long unpaddedSize, long uncompressedSize) throws XZIOException { 36 | blocksSum += (unpaddedSize + 3) & ~3; 37 | uncompressedSum += uncompressedSize; 38 | indexListSize += Util.getVLISize(unpaddedSize) 39 | + Util.getVLISize(uncompressedSize); 40 | ++recordCount; 41 | 42 | if (blocksSum < 0 || uncompressedSum < 0 43 | || getIndexSize() > Util.BACKWARD_SIZE_MAX 44 | || getStreamSize() < 0) { 45 | throw invalidIndexException; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/cpio/CpioArchiveCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.cpio; 17 | 18 | import org.xbib.io.BytesProgressWatcher; 19 | import org.xbib.io.archive.ArchiveCodec; 20 | 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | import java.io.OutputStream; 24 | 25 | public class CpioArchiveCodec implements ArchiveCodec { 26 | 27 | protected final static String NAME = "cpio"; 28 | 29 | public String getName() { 30 | return NAME; 31 | } 32 | 33 | @Override 34 | public CpioSession newSession(BytesProgressWatcher watcher) { 35 | return new CpioSession(watcher); 36 | } 37 | 38 | @Override 39 | public CpioArchiveInputStream createArchiveInputStream(InputStream in) throws IOException { 40 | return new CpioArchiveInputStream(in); 41 | } 42 | 43 | @Override 44 | public CpioArchiveOutputStream createArchiveOutputStream(OutputStream out) throws IOException { 45 | return new CpioArchiveOutputStream(out); 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/tar/TarArchiveCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.tar; 17 | 18 | import org.xbib.io.BytesProgressWatcher; 19 | import org.xbib.io.archive.ArchiveCodec; 20 | 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | import java.io.OutputStream; 24 | 25 | public class TarArchiveCodec implements ArchiveCodec { 26 | 27 | protected final static String NAME = "tar"; 28 | 29 | @Override 30 | public String getName() { 31 | return NAME; 32 | } 33 | 34 | @Override 35 | public TarSession newSession(BytesProgressWatcher watcher) { 36 | return new TarSession(watcher); 37 | } 38 | 39 | @Override 40 | public TarArchiveInputStream createArchiveInputStream(InputStream in) throws IOException { 41 | return new TarArchiveInputStream(in); 42 | } 43 | 44 | @Override 45 | public TarArchiveOutputStream createArchiveOutputStream(OutputStream out) throws IOException { 46 | return new TarArchiveOutputStream(out); 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/lzma/Optimum.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.lzma; 2 | 3 | final class Optimum { 4 | private static final int INFINITY_PRICE = 1 << 30; 5 | 6 | final State state = new State(); 7 | final int[] reps = new int[LZMACoder.REPS]; 8 | 9 | /** 10 | * Cumulative price of arriving to this byte. 11 | */ 12 | int price; 13 | 14 | int optPrev; 15 | int backPrev; 16 | boolean prev1IsLiteral; 17 | 18 | boolean hasPrev2; 19 | int optPrev2; 20 | int backPrev2; 21 | 22 | /** 23 | * Resets the price. 24 | */ 25 | void reset() { 26 | price = INFINITY_PRICE; 27 | } 28 | 29 | /** 30 | * Sets to indicate one LZMA symbol (literal, rep, or match). 31 | */ 32 | void set1(int newPrice, int optCur, int back) { 33 | price = newPrice; 34 | optPrev = optCur; 35 | backPrev = back; 36 | prev1IsLiteral = false; 37 | } 38 | 39 | /** 40 | * Sets to indicate two LZMA symbols of which the first one is a literal. 41 | */ 42 | void set2(int newPrice, int optCur, int back) { 43 | price = newPrice; 44 | optPrev = optCur + 1; 45 | backPrev = back; 46 | prev1IsLiteral = true; 47 | hasPrev2 = false; 48 | } 49 | 50 | /** 51 | * Sets to indicate three LZMA symbols of which the second one 52 | * is a literal. 53 | */ 54 | void set3(int newPrice, int optCur, int back2, int len2, int back) { 55 | price = newPrice; 56 | optPrev = optCur + len2 + 1; 57 | backPrev = back; 58 | prev1IsLiteral = true; 59 | hasPrev2 = true; 60 | optPrev2 = optCur; 61 | backPrev2 = back2; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/lzf/LZFCompressCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.compress.lzf; 17 | 18 | import org.xbib.io.compress.CompressCodec; 19 | 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | import java.io.OutputStream; 23 | 24 | public class LZFCompressCodec implements CompressCodec { 25 | 26 | @Override 27 | public String getName() { 28 | return "lzf"; 29 | } 30 | 31 | @Override 32 | public LZFInputStream decode(InputStream in) throws IOException { 33 | return new LZFInputStream(in); 34 | } 35 | 36 | @Override 37 | public LZFInputStream decode(InputStream in, int bufsize) throws IOException { 38 | return new LZFInputStream(in, true); 39 | } 40 | 41 | @Override 42 | public LZFOutputStream encode(OutputStream out) throws IOException { 43 | return new LZFOutputStream(out); 44 | } 45 | 46 | @Override 47 | public LZFOutputStream encode(OutputStream out, int bufsize) throws IOException { 48 | return new LZFOutputStream(out, bufsize); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/org/xbib/elasticsearch/plugin/knapsack/KnapsackSimpleTests.java: -------------------------------------------------------------------------------- 1 | package org.xbib.elasticsearch.plugin.knapsack; 2 | 3 | import org.junit.Test; 4 | import org.xbib.elasticsearch.action.knapsack.exp.KnapsackExportRequestBuilder; 5 | import org.xbib.elasticsearch.action.knapsack.exp.KnapsackExportResponse; 6 | import org.xbib.elasticsearch.action.knapsack.imp.KnapsackImportRequestBuilder; 7 | import org.xbib.elasticsearch.action.knapsack.imp.KnapsackImportResponse; 8 | import org.xbib.elasticsearch.util.NodeTestUtils; 9 | 10 | public class KnapsackSimpleTests extends NodeTestUtils { 11 | 12 | @Test 13 | public void testSimpleEmptyExport() throws Exception { 14 | KnapsackExportRequestBuilder requestBuilder = 15 | new KnapsackExportRequestBuilder(client("1")); 16 | KnapsackExportResponse knapsackExportResponse = requestBuilder.execute().actionGet(); 17 | Thread.sleep(2000L); 18 | // delete for other test 19 | knapsackExportResponse.getState().getPath().toFile().delete(); 20 | } 21 | 22 | @Test 23 | public void testSimpleEmptyImport() throws Exception { 24 | KnapsackExportRequestBuilder knapsackExportRequestBuilder = 25 | new KnapsackExportRequestBuilder(client("1")); 26 | knapsackExportRequestBuilder.execute().actionGet(); 27 | Thread.sleep(2000L); 28 | KnapsackImportRequestBuilder knapsackImportRequestBuilder = 29 | new KnapsackImportRequestBuilder(client("1")); 30 | KnapsackImportResponse knapsackImportResponse = knapsackImportRequestBuilder.execute().actionGet(); 31 | Thread.sleep(2000L); 32 | // delete for other test 33 | knapsackImportResponse.getState().getPath().toFile().delete(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/MemoryLimitException.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | /** 4 | * Thrown when the memory usage limit given to the XZ decompressor 5 | * would be exceeded. 6 | *

7 | * The amount of memory required and the memory usage limit are 8 | * included in the error detail message in human readable format. 9 | */ 10 | public class MemoryLimitException extends XZIOException { 11 | private static final long serialVersionUID = 3L; 12 | 13 | private final int memoryNeeded; 14 | private final int memoryLimit; 15 | 16 | /** 17 | * Creates a new MemoryLimitException. 18 | *

19 | * The amount of memory needed and the memory usage limit are 20 | * included in the error detail message. 21 | * 22 | * @param memoryNeeded amount of memory needed as kibibytes (KiB) 23 | * @param memoryLimit specified memory usage limit as kibibytes (KiB) 24 | */ 25 | public MemoryLimitException(int memoryNeeded, int memoryLimit) { 26 | super("" + memoryNeeded + " KiB of memory would be needed; limit was " 27 | + memoryLimit + " KiB"); 28 | 29 | this.memoryNeeded = memoryNeeded; 30 | this.memoryLimit = memoryLimit; 31 | } 32 | 33 | /** 34 | * Gets how much memory is required to decompress the data. 35 | * 36 | * @return amount of memory needed as kibibytes (KiB) 37 | */ 38 | public int getMemoryNeeded() { 39 | return memoryNeeded; 40 | } 41 | 42 | /** 43 | * Gets what the memory usage limit was at the time the exception 44 | * was created. 45 | * 46 | * @return memory usage limit as kibibytes (KiB) 47 | */ 48 | public int getMemoryLimit() { 49 | return memoryLimit; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/esbulk/EsBulkArchiveCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.esbulk; 17 | 18 | import org.xbib.io.BytesProgressWatcher; 19 | import org.xbib.io.archive.ArchiveCodec; 20 | 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | import java.io.OutputStream; 24 | 25 | public class EsBulkArchiveCodec implements ArchiveCodec { 26 | 27 | protected final static String NAME = "bulk"; 28 | 29 | @Override 30 | public String getName() { 31 | return NAME; 32 | } 33 | 34 | @Override 35 | public EsBulkSession newSession(BytesProgressWatcher watcher) { 36 | return new EsBulkSession(watcher); 37 | } 38 | 39 | @Override 40 | public EsBulkArchiveInputStream createArchiveInputStream(InputStream in) throws IOException { 41 | return new EsBulkArchiveInputStream(in); 42 | } 43 | 44 | @Override 45 | public EsBulkArchiveOutputStream createArchiveOutputStream(OutputStream out) throws IOException { 46 | return new EsBulkArchiveOutputStream(out); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/org/xbib/io/compress/bzip2/BZip2DivSufSortTests.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.bzip2; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.ByteArrayOutputStream; 6 | import java.io.IOException; 7 | 8 | import org.junit.Test; 9 | 10 | /** 11 | * Tests BZip2DivSufSort 12 | */ 13 | public class BZip2DivSufSortTests { 14 | 15 | /** 16 | * Tests a bug whereby trIntroSort() doesn't terminate 17 | * @throws java.io.IOException 18 | */ 19 | @Test(timeout=2000) 20 | public void testBug1() throws IOException { 21 | 22 | String expectedData = 23 | "dddddddddeeeeeeeeesssssssssyyyyyyyyy,,,,,,,,,eeeeeeeeeaaaaaaaaassssssssseeeeeeeeesss" + 24 | "ssssssbbbbbbbbbwwwwwwwww hhhhhhhhhlllllllllMMMMMMMMM wwwwwwwwwmmmmmm" + 25 | "mmmeeeeeeeeeaaaaaaaaatttttttttlllllllllccccccccceeeeeeeeelllllllll " + 26 | "wwwwwwwwwhhhhhhhhh lllllllll tttttttttfffffffff aaaaaaaaasss" + 27 | "ssssssnnnnnnnnnaaaaaaaaatttttttttaaaaaaaaaaaaaaaaaa iiiiiiiiitttttttttiiiiii" + 28 | "iiiiiiiiiiiiooooooooo rrrrrrrrr"; 29 | 30 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 31 | for (int i = 0; i < 9; i++) { 32 | outputStream.write ("Mary had a little lamb, its fleece was white as snow".getBytes()); 33 | } 34 | outputStream.write(0); 35 | 36 | byte[] input = outputStream.toByteArray(); 37 | input[input.length - 1] = input[0]; 38 | int[] output = new int[input.length]; 39 | new BZip2DivSufSort(input, output, input.length - 1).bwt(); 40 | 41 | StringBuilder sb = new StringBuilder(); 42 | for (int i = 0; i < output.length; i++) { 43 | sb.append ((char)output[i]); 44 | } 45 | 46 | assertEquals (expectedData, sb.toString()); 47 | 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/esbulk/EsBulkArchiveEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.esbulk; 17 | 18 | import org.xbib.io.archive.ArchiveEntry; 19 | 20 | import java.util.Date; 21 | 22 | public class EsBulkArchiveEntry implements ArchiveEntry { 23 | 24 | private String name; 25 | 26 | private long size; 27 | 28 | public EsBulkArchiveEntry() { 29 | } 30 | 31 | @Override 32 | public ArchiveEntry setName(String name) { 33 | this.name = name; 34 | return this; 35 | } 36 | 37 | @Override 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | @Override 43 | public ArchiveEntry setEntrySize(long size) { 44 | this.size = size; 45 | return this; 46 | } 47 | 48 | @Override 49 | public long getEntrySize() { 50 | return size; 51 | } 52 | 53 | @Override 54 | public ArchiveEntry setLastModified(Date lastmodified) { 55 | return this; 56 | } 57 | 58 | @Override 59 | public Date getLastModified() { 60 | return null; 61 | } 62 | 63 | @Override 64 | public boolean isDirectory() { 65 | return false; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/gzip/GzipCompressCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.compress.gzip; 17 | 18 | import org.xbib.io.compress.CompressCodec; 19 | 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | import java.io.OutputStream; 23 | import java.util.zip.GZIPInputStream; 24 | import java.util.zip.GZIPOutputStream; 25 | 26 | public class GzipCompressCodec implements CompressCodec { 27 | 28 | @Override 29 | public String getName() { 30 | return "gz"; 31 | } 32 | 33 | @Override 34 | public GZIPInputStream decode(InputStream in) throws IOException { 35 | return new GZIPInputStream(in); 36 | } 37 | 38 | @Override 39 | public GZIPInputStream decode(InputStream in, int bufsize) throws IOException { 40 | return new GZIPInputStream(in, bufsize); 41 | } 42 | 43 | @Override 44 | public GZIPOutputStream encode(OutputStream out) throws IOException { 45 | return new GZIPOutputStream(out); 46 | } 47 | 48 | @Override 49 | public GZIPOutputStream encode(OutputStream out, int bufsize) throws IOException { 50 | return new GZIPOutputStream(out, bufsize); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/lzma/State.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.lzma; 2 | 3 | final class State { 4 | static final int STATES = 12; 5 | 6 | private static final int LIT_STATES = 7; 7 | 8 | private static final int LIT_LIT = 0; 9 | private static final int MATCH_LIT_LIT = 1; 10 | private static final int REP_LIT_LIT = 2; 11 | private static final int SHORTREP_LIT_LIT = 3; 12 | private static final int MATCH_LIT = 4; 13 | private static final int REP_LIT = 5; 14 | private static final int SHORTREP_LIT = 6; 15 | private static final int LIT_MATCH = 7; 16 | private static final int LIT_LONGREP = 8; 17 | private static final int LIT_SHORTREP = 9; 18 | private static final int NONLIT_MATCH = 10; 19 | private static final int NONLIT_REP = 11; 20 | 21 | private int state; 22 | 23 | State() { 24 | } 25 | 26 | State(State other) { 27 | state = other.state; 28 | } 29 | 30 | void reset() { 31 | state = LIT_LIT; 32 | } 33 | 34 | int get() { 35 | return state; 36 | } 37 | 38 | void set(State other) { 39 | state = other.state; 40 | } 41 | 42 | void updateLiteral() { 43 | if (state <= SHORTREP_LIT_LIT) { 44 | state = LIT_LIT; 45 | } else if (state <= LIT_SHORTREP) { 46 | state -= 3; 47 | } else { 48 | state -= 6; 49 | } 50 | } 51 | 52 | void updateMatch() { 53 | state = state < LIT_STATES ? LIT_MATCH : NONLIT_MATCH; 54 | } 55 | 56 | void updateLongRep() { 57 | state = state < LIT_STATES ? LIT_LONGREP : NONLIT_REP; 58 | } 59 | 60 | void updateShortRep() { 61 | state = state < LIT_STATES ? LIT_SHORTREP : NONLIT_REP; 62 | } 63 | 64 | boolean isLiteral() { 65 | return state < LIT_STATES; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/abort/KnapsackAbortRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.abort; 17 | 18 | import org.elasticsearch.action.ActionRequest; 19 | import org.elasticsearch.action.ActionRequestValidationException; 20 | import org.elasticsearch.common.io.stream.StreamInput; 21 | import org.elasticsearch.common.io.stream.StreamOutput; 22 | 23 | import java.io.IOException; 24 | 25 | public class KnapsackAbortRequest extends ActionRequest { 26 | 27 | private boolean reset; 28 | 29 | public KnapsackAbortRequest setReset(boolean reset) { 30 | this.reset = reset; 31 | return this; 32 | } 33 | 34 | public boolean getReset() { 35 | return reset; 36 | } 37 | 38 | @Override 39 | public ActionRequestValidationException validate() { 40 | return null; 41 | } 42 | 43 | @Override 44 | public void readFrom(StreamInput in) throws IOException { 45 | super.readFrom(in); 46 | reset = in.readBoolean(); 47 | } 48 | 49 | @Override 50 | public void writeTo(StreamOutput out) throws IOException { 51 | super.writeTo(out); 52 | out.writeBoolean(reset); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/FinishableWrapperOutputStream.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import java.io.IOException; 4 | import java.io.OutputStream; 5 | 6 | /** 7 | * Wraps an output stream to a finishable output stream for use with 8 | * raw encoders. This is not needed for XZ compression and thus most 9 | * people will never need this. 10 | */ 11 | public class FinishableWrapperOutputStream extends FinishableOutputStream { 12 | /** 13 | * The {@link java.io.OutputStream OutputStream} that has been 14 | * wrapped into a FinishableWrapperOutputStream. 15 | */ 16 | protected OutputStream out; 17 | 18 | /** 19 | * Creates a new output stream which support finishing. 20 | * The finish() method will do nothing. 21 | */ 22 | public FinishableWrapperOutputStream(OutputStream out) { 23 | this.out = out; 24 | } 25 | 26 | /** 27 | * Calls {@link java.io.OutputStream#write(int) out.write(b)}. 28 | */ 29 | public void write(int b) throws IOException { 30 | out.write(b); 31 | } 32 | 33 | /** 34 | * Calls {@link java.io.OutputStream#write(byte[]) out.write(buf)}. 35 | */ 36 | public void write(byte[] buf) throws IOException { 37 | out.write(buf); 38 | } 39 | 40 | /** 41 | * Calls {@link java.io.OutputStream#write(byte[], int, int) 42 | * out.write(buf, off, len)}. 43 | */ 44 | public void write(byte[] buf, int off, int len) throws IOException { 45 | out.write(buf, off, len); 46 | } 47 | 48 | /** 49 | * Calls {@link java.io.OutputStream#flush() out.flush()}. 50 | */ 51 | public void flush() throws IOException { 52 | out.flush(); 53 | } 54 | 55 | /** 56 | * Calls {@link java.io.OutputStream#close() out.close()}. 57 | */ 58 | public void close() throws IOException { 59 | out.close(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/ArchiveEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive; 17 | 18 | import java.util.Date; 19 | 20 | /** 21 | * Represents an entry of an archive. 22 | */ 23 | public interface ArchiveEntry { 24 | 25 | /** 26 | * Special value indicating that the size is unknown 27 | */ 28 | long SIZE_UNKNOWN = -1; 29 | 30 | ArchiveEntry setName(String name); 31 | 32 | /** 33 | * The name of the entry in the archive. May refer to a file or directory or other item 34 | */ 35 | String getName(); 36 | 37 | /** 38 | * Set the (uncompressed) entry size in bytes 39 | */ 40 | ArchiveEntry setEntrySize(long size); 41 | 42 | /** 43 | * The (uncompressed) size of the entry. May be -1 (SIZE_UNKNOWN) if the size is unknown 44 | */ 45 | long getEntrySize(); 46 | 47 | /** 48 | * Sets the date of the last modification of this entry. 49 | * 50 | * @param date the date 51 | * @return this archive entry 52 | */ 53 | ArchiveEntry setLastModified(Date date); 54 | 55 | /** 56 | * The last modified date of the entry. 57 | */ 58 | Date getLastModified(); 59 | 60 | /** 61 | * True if the entry refers to a directory 62 | */ 63 | boolean isDirectory(); 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/index/IndexEncoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.index; 2 | 3 | import org.xbib.io.compress.xz.XZIOException; 4 | import org.xbib.io.compress.xz.common.EncoderUtil; 5 | 6 | import java.io.IOException; 7 | import java.io.OutputStream; 8 | import java.util.ArrayList; 9 | import java.util.Iterator; 10 | import java.util.zip.CheckedOutputStream; 11 | 12 | public class IndexEncoder extends IndexBase { 13 | 14 | private final ArrayList records = new ArrayList(); 15 | 16 | public IndexEncoder() { 17 | super(new XZIOException("XZ Stream or its Index has grown too big")); 18 | } 19 | 20 | public void add(long unpaddedSize, long uncompressedSize) 21 | throws XZIOException { 22 | super.add(unpaddedSize, uncompressedSize); 23 | records.add(new IndexRecord(unpaddedSize, uncompressedSize)); 24 | } 25 | 26 | public void encode(OutputStream out) throws IOException { 27 | java.util.zip.CRC32 crc32 = new java.util.zip.CRC32(); 28 | CheckedOutputStream outChecked = new CheckedOutputStream(out, crc32); 29 | 30 | // Index Indicator 31 | outChecked.write(0x00); 32 | 33 | // Number of Records 34 | EncoderUtil.encodeVLI(outChecked, recordCount); 35 | 36 | // List of Records 37 | for (Iterator i = records.iterator(); i.hasNext(); ) { 38 | IndexRecord record = (IndexRecord) i.next(); 39 | EncoderUtil.encodeVLI(outChecked, record.unpadded); 40 | EncoderUtil.encodeVLI(outChecked, record.uncompressed); 41 | } 42 | 43 | // Index Padding 44 | for (int i = getIndexPaddingSize(); i > 0; --i) { 45 | outChecked.write(0x00); 46 | } 47 | 48 | // CRC32 49 | long value = crc32.getValue(); 50 | for (int i = 0; i < 4; ++i) { 51 | out.write((byte) (value >>> (i * 8))); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/CompressCodecService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.compress; 17 | 18 | import java.util.Map; 19 | import java.util.ServiceLoader; 20 | import java.util.Set; 21 | import java.util.WeakHashMap; 22 | 23 | public class CompressCodecService { 24 | 25 | private final static Map codecs = new WeakHashMap(); 26 | 27 | private final static CompressCodecService instance = new CompressCodecService(); 28 | 29 | private CompressCodecService() { 30 | ServiceLoader loader = ServiceLoader.load(CompressCodec.class, getClass().getClassLoader()); 31 | for (CompressCodec codec : loader) { 32 | if (!codecs.containsKey(codec.getName())) { 33 | codecs.put(codec.getName(), codec); 34 | } 35 | } 36 | } 37 | 38 | public static CompressCodecService getInstance() { 39 | return instance; 40 | } 41 | 42 | public CompressCodec getCodec(String suffix) { 43 | if (codecs.containsKey(suffix)) { 44 | return codecs.get(suffix); 45 | } 46 | throw new IllegalArgumentException("Stream codec for " + suffix + " not found in " + codecs); 47 | } 48 | 49 | public static Set getCodecs() { 50 | return codecs.keySet(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/state/KnapsackStateRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.state; 17 | 18 | import org.elasticsearch.action.ActionRequest; 19 | import org.elasticsearch.action.ActionRequestValidationException; 20 | import org.elasticsearch.common.io.stream.StreamInput; 21 | import org.elasticsearch.common.io.stream.StreamOutput; 22 | 23 | import java.io.IOException; 24 | import java.net.URI; 25 | import java.nio.file.Path; 26 | import java.nio.file.Paths; 27 | 28 | public class KnapsackStateRequest extends ActionRequest { 29 | 30 | private Path path; 31 | 32 | public KnapsackStateRequest setPath(Path path) { 33 | this.path = path; 34 | return this; 35 | } 36 | 37 | public Path getPath() { 38 | return path; 39 | } 40 | 41 | @Override 42 | public void writeTo(StreamOutput out) throws IOException { 43 | super.writeTo(out); 44 | out.writeString(path != null ? path.toUri().toString() : ""); 45 | } 46 | 47 | @Override 48 | public ActionRequestValidationException validate() { 49 | return null; 50 | } 51 | 52 | @Override 53 | public void readFrom(StreamInput in) throws IOException { 54 | super.readFrom(in); 55 | path = Paths.get(URI.create(in.readString())); 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/zip/ZipArchiveEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.zip; 17 | 18 | import org.xbib.io.archive.ArchiveEntry; 19 | 20 | import java.util.Date; 21 | import java.util.zip.ZipEntry; 22 | 23 | public class ZipArchiveEntry implements ArchiveEntry { 24 | 25 | private ZipEntry entry; 26 | 27 | public ZipArchiveEntry setEntry(ZipEntry entry) { 28 | this.entry = entry; 29 | return this; 30 | } 31 | 32 | public ZipEntry getEntry() { 33 | return entry; 34 | } 35 | 36 | @Override 37 | public ArchiveEntry setName(String name) { 38 | entry = new ZipEntry(name); 39 | return this; 40 | } 41 | 42 | @Override 43 | public String getName() { 44 | return entry.getName(); 45 | } 46 | 47 | @Override 48 | public ArchiveEntry setEntrySize(long size) { 49 | entry.setSize(size); 50 | return this; 51 | } 52 | 53 | @Override 54 | public long getEntrySize() { 55 | return entry.getSize(); 56 | } 57 | 58 | @Override 59 | public ArchiveEntry setLastModified(Date date) { 60 | entry.setTime(date.getTime()); 61 | return this; 62 | } 63 | 64 | @Override 65 | public Date getLastModified() { 66 | return new Date(entry.getTime()); 67 | } 68 | 69 | @Override 70 | public boolean isDirectory() { 71 | return false; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/abort/KnapsackAbortResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.abort; 17 | 18 | import org.elasticsearch.action.ActionResponse; 19 | import org.elasticsearch.common.io.stream.StreamInput; 20 | import org.elasticsearch.common.io.stream.StreamOutput; 21 | import org.elasticsearch.common.xcontent.ToXContent; 22 | import org.elasticsearch.common.xcontent.XContentBuilder; 23 | 24 | import java.io.IOException; 25 | 26 | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; 27 | 28 | public class KnapsackAbortResponse extends ActionResponse implements ToXContent { 29 | 30 | @Override 31 | public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { 32 | return builder; 33 | } 34 | 35 | @Override 36 | public void readFrom(StreamInput in) throws IOException { 37 | super.readFrom(in); 38 | } 39 | 40 | @Override 41 | public void writeTo(StreamOutput out) throws IOException { 42 | super.writeTo(out); 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | try { 48 | XContentBuilder builder = jsonBuilder(); 49 | builder.startObject(); 50 | builder = toXContent(builder, EMPTY_PARAMS); 51 | builder.endObject(); 52 | return builder.string(); 53 | } catch (IOException e) { 54 | return ""; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/CountingOutputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io; 17 | 18 | import java.io.FilterOutputStream; 19 | import java.io.IOException; 20 | import java.io.OutputStream; 21 | 22 | /** 23 | * Stream that tracks the number of bytes read. 24 | */ 25 | public class CountingOutputStream extends FilterOutputStream { 26 | private long bytesWritten = 0; 27 | 28 | public CountingOutputStream(final OutputStream out) { 29 | super(out); 30 | } 31 | 32 | @Override 33 | public void write(int b) throws IOException { 34 | out.write(b); 35 | count(1); 36 | } 37 | 38 | @Override 39 | public void write(byte[] b) throws IOException { 40 | write(b, 0, b.length); 41 | } 42 | 43 | @Override 44 | public void write(byte[] b, int off, int len) throws IOException { 45 | out.write(b, off, len); 46 | count(len); 47 | } 48 | 49 | /** 50 | * Increments the counter of already written bytes. 51 | * Doesn't increment if the EOF has been hit (written == -1) 52 | * 53 | * @param written the number of bytes written 54 | */ 55 | protected void count(long written) { 56 | if (written != -1) { 57 | bytesWritten += written; 58 | } 59 | } 60 | 61 | /** 62 | * Returns the current number of bytes written to this stream. 63 | * 64 | * @return the number of written bytes 65 | */ 66 | public long getBytesWritten() { 67 | return bytesWritten; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/lzf/LZFDecoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.compress.lzf; 17 | 18 | import java.io.IOException; 19 | 20 | /** 21 | * Decoder that handles decoding of sequence of encoded LZF chunks, combining 22 | * them into a single contiguous result byte array. As of version 0.9, this 23 | * class has been mostly replaced by {@link ChunkDecoder}, although static 24 | * methods are left here and may still be used for convenience. All static 25 | * methods use {@link ChunkDecoderFactory#optimalInstance} to find actual 26 | * {@link ChunkDecoder} instance to use. 27 | */ 28 | public class LZFDecoder { 29 | 30 | public static byte[] decode(final byte[] inputBuffer) throws IOException { 31 | return decode(inputBuffer, 0, inputBuffer.length); 32 | } 33 | 34 | public static byte[] decode(final byte[] inputBuffer, int offset, int length) throws IOException { 35 | return ChunkDecoderFactory.optimalInstance().decode(inputBuffer, offset, length); 36 | } 37 | 38 | public static int decode(final byte[] inputBuffer, final byte[] targetBuffer) throws IOException { 39 | return decode(inputBuffer, 0, inputBuffer.length, targetBuffer); 40 | } 41 | 42 | public static int decode(final byte[] sourceBuffer, int offset, int length, final byte[] targetBuffer) throws IOException { 43 | return ChunkDecoderFactory.optimalInstance().decode(sourceBuffer, offset, length, targetBuffer); 44 | } 45 | 46 | public static int calculateUncompressedSize(byte[] data, int offset, int length) throws IOException { 47 | return ChunkDecoder.calculateUncompressedSize(data, length, length); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/BCJDecoder.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import org.xbib.io.compress.xz.simple.ARM; 4 | import org.xbib.io.compress.xz.simple.ARMThumb; 5 | import org.xbib.io.compress.xz.simple.IA64; 6 | import org.xbib.io.compress.xz.simple.PowerPC; 7 | import org.xbib.io.compress.xz.simple.SPARC; 8 | import org.xbib.io.compress.xz.simple.SimpleFilter; 9 | import org.xbib.io.compress.xz.simple.X86; 10 | 11 | import java.io.InputStream; 12 | 13 | class BCJDecoder extends BCJCoder implements FilterDecoder { 14 | private final long filterID; 15 | private final int startOffset; 16 | 17 | BCJDecoder(long filterID, byte[] props) 18 | throws UnsupportedOptionsException { 19 | assert isBCJFilterID(filterID); 20 | this.filterID = filterID; 21 | 22 | if (props.length == 0) { 23 | startOffset = 0; 24 | } else if (props.length == 4) { 25 | int n = 0; 26 | for (int i = 0; i < 4; ++i) { 27 | n |= (props[i] & 0xFF) << (i * 8); 28 | } 29 | 30 | startOffset = n; 31 | } else { 32 | throw new UnsupportedOptionsException( 33 | "Unsupported BCJ filter properties"); 34 | } 35 | } 36 | 37 | public int getMemoryUsage() { 38 | return SimpleInputStream.getMemoryUsage(); 39 | } 40 | 41 | public InputStream getInputStream(InputStream in) { 42 | SimpleFilter simpleFilter = null; 43 | 44 | if (filterID == X86_FILTER_ID) { 45 | simpleFilter = new X86(false, startOffset); 46 | } else if (filterID == POWERPC_FILTER_ID) { 47 | simpleFilter = new PowerPC(false, startOffset); 48 | } else if (filterID == IA64_FILTER_ID) { 49 | simpleFilter = new IA64(false, startOffset); 50 | } else if (filterID == ARM_FILTER_ID) { 51 | simpleFilter = new ARM(false, startOffset); 52 | } else if (filterID == ARMTHUMB_FILTER_ID) { 53 | simpleFilter = new ARMThumb(false, startOffset); 54 | } else if (filterID == SPARC_FILTER_ID) { 55 | simpleFilter = new SPARC(false, startOffset); 56 | } else { 57 | assert false; 58 | } 59 | 60 | return new SimpleInputStream(in, simpleFilter); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/org/xbib/io/compress/bzip2/BZip2HuffmanStageDecoderTests.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.bzip2; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.IOException; 6 | 7 | import org.junit.Test; 8 | 9 | /** 10 | * Tests BZip2HuffmanStageDecoder 11 | */ 12 | public class BZip2HuffmanStageDecoderTests { 13 | 14 | /** 15 | * Tests decoding an invalid symbol 16 | * @throws Exception 17 | */ 18 | @Test(expected=IOException.class) 19 | public void testExceptionOnInvalidSymbol() throws Exception { 20 | 21 | byte[][] tableCodeLengths = { { 23, 23, 23, 22, 22, 21, 21, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 3, 3, 3, 3, 3 } }; 22 | byte[] selectors = new byte[1024]; 23 | 24 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 25 | BZip2BitOutputStream bitOutputStream = new BZip2BitOutputStream (outputStream); 26 | bitOutputStream.writeBits (23, 8388607); // This value would be the 4th 23-length code 27 | bitOutputStream.flush(); 28 | 29 | BZip2BitInputStream bitInputStream = new BZip2BitInputStream (new ByteArrayInputStream (outputStream.toByteArray())); 30 | BZip2HuffmanStageDecoder decoder = new BZip2HuffmanStageDecoder (bitInputStream, tableCodeLengths[0].length, tableCodeLengths, selectors); 31 | 32 | decoder.nextSymbol(); 33 | 34 | } 35 | 36 | 37 | /** 38 | * Tests that an exception is thrown when there is data beyond the available selectors 39 | * @throws Exception 40 | */ 41 | @Test(expected=IOException.class) 42 | public void testExceptionOnTooMuchData() throws Exception { 43 | 44 | byte[][] tableCodeLengths = { { 1, 1 } }; 45 | byte[] selectors = new byte[1]; 46 | 47 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 48 | BZip2BitOutputStream bitOutputStream = new BZip2BitOutputStream (outputStream); 49 | for (int i = 0; i < 51; i++) { 50 | bitOutputStream.writeBits (1, 0); 51 | } 52 | bitOutputStream.flush(); 53 | 54 | BZip2BitInputStream bitInputStream = new BZip2BitInputStream (new ByteArrayInputStream (outputStream.toByteArray())); 55 | BZip2HuffmanStageDecoder decoder = new BZip2HuffmanStageDecoder (bitInputStream, tableCodeLengths[0].length, tableCodeLengths, selectors); 56 | 57 | for (int i = 0; i < 51; i++) { 58 | decoder.nextSymbol(); 59 | } 60 | 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/ArchiveCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive; 17 | 18 | import org.xbib.io.BytesProgressWatcher; 19 | 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | import java.io.OutputStream; 23 | 24 | /** 25 | * An archive codec defines the session and the input or output stream that are 26 | * used for reading or writing to an archive. 27 | * 28 | * @param the archive session type 29 | * @param the archive input stream type 30 | * @param the archive output type 31 | */ 32 | public interface ArchiveCodec { 33 | 34 | /** 35 | * Returns the name of this archive codec ("cpio", "tar", "zip") 36 | * 37 | * @return the name 38 | */ 39 | String getName(); 40 | 41 | /** 42 | * Creates a new archive session with a progress watcher 43 | * 44 | * @param watcher the progress watcher 45 | * @return the new archive session 46 | */ 47 | S newSession(BytesProgressWatcher watcher); 48 | 49 | /** 50 | * Creates a new archive input stream 51 | * 52 | * @param in the input stream for the archive input stream 53 | * @return the archive input stream 54 | * @throws IOException if archive input stream can not be created 55 | */ 56 | I createArchiveInputStream(InputStream in) throws IOException; 57 | 58 | /** 59 | * Creates a new archive output stream 60 | * 61 | * @param out the output stream for the archive output stream 62 | * @return the archive output stream 63 | * @throws IOException if archive output stream can not be created 64 | */ 65 | O createArchiveOutputStream(OutputStream out) throws IOException; 66 | 67 | } -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/lz/Hash234.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.lz; 2 | 3 | final class Hash234 extends CRC32Hash { 4 | private static final int HASH_2_SIZE = 1 << 10; 5 | private static final int HASH_2_MASK = HASH_2_SIZE - 1; 6 | 7 | private static final int HASH_3_SIZE = 1 << 16; 8 | private static final int HASH_3_MASK = HASH_3_SIZE - 1; 9 | 10 | private final int hash4Mask; 11 | 12 | private final int[] hash2Table = new int[HASH_2_SIZE]; 13 | private final int[] hash3Table = new int[HASH_3_SIZE]; 14 | private final int[] hash4Table; 15 | 16 | private int hash2Value = 0; 17 | private int hash3Value = 0; 18 | private int hash4Value = 0; 19 | 20 | static int getHash4Size(int dictSize) { 21 | int h = dictSize - 1; 22 | h |= h >>> 1; 23 | h |= h >>> 2; 24 | h |= h >>> 4; 25 | h |= h >>> 8; 26 | h >>>= 1; 27 | h |= 0xFFFF; 28 | if (h > (1 << 24)) { 29 | h >>>= 1; 30 | } 31 | 32 | return h + 1; 33 | } 34 | 35 | static int getMemoryUsage(int dictSize) { 36 | // Sizes of the hash arrays + a little extra 37 | return (HASH_2_SIZE + HASH_3_SIZE + getHash4Size(dictSize)) 38 | / (1024 / 4) + 4; 39 | } 40 | 41 | Hash234(int dictSize) { 42 | hash4Table = new int[getHash4Size(dictSize)]; 43 | hash4Mask = hash4Table.length - 1; 44 | } 45 | 46 | void calcHashes(byte[] buf, int off) { 47 | int temp = crcTable[buf[off] & 0xFF] ^ (buf[off + 1] & 0xFF); 48 | hash2Value = temp & HASH_2_MASK; 49 | 50 | temp ^= (buf[off + 2] & 0xFF) << 8; 51 | hash3Value = temp & HASH_3_MASK; 52 | 53 | temp ^= crcTable[buf[off + 3] & 0xFF] << 5; 54 | hash4Value = temp & hash4Mask; 55 | } 56 | 57 | int getHash2Pos() { 58 | return hash2Table[hash2Value]; 59 | } 60 | 61 | int getHash3Pos() { 62 | return hash3Table[hash3Value]; 63 | } 64 | 65 | int getHash4Pos() { 66 | return hash4Table[hash4Value]; 67 | } 68 | 69 | void updateTables(int pos) { 70 | hash2Table[hash2Value] = pos; 71 | hash3Table[hash3Value] = pos; 72 | hash4Table[hash4Value] = pos; 73 | } 74 | 75 | void normalize(int normalizeOffset) { 76 | LZEncoder.normalize(hash2Table, normalizeOffset); 77 | LZEncoder.normalize(hash3Table, normalizeOffset); 78 | LZEncoder.normalize(hash4Table, normalizeOffset); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/FilterOptions.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | 6 | /** 7 | * Base class for filter-specific options classes. 8 | */ 9 | public abstract class FilterOptions implements Cloneable { 10 | /** 11 | * Gets how much memory the encoder will need with 12 | * the given filter chain. This function simply calls 13 | * getEncoderMemoryUsage() for every filter 14 | * in the array and returns the sum of the returned values. 15 | */ 16 | public static int getEncoderMemoryUsage(FilterOptions[] options) { 17 | int m = 0; 18 | 19 | for (int i = 0; i < options.length; ++i) { 20 | m += options[i].getEncoderMemoryUsage(); 21 | } 22 | 23 | return m; 24 | } 25 | 26 | /** 27 | * Gets how much memory the decoder will need with 28 | * the given filter chain. This function simply calls 29 | * getDecoderMemoryUsage() for every filter 30 | * in the array and returns the sum of the returned values. 31 | */ 32 | public static int getDecoderMemoryUsage(FilterOptions[] options) { 33 | int m = 0; 34 | 35 | for (int i = 0; i < options.length; ++i) { 36 | m += options[i].getDecoderMemoryUsage(); 37 | } 38 | 39 | return m; 40 | } 41 | 42 | /** 43 | * Gets how much memory the encoder will need with these options. 44 | */ 45 | public abstract int getEncoderMemoryUsage(); 46 | 47 | /** 48 | * Gets a raw (no XZ headers) encoder output stream using these options. 49 | * Raw streams are an advanced feature. In most cases you want to store 50 | * the compressed data in the .xz container format instead of using 51 | * a raw stream. To use this filter in a .xz file, pass this object 52 | * to XZOutputStream. 53 | */ 54 | public abstract FinishableOutputStream getOutputStream( 55 | FinishableOutputStream out); 56 | 57 | /** 58 | * Gets how much memory the decoder will need to decompress the data 59 | * that was encoded with these options. 60 | */ 61 | public abstract int getDecoderMemoryUsage(); 62 | 63 | /** 64 | * Gets a raw (no XZ headers) decoder input stream using these options. 65 | */ 66 | public abstract InputStream getInputStream(InputStream in) 67 | throws IOException; 68 | 69 | abstract FilterEncoder getFilterEncoder(); 70 | 71 | FilterOptions() { 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/simple/IA64.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.simple; 2 | 3 | public final class IA64 implements SimpleFilter { 4 | private static final int[] BRANCH_TABLE = { 5 | 0, 0, 0, 0, 0, 0, 0, 0, 6 | 0, 0, 0, 0, 0, 0, 0, 0, 7 | 4, 4, 6, 6, 0, 0, 7, 7, 8 | 4, 4, 0, 0, 4, 4, 0, 0}; 9 | 10 | private final boolean isEncoder; 11 | private int pos; 12 | 13 | public IA64(boolean isEncoder, int startPos) { 14 | this.isEncoder = isEncoder; 15 | pos = startPos; 16 | } 17 | 18 | public int code(byte[] buf, int off, int len) { 19 | int end = off + len - 16; 20 | int i; 21 | 22 | for (i = off; i <= end; i += 16) { 23 | int instrTemplate = buf[i] & 0x1F; 24 | int mask = BRANCH_TABLE[instrTemplate]; 25 | 26 | for (int slot = 0, bitPos = 5; slot < 3; ++slot, bitPos += 41) { 27 | if (((mask >>> slot) & 1) == 0) { 28 | continue; 29 | } 30 | 31 | int bytePos = bitPos >>> 3; 32 | int bitRes = bitPos & 7; 33 | 34 | long instr = 0; 35 | for (int j = 0; j < 6; ++j) { 36 | instr |= (buf[i + bytePos + j] & 0xFFL) << (8 * j); 37 | } 38 | 39 | long instrNorm = instr >>> bitRes; 40 | 41 | if (((instrNorm >>> 37) & 0x0F) != 0x05 42 | || ((instrNorm >>> 9) & 0x07) != 0x00) { 43 | continue; 44 | } 45 | 46 | int src = (int) ((instrNorm >>> 13) & 0x0FFFFF); 47 | src |= ((int) (instrNorm >>> 36) & 1) << 20; 48 | src <<= 4; 49 | 50 | int dest; 51 | if (isEncoder) { 52 | dest = src + (pos + i - off); 53 | } else { 54 | dest = src - (pos + i - off); 55 | } 56 | 57 | dest >>>= 4; 58 | 59 | instrNorm &= ~(0x8FFFFFL << 13); 60 | instrNorm |= (dest & 0x0FFFFFL) << 13; 61 | instrNorm |= (dest & 0x100000L) << (36 - 20); 62 | 63 | instr &= (1 << bitRes) - 1; 64 | instr |= instrNorm << bitRes; 65 | 66 | for (int j = 0; j < 6; ++j) { 67 | buf[i + bytePos + j] = (byte) (instr >>> (8 * j)); 68 | } 69 | } 70 | } 71 | 72 | i -= off; 73 | pos += i; 74 | return i; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/ArchiveService.java: -------------------------------------------------------------------------------- 1 | 2 | package org.xbib.io.archive; 3 | 4 | import org.xbib.io.BytesProgressWatcher; 5 | import org.xbib.io.compress.CompressCodecService; 6 | 7 | import java.nio.file.Path; 8 | import java.util.Map; 9 | import java.util.ServiceLoader; 10 | import java.util.Set; 11 | import java.util.WeakHashMap; 12 | 13 | public class ArchiveService { 14 | 15 | private final static Map archiveCodecs = new WeakHashMap(); 16 | 17 | private final static Set codecs = CompressCodecService.getCodecs(); 18 | 19 | private final static ArchiveService instance = new ArchiveService(); 20 | 21 | private ArchiveService() { 22 | ServiceLoader loader = ServiceLoader.load(ArchiveCodec.class, getClass().getClassLoader()); 23 | for (ArchiveCodec codec : loader) { 24 | if (!archiveCodecs.containsKey(codec.getName())) { 25 | archiveCodecs.put(codec.getName(), codec); 26 | } 27 | } 28 | } 29 | 30 | public static ArchiveService getInstance() { 31 | return instance; 32 | } 33 | 34 | public ArchiveCodec getCodec(String name) { 35 | if (archiveCodecs.containsKey(name)) { 36 | return archiveCodecs.get(name); 37 | } 38 | throw new IllegalArgumentException("archive codec for " + name + " not found in " + archiveCodecs); 39 | } 40 | 41 | public static Set getCodecs() { 42 | return archiveCodecs.keySet(); 43 | } 44 | 45 | @SuppressWarnings("unchecked") 46 | public static ArchiveSession newSession(Path path, BytesProgressWatcher watcher) { 47 | for (String archiverName : getCodecs()) { 48 | if (canOpen(archiverName, path)) { 49 | return archiveCodecs.get(archiverName).newSession(watcher); 50 | } 51 | } 52 | throw new IllegalArgumentException("no archive session implementation found for path " + path); 53 | } 54 | 55 | private static boolean canOpen(String suffix, Path path) { 56 | String pathStr = path.toString(); 57 | if (pathStr.endsWith("." + suffix.toLowerCase()) || pathStr.endsWith("." + suffix.toUpperCase())) { 58 | return true; 59 | } 60 | 61 | for (String codec : codecs) { 62 | String s = "." + suffix + "." + codec; 63 | if (pathStr.endsWith(s) || pathStr.endsWith(s.toLowerCase()) || pathStr.endsWith(s.toUpperCase())) { 64 | return true; 65 | } 66 | } 67 | return false; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/ArchiveInputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive; 17 | 18 | import org.xbib.io.BytesProgressWatcher; 19 | 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | 23 | /** 24 | * Archive input streams MUST override the 25 | * {@link #read(byte[], int, int)} - or {@link #read()} - 26 | * method so that reading from the stream generates EOF for the end of 27 | * data in each entry as well as at the end of the file proper. 28 | * The {@link #getNextEntry()} method is used to reset the input stream 29 | * ready for reading the data from the next entry. 30 | */ 31 | public abstract class ArchiveInputStream extends InputStream { 32 | 33 | private BytesProgressWatcher watcher; 34 | 35 | public ArchiveInputStream setWatcher(BytesProgressWatcher watcher) { 36 | this.watcher = watcher; 37 | return this; 38 | } 39 | 40 | public BytesProgressWatcher getWatcher() { 41 | return watcher; 42 | } 43 | 44 | /** 45 | * Returns the next Archive Entry in this Stream. 46 | * 47 | * @return the next entry, 48 | * or {@code null} if there are no more entries 49 | * @throws java.io.IOException if the next entry could not be read 50 | */ 51 | public abstract E getNextEntry() throws IOException; 52 | 53 | /** 54 | * Reads a byte of data. This method will block until enough input is 55 | * available. 56 | *

57 | * Simply calls the {@link #read(byte[], int, int)} method. 58 | *

59 | * MUST be overridden if the {@link #read(byte[], int, int)} method 60 | * is not overridden; may be overridden otherwise. 61 | * 62 | * @return the byte read, or -1 if end of input is reached 63 | * @throws java.io.IOException if an I/O error has occurred 64 | */ 65 | @Override 66 | public int read() throws IOException { 67 | byte[] b = new byte[1]; 68 | int num = read(b, 0, 1); 69 | return num == -1 ? -1 : b[0] & 0xFF; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/lzf/ChunkDecoderFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.compress.lzf; 17 | 18 | /** 19 | * Simple helper class used for loading {@link ChunkDecoder} implementations, 20 | * based on criteria such as "fastest available".

Yes, it looks butt-ugly, 21 | * but does the job. Nonetheless, if anyone has lipstick for this pig, let me 22 | * know. 23 | */ 24 | public class ChunkDecoderFactory { 25 | 26 | private final static ChunkDecoderFactory _instance; 27 | 28 | static { 29 | Class impl = VanillaChunkDecoder.class; 30 | _instance = new ChunkDecoderFactory(impl); 31 | } 32 | 33 | private final Class _implClass; 34 | 35 | @SuppressWarnings("unchecked") 36 | private ChunkDecoderFactory(Class imp) { 37 | _implClass = (Class) imp; 38 | } 39 | 40 | /** 41 | * Method to use for getting decompressor instance that uses the most 42 | * optimal available methods for underlying data access. It should be safe 43 | * to call this method as implementations are dynamically loaded; however, 44 | * on some non-standard platforms it may be necessary to either directly 45 | * load instances, or use {@link #safeInstance()}. 46 | */ 47 | public static ChunkDecoder optimalInstance() { 48 | try { 49 | return _instance._implClass.newInstance(); 50 | } catch (Exception e) { 51 | throw new IllegalStateException("Failed to load a ChunkDecoder instance (" + e.getClass().getName() + "): " 52 | + e.getMessage(), e); 53 | } 54 | } 55 | 56 | /** 57 | * Method that can be used to ensure that a "safe" decompressor instance is 58 | * loaded. Safe here means that it should work on any and all Java 59 | * platforms. 60 | */ 61 | public static ChunkDecoder safeInstance() { 62 | // this will always succeed loading; no need to use dynamic class loading or instantiation 63 | return new VanillaChunkDecoder(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/SeekableInputStream.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | 6 | /** 7 | * Input stream with random access support. 8 | */ 9 | public abstract class SeekableInputStream extends InputStream { 10 | /** 11 | * Seeks n bytes forward in this stream. 12 | *

13 | * This will not seek past the end of the file. If the current position 14 | * is already at or past the end of the file, this doesn't seek at all 15 | * and returns 0. Otherwise, if skipping n bytes 16 | * would cause the position to exceed the stream size, this will do 17 | * equivalent of seek(length()) and the return value will 18 | * be adjusted accordingly. 19 | *

20 | * If n is negative, the position isn't changed and 21 | * the return value is 0. It doesn't seek backward 22 | * because it would conflict with the specification of 23 | * {@link java.io.InputStream#skip(long) InputStream.skip}. 24 | * 25 | * @return 0 if n is negative, 26 | * less than n if skipping n 27 | * bytes would seek past the end of the file, 28 | * n otherwise 29 | * @throws java.io.IOException might be thrown by {@link #seek(long)} 30 | */ 31 | public long skip(long n) throws IOException { 32 | if (n <= 0) { 33 | return 0; 34 | } 35 | 36 | long size = length(); 37 | long pos = position(); 38 | if (pos >= size) { 39 | return 0; 40 | } 41 | 42 | if (size - pos < n) { 43 | n = size - pos; 44 | } 45 | 46 | seek(pos + n); 47 | return n; 48 | } 49 | 50 | /** 51 | * Gets the size of the stream. 52 | */ 53 | public abstract long length() throws IOException; 54 | 55 | /** 56 | * Gets the current position in the stream. 57 | */ 58 | public abstract long position() throws IOException; 59 | 60 | /** 61 | * Seeks to the specified absolute position in the stream. 62 | *

63 | * Seeking past the end of the file should be supported by the subclasses 64 | * unless there is a good reason to do otherwise. If one has seeked 65 | * past the end of the stream, read will return 66 | * -1 to indicate end of stream. 67 | * 68 | * @param pos new read position in the stream 69 | * @throws java.io.IOException if pos is negative or if 70 | * a stream-specific I/O error occurs 71 | */ 72 | public abstract void seek(long pos) throws IOException; 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/abort/TransportKnapsackAbortAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.abort; 17 | 18 | import org.elasticsearch.action.ActionListener; 19 | import org.elasticsearch.action.support.ActionFilters; 20 | import org.elasticsearch.action.support.TransportAction; 21 | import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; 22 | import org.elasticsearch.common.inject.Inject; 23 | import org.elasticsearch.common.logging.ESLogger; 24 | import org.elasticsearch.common.logging.ESLoggerFactory; 25 | import org.elasticsearch.common.settings.Settings; 26 | import org.elasticsearch.threadpool.ThreadPool; 27 | import org.elasticsearch.transport.TransportService; 28 | import org.xbib.elasticsearch.knapsack.KnapsackService; 29 | 30 | public class TransportKnapsackAbortAction extends TransportAction { 31 | 32 | private final static ESLogger logger = ESLoggerFactory.getLogger(KnapsackAbortAction.class.getSimpleName()); 33 | 34 | private final KnapsackService knapsack; 35 | 36 | @Inject 37 | public TransportKnapsackAbortAction(Settings settings, TransportService transportService, 38 | ThreadPool threadPool, ActionFilters actionFilters, 39 | IndexNameExpressionResolver indexNameExpressionResolver, 40 | KnapsackService knapsack) { 41 | super(settings, KnapsackAbortAction.NAME, threadPool, actionFilters, indexNameExpressionResolver, transportService.getTaskManager()); 42 | this.knapsack = knapsack; 43 | } 44 | 45 | @Override 46 | protected void doExecute(final KnapsackAbortRequest request, ActionListener listener) { 47 | final KnapsackAbortResponse response = new KnapsackAbortResponse(); 48 | try { 49 | knapsack.abort(request.getReset()); 50 | listener.onResponse(response); 51 | } catch (Throwable e) { 52 | logger.error(e.getMessage(), e); 53 | listener.onFailure(e); 54 | } 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/esbulk/EsBulkArchiveOutputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.esbulk; 17 | 18 | import org.xbib.io.StringPacket; 19 | import org.xbib.io.archive.ArchiveOutputStream; 20 | import org.xbib.io.archive.ArchiveUtils; 21 | 22 | import java.io.IOException; 23 | import java.io.OutputStream; 24 | 25 | public class EsBulkArchiveOutputStream extends ArchiveOutputStream { 26 | 27 | private final OutputStream out; 28 | 29 | private boolean closed = false; 30 | 31 | private boolean finished; 32 | 33 | public EsBulkArchiveOutputStream(OutputStream out) { 34 | this.out = out; 35 | } 36 | 37 | @Override 38 | public EsBulkArchiveEntry newArchiveEntry() { 39 | return new EsBulkArchiveEntry(); 40 | } 41 | 42 | @Override 43 | public void putArchiveEntry(EsBulkArchiveEntry entry) throws IOException { 44 | StringPacket packet = new StringPacket(); 45 | ArchiveUtils.decodeArchiveEntryName(packet, entry.getName()); 46 | StringBuilder sb = new StringBuilder(); 47 | sb.append("{\"index\":{\"_index\":\"").append(packet.meta().get(ArchiveUtils.keys[0])) 48 | .append("\",\"_type\":\"").append(packet.meta().get(ArchiveUtils.keys[1])) 49 | .append("\",\"_id\":\"").append(packet.meta().get(ArchiveUtils.keys[2])) 50 | .append("\"}\n"); 51 | out.write(sb.toString().getBytes("UTF-8")); 52 | } 53 | 54 | @Override 55 | public void write(byte[] buf, int offset, int size) throws IOException { 56 | out.write(buf, offset, size); 57 | out.write('\n'); 58 | getWatcher().updateBytesTransferred(size); 59 | } 60 | 61 | @Override 62 | public void closeArchiveEntry() throws IOException { 63 | // do nothing 64 | } 65 | 66 | @Override 67 | public void finish() throws IOException { 68 | // do nothing 69 | } 70 | 71 | @Override 72 | public void close() throws IOException { 73 | if (!finished) { 74 | finish(); 75 | finished = true; 76 | } 77 | if (!closed) { 78 | out.close(); 79 | closed = true; 80 | } 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/entry/FallbackArchiveEntryEncoding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.entry; 17 | 18 | import java.io.IOException; 19 | import java.nio.ByteBuffer; 20 | 21 | /** 22 | * A fallback ZipEncoding, which uses a java.io means to encode names. 23 | *

24 | *

This implementation is not suitable for encodings other than 25 | * utf-8, because java.io encodes unmappable character as question 26 | * marks leading to unreadable ZIP entries on some operating 27 | * systems.

28 | *

29 | *

Furthermore this implementation is unable to tell whether a 30 | * given name can be safely encoded or not.

31 | *

32 | *

This implementation acts as a last resort implementation, when 33 | * neither {@link Simple8BitArchiveEntryEncoding} nor {@link NioArchiveEntryEncoding} is 34 | * available.

35 | */ 36 | class FallbackArchiveEntryEncoding implements ArchiveEntryEncoding { 37 | private final String charset; 38 | 39 | /** 40 | * Construct a fallback zip encoding, which uses the platform's 41 | * default charset. 42 | */ 43 | public FallbackArchiveEntryEncoding() { 44 | this.charset = null; 45 | } 46 | 47 | /** 48 | * Construct a fallback zip encoding, which uses the given charset. 49 | * 50 | * @param charset The name of the charset or {@code null} for 51 | * the platform's default character set. 52 | */ 53 | public FallbackArchiveEntryEncoding(String charset) { 54 | this.charset = charset; 55 | } 56 | 57 | public boolean canEncode(String name) { 58 | return true; 59 | } 60 | 61 | public ByteBuffer encode(String name) throws IOException { 62 | if (this.charset == null) { // i.e. use default charset, see no-args constructor 63 | return ByteBuffer.wrap(name.getBytes()); 64 | } else { 65 | return ByteBuffer.wrap(name.getBytes(this.charset)); 66 | } 67 | } 68 | 69 | public String decode(byte[] data) throws IOException { 70 | if (this.charset == null) { // i.e. use default charset, see no-args constructor 71 | return new String(data); 72 | } else { 73 | return new String(data, this.charset); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /gradle/publish.gradle: -------------------------------------------------------------------------------- 1 | task xbibUpload(type: Upload) { 2 | configuration = configurations.archives 3 | uploadDescriptor = true 4 | repositories { 5 | if (project.hasProperty("xbibUsername")) { 6 | mavenDeployer { 7 | configuration = configurations.wagon 8 | repository(url: uri('scpexe://xbib.org/repository')) { 9 | authentication(userName: xbibUsername, privateKey: xbibPrivateKey) 10 | } 11 | } 12 | } 13 | } 14 | } 15 | 16 | task sonaTypeUpload(type: Upload) { 17 | configuration = configurations.archives 18 | uploadDescriptor = true 19 | repositories { 20 | if (project.hasProperty('ossrhUsername')) { 21 | mavenDeployer { 22 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 23 | repository(url: uri(ossrhReleaseUrl)) { 24 | authentication(userName: ossrhUsername, password: ossrhPassword) 25 | } 26 | snapshotRepository(url: uri(ossrhSnapshotUrl)) { 27 | authentication(userName: ossrhUsername, password: ossrhPassword) 28 | } 29 | pom.project { 30 | name pluginName 31 | description pluginDescription 32 | packaging 'jar' 33 | inceptionYear '2012' 34 | url scmUrl 35 | organization { 36 | name 'xbib' 37 | url 'http://xbib.org' 38 | } 39 | developers { 40 | developer { 41 | id user 42 | name 'Jörg Prante' 43 | email 'joergprante@gmail.com' 44 | url 'https://github.com/jprante' 45 | } 46 | } 47 | scm { 48 | url scmUrl 49 | connection scmConnection 50 | developerConnection scmDeveloperConnection 51 | } 52 | licenses { 53 | license { 54 | name 'The Apache License, Version 2.0' 55 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 56 | } 57 | } 58 | } 59 | } 60 | } 61 | } 62 | } 63 | 64 | 65 | if (project.hasProperty('githubToken')) { 66 | github { 67 | owner = user 68 | token = githubToken 69 | repo = project.name 70 | name = project.version 71 | tagName = project.version 72 | targetCommitish = 'master' 73 | assets = [ 74 | "build/distributions/${project.name}-${project.version}-plugin.zip" 75 | ] 76 | } 77 | githubRelease { 78 | dependsOn gitRelease, buildPluginZip 79 | } 80 | } -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/knapsack/KnapsackParameter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.knapsack; 17 | 18 | public interface KnapsackParameter { 19 | 20 | /** 21 | * The indices used for knapsack 22 | */ 23 | String INDEX_PARAM = "index"; 24 | 25 | /** 26 | * The type used for knapsack 27 | */ 28 | String TYPE_PARAM = "type"; 29 | 30 | /** 31 | * The archive file path parameter name for the knapsack archive 32 | */ 33 | String PATH_PARAM = "archivepath"; 34 | 35 | /** 36 | * The parameter for configuring archive location 37 | */ 38 | String KNAPSACK_PATH = "path.knapsack"; 39 | 40 | /** 41 | * The default archives are read.written into this path 42 | */ 43 | String KNAPSACK_DEFAULT_PATH = "path.logs"; 44 | 45 | /** 46 | * the Elasticsearch host for knapsack push/pull 47 | */ 48 | String HOST_PARAM = "host"; 49 | 50 | /** 51 | * The knapsack port 52 | */ 53 | String PORT_PARAM = "port"; 54 | 55 | /** 56 | * The Elasticsearch cluster name for knapsack push/pull 57 | */ 58 | String CLUSTER_PARAM = "cluster"; 59 | 60 | /** 61 | * Should knapsack sniff for nodes in the Elasticsearch cluster 62 | */ 63 | String SNIFF_PARAM = "sniff"; 64 | 65 | /** 66 | * Timeout for connections that Knapsack uses 67 | */ 68 | String TIMEOUT_PARAM = "timeout"; 69 | 70 | /** 71 | * Is knapsack allowed to overwrite archive files or not 72 | */ 73 | String OVERWRITE_PARAM = "overwrite"; 74 | 75 | /** 76 | * Bulk indexing setting, maximum actions per bulk request 77 | */ 78 | String MAX_BULK_ACTIONS_PER_REQUEST_PARAM = "max_bulk_actions_per_request"; 79 | 80 | /** 81 | * Bulk indexing setting, maximum concurrecny for bulk requests 82 | */ 83 | String MAX_BULK_CONCURRENCY_PARAM = "max_bulk_concurrency"; 84 | 85 | /** 86 | * A map for renaming indices/types 87 | */ 88 | String MAP_PARAM = "map"; 89 | 90 | /** 91 | * Should knapsack consider index metadata (mappings, aliases) or not 92 | */ 93 | String WITH_METADATA_PARAM = "with_metadata"; 94 | 95 | String BYTES_PARAM = "bytes"; 96 | 97 | String WITH_ALIASES = "with_aliases"; 98 | 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/rest/action/knapsack/state/RestKnapsackStateAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.rest.action.knapsack.state; 17 | 18 | import org.elasticsearch.client.Client; 19 | import org.elasticsearch.common.inject.Inject; 20 | import org.elasticsearch.common.logging.ESLogger; 21 | import org.elasticsearch.common.logging.ESLoggerFactory; 22 | import org.elasticsearch.common.settings.Settings; 23 | import org.elasticsearch.rest.BaseRestHandler; 24 | import org.elasticsearch.rest.BytesRestResponse; 25 | import org.elasticsearch.rest.RestChannel; 26 | import org.elasticsearch.rest.RestController; 27 | import org.elasticsearch.rest.RestRequest; 28 | import org.elasticsearch.rest.action.support.RestToXContentListener; 29 | import org.xbib.elasticsearch.action.knapsack.state.KnapsackStateAction; 30 | import org.xbib.elasticsearch.action.knapsack.state.KnapsackStateRequest; 31 | import org.xbib.elasticsearch.action.knapsack.state.KnapsackStateResponse; 32 | import org.xbib.elasticsearch.knapsack.KnapsackParameter; 33 | 34 | import static org.elasticsearch.rest.RestRequest.Method.POST; 35 | 36 | public class RestKnapsackStateAction extends BaseRestHandler implements KnapsackParameter { 37 | 38 | private final static ESLogger logger = ESLoggerFactory.getLogger(RestKnapsackStateAction.class.getSimpleName()); 39 | 40 | @Inject 41 | public RestKnapsackStateAction(Settings settings, Client client, RestController controller) { 42 | super(settings, controller, client); 43 | 44 | controller.registerHandler(POST, "/_export/state", this); 45 | controller.registerHandler(POST, "/_import/state", this); 46 | } 47 | 48 | @Override 49 | public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) { 50 | try { 51 | KnapsackStateRequest stateRequest = new KnapsackStateRequest(); 52 | client.admin().indices().execute(KnapsackStateAction.INSTANCE, stateRequest, 53 | new RestToXContentListener(channel)); 54 | } catch (Throwable ex) { 55 | try { 56 | logger.error(ex.getMessage(), ex); 57 | channel.sendResponse(new BytesRestResponse(channel, ex)); 58 | } catch (Exception ex2) { 59 | logger.error(ex2.getMessage(), ex2); 60 | } 61 | } 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/org/xbib/elasticsearch/plugin/knapsack/KnapsackExportTests.java: -------------------------------------------------------------------------------- 1 | package org.xbib.elasticsearch.plugin.knapsack; 2 | 3 | import org.elasticsearch.action.index.IndexRequest; 4 | import org.elasticsearch.common.logging.ESLogger; 5 | import org.elasticsearch.common.logging.ESLoggerFactory; 6 | import org.junit.Test; 7 | import org.xbib.elasticsearch.action.knapsack.exp.KnapsackExportRequestBuilder; 8 | import org.xbib.elasticsearch.action.knapsack.exp.KnapsackExportResponse; 9 | import org.xbib.elasticsearch.action.knapsack.state.KnapsackStateRequestBuilder; 10 | import org.xbib.elasticsearch.action.knapsack.state.KnapsackStateResponse; 11 | import org.xbib.elasticsearch.util.NodeTestUtils; 12 | 13 | import java.io.BufferedReader; 14 | import java.io.File; 15 | import java.io.FileReader; 16 | import java.io.IOException; 17 | import java.net.URI; 18 | import java.nio.file.Path; 19 | import java.nio.file.Paths; 20 | 21 | import static org.junit.Assert.assertEquals; 22 | import static org.junit.Assert.assertFalse; 23 | import static org.junit.Assert.assertTrue; 24 | 25 | public class KnapsackExportTests extends NodeTestUtils { 26 | 27 | private final static ESLogger logger = ESLoggerFactory.getLogger(KnapsackExportTests.class.getName()); 28 | 29 | @Test 30 | public void testMinimalExport() throws Exception { 31 | File exportFile = File.createTempFile("minimal-export-", ".bulk"); 32 | Path exportPath = Paths.get(URI.create("file:" + exportFile.getAbsolutePath())); 33 | client("1").index(new IndexRequest().index("index1").type("test1").id("doc1").source("content", "Hello World").refresh(true)).actionGet(); 34 | logger.info("waiting for recovery"); 35 | logger.info("recovered"); 36 | KnapsackExportRequestBuilder requestBuilder = new KnapsackExportRequestBuilder(client("1")) 37 | .setArchivePath(exportPath) 38 | .setOverwriteAllowed(true); 39 | KnapsackExportResponse knapsackExportResponse = requestBuilder.execute().actionGet(); 40 | if (!knapsackExportResponse.isRunning()) { 41 | logger.error(knapsackExportResponse.getReason()); 42 | throw new IOException("knapsack export not running"); 43 | } 44 | assertTrue(knapsackExportResponse.isRunning()); 45 | Thread.sleep(2000L); 46 | KnapsackStateRequestBuilder knapsackStateRequestBuilder = 47 | new KnapsackStateRequestBuilder(client("1")); 48 | KnapsackStateResponse knapsackStateResponse = knapsackStateRequestBuilder.execute().actionGet(); 49 | logger.info("response={}", knapsackStateResponse); 50 | assertFalse(knapsackStateResponse.isExportActive(exportPath)); 51 | BufferedReader reader = new BufferedReader(new FileReader(exportFile)); 52 | assertEquals("{\"index\":{\"_index\":\"index1\",\"_type\":\"test1\",\"_id\":\"doc1\"}", reader.readLine()); 53 | assertEquals("{\"content\":\"Hello World\"}", reader.readLine()); 54 | reader.close(); 55 | logger.info("done"); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/zip/ZipArchiveOutputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.zip; 17 | 18 | import org.xbib.io.archive.ArchiveOutputStream; 19 | 20 | import java.io.IOException; 21 | import java.io.OutputStream; 22 | import java.nio.ByteBuffer; 23 | import java.nio.channels.Channels; 24 | import java.nio.channels.WritableByteChannel; 25 | import java.util.LinkedList; 26 | import java.util.List; 27 | import java.util.zip.CRC32; 28 | import java.util.zip.ZipEntry; 29 | import java.util.zip.ZipOutputStream; 30 | 31 | public class ZipArchiveOutputStream extends ArchiveOutputStream { 32 | 33 | private final ZipOutputStream out; 34 | 35 | private ZipEntry zipEntry; 36 | 37 | private List bytes; 38 | 39 | private CRC32 crc; 40 | 41 | private boolean finished; 42 | 43 | private boolean closed; 44 | 45 | public ZipArchiveOutputStream(OutputStream out) { 46 | this.out = new ZipOutputStream(out); 47 | this.bytes = new LinkedList(); 48 | this.crc = new CRC32(); 49 | } 50 | 51 | @Override 52 | public ZipArchiveEntry newArchiveEntry() { 53 | return new ZipArchiveEntry(); 54 | } 55 | 56 | @Override 57 | public void putArchiveEntry(ZipArchiveEntry entry) throws IOException { 58 | zipEntry = entry.getEntry(); 59 | } 60 | 61 | @Override 62 | public void closeArchiveEntry() throws IOException { 63 | zipEntry.setCrc(crc.getValue()); 64 | out.putNextEntry(zipEntry); 65 | WritableByteChannel channel = Channels.newChannel(out); 66 | for (ByteBuffer buffer : bytes) { 67 | channel.write(buffer); 68 | } 69 | bytes.clear(); 70 | out.closeEntry(); 71 | } 72 | 73 | @Override 74 | public void finish() throws IOException { 75 | out.finish(); 76 | finished = true; 77 | } 78 | 79 | @Override 80 | public void close() throws IOException { 81 | if (!finished) { 82 | finish(); 83 | } 84 | if (!closed) { 85 | out.close(); 86 | closed = true; 87 | } 88 | } 89 | 90 | @Override 91 | public void write(byte[] b, int offset, int length) throws IOException { 92 | getWatcher().updateBytesTransferred(length); 93 | crc.update(b, offset, length); 94 | bytes.add(ByteBuffer.wrap(b, offset, length)); 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/SeekableFileInputStream.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import java.io.File; 4 | import java.io.FileNotFoundException; 5 | import java.io.IOException; 6 | import java.io.RandomAccessFile; 7 | 8 | /** 9 | * Wraps a {@link java.io.RandomAccessFile RandomAccessFile} 10 | * in a SeekableInputStream. 11 | */ 12 | public class SeekableFileInputStream extends SeekableInputStream { 13 | /** 14 | * The RandomAccessFile that has been wrapped 15 | * into a SeekableFileInputStream. 16 | */ 17 | protected RandomAccessFile randomAccessFile; 18 | 19 | /** 20 | * Creates a new seekable input stream that reads from the specified file. 21 | */ 22 | public SeekableFileInputStream(File file) throws FileNotFoundException { 23 | randomAccessFile = new RandomAccessFile(file, "r"); 24 | } 25 | 26 | /** 27 | * Creates a new seekable input stream that reads from a file with 28 | * the specified name. 29 | */ 30 | public SeekableFileInputStream(String name) throws FileNotFoundException { 31 | randomAccessFile = new RandomAccessFile(name, "r"); 32 | } 33 | 34 | /** 35 | * Creates a new seekable input stream from an existing 36 | * RandomAccessFile object. 37 | */ 38 | public SeekableFileInputStream(RandomAccessFile randomAccessFile) { 39 | this.randomAccessFile = randomAccessFile; 40 | } 41 | 42 | /** 43 | * Calls {@link java.io.RandomAccessFile#read() randomAccessFile.read()}. 44 | */ 45 | public int read() throws IOException { 46 | return randomAccessFile.read(); 47 | } 48 | 49 | /** 50 | * Calls {@link java.io.RandomAccessFile#read(byte[]) randomAccessFile.read(buf)}. 51 | */ 52 | public int read(byte[] buf) throws IOException { 53 | return randomAccessFile.read(buf); 54 | } 55 | 56 | /** 57 | * Calls 58 | * {@link java.io.RandomAccessFile#read(byte[], int, int) 59 | * randomAccessFile.read(buf, off, len)}. 60 | */ 61 | public int read(byte[] buf, int off, int len) throws IOException { 62 | return randomAccessFile.read(buf, off, len); 63 | } 64 | 65 | /** 66 | * Calls {@link java.io.RandomAccessFile#close() randomAccessFile.close()}. 67 | */ 68 | public void close() throws IOException { 69 | randomAccessFile.close(); 70 | } 71 | 72 | /** 73 | * Calls {@link java.io.RandomAccessFile#length() randomAccessFile.length()}. 74 | */ 75 | public long length() throws IOException { 76 | return randomAccessFile.length(); 77 | } 78 | 79 | /** 80 | * Calls {@link java.io.RandomAccessFile#getFilePointer() 81 | * randomAccessFile.getFilePointer()}. 82 | */ 83 | public long position() throws IOException { 84 | return randomAccessFile.getFilePointer(); 85 | } 86 | 87 | /** 88 | * Calls {@link java.io.RandomAccessFile#seek(long) randomAccessFile.seek(long)}. 89 | */ 90 | public void seek(long pos) throws IOException { 91 | randomAccessFile.seek(pos); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/pull/KnapsackPullResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.pull; 17 | 18 | import org.elasticsearch.action.ActionResponse; 19 | import org.elasticsearch.common.io.stream.StreamInput; 20 | import org.elasticsearch.common.io.stream.StreamOutput; 21 | import org.elasticsearch.common.xcontent.ToXContent; 22 | import org.elasticsearch.common.xcontent.XContentBuilder; 23 | import org.xbib.elasticsearch.knapsack.KnapsackState; 24 | 25 | import java.io.IOException; 26 | 27 | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; 28 | 29 | public class KnapsackPullResponse extends ActionResponse implements ToXContent { 30 | 31 | private KnapsackState state; 32 | 33 | private boolean running; 34 | 35 | private String archive; 36 | 37 | public KnapsackPullResponse setState(KnapsackState state) { 38 | this.state = state; 39 | return this; 40 | } 41 | 42 | public KnapsackState getState() { 43 | return state; 44 | } 45 | 46 | public KnapsackPullResponse setRunning(boolean running) { 47 | this.running = running; 48 | return this; 49 | } 50 | 51 | public boolean isRunning() { 52 | return running; 53 | } 54 | 55 | @Override 56 | public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { 57 | builder.field("running", running).field("state"); 58 | state.toXContent(builder, params); 59 | return builder; 60 | } 61 | 62 | 63 | @Override 64 | public void readFrom(StreamInput in) throws IOException { 65 | super.readFrom(in); 66 | state = new KnapsackState(); 67 | state.readFrom(in); 68 | running = in.readBoolean(); 69 | archive = in.readString(); 70 | } 71 | 72 | @Override 73 | public void writeTo(StreamOutput out) throws IOException { 74 | super.writeTo(out); 75 | state.writeTo(out); 76 | out.writeBoolean(running); 77 | out.writeString(archive); 78 | } 79 | 80 | public String toString() { 81 | try { 82 | XContentBuilder builder = jsonBuilder(); 83 | builder.startObject(); 84 | builder = toXContent(builder, EMPTY_PARAMS); 85 | builder.endObject(); 86 | return builder.string(); 87 | } catch (IOException e) { 88 | return ""; 89 | } 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/push/KnapsackPushResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.push; 17 | 18 | import org.elasticsearch.action.ActionResponse; 19 | import org.elasticsearch.common.io.stream.StreamInput; 20 | import org.elasticsearch.common.io.stream.StreamOutput; 21 | import org.elasticsearch.common.xcontent.ToXContent; 22 | import org.elasticsearch.common.xcontent.XContentBuilder; 23 | import org.xbib.elasticsearch.knapsack.KnapsackState; 24 | 25 | import java.io.IOException; 26 | 27 | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; 28 | 29 | public class KnapsackPushResponse extends ActionResponse implements ToXContent { 30 | 31 | private KnapsackState state; 32 | 33 | private boolean running; 34 | 35 | private String archive; 36 | 37 | public KnapsackPushResponse setState(KnapsackState state) { 38 | this.state = state; 39 | return this; 40 | } 41 | 42 | public KnapsackState getState() { 43 | return state; 44 | } 45 | 46 | public KnapsackPushResponse setRunning(boolean running) { 47 | this.running = running; 48 | return this; 49 | } 50 | 51 | public boolean isRunning() { 52 | return running; 53 | } 54 | 55 | @Override 56 | public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { 57 | builder.field("running", running).field("state"); 58 | state.toXContent(builder, params); 59 | return builder; 60 | } 61 | 62 | @Override 63 | public void readFrom(StreamInput in) throws IOException { 64 | super.readFrom(in); 65 | state = new KnapsackState(); 66 | state.readFrom(in); 67 | running = in.readBoolean(); 68 | archive = in.readString(); 69 | } 70 | 71 | @Override 72 | public void writeTo(StreamOutput out) throws IOException { 73 | super.writeTo(out); 74 | state.writeTo(out); 75 | out.writeBoolean(running); 76 | out.writeString(archive); 77 | } 78 | 79 | @Override 80 | public String toString() { 81 | try { 82 | XContentBuilder builder = jsonBuilder(); 83 | builder.startObject(); 84 | builder = toXContent(builder, EMPTY_PARAMS); 85 | builder.endObject(); 86 | return builder.string(); 87 | } catch (IOException e) { 88 | return ""; 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/exp/KnapsackExportRequestBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.exp; 17 | 18 | import org.elasticsearch.action.ActionRequestBuilder; 19 | import org.elasticsearch.action.search.SearchRequest; 20 | import org.elasticsearch.client.ElasticsearchClient; 21 | import org.elasticsearch.common.unit.ByteSizeValue; 22 | 23 | import java.nio.file.Path; 24 | import java.util.Map; 25 | 26 | /** 27 | * Build request for knapsack export action 28 | */ 29 | public class KnapsackExportRequestBuilder extends ActionRequestBuilder { 30 | 31 | public KnapsackExportRequestBuilder(ElasticsearchClient client) { 32 | super(client, KnapsackExportAction.INSTANCE, new KnapsackExportRequest()); 33 | } 34 | 35 | public KnapsackExportRequestBuilder setArchivePath(Path archivePath) { 36 | request.setArchivePath(archivePath); 37 | return this; 38 | } 39 | 40 | public KnapsackExportRequestBuilder setIndex(String index) { 41 | request.setIndex(index); 42 | return this; 43 | } 44 | 45 | public KnapsackExportRequestBuilder setType(String type) { 46 | request.setType(type); 47 | return this; 48 | } 49 | 50 | public KnapsackExportRequestBuilder setLimit(int limit) { 51 | request.setLimit(limit); 52 | return this; 53 | } 54 | 55 | public KnapsackExportRequestBuilder setIndexTypeNames(Map indexTypeNames) { 56 | request.setIndexTypeNames(indexTypeNames); 57 | return this; 58 | } 59 | 60 | public KnapsackExportRequestBuilder setSearchRequest(SearchRequest searchRequest) { 61 | request.setSearchRequest(searchRequest); 62 | return this; 63 | } 64 | 65 | public KnapsackExportRequestBuilder setBytesToTransfer(ByteSizeValue bytesToTransfer) { 66 | request.setBytesToTransfer(bytesToTransfer); 67 | return this; 68 | } 69 | 70 | public KnapsackExportRequestBuilder withMetadata(boolean withMetadata) { 71 | request.withMetadata(withMetadata); 72 | return this; 73 | } 74 | 75 | public KnapsackExportRequestBuilder setOverwriteAllowed(boolean overwrite) { 76 | request.setOverwriteAllowed(overwrite); 77 | return this; 78 | } 79 | 80 | public KnapsackExportRequestBuilder withAliases(boolean withAliases) { 81 | request.withAliases(withAliases); 82 | return this; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/DeltaOptions.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import java.io.InputStream; 4 | 5 | /** 6 | * Delta filter options. The Delta filter can be used only as a non-last 7 | * filter in the chain, for example Delta + LZMA2. 8 | *

9 | * Currently only simple byte-wise delta is supported. The only option 10 | * is the delta distance, which you should set to match your data. 11 | * It's not possible to provide a generic default value for it. 12 | *

13 | * For example, with distance = 2 and eight-byte input 14 | * A1 B1 A2 B3 A3 B5 A4 B7, the output will be A1 B1 01 02 01 02 01 02. 15 | *

16 | * The Delta filter can be good with uncompressed bitmap images. It can 17 | * also help with PCM audio, although special-purpose compressors like 18 | * FLAC will give much smaller result at much better compression speed. 19 | */ 20 | public class DeltaOptions extends FilterOptions { 21 | /** 22 | * Smallest supported delta calculation distance. 23 | */ 24 | public static final int DISTANCE_MIN = 1; 25 | 26 | /** 27 | * Largest supported delta calculation distance. 28 | */ 29 | public static final int DISTANCE_MAX = 256; 30 | 31 | private int distance = DISTANCE_MIN; 32 | 33 | /** 34 | * Creates new Delta options and sets the delta distance to 1 byte. 35 | */ 36 | public DeltaOptions() { 37 | } 38 | 39 | /** 40 | * Creates new Delta options and sets the distance to the given value. 41 | */ 42 | public DeltaOptions(int distance) throws UnsupportedOptionsException { 43 | setDistance(distance); 44 | } 45 | 46 | /** 47 | * Sets the delta distance in bytes. The new distance must be in 48 | * the range [DISTANCE_MIN, DISTANCE_MAX]. 49 | */ 50 | public void setDistance(int distance) throws UnsupportedOptionsException { 51 | if (distance < DISTANCE_MIN || distance > DISTANCE_MAX) { 52 | throw new UnsupportedOptionsException( 53 | "Delta distance must be in the range [" + DISTANCE_MIN 54 | + ", " + DISTANCE_MAX + "]: " + distance); 55 | } 56 | 57 | this.distance = distance; 58 | } 59 | 60 | /** 61 | * Gets the delta distance. 62 | */ 63 | public int getDistance() { 64 | return distance; 65 | } 66 | 67 | public int getEncoderMemoryUsage() { 68 | return DeltaOutputStream.getMemoryUsage(); 69 | } 70 | 71 | public FinishableOutputStream getOutputStream(FinishableOutputStream out) { 72 | return new DeltaOutputStream(out, this); 73 | } 74 | 75 | public int getDecoderMemoryUsage() { 76 | return 1; 77 | } 78 | 79 | public InputStream getInputStream(InputStream in) { 80 | return new DeltaInputStream(in, distance); 81 | } 82 | 83 | FilterEncoder getFilterEncoder() { 84 | return new DeltaEncoder(this); 85 | } 86 | 87 | public Object clone() { 88 | try { 89 | return super.clone(); 90 | } catch (CloneNotSupportedException e) { 91 | assert false; 92 | throw new RuntimeException(); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/action/knapsack/state/TransportKnapsackStateAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.action.knapsack.state; 17 | 18 | import org.elasticsearch.action.ActionListener; 19 | import org.elasticsearch.action.support.ActionFilters; 20 | import org.elasticsearch.action.support.TransportAction; 21 | import org.elasticsearch.client.Client; 22 | import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; 23 | import org.elasticsearch.common.inject.Inject; 24 | import org.elasticsearch.common.logging.ESLogger; 25 | import org.elasticsearch.common.logging.ESLoggerFactory; 26 | import org.elasticsearch.common.settings.Settings; 27 | import org.elasticsearch.threadpool.ThreadPool; 28 | import org.elasticsearch.transport.TransportService; 29 | import org.xbib.elasticsearch.knapsack.KnapsackService; 30 | import org.xbib.elasticsearch.knapsack.KnapsackState; 31 | 32 | public class TransportKnapsackStateAction extends TransportAction { 33 | 34 | private final static ESLogger logger = ESLoggerFactory.getLogger(KnapsackStateAction.class.getSimpleName()); 35 | 36 | private final KnapsackService knapsack; 37 | 38 | @Inject 39 | public TransportKnapsackStateAction(Settings settings, 40 | ThreadPool threadPool, ActionFilters actionFilters, 41 | IndexNameExpressionResolver indexNameExpressionResolver, 42 | TransportService transportService, 43 | KnapsackService knapsack) { 44 | super(settings, KnapsackStateAction.NAME, threadPool, actionFilters, indexNameExpressionResolver, transportService.getTaskManager()); 45 | this.knapsack = knapsack; 46 | } 47 | 48 | @Override 49 | protected void doExecute(final KnapsackStateRequest request, ActionListener listener) { 50 | final KnapsackStateResponse response = new KnapsackStateResponse(); 51 | try { 52 | if (knapsack != null) { 53 | for (KnapsackState state : knapsack.getExports()) { 54 | response.addState(state); 55 | } 56 | for (KnapsackState state : knapsack.getImports()) { 57 | response.addState(state); 58 | } 59 | } 60 | listener.onResponse(response); 61 | } catch (Throwable e) { 62 | logger.error(e.getMessage(), e); 63 | listener.onFailure(e); 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/elasticsearch/rest/action/knapsack/abort/RestKnapsackAbortAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.elasticsearch.rest.action.knapsack.abort; 17 | 18 | import org.elasticsearch.client.Client; 19 | import org.elasticsearch.common.inject.Inject; 20 | import org.elasticsearch.common.logging.ESLogger; 21 | import org.elasticsearch.common.logging.ESLoggerFactory; 22 | import org.elasticsearch.common.settings.Settings; 23 | import org.elasticsearch.rest.BaseRestHandler; 24 | import org.elasticsearch.rest.BytesRestResponse; 25 | import org.elasticsearch.rest.RestChannel; 26 | import org.elasticsearch.rest.RestController; 27 | import org.elasticsearch.rest.RestRequest; 28 | import org.elasticsearch.rest.action.support.RestToXContentListener; 29 | import org.xbib.elasticsearch.action.knapsack.abort.KnapsackAbortAction; 30 | import org.xbib.elasticsearch.action.knapsack.abort.KnapsackAbortRequest; 31 | import org.xbib.elasticsearch.action.knapsack.abort.KnapsackAbortResponse; 32 | 33 | import static org.elasticsearch.rest.RestRequest.Method.POST; 34 | import static org.elasticsearch.rest.RestRequest.Method.DELETE; 35 | 36 | public class RestKnapsackAbortAction extends BaseRestHandler { 37 | 38 | private final static ESLogger logger = ESLoggerFactory.getLogger(RestKnapsackAbortAction.class.getSimpleName()); 39 | 40 | @Inject 41 | public RestKnapsackAbortAction(Settings settings, Client client, RestController controller) { 42 | super(settings, controller, client); 43 | 44 | controller.registerHandler(POST, "/_export/abort", this); 45 | controller.registerHandler(POST, "/_import/abort", this); 46 | controller.registerHandler(DELETE, "/_export/abort", this); 47 | controller.registerHandler(DELETE, "/_import/abort", this); 48 | } 49 | 50 | @Override 51 | public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) { 52 | try { 53 | KnapsackAbortRequest abortRequest = new KnapsackAbortRequest(); 54 | abortRequest.setReset(request.method().equals(DELETE)); 55 | client.admin().indices().execute(KnapsackAbortAction.INSTANCE, abortRequest, 56 | new RestToXContentListener(channel)); 57 | } catch (Throwable ex) { 58 | try { 59 | logger.error(ex.getMessage(), ex); 60 | channel.sendResponse(new BytesRestResponse(channel, ex)); 61 | } catch (Exception ex2) { 62 | logger.error(ex2.getMessage(), ex2); 63 | } 64 | } 65 | } 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/simple/X86.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz.simple; 2 | 3 | public final class X86 implements SimpleFilter { 4 | private static final boolean[] MASK_TO_ALLOWED_STATUS 5 | = {true, true, true, false, true, false, false, false}; 6 | 7 | private static final int[] MASK_TO_BIT_NUMBER = {0, 1, 2, 2, 3, 3, 3, 3}; 8 | 9 | private final boolean isEncoder; 10 | private int pos; 11 | private int prevMask = 0; 12 | 13 | private static boolean test86MSByte(byte b) { 14 | int i = b & 0xFF; 15 | return i == 0x00 || i == 0xFF; 16 | } 17 | 18 | public X86(boolean isEncoder, int startPos) { 19 | this.isEncoder = isEncoder; 20 | pos = startPos + 5; 21 | } 22 | 23 | public int code(byte[] buf, int off, int len) { 24 | int prevPos = off - 1; 25 | int end = off + len - 5; 26 | int i; 27 | 28 | for (i = off; i <= end; ++i) { 29 | if ((buf[i] & 0xFE) != 0xE8) { 30 | continue; 31 | } 32 | 33 | prevPos = i - prevPos; 34 | if ((prevPos & ~3) != 0) { // (unsigned)prevPos > 3 35 | prevMask = 0; 36 | } else { 37 | prevMask = (prevMask << (prevPos - 1)) & 7; 38 | if (prevMask != 0) { 39 | if (!MASK_TO_ALLOWED_STATUS[prevMask] || test86MSByte( 40 | buf[i + 4 - MASK_TO_BIT_NUMBER[prevMask]])) { 41 | prevPos = i; 42 | prevMask = (prevMask << 1) | 1; 43 | continue; 44 | } 45 | } 46 | } 47 | 48 | prevPos = i; 49 | 50 | if (test86MSByte(buf[i + 4])) { 51 | int src = (buf[i + 1] & 0xFF) 52 | | ((buf[i + 2] & 0xFF) << 8) 53 | | ((buf[i + 3] & 0xFF) << 16) 54 | | ((buf[i + 4] & 0xFF) << 24); 55 | int dest; 56 | while (true) { 57 | if (isEncoder) { 58 | dest = src + (pos + i - off); 59 | } else { 60 | dest = src - (pos + i - off); 61 | } 62 | 63 | if (prevMask == 0) { 64 | break; 65 | } 66 | 67 | int index = MASK_TO_BIT_NUMBER[prevMask] * 8; 68 | if (!test86MSByte((byte) (dest >>> (24 - index)))) { 69 | break; 70 | } 71 | 72 | src = dest ^ ((1 << (32 - index)) - 1); 73 | } 74 | 75 | buf[i + 1] = (byte) dest; 76 | buf[i + 2] = (byte) (dest >>> 8); 77 | buf[i + 3] = (byte) (dest >>> 16); 78 | buf[i + 4] = (byte) (~(((dest >>> 24) & 1) - 1)); 79 | i += 4; 80 | } else { 81 | prevMask = (prevMask << 1) | 1; 82 | } 83 | } 84 | 85 | prevPos = i - prevPos; 86 | prevMask = ((prevPos & ~3) != 0) ? 0 : prevMask << (prevPos - 1); 87 | 88 | i -= off; 89 | pos += i; 90 | return i; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/archive/entry/ArchiveEntryEncoding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Jörg Prante 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.xbib.io.archive.entry; 17 | 18 | import java.io.IOException; 19 | import java.nio.ByteBuffer; 20 | 21 | /** 22 | * An interface for encoders that do a pretty encoding of archive 23 | * filenames. 24 | * 25 | *

There are mostly two implementations, one that uses java.nio 26 | * {@link java.nio.charset.Charset Charset} and one implementation, 27 | * which copes with simple 8 bit charsets, because java-1.4 did not 28 | * support Cp437 in java.nio.

29 | * 30 | *

The main reason for defining an own encoding layer comes from 31 | * the problems with {@link String#getBytes(String) 32 | * String.getBytes}, which encodes unknown characters as ASCII 33 | * quotation marks ('?'). Quotation marks are per definition an 34 | * invalid filename on some operating systems like Windows, which 35 | * leads to ignored ZIP entries.

36 | * 37 | *

All implementations should implement this interface in a 38 | * reentrant way.

39 | */ 40 | public interface ArchiveEntryEncoding { 41 | /** 42 | * Check, whether the given string may be losslessly encoded using this 43 | * encoding. 44 | * 45 | * @param name A filename or ZIP comment. 46 | * @return Whether the given name may be encoded with out any losses. 47 | */ 48 | boolean canEncode(String name); 49 | 50 | /** 51 | * Encode a filename or a comment to a byte array suitable for 52 | * storing it to a zip entry. 53 | * 54 | *

Examples for CP 437 (in pseudo-notation, right hand side is 55 | * C-style notation):

56 | *
57 |      *  encode("\u20AC_for_Dollar.txt") = "%U20AC_for_Dollar.txt"
58 |      *  encode("\u00D6lf\u00E4sser.txt") = "\231lf\204sser.txt"
59 |      * 
60 | * 61 | * @param name A filename or ZIP comment. 62 | * @return A byte buffer with a backing array containing the 63 | * encoded name. Unmappable characters or malformed 64 | * character sequences are mapped to a sequence of utf-16 65 | * words encoded in the format %Uxxxx. It is 66 | * assumed, that the byte buffer is positioned at the 67 | * beginning of the encoded result, the byte buffer has a 68 | * backing array and the limit of the byte buffer points 69 | * to the end of the encoded result. 70 | * @throws java.io.IOException 71 | */ 72 | ByteBuffer encode(String name) throws IOException; 73 | 74 | /** 75 | * @param data The byte values to decode. 76 | * @return The decoded string. 77 | * @throws java.io.IOException 78 | */ 79 | String decode(byte[] data) throws IOException; 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/org/xbib/io/compress/xz/DeltaOutputStream.java: -------------------------------------------------------------------------------- 1 | package org.xbib.io.compress.xz; 2 | 3 | import org.xbib.io.compress.xz.delta.DeltaEncoder; 4 | 5 | import java.io.IOException; 6 | 7 | 8 | class DeltaOutputStream extends FinishableOutputStream { 9 | private static final int TMPBUF_SIZE = 4096; 10 | 11 | private FinishableOutputStream out; 12 | private final DeltaEncoder delta; 13 | private final byte[] tmpbuf = new byte[TMPBUF_SIZE]; 14 | 15 | private boolean finished = false; 16 | private IOException exception = null; 17 | 18 | static int getMemoryUsage() { 19 | return 1 + TMPBUF_SIZE / 1024; 20 | } 21 | 22 | DeltaOutputStream(FinishableOutputStream out, DeltaOptions options) { 23 | this.out = out; 24 | delta = new DeltaEncoder(options.getDistance()); 25 | } 26 | 27 | public void write(int b) throws IOException { 28 | byte[] buf = new byte[1]; 29 | buf[0] = (byte) b; 30 | write(buf, 0, 1); 31 | } 32 | 33 | public void write(byte[] buf, int off, int len) throws IOException { 34 | if (off < 0 || len < 0 || off + len < 0 || off + len > buf.length) { 35 | throw new IndexOutOfBoundsException(); 36 | } 37 | 38 | if (exception != null) { 39 | throw exception; 40 | } 41 | 42 | if (finished) { 43 | throw new XZIOException("Stream finished"); 44 | } 45 | 46 | try { 47 | while (len > TMPBUF_SIZE) { 48 | delta.encode(buf, off, TMPBUF_SIZE, tmpbuf); 49 | out.write(tmpbuf); 50 | off += TMPBUF_SIZE; 51 | len -= TMPBUF_SIZE; 52 | } 53 | 54 | delta.encode(buf, off, len, tmpbuf); 55 | out.write(tmpbuf, 0, len); 56 | } catch (IOException e) { 57 | exception = e; 58 | throw e; 59 | } 60 | } 61 | 62 | public void flush() throws IOException { 63 | if (exception != null) { 64 | throw exception; 65 | } 66 | 67 | if (finished) { 68 | throw new XZIOException("Stream finished or closed"); 69 | } 70 | 71 | try { 72 | out.flush(); 73 | } catch (IOException e) { 74 | exception = e; 75 | throw e; 76 | } 77 | } 78 | 79 | public void finish() throws IOException { 80 | if (!finished) { 81 | if (exception != null) { 82 | throw exception; 83 | } 84 | 85 | try { 86 | out.finish(); 87 | } catch (IOException e) { 88 | exception = e; 89 | throw e; 90 | } 91 | 92 | finished = true; 93 | } 94 | } 95 | 96 | public void close() throws IOException { 97 | if (out != null) { 98 | try { 99 | out.close(); 100 | } catch (IOException e) { 101 | if (exception == null) { 102 | exception = e; 103 | } 104 | } 105 | 106 | out = null; 107 | } 108 | 109 | if (exception != null) { 110 | throw exception; 111 | } 112 | } 113 | } 114 | --------------------------------------------------------------------------------