Ejemplo n.º 1
0
  private boolean innerIndex(Index index) throws IOException {
    synchronized (dirtyLock(index.uid())) {
      lastWriteNanos = index.startTime();
      final long currentVersion;
      final boolean deleted;
      VersionValue versionValue = versionMap.getUnderLock(index.uid().bytes());
      if (versionValue == null) {
        currentVersion = loadCurrentVersionFromIndex(index.uid());
        deleted = currentVersion == Versions.NOT_FOUND;
      } else {
        deleted = versionValue.delete();
        if (engineConfig.isEnableGcDeletes()
            && versionValue.delete()
            && (engineConfig.getThreadPool().estimatedTimeInMillis() - versionValue.time())
                > getGcDeletesInMillis()) {
          currentVersion = Versions.NOT_FOUND; // deleted, and GC
        } else {
          currentVersion = versionValue.version();
        }
      }

      long expectedVersion = index.version();
      if (isVersionConflictForWrites(index, currentVersion, deleted, expectedVersion)) {
        if (index.origin() != Operation.Origin.RECOVERY) {
          throw new VersionConflictEngineException(
              shardId,
              index.type(),
              index.id(),
              index
                  .versionType()
                  .explainConflictForWrites(currentVersion, expectedVersion, deleted));
        }
        return false;
      }
      long updatedVersion = index.versionType().updateVersion(currentVersion, expectedVersion);

      final boolean created;
      index.updateVersion(updatedVersion);

      if (currentVersion == Versions.NOT_FOUND) {
        // document does not exists, we can optimize for create
        created = true;
        index(index, indexWriter);
      } else {
        created = update(index, versionValue, indexWriter);
      }
      Translog.Location translogLocation = translog.add(new Translog.Index(index));

      versionMap.putUnderLock(
          index.uid().bytes(), new VersionValue(updatedVersion, translogLocation));
      index.setTranslogLocation(translogLocation);
      return created;
    }
  }
Ejemplo n.º 2
0
 private boolean isVersionConflictForWrites(
     Index index, long currentVersion, boolean deleted, long expectedVersion) {
   return index.versionType().isVersionConflictForWrites(currentVersion, expectedVersion, deleted);
 }