Java Code Examples for org.elasticsearch.common.collect.MapBuilder#newMapBuilder()

The following examples show how to use org.elasticsearch.common.collect.MapBuilder#newMapBuilder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FsBlobContainer.java    From Elasticsearch with Apache License 2.0 7 votes vote down vote up
@Override
public ImmutableMap<String, BlobMetaData> listBlobsByPrefix(String blobNamePrefix) throws IOException {
    // using MapBuilder and not ImmutableMap.Builder as it seems like File#listFiles might return duplicate files!
    MapBuilder<String, BlobMetaData> builder = MapBuilder.newMapBuilder();

    blobNamePrefix = blobNamePrefix == null ? "" : blobNamePrefix;
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, blobNamePrefix + "*")) {
        for (Path file : stream) {
            final BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
            if (attrs.isRegularFile()) {
                builder.put(file.getFileName().toString(), new PlainBlobMetaData(file.getFileName().toString(), attrs.size()));
            }
        }
    }
    return builder.immutableMap();
}
 
Example 2
Source File: CodecService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private CodecService(Index index, Settings indexSettings, MapperService mapperService) {
    super(index, indexSettings);
    this.mapperService = mapperService;
    MapBuilder<String, Codec> codecs = MapBuilder.<String, Codec>newMapBuilder();
    if (mapperService == null) {
        codecs.put(DEFAULT_CODEC, new Lucene54Codec());
        codecs.put(BEST_COMPRESSION_CODEC, new Lucene54Codec(Mode.BEST_COMPRESSION));
    } else {
        codecs.put(DEFAULT_CODEC, 
                new PerFieldMappingPostingFormatCodec(Mode.BEST_SPEED, mapperService, logger));
        codecs.put(BEST_COMPRESSION_CODEC, 
                new PerFieldMappingPostingFormatCodec(Mode.BEST_COMPRESSION, mapperService, logger));
    }
    codecs.put(LUCENE_DEFAULT_CODEC, Codec.getDefault());
    for (String codec : Codec.availableCodecs()) {
        codecs.put(codec, Codec.forName(codec));
    }
    this.codecs = codecs.immutableMap();
}
 
Example 3
Source File: CrateComponentLoader.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private CrateComponentLoader(Settings settings) {
    ServiceLoader<CrateComponent> crateComponents = ServiceLoader.load(CrateComponent.class);
    plugins = new ArrayList<>();
    MapBuilder<Plugin, List<OnModuleReference>> onModuleReferences = MapBuilder.newMapBuilder();
    for (CrateComponent crateComponent : crateComponents) {
        logger.trace("Loading crateComponent: {}", crateComponent);
        Plugin plugin = crateComponent.createPlugin(settings);
        plugins.add(plugin);
        List<OnModuleReference> list = Lists.newArrayList();
        for (Method method : plugin.getClass().getDeclaredMethods()) {
            if (!method.getName().equals("onModule")) {
                continue;
            }
            if (method.getParameterTypes().length == 0 || method.getParameterTypes().length > 1) {
                logger.warn("Plugin: {} implementing onModule with no parameters or more than one parameter", plugin.name());
                continue;
            }
            Class moduleClass = method.getParameterTypes()[0];
            if (!Module.class.isAssignableFrom(moduleClass)) {
                logger.warn("Plugin: {} implementing onModule by the type is not of Module type {}", plugin.name(), moduleClass);
                continue;
            }
            method.setAccessible(true);
            //noinspection unchecked
            list.add(new OnModuleReference(moduleClass, method));
        }
        if (!list.isEmpty()) {
            onModuleReferences.put(plugin, list);
        }
    }
    this.onModuleReferences = onModuleReferences.immutableMap();
}
 
Example 4
Source File: PipelineAggregatorStreams.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Registers the given stream and associate it with the given types.
 *
 * @param stream    The streams to register
 * @param types     The types associated with the streams
 */
public static synchronized void registerStream(Stream stream, BytesReference... types) {
    MapBuilder<BytesReference, Stream> uStreams = MapBuilder.newMapBuilder(streams);
    for (BytesReference type : types) {
        uStreams.put(type, stream);
    }
    streams = uStreams.immutableMap();
}
 
Example 5
Source File: BucketStreams.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Registers the given stream and associate it with the given types.
 *
 * @param stream    The streams to register
 * @param types     The types associated with the streams
 */
public static synchronized void registerStream(Stream stream, BytesReference... types) {
    MapBuilder<BytesReference, Stream> uStreams = MapBuilder.newMapBuilder(STREAMS);
    for (BytesReference type : types) {
        uStreams.put(type, stream);
    }
    STREAMS = uStreams.immutableMap();
}
 
Example 6
Source File: AggregationStreams.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Registers the given stream and associate it with the given types.
 *
 * @param stream    The streams to register
 * @param types     The types associated with the streams
 */
public static synchronized void registerStream(Stream stream, BytesReference... types) {
    MapBuilder<BytesReference, Stream> uStreams = MapBuilder.newMapBuilder(streams);
    for (BytesReference type : types) {
        uStreams.put(type, stream);
    }
    streams = uStreams.immutableMap();
}
 
Example 7
Source File: CommitStats.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    MapBuilder<String, String> builder = MapBuilder.newMapBuilder();
    for (int i = in.readVInt(); i > 0; i--) {
        builder.put(in.readString(), in.readString());
    }
    userData = builder.immutableMap();
    generation = in.readLong();
    id = in.readOptionalString();
    numDocs = in.readInt();
}
 
Example 8
Source File: ShardIndexingService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public void clear() {
    totalStats.clear();
    synchronized (this) {
        if (!typesStats.isEmpty()) {
            MapBuilder<String, StatsHolder> typesStatsBuilder = MapBuilder.newMapBuilder();
            for (Map.Entry<String, StatsHolder> typeStats : typesStats.entrySet()) {
                if (typeStats.getValue().totalCurrent() > 0) {
                    typeStats.getValue().clear();
                    typesStatsBuilder.put(typeStats.getKey(), typeStats.getValue());
                }
            }
            typesStats = typesStatsBuilder.immutableMap();
        }
    }
}
 
Example 9
Source File: ShardSearchStats.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public void clear() {
    totalStats.clear();
    synchronized (this) {
        if (!groupsStats.isEmpty()) {
            MapBuilder<String, StatsHolder> typesStatsBuilder = MapBuilder.newMapBuilder();
            for (Map.Entry<String, StatsHolder> typeStats : groupsStats.entrySet()) {
                if (typeStats.getValue().totalCurrent() > 0) {
                    typeStats.getValue().clear();
                    typesStatsBuilder.put(typeStats.getKey(), typeStats.getValue());
                }
            }
            groupsStats = typesStatsBuilder.immutableMap();
        }
    }
}
 
Example 10
Source File: JvmMonitorService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Inject
public JvmMonitorService(Settings settings, ThreadPool threadPool) {
    super(settings);
    this.threadPool = threadPool;

    this.enabled = this.settings.getAsBoolean("monitor.jvm.enabled", true);
    this.interval = this.settings.getAsTime("monitor.jvm.interval", timeValueSeconds(1));

    MapBuilder<String, GcThreshold> gcThresholds = MapBuilder.newMapBuilder();
    Map<String, Settings> gcThresholdGroups = this.settings.getGroups("monitor.jvm.gc");
    for (Map.Entry<String, Settings> entry : gcThresholdGroups.entrySet()) {
        String name = entry.getKey();
        TimeValue warn = entry.getValue().getAsTime("warn", null);
        TimeValue info = entry.getValue().getAsTime("info", null);
        TimeValue debug = entry.getValue().getAsTime("debug", null);
        if (warn == null || info == null || debug == null) {
            logger.warn("ignoring gc_threshold for [{}], missing warn/info/debug values", name);
        } else {
            gcThresholds.put(name, new GcThreshold(name, warn.millis(), info.millis(), debug.millis()));
        }
    }
    if (!gcThresholds.containsKey(GcNames.YOUNG)) {
        gcThresholds.put(GcNames.YOUNG, new GcThreshold(GcNames.YOUNG, 1000, 700, 400));
    }
    if (!gcThresholds.containsKey(GcNames.OLD)) {
        gcThresholds.put(GcNames.OLD, new GcThreshold(GcNames.OLD, 10000, 5000, 2000));
    }
    if (!gcThresholds.containsKey("default")) {
        gcThresholds.put("default", new GcThreshold("default", 10000, 5000, 2000));
    }

    this.gcThresholds = gcThresholds.immutableMap();

    logger.debug("enabled [{}], interval [{}], gc_threshold [{}]", enabled, interval, this.gcThresholds);
}
 
Example 11
Source File: TransportGetFieldMappingsAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private GetFieldMappingsResponse merge(AtomicReferenceArray<Object> indexResponses) {
    MapBuilder<String, ImmutableMap<String, ImmutableMap<String, GetFieldMappingsResponse.FieldMappingMetaData>>> mergedResponses = MapBuilder.newMapBuilder();
    for (int i = 0; i < indexResponses.length(); i++) {
        Object element = indexResponses.get(i);
        if (element instanceof GetFieldMappingsResponse) {
            GetFieldMappingsResponse response = (GetFieldMappingsResponse) element;
            mergedResponses.putAll(response.mappings());
        }
    }
    return new GetFieldMappingsResponse(mergedResponses.immutableMap());
}
 
Example 12
Source File: ElasticSearchJSONEventSerializer.java    From bboss-elasticsearch with Apache License 2.0 4 votes vote down vote up
private   void appendHeaders(Map<String,Object> builder, Event event)
      throws IOException {
//    Map<String, String> headers = Maps.newHashMap(event.getHeaders());
	  MapBuilder mapBuilder = MapBuilder.newMapBuilder();
//	  mapBuilder.putAll(event.getHeaders());
//	  Map<String, String> headers = mapBuilder.map();
    Map<String, String> headers = event.getHeaders();
    if(headers == null || headers.size() == 0){
      return;
    }
    String timestamp = headers.remove("timestamp");
    if (!SimpleStringUtil.isEmpty(timestamp)
        && SimpleStringUtil.isEmpty(headers.get("@timestamp"))) {
      long timestampMs = Long.parseLong(timestamp);
      builder.put("@timestamp", new Date(timestampMs));
    }

    String source = headers.remove("source");
    if (!SimpleStringUtil.isEmpty(source)
        && SimpleStringUtil.isEmpty(headers.get("@source"))) {
      builder.put( "@source",
          source);
    }

    String type = headers.remove("type");
    if (!SimpleStringUtil.isEmpty(type)
        && SimpleStringUtil.isEmpty(headers.get("@type"))) {
      builder.put("@type", type);
    }

    String host = headers.remove("host");
    if (!SimpleStringUtil.isEmpty(host)
        && SimpleStringUtil.isEmpty(headers.get("@source_host"))) {

      builder.put( "beat.host",
          host);
    }

    String srcPath = headers.remove("src_path");
    if (!SimpleStringUtil.isEmpty(srcPath)
        && SimpleStringUtil.isEmpty(headers.get("@source_path"))) {
      builder.put( "@source_path",
          srcPath);
    }


    for (String key : headers.keySet()) {
      String val = headers.get(key);
      builder.put(key, val);
    }

  }
 
Example 13
Source File: ElasticSearchLogStashEventSerializer.java    From bboss-elasticsearch with Apache License 2.0 4 votes vote down vote up
private  void appendHeaders(XContentBuilder builder, Event event)
      throws IOException {
//    Map<String, String> headers = Maps.newHashMap(event.getHeaders());
	  MapBuilder mapBuilder = MapBuilder.newMapBuilder();
//	  mapBuilder.putAll(event.getHeaders());
//	  Map<String, String> headers = mapBuilder.map();
    Map<String, String> headers = event.getHeaders();
    if(headers == null || headers.size() == 0){
      return;
    }
    String timestamp = headers.remove("timestamp");
    if (!SimpleStringUtil.isEmpty(timestamp)
        && SimpleStringUtil.isEmpty(headers.get("@timestamp"))) {
      long timestampMs = Long.parseLong(timestamp);
      builder.field("@timestamp", new Date(timestampMs));
    }

    String source = headers.remove("source");
    if (!SimpleStringUtil.isEmpty(source)
        && SimpleStringUtil.isEmpty(headers.get("@source"))) {
      ContentBuilderUtil.appendField(builder, "@source",
          source.getBytes(charset));
    }

    String type = headers.remove("type");
    if (!SimpleStringUtil.isEmpty(type)
        && SimpleStringUtil.isEmpty(headers.get("@type"))) {
      ContentBuilderUtil.appendField(builder, "@type", type.getBytes(charset));
    }

    String host = headers.remove("host");
    if (!SimpleStringUtil.isEmpty(host)
        && SimpleStringUtil.isEmpty(headers.get("@source_host"))) {
      ContentBuilderUtil.appendField(builder, "@source_host",
          host.getBytes(charset));
    }

    String srcPath = headers.remove("src_path");
    if (!SimpleStringUtil.isEmpty(srcPath)
        && SimpleStringUtil.isEmpty(headers.get("@source_path"))) {
      ContentBuilderUtil.appendField(builder, "@source_path",
          srcPath.getBytes(charset));
    }

    builder.startObject("@fields");
    for (String key : headers.keySet()) {
      byte[] val = headers.get(key).getBytes(charset);
      ContentBuilderUtil.appendField(builder, key, val);
    }
    builder.endObject();
  }