/* * Copyright (C) 2014 Jörg Prante * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.xbib.elasticsearch.action.knapsack.imp; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; import org.xbib.elasticsearch.knapsack.KnapsackRequest; import java.io.IOException; import java.net.URI; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; public class KnapsackImportRequest extends ActionRequest<KnapsackImportRequest> implements KnapsackRequest { private Path archivePath; private String host; private int port; private String cluster; private boolean sniff; private TimeValue timeout; private int maxActionsPerBulkRequest = 1000; private int maxBulkConcurrency = 2 * Runtime.getRuntime().availableProcessors(); private Map<String, Object> indexTypeNames = new HashMap<>(); private Map<String,Object> indexTypeDefinitions = new HashMap<>(); private String index = "_all"; private String type; private SearchRequest searchRequest; private boolean withMetadata; private ByteSizeValue bytesToTransfer = ByteSizeValue.parseBytesSizeValue("0", ""); public KnapsackImportRequest setHost(String host) { this.host = host; return this; } public String getHost() { return host; } public KnapsackImportRequest setPort(int port) { this.port = port; return this; } public int getPort() { return port; } public KnapsackImportRequest setCluster(String cluster) { this.cluster = cluster; return this; } public String getCluster() { return cluster; } public KnapsackImportRequest setSniff(boolean sniff) { this.sniff = sniff; return this; } public boolean getSniff() { return sniff; } public KnapsackImportRequest setIndex(String index) { this.index = index; return this; } public String getIndex() { return index; } public KnapsackImportRequest setType(String type) { this.type = type; return this; } public String getType() { return type; } public KnapsackImportRequest setTimeout(TimeValue timeValue) { this.timeout = timeValue; return this; } public TimeValue getTimeout() { return timeout; } public KnapsackImportRequest setMaxActionsPerBulkRequest(int maxActionsPerBulkRequest) { this.maxActionsPerBulkRequest = maxActionsPerBulkRequest; return this; } public int getMaxActionsPerBulkRequest() { return maxActionsPerBulkRequest; } public KnapsackImportRequest setMaxBulkConcurrency(int maxBulkConcurrency) { this.maxBulkConcurrency = maxBulkConcurrency; return this; } public int getMaxBulkConcurrency() { return maxBulkConcurrency; } public KnapsackImportRequest setIndexTypeNames(Map<String, Object> indexTypeNames) { this.indexTypeNames = indexTypeNames; return this; } public Map getIndexTypeNames() { return indexTypeNames; } public KnapsackImportRequest addIndexSettings(String index, String settingsSpec) { indexTypeDefinitions.put(index, settingsSpec); return this; } public KnapsackImportRequest addIndexTypeMapping(String indexType, String mappingSpec) { indexTypeDefinitions.put(indexType, mappingSpec); return this; } public boolean hasIndexSettings(String index) { return indexTypeDefinitions.containsKey(index + "_settings"); } public String getIndexSettings(String index) { return (String) indexTypeDefinitions.get(index + "_settings"); } public boolean hasIndexTypeMapping(String index, String type) { return indexTypeDefinitions.containsKey(index + "_" + type + "_mapping"); } public String getIndexTypeMapping(String index, String type) { return (String) indexTypeDefinitions.get(index + "_" + type + "_mapping"); } public KnapsackImportRequest setSearchRequest(SearchRequest searchRequest) { this.searchRequest = searchRequest; return this; } public SearchRequest getSearchRequest() { return searchRequest; } public KnapsackImportRequest withMetadata(boolean withMetadata) { this.withMetadata = withMetadata; return this; } public boolean withMetadata() { return withMetadata; } public KnapsackImportRequest setArchivePath(Path archivePath) { this.archivePath = archivePath; return this; } public Path getArchivePath() { return archivePath; } public KnapsackImportRequest setBytesToTransfer(ByteSizeValue bytesToTransfer) { this.bytesToTransfer = bytesToTransfer; return this; } public ByteSizeValue getBytesToTransfer() { return bytesToTransfer; } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeString(archivePath.toUri().toString()); out.writeString(host); out.writeInt(port); out.writeString(cluster); out.writeBoolean(sniff); if (timeout != null) { out.writeBoolean(true); timeout.writeTo(out); } else { out.writeBoolean(false); } out.writeInt(maxActionsPerBulkRequest); out.writeInt(maxBulkConcurrency); out.writeMap(indexTypeDefinitions); out.writeMap(indexTypeNames); out.writeBoolean(withMetadata); out.writeString(index); out.writeString(type); if (searchRequest != null) { out.writeBoolean(true); searchRequest.writeTo(out); } else { out.writeBoolean(false); } bytesToTransfer.writeTo(out); } @Override public ActionRequestValidationException validate() { return null; } @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); archivePath = Paths.get(URI.create(in.readString())); host = in.readString(); port = in.readInt(); cluster = in.readString(); sniff = in.readBoolean(); if (in.readBoolean()) { timeout = TimeValue.readTimeValue(in); } maxActionsPerBulkRequest = in.readInt(); maxBulkConcurrency = in.readInt(); indexTypeDefinitions = in.readMap(); indexTypeNames = in.readMap(); withMetadata = in.readBoolean(); index = in.readString(); type = in.readString(); if (in.readBoolean()) { searchRequest = new SearchRequest(); searchRequest.readFrom(in); } bytesToTransfer.readFrom(in); } }