View Javadoc
1   package org.wikimedia.search.extra.superdetectnoop;
2   
3   import static org.wikimedia.search.extra.superdetectnoop.ChangeHandler.TypeSafe.nullAndTypeSafe;
4   
5   import javax.annotation.Nonnull;
6   
7   /**
8    * Detects if the stored version of the document is newer than the provided
9    * version, and noop's the entire update. Necessary because the elasticsearch
10   * _update api, which is used to run super_detect_noop, does not support native
11   * versioning.
12   *
13   * Only works properly with whole numbers up to 2^63-1
14   */
15  public final class VersionedDocumentHandler implements ChangeHandler.NonnullChangeHandler<Number> {
16      public static class Recognizer implements ChangeHandler.Recognizer {
17          private static final String DESCRIPTION = "documentVersion";
18  
19          @Override
20          public ChangeHandler<Object> build(String description) {
21              if (!description.equals(DESCRIPTION)) {
22                  return null;
23              }
24              return INSTANCE;
25          }
26      }
27  
28      private static final ChangeHandler<Object> INSTANCE =
29              nullAndTypeSafe(Number.class, new VersionedDocumentHandler());
30  
31      private VersionedDocumentHandler() {
32          // Only a single instance is used
33      }
34  
35      @Override
36      public ChangeHandler.Result handle(@Nonnull Number oldVersion, @Nonnull Number newVersion) {
37          if (oldVersion.longValue() == newVersion.longValue()) {
38              // If version is identical we should let other
39              // fields decide if the update is necessary
40              // otherwize it's like disabling the benefit
41              // of the super_noop_script, we won't update the fields
42              // but still update the lucene doc.
43              return ChangeHandler.CloseEnough.INSTANCE;
44          } else if (oldVersion.longValue() > newVersion.longValue()) {
45              return ChangeHandler.NoopDocument.INSTANCE;
46          }
47          return new ChangeHandler.Changed(newVersion);
48      }
49  }