private boolean commitPointFileExistsInBlobs(
     CommitPoint.FileInfo fileInfo, ImmutableMap<String, BlobMetaData> blobs) {
   BlobMetaData blobMetaData = blobs.get(fileInfo.name());
   if (blobMetaData != null) {
     if (blobMetaData.length() != fileInfo.length()) {
       return false;
     }
   } else if (blobs.containsKey(fileInfo.name() + ".part0")) {
     // multi part file sum up the size and check
     int part = 0;
     long totalSize = 0;
     while (true) {
       blobMetaData = blobs.get(fileInfo.name() + ".part" + part++);
       if (blobMetaData == null) {
         break;
       }
       totalSize += blobMetaData.length();
     }
     if (totalSize != fileInfo.length()) {
       return false;
     }
   } else {
     // no file, not exact and not multipart
     return false;
   }
   return true;
 }
 private void serializeNullValue(ParseContext context, String lastFieldName) throws IOException {
   // we can only handle null values if we have mappings for them
   Mapper mapper = mappers.get(lastFieldName);
   if (mapper != null) {
     mapper.parse(context);
   }
 }
 private void serializeArray(ParseContext context, String lastFieldName) throws IOException {
   Mapper mapper = mappers.get(lastFieldName);
   if (mapper != null && mapper instanceof ArrayValueMapperParser) {
     mapper.parse(context);
   } else {
     XContentParser parser = context.parser();
     XContentParser.Token token;
     while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
       if (token == XContentParser.Token.START_OBJECT) {
         serializeObject(context, lastFieldName);
       } else if (token == XContentParser.Token.START_ARRAY) {
         serializeArray(context, lastFieldName);
       } else if (token == XContentParser.Token.FIELD_NAME) {
         lastFieldName = parser.currentName();
       } else if (token == XContentParser.Token.VALUE_NULL) {
         serializeNullValue(context, lastFieldName);
       } else {
         serializeValue(context, lastFieldName, token);
       }
     }
   }
 }
 @Override
 public Injector shardInjector(int shardId) throws ElasticSearchException {
   return shardsInjectors.get(shardId);
 }
 @Override
 public IndexShard shard(int shardId) {
   return shards.get(shardId);
 }
  @Override
  public void merge(final Mapper mergeWith, final MergeContext mergeContext)
      throws MergeMappingException {
    if (!(mergeWith instanceof ObjectMapper)) {
      mergeContext.addConflict(
          "Can't merge a non object mapping ["
              + mergeWith.name()
              + "] with an object mapping ["
              + name()
              + "]");
      return;
    }
    ObjectMapper mergeWithObject = (ObjectMapper) mergeWith;

    doMerge(mergeWithObject, mergeContext);

    List<Mapper> mappersToTraverse = new ArrayList<Mapper>();
    synchronized (mutex) {
      for (Mapper mergeWithMapper : mergeWithObject.mappers.values()) {
        Mapper mergeIntoMapper = mappers.get(mergeWithMapper.name());
        if (mergeIntoMapper == null) {
          // no mapping, simply add it if not simulating
          if (!mergeContext.mergeFlags().simulate()) {
            putMapper(mergeWithMapper);
            mappersToTraverse.add(mergeWithMapper);
          }
        } else {
          if ((mergeWithMapper instanceof MultiFieldMapper)
              && !(mergeIntoMapper instanceof MultiFieldMapper)) {
            MultiFieldMapper mergeWithMultiField = (MultiFieldMapper) mergeWithMapper;
            mergeWithMultiField.merge(mergeIntoMapper, mergeContext);
            if (!mergeContext.mergeFlags().simulate()) {
              putMapper(mergeWithMultiField);
              // now, record mappers to traverse events for all mappers
              for (Mapper mapper : mergeWithMultiField.mappers().values()) {
                mappersToTraverse.add(mapper);
              }
            }
          } else {
            mergeIntoMapper.merge(mergeWithMapper, mergeContext);
          }
        }
      }
    }
    // call this outside of the mutex
    for (Mapper mapper : mappersToTraverse) {
      mapper.traverse(
          new FieldMapperListener() {
            @Override
            public void fieldMapper(FieldMapper fieldMapper) {
              mergeContext.docMapper().addFieldMapper(fieldMapper);
            }
          });
      mapper.traverse(
          new ObjectMapperListener() {
            @Override
            public void objectMapper(ObjectMapper objectMapper) {
              mergeContext.docMapper().addObjectMapper(objectMapper);
            }
          });
    }
  }
  private void serializeValue(
      final ParseContext context, String currentFieldName, XContentParser.Token token)
      throws IOException {
    if (currentFieldName == null) {
      throw new MapperParsingException(
          "object mapping ["
              + name
              + "] trying to serialize a value with no field associated with it, current value ["
              + context.parser().textOrNull()
              + "]");
    }
    Mapper mapper = mappers.get(currentFieldName);
    if (mapper != null) {
      mapper.parse(context);
      return;
    }
    Dynamic dynamic = this.dynamic;
    if (dynamic == null) {
      dynamic = context.root().dynamic();
    }
    if (dynamic == Dynamic.STRICT) {
      throw new StrictDynamicMappingException(currentFieldName);
    }
    if (dynamic == Dynamic.FALSE) {
      return;
    }
    // we sync here since we don't want to add this field twice to the document mapper
    // its not the end of the world, since we add it to the mappers once we create it
    // so next time we won't even get here for this field
    boolean newMapper = false;
    synchronized (mutex) {
      mapper = mappers.get(currentFieldName);
      if (mapper == null) {
        newMapper = true;
        BuilderContext builderContext = new BuilderContext(context.indexSettings(), context.path());
        if (token == XContentParser.Token.VALUE_STRING) {
          boolean resolved = false;

          // do a quick test to see if its fits a dynamic template, if so, use it.
          // we need to do it here so we can handle things like attachment templates, where calling
          // text (to see if its a date) causes the binary value to be cleared
          if (!resolved) {
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "string", null);
            if (builder != null) {
              mapper = builder.build(builderContext);
              resolved = true;
            }
          }

          if (!resolved && context.parser().textLength() == 0) {
            // empty string with no mapping, treat it like null value
            return;
          }

          if (!resolved && context.root().dateDetection()) {
            String text = context.parser().text();
            // a safe check since "1" gets parsed as well
            if (text.contains(":") || text.contains("-") || text.contains("/")) {
              for (FormatDateTimeFormatter dateTimeFormatter :
                  context.root().dynamicDateTimeFormatters()) {
                try {
                  dateTimeFormatter.parser().parseMillis(text);
                  Mapper.Builder builder =
                      context.root().findTemplateBuilder(context, currentFieldName, "date");
                  if (builder == null) {
                    builder = dateField(currentFieldName).dateTimeFormatter(dateTimeFormatter);
                  }
                  mapper = builder.build(builderContext);
                  resolved = true;
                  break;
                } catch (Exception e) {
                  // failure to parse this, continue
                }
              }
            }
          }
          if (!resolved && context.root().numericDetection()) {
            String text = context.parser().text();
            try {
              Long.parseLong(text);
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "long");
              if (builder == null) {
                builder = longField(currentFieldName);
              }
              mapper = builder.build(builderContext);
              resolved = true;
            } catch (Exception e) {
              // not a long number
            }
            if (!resolved) {
              try {
                Double.parseDouble(text);
                Mapper.Builder builder =
                    context.root().findTemplateBuilder(context, currentFieldName, "double");
                if (builder == null) {
                  builder = doubleField(currentFieldName);
                }
                mapper = builder.build(builderContext);
                resolved = true;
              } catch (Exception e) {
                // not a long number
              }
            }
          }
          // DON'T do automatic ip detection logic, since it messes up with docs that have hosts and
          // ips
          // check if its an ip
          //                if (!resolved && text.indexOf('.') != -1) {
          //                    try {
          //                        IpFieldMapper.ipToLong(text);
          //                        XContentMapper.Builder builder =
          // context.root().findTemplateBuilder(context, currentFieldName, "ip");
          //                        if (builder == null) {
          //                            builder = ipField(currentFieldName);
          //                        }
          //                        mapper = builder.build(builderContext);
          //                        resolved = true;
          //                    } catch (Exception e) {
          //                        // failure to parse, not ip...
          //                    }
          //                }
          if (!resolved) {
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "string");
            if (builder == null) {
              builder = stringField(currentFieldName);
            }
            mapper = builder.build(builderContext);
          }
        } else if (token == XContentParser.Token.VALUE_NUMBER) {
          XContentParser.NumberType numberType = context.parser().numberType();
          if (numberType == XContentParser.NumberType.INT) {
            if (context.parser().estimatedNumberType()) {
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "long");
              if (builder == null) {
                builder = longField(currentFieldName);
              }
              mapper = builder.build(builderContext);
            } else {
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "integer");
              if (builder == null) {
                builder = integerField(currentFieldName);
              }
              mapper = builder.build(builderContext);
            }
          } else if (numberType == XContentParser.NumberType.LONG) {
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "long");
            if (builder == null) {
              builder = longField(currentFieldName);
            }
            mapper = builder.build(builderContext);
          } else if (numberType == XContentParser.NumberType.FLOAT) {
            if (context.parser().estimatedNumberType()) {
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "double");
              if (builder == null) {
                builder = doubleField(currentFieldName);
              }
              mapper = builder.build(builderContext);
            } else {
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "float");
              if (builder == null) {
                builder = floatField(currentFieldName);
              }
              mapper = builder.build(builderContext);
            }
          } else if (numberType == XContentParser.NumberType.DOUBLE) {
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "double");
            if (builder == null) {
              builder = doubleField(currentFieldName);
            }
            mapper = builder.build(builderContext);
          }
        } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
          Mapper.Builder builder =
              context.root().findTemplateBuilder(context, currentFieldName, "boolean");
          if (builder == null) {
            builder = booleanField(currentFieldName);
          }
          mapper = builder.build(builderContext);
        } else {
          Mapper.Builder builder =
              context.root().findTemplateBuilder(context, currentFieldName, null);
          if (builder != null) {
            mapper = builder.build(builderContext);
          } else {
            // TODO how do we identify dynamically that its a binary value?
            throw new ElasticSearchIllegalStateException(
                "Can't handle serializing a dynamic type with content token ["
                    + token
                    + "] and field name ["
                    + currentFieldName
                    + "]");
          }
        }
        putMapper(mapper);
        context.addedMapper();
      }
    }
    if (newMapper) {
      mapper.traverse(
          new FieldMapperListener() {
            @Override
            public void fieldMapper(FieldMapper fieldMapper) {
              context.docMapper().addFieldMapper(fieldMapper);
            }
          });
    }
    mapper.parse(context);
  }
  private void serializeObject(final ParseContext context, String currentFieldName)
      throws IOException {
    if (currentFieldName == null) {
      throw new MapperParsingException(
          "object mapping ["
              + name
              + "] trying to serialize an object with no field associated with it, current value ["
              + context.parser().textOrNull()
              + "]");
    }
    context.path().add(currentFieldName);

    Mapper objectMapper = mappers.get(currentFieldName);
    if (objectMapper != null) {
      objectMapper.parse(context);
    } else {
      Dynamic dynamic = this.dynamic;
      if (dynamic == null) {
        dynamic = context.root().dynamic();
      }
      if (dynamic == Dynamic.STRICT) {
        throw new StrictDynamicMappingException(currentFieldName);
      } else if (dynamic == Dynamic.TRUE) {
        // we sync here just so we won't add it twice. Its not the end of the world
        // to sync here since next operations will get it before
        boolean newMapper = false;
        synchronized (mutex) {
          objectMapper = mappers.get(currentFieldName);
          if (objectMapper == null) {
            newMapper = true;
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "object");
            if (builder == null) {
              builder =
                  MapperBuilders.object(currentFieldName)
                      .enabled(true)
                      .dynamic(dynamic)
                      .pathType(pathType);
            }
            // remove the current field name from path, since the object builder adds it as well...
            context.path().remove();
            BuilderContext builderContext =
                new BuilderContext(context.indexSettings(), context.path());
            objectMapper = builder.build(builderContext);
            putMapper(objectMapper);
            // now re add it
            context.path().add(currentFieldName);
            context.addedMapper();
          }
        }
        // traverse and parse outside of the mutex
        if (newMapper) {
          // we need to traverse in case we have a dynamic template and need to add field mappers
          // introduced by it
          objectMapper.traverse(
              new FieldMapperListener() {
                @Override
                public void fieldMapper(FieldMapper fieldMapper) {
                  context.docMapper().addFieldMapper(fieldMapper);
                }
              });
          objectMapper.traverse(
              new ObjectMapperListener() {
                @Override
                public void objectMapper(ObjectMapper objectMapper) {
                  context.docMapper().addObjectMapper(objectMapper);
                }
              });
        }
        // now, parse it
        objectMapper.parse(context);
      } else {
        // not dynamic, read everything up to end object
        context.parser().skipChildren();
      }
    }

    context.path().remove();
  }
  @Override
  public void parse(XContentParser parser, SearchContext context) throws Exception {
    XContentParser.Token token;

    List<FacetCollector> facetCollectors = null;

    String topLevelFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        topLevelFieldName = parser.currentName();
      } else if (token == XContentParser.Token.START_OBJECT) {
        FacetCollector facet = null;
        String scope = ContextIndexSearcher.Scopes.MAIN;
        String facetFieldName = null;
        Filter filter = null;
        boolean cacheFilter = true;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
          if (token == XContentParser.Token.FIELD_NAME) {
            facetFieldName = parser.currentName();
          } else if (token == XContentParser.Token.START_OBJECT) {
            if ("facet_filter".equals(facetFieldName) || "facetFilter".equals(facetFieldName)) {
              XContentIndexQueryParser indexQueryParser =
                  (XContentIndexQueryParser) context.queryParser();
              filter = indexQueryParser.parseInnerFilter(parser);
            } else {
              FacetCollectorParser facetCollectorParser = facetCollectorParsers.get(facetFieldName);
              if (facetCollectorParser == null) {
                throw new SearchParseException(
                    context, "No facet type for [" + facetFieldName + "]");
              }
              facet = facetCollectorParser.parse(topLevelFieldName, parser, context);
            }
          } else if (token.isValue()) {
            if ("global".equals(facetFieldName)) {
              if (parser.booleanValue()) {
                scope = ContextIndexSearcher.Scopes.GLOBAL;
              }
            } else if ("scope".equals(facetFieldName)) {
              scope = parser.text();
            } else if ("cache_filter".equals(facetFieldName)
                || "cacheFilter".equals(facetFieldName)) {
              cacheFilter = parser.booleanValue();
            }
          }
        }
        if (filter != null) {
          if (cacheFilter) {
            filter = context.filterCache().cache(filter);
          }
          facet.setFilter(filter);
        }

        if (facetCollectors == null) {
          facetCollectors = Lists.newArrayList();
        }
        facetCollectors.add(facet);
        context.searcher().addCollector(scope, facet);
      }
    }

    context.facets(new SearchContextFacets(facetCollectors));
  }