@Test
  public void testMapAccess() {
    Map<String, Object> vars = new HashMap<String, Object>();

    Map<String, Object> obj2 =
        MapBuilder.<String, Object>newMapBuilder().put("prop2", "value2").map();
    Map<String, Object> obj1 =
        MapBuilder.<String, Object>newMapBuilder()
            .put("prop1", "value1")
            .put("obj2", obj2)
            .put("l", Arrays.asList("2", "1"))
            .map();
    vars.put("obj1", obj1);
    Object o =
        se.execute(
            new CompiledScript(
                ScriptService.ScriptType.INLINE, "testMapAccess", "python", se.compile("obj1")),
            vars);
    assertThat(o, instanceOf(Map.class));
    obj1 = (Map<String, Object>) o;
    assertThat((String) obj1.get("prop1"), equalTo("value1"));
    assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2"));

    o =
        se.execute(
            new CompiledScript(
                ScriptService.ScriptType.INLINE,
                "testMapAccess",
                "python",
                se.compile("obj1['l'][0]")),
            vars);
    assertThat(((String) o), equalTo("2"));
  }
Exemplo n.º 2
0
 /**
  * Constructs the AggregatorParsers out of all the given parsers
  *
  * @param parsers The available aggregator parsers (dynamically injected by the {@link
  *     org.elasticsearch.search.aggregations.AggregationModule}).
  */
 @Inject
 public AggregatorParsers(Set<Aggregator.Parser> parsers) {
   MapBuilder<String, Aggregator.Parser> builder = MapBuilder.newMapBuilder();
   for (Aggregator.Parser parser : parsers) {
     builder.put(parser.type(), parser);
   }
   this.parsers = builder.immutableMap();
 }
Exemplo n.º 3
0
 public Builder(IndexMetaData indexMetaData) {
   this(indexMetaData.index());
   settings(indexMetaData.settings());
   mappings.putAll(indexMetaData.mappings);
   aliases.putAll(indexMetaData.aliases);
   customs.putAll(indexMetaData.customs);
   this.state = indexMetaData.state;
   this.version = indexMetaData.version;
 }
 public FacetsParseElement() {
   MapBuilder<String, FacetCollectorParser> builder = newMapBuilder();
   addFacetParser(builder, new TermsFacetCollectorParser());
   addFacetParser(builder, new QueryFacetCollectorParser());
   addFacetParser(builder, new StatisticalFacetCollectorParser());
   addFacetParser(builder, new HistogramFacetCollectorParser());
   addFacetParser(builder, new GeoDistanceFacetCollectorParser());
   addFacetParser(builder, new RangeFacetCollectorParser());
   addFacetParser(builder, new FilterFacetCollectorParser());
   this.facetCollectorParsers = builder.immutableMap();
 }
Exemplo n.º 5
0
 @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();
 }
  private Map<String, FieldMappingMetaData> findFieldMappingsByType(
      DocumentMapper documentMapper, GetFieldMappingsIndexRequest request) {
    MapBuilder<String, FieldMappingMetaData> fieldMappings = new MapBuilder<>();
    final DocumentFieldMappers allFieldMappers = documentMapper.mappers();
    for (String field : request.fields()) {
      if (Regex.isMatchAllPattern(field)) {
        for (FieldMapper fieldMapper : allFieldMappers) {
          addFieldMapper(
              fieldMapper.fieldType().name(),
              fieldMapper,
              fieldMappings,
              request.includeDefaults());
        }
      } else if (Regex.isSimpleMatchPattern(field)) {
        // go through the field mappers 3 times, to make sure we give preference to the resolve
        // order: full name, index name, name.
        // also make sure we only store each mapper once.
        Collection<FieldMapper> remainingFieldMappers = newLinkedList(allFieldMappers);
        for (Iterator<FieldMapper> it = remainingFieldMappers.iterator(); it.hasNext(); ) {
          final FieldMapper fieldMapper = it.next();
          if (Regex.simpleMatch(field, fieldMapper.fieldType().name())) {
            addFieldMapper(
                fieldMapper.fieldType().name(),
                fieldMapper,
                fieldMappings,
                request.includeDefaults());
            it.remove();
          }
        }
        for (Iterator<FieldMapper> it = remainingFieldMappers.iterator(); it.hasNext(); ) {
          final FieldMapper fieldMapper = it.next();
          if (Regex.simpleMatch(field, fieldMapper.fieldType().name())) {
            addFieldMapper(
                fieldMapper.fieldType().name(),
                fieldMapper,
                fieldMappings,
                request.includeDefaults());
            it.remove();
          }
        }

      } else {
        // not a pattern
        FieldMapper fieldMapper = allFieldMappers.smartNameFieldMapper(field);
        if (fieldMapper != null) {
          addFieldMapper(field, fieldMapper, fieldMappings, request.includeDefaults());
        } else if (request.probablySingleFieldRequest()) {
          fieldMappings.put(field, FieldMappingMetaData.NULL);
        }
      }
    }
    return fieldMappings.immutableMap();
  }
Exemplo n.º 7
0
 private void addObjectMappers(ObjectMapper... objectMappers) {
   synchronized (mutex) {
     MapBuilder<String, ObjectMapper> builder = MapBuilder.newMapBuilder(this.objectMappers);
     for (ObjectMapper objectMapper : objectMappers) {
       builder.put(objectMapper.fullPath(), objectMapper);
       if (objectMapper.nested().isNested()) {
         hasNestedObjects = true;
       }
     }
     this.objectMappers = builder.immutableMap();
   }
   for (ObjectMapperListener objectMapperListener : objectMapperListeners) {
     objectMapperListener.objectMappers(objectMappers);
   }
 }
 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();
     }
   }
 }
Exemplo n.º 9
0
 public IndexOutput createOutput(String name, IOContext context, boolean raw)
     throws IOException {
   Directory directory;
   if (isChecksum(name)) {
     directory = distributor.primary();
   } else {
     directory = distributor.any();
   }
   IndexOutput out = directory.createOutput(name, context);
   synchronized (mutex) {
     StoreFileMetaData metaData = new StoreFileMetaData(name, -1, null, directory);
     filesMetadata = MapBuilder.newMapBuilder(filesMetadata).put(name, metaData).immutableMap();
     files = filesMetadata.keySet().toArray(new String[filesMetadata.size()]);
     boolean computeChecksum = !raw;
     if (computeChecksum) {
       // don't compute checksum for segment based files
       if ("segments.gen".equals(name) || name.startsWith("segments")) {
         computeChecksum = false;
       }
     }
     if (computeChecksum) {
       out = new BufferedChecksumIndexOutput(out, new Adler32());
     }
     return new StoreIndexOutput(metaData, out, name);
   }
 }
Exemplo n.º 10
0
  /** Refresh all apps */
  public synchronized void refreshAllApps() {

    this.artifactApps = refreshArtifactApps();
    this.pluginApps = refreshPluginApps();
    this.siteApps = refreshSiteApps();

    this.apps = newHashMap();
    apps.putAll(artifactApps);
    apps.putAll(pluginApps);
    apps.putAll(siteApps);

    this.moduleApps = newHashMap();
    moduleApps.putAll(artifactApps);
    moduleApps.putAll(pluginApps);

    checkMandatory();

    // TODO check versions

    MapBuilder<App, List<OnModuleReference>> refs = MapBuilder.newMapBuilder();
    for (App app : moduleApps.values()) {
      List<OnModuleReference> list = onModuleRefs(app);
      if (!list.isEmpty()) {
        refs.put(app, list);
      }
    }
    this.onModuleReferences = refs.map();
  }
 public Builder(IndexTemplateMetaData indexTemplateMetaData) {
   this(indexTemplateMetaData.name());
   order(indexTemplateMetaData.order());
   template(indexTemplateMetaData.template());
   settings(indexTemplateMetaData.settings());
   mappings.putAll(indexTemplateMetaData.mappings());
 }
Exemplo n.º 12
0
 StoreDirectory(Distributor distributor) throws IOException {
   this.distributor = distributor;
   synchronized (mutex) {
     MapBuilder<String, StoreFileMetaData> builder = MapBuilder.newMapBuilder();
     Map<String, String> checksums =
         readChecksums(distributor.all(), new HashMap<String, String>());
     for (Directory delegate : distributor.all()) {
       for (String file : delegate.listAll()) {
         String checksum = checksums.get(file);
         builder.put(
             file, new StoreFileMetaData(file, delegate.fileLength(file), checksum, delegate));
       }
     }
     filesMetadata = builder.immutableMap();
     files = filesMetadata.keySet().toArray(new String[filesMetadata.size()]);
   }
 }
  @Test
  public void testAccessListInScript() {

    Map<String, Object> vars = new HashMap<String, Object>();
    Map<String, Object> obj2 =
        MapBuilder.<String, Object>newMapBuilder().put("prop2", "value2").map();
    Map<String, Object> obj1 =
        MapBuilder.<String, Object>newMapBuilder().put("prop1", "value1").put("obj2", obj2).map();
    vars.put("l", Arrays.asList("1", "2", "3", obj1));

    //        Object o = se.execute(se.compile("l.length"), vars);
    //        assertThat(((Number) o).intValue(), equalTo(4));

    Object o =
        se.execute(
            new CompiledScript(
                ScriptService.ScriptType.INLINE,
                "testAccessListInScript",
                "python",
                se.compile("l[0]")),
            vars);
    assertThat(((String) o), equalTo("1"));

    o =
        se.execute(
            new CompiledScript(
                ScriptService.ScriptType.INLINE,
                "testAccessListInScript",
                "python",
                se.compile("l[3]")),
            vars);
    obj1 = (Map<String, Object>) o;
    assertThat((String) obj1.get("prop1"), equalTo("value1"));
    assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2"));

    o =
        se.execute(
            new CompiledScript(
                ScriptService.ScriptType.INLINE,
                "testAccessListInScript",
                "python",
                se.compile("l[3]['prop1']")),
            vars);
    assertThat(((String) o), equalTo("value1"));
  }
Exemplo n.º 14
0
 public void writeChecksum(String name, String checksum) throws IOException {
   // update the metadata to include the checksum and write a new checksums file
   synchronized (mutex) {
     StoreFileMetaData metaData = filesMetadata.get(name);
     metaData =
         new StoreFileMetaData(metaData.name(), metaData.length(), checksum, metaData.directory());
     filesMetadata = MapBuilder.newMapBuilder(filesMetadata).put(name, metaData).immutableMap();
     writeChecksums();
   }
 }
 private void addFieldMapper(
     String field,
     FieldMapper<?> fieldMapper,
     MapBuilder<String, FieldMappingMetaData> fieldMappings,
     boolean includeDefaults) {
   if (fieldMappings.containsKey(field)) {
     return;
   }
   try {
     XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
     builder.startObject();
     fieldMapper.toXContent(
         builder, includeDefaults ? includeDefaultsParams : ToXContent.EMPTY_PARAMS);
     builder.endObject();
     fieldMappings.put(
         field, new FieldMappingMetaData(fieldMapper.names().fullName(), builder.bytes()));
   } catch (IOException e) {
     throw new ElasticsearchException("failed to serialize XContent of field [" + field + "]", e);
   }
 }
Exemplo n.º 16
0
  private void addMappers(
      Collection<ObjectMapper> objectMappers, Collection<FieldMapper> fieldMappers) {
    assert mappingLock.isWriteLockedByCurrentThread();
    // first ensure we don't have any incompatible new fields
    mapperService.checkNewMappersCompatibility(objectMappers, fieldMappers, true);

    // update mappers for this document type
    MapBuilder<String, ObjectMapper> builder = MapBuilder.newMapBuilder(this.objectMappers);
    for (ObjectMapper objectMapper : objectMappers) {
      builder.put(objectMapper.fullPath(), objectMapper);
      if (objectMapper.nested().isNested()) {
        hasNestedObjects = true;
      }
    }
    this.objectMappers = builder.immutableMap();
    this.fieldMappers = this.fieldMappers.copyAndAllAll(fieldMappers);

    // finally update for the entire index
    mapperService.addMappers(objectMappers, fieldMappers);
  }
Exemplo n.º 17
0
 private void addObjectMappers(ObjectMapper[] objectMappers) {
   synchronized (mutex) {
     MapBuilder<String, ObjectMappers> fullPathObjectMappers =
         newMapBuilder(this.fullPathObjectMappers);
     for (ObjectMapper objectMapper : objectMappers) {
       ObjectMappers mappers = fullPathObjectMappers.get(objectMapper.fullPath());
       if (mappers == null) {
         mappers = new ObjectMappers(objectMapper);
       } else {
         mappers = mappers.concat(objectMapper);
       }
       fullPathObjectMappers.put(objectMapper.fullPath(), mappers);
       // update the hasNested flag
       if (objectMapper.nested().isNested()) {
         hasNested = true;
       }
     }
     this.fullPathObjectMappers = fullPathObjectMappers.map();
   }
 }
 @Inject
 public InternalTransportClusterAdminClient(
     Settings settings,
     TransportClientNodesService nodesService,
     ThreadPool threadPool,
     TransportService transportService,
     Map<String, GenericAction> actions) {
   this.nodesService = nodesService;
   this.threadPool = threadPool;
   MapBuilder<ClusterAction, TransportActionNodeProxy> actionsBuilder =
       new MapBuilder<ClusterAction, TransportActionNodeProxy>();
   for (GenericAction action : actions.values()) {
     if (action instanceof ClusterAction) {
       actionsBuilder.put(
           (ClusterAction) action,
           new TransportActionNodeProxy(settings, action, transportService));
     }
   }
   this.actions = actionsBuilder.immutableMap();
 }
  @Test
  public void testMapAccess() {
    Map<String, Object> vars = new HashMap<String, Object>();

    Map<String, Object> obj2 =
        MapBuilder.<String, Object>newMapBuilder().put("prop2", "value2").map();
    Map<String, Object> obj1 =
        MapBuilder.<String, Object>newMapBuilder()
            .put("prop1", "value1")
            .put("obj2", obj2)
            .put("l", Lists.newArrayList("2", "1"))
            .map();
    vars.put("obj1", obj1);
    Object o = se.execute(se.compile("obj1"), vars);
    assertThat(o, instanceOf(Map.class));
    obj1 = (Map<String, Object>) o;
    assertThat((String) obj1.get("prop1"), equalTo("value1"));
    assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2"));

    o = se.execute(se.compile("obj1.l[0]"), vars);
    assertThat(((String) o), equalTo("2"));
  }
Exemplo n.º 20
0
 public CommitStats(SegmentInfos segmentInfos) {
   // clone the map to protect against concurrent changes
   userData =
       MapBuilder.<String, String>newMapBuilder()
           .putAll(segmentInfos.getUserData())
           .immutableMap();
   // lucene calls the current generation, last generation.
   generation = segmentInfos.getLastGeneration();
   if (segmentInfos.getId() != null) { // id is only written starting with Lucene 5.0
     id = Base64.encodeBytes(segmentInfos.getId());
   }
   numDocs = Lucene.getNumDocs(segmentInfos);
 }
Exemplo n.º 21
0
 public void registerHandler(String action, TransportRequestHandler handler) {
   synchronized (serverHandlersMutex) {
     TransportRequestHandler handlerReplaced = serverHandlers.get(action);
     serverHandlers = MapBuilder.newMapBuilder(serverHandlers).put(action, handler).immutableMap();
     if (handlerReplaced != null) {
       logger.warn(
           "Registered two transport handlers for action {}, handlers: {}, {}",
           action,
           handler,
           handlerReplaced);
     }
   }
 }
 /**
  * Adds mapping that will be added when the index gets created.
  *
  * @param type The mapping type
  * @param source The mapping source
  */
 public PutIndexTemplateRequest mapping(String type, Map<String, Object> source) {
   // wrap it in a type map if its not
   if (source.size() != 1 || !source.containsKey(type)) {
     source = MapBuilder.<String, Object>newMapBuilder().put(type, source).map();
   }
   try {
     XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
     builder.map(source);
     return mapping(type, builder.string());
   } catch (IOException e) {
     throw new ElasticSearchGenerationException("Failed to generate [" + source + "]", e);
   }
 }
Exemplo n.º 23
0
 private StatsHolder groupStats(String group) {
   StatsHolder stats = groupsStats.get(group);
   if (stats == null) {
     synchronized (this) {
       stats = groupsStats.get(group);
       if (stats == null) {
         stats = new StatsHolder();
         groupsStats = MapBuilder.newMapBuilder(groupsStats).put(group, stats).immutableMap();
       }
     }
   }
   return stats;
 }
 private StatsHolder typeStats(String type) {
   StatsHolder stats = typesStats.get(type);
   if (stats == null) {
     synchronized (this) {
       stats = typesStats.get(type);
       if (stats == null) {
         stats = new StatsHolder();
         typesStats = MapBuilder.newMapBuilder(typesStats).put(type, stats).immutableMap();
       }
     }
   }
   return stats;
 }
  @Test
  public void testAccessListInScript() {
    Map<String, Object> vars = new HashMap<String, Object>();
    Map<String, Object> obj2 =
        MapBuilder.<String, Object>newMapBuilder().put("prop2", "value2").map();
    Map<String, Object> obj1 =
        MapBuilder.<String, Object>newMapBuilder().put("prop1", "value1").put("obj2", obj2).map();
    vars.put("l", Lists.newArrayList("1", "2", "3", obj1));

    Object o = se.execute(se.compile("l.length"), vars);
    assertThat(((Number) o).intValue(), equalTo(4));

    o = se.execute(se.compile("l[0]"), vars);
    assertThat(((String) o), equalTo("1"));

    o = se.execute(se.compile("l[3]"), vars);
    obj1 = (Map<String, Object>) o;
    assertThat((String) obj1.get("prop1"), equalTo("value1"));
    assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2"));

    o = se.execute(se.compile("l[3].prop1"), vars);
    assertThat(((String) o), equalTo("value1"));
  }
Exemplo n.º 26
0
 public void writeChecksums(Map<String, String> checksums) throws IOException {
   // update the metadata to include the checksum and write a new checksums file
   synchronized (mutex) {
     for (Map.Entry<String, String> entry : checksums.entrySet()) {
       StoreFileMetaData metaData = filesMetadata.get(entry.getKey());
       metaData =
           new StoreFileMetaData(
               metaData.name(), metaData.length(), entry.getValue(), metaData.directory());
       filesMetadata =
           MapBuilder.newMapBuilder(filesMetadata).put(entry.getKey(), metaData).immutableMap();
     }
     writeChecksums();
   }
 }
 private AttributesRoutings getInitializingAttribute(AttributesKey key, DiscoveryNodes nodes) {
   AttributesRoutings shardRoutings = initializingShardsByAttributes.get(key);
   if (shardRoutings == null) {
     synchronized (shardsByAttributeMutex) {
       ArrayList<ShardRouting> from = new ArrayList<>(allInitializingShards);
       ImmutableList<ShardRouting> to = collectAttributeShards(key, nodes, from);
       shardRoutings = new AttributesRoutings(to, ImmutableList.copyOf(from));
       initializingShardsByAttributes =
           MapBuilder.newMapBuilder(initializingShardsByAttributes)
               .put(key, shardRoutings)
               .immutableMap();
     }
   }
   return shardRoutings;
 }
Exemplo n.º 28
0
 public void renameFile(String from, String to) throws IOException {
   synchronized (mutex) {
     StoreFileMetaData fromMetaData = filesMetadata.get(from); // we should always find this one
     if (fromMetaData == null) {
       throw new FileNotFoundException(from);
     }
     directoryService.renameFile(fromMetaData.directory(), from, to);
     StoreFileMetaData toMetaData =
         new StoreFileMetaData(
             to, fromMetaData.length(), fromMetaData.checksum(), fromMetaData.directory());
     filesMetadata =
         MapBuilder.newMapBuilder(filesMetadata).remove(from).put(to, toMetaData).immutableMap();
     files = filesMetadata.keySet().toArray(new String[filesMetadata.size()]);
   }
 }
Exemplo n.º 29
0
 public void deleteFileChecksum(String name) throws IOException {
   StoreFileMetaData metaData = filesMetadata.get(name);
   if (metaData != null) {
     try {
       metaData.directory().deleteFile(name);
     } catch (IOException e) {
       if (metaData.directory().fileExists(name)) {
         throw e;
       }
     }
   }
   synchronized (mutex) {
     filesMetadata = MapBuilder.newMapBuilder(filesMetadata).remove(name).immutableMap();
     files = filesMetadata.keySet().toArray(new String[filesMetadata.size()]);
   }
 }
Exemplo n.º 30
0
 /**
  * Adds mapping that will be added when the index gets created.
  *
  * @param type The mapping type
  * @param source The mapping source
  */
 @SuppressWarnings("unchecked")
 public CreateIndexRequest mapping(String type, Map source) {
   if (mappings.containsKey(type)) {
     throw new IllegalStateException("mappings for type \"" + type + "\" were already defined");
   }
   // wrap it in a type map if its not
   if (source.size() != 1 || !source.containsKey(type)) {
     source = MapBuilder.<String, Object>newMapBuilder().put(type, source).map();
   }
   try {
     XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
     builder.map(source);
     return mapping(type, builder.string());
   } catch (IOException e) {
     throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
   }
 }