All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.weaviate.client.v1.backup.api.BackupCreator Maven / Gradle / Ivy

There is a newer version: 4.9.0
Show newest version
// Generated by delombok at Fri Nov 24 13:20:07 UTC 2023
package io.weaviate.client.v1.backup.api;

import io.weaviate.client.v1.backup.model.BackupCreateResponse;
import io.weaviate.client.v1.backup.model.BackupCreateStatusResponse;
import io.weaviate.client.v1.backup.model.CreateStatus;
import io.weaviate.client.Config;
import io.weaviate.client.base.BaseClient;
import io.weaviate.client.base.ClientResult;
import io.weaviate.client.base.Response;
import io.weaviate.client.base.Result;
import io.weaviate.client.base.http.HttpClient;

public class BackupCreator extends BaseClient implements ClientResult {
  private static final long WAIT_INTERVAL = 1000;
  private final BackupCreateStatusGetter statusGetter;
  private String[] includeClassNames;
  private String[] excludeClassNames;
  private String backend;
  private String backupId;
  private boolean waitForCompletion;

  public BackupCreator(HttpClient httpClient, Config config, BackupCreateStatusGetter statusGetter) {
    super(httpClient, config);
    this.statusGetter = statusGetter;
  }

  public BackupCreator withIncludeClassNames(String... classNames) {
    this.includeClassNames = classNames;
    return this;
  }

  public BackupCreator withExcludeClassNames(String... classNames) {
    this.excludeClassNames = classNames;
    return this;
  }

  public BackupCreator withBackend(String backend) {
    this.backend = backend;
    return this;
  }

  public BackupCreator withBackupId(String backupId) {
    this.backupId = backupId;
    return this;
  }

  public BackupCreator withWaitForCompletion(boolean waitForCompletion) {
    this.waitForCompletion = waitForCompletion;
    return this;
  }

  @Override
  public Result run() {
    BackupCreate payload = BackupCreate.builder().id(backupId).config(BackupCreateConfig.builder().build()).include(includeClassNames).exclude(excludeClassNames).build();
    if (waitForCompletion) {
      return createAndWaitForCompletion(payload);
    }
    return create(payload);
  }

  private Result create(BackupCreate payload) {
    Response response = sendPostRequest(path(), payload, BackupCreateResponse.class);
    return new Result<>(response);
  }

  private Result createAndWaitForCompletion(BackupCreate payload) {
    Result result = create(payload);
    if (result.hasErrors()) {
      return result;
    }
    statusGetter.withBackend(backend).withBackupId(backupId);
    while (true) {
      Response statusResponse = statusGetter.statusCreate();
      if (new Result<>(statusResponse).hasErrors()) {
        return merge(statusResponse, result);
      }
      switch (statusResponse.getBody().getStatus()) {
      case CreateStatus.SUCCESS: 
      case CreateStatus.FAILED: 
        return merge(statusResponse, result);
      }
      try {
        Thread.sleep(WAIT_INTERVAL);
      } catch (InterruptedException e) {
        return merge(statusResponse, result);
      }
    }
  }

  private String path() {
    return String.format("/backups/%s", backend);
  }

  private Result merge(Response response, Result result) {
    BackupCreateStatusResponse statusCreateResponse = response.getBody();
    BackupCreateResponse createResponse = result.getResult();
    BackupCreateResponse merged = null;
    if (statusCreateResponse != null) {
      merged = new BackupCreateResponse();
      merged.setId(statusCreateResponse.getId());
      merged.setBackend(statusCreateResponse.getBackend());
      merged.setPath(statusCreateResponse.getPath());
      merged.setStatus(statusCreateResponse.getStatus());
      merged.setError(statusCreateResponse.getError());
      merged.setClassNames(createResponse.getClassNames());
    }
    return new Result<>(response.getStatusCode(), merged, response.getErrors());
  }


  private static class BackupCreate {
    String id;
    BackupCreateConfig config;
    String[] include;
    String[] exclude;

    @java.lang.SuppressWarnings("all")
    BackupCreate(final String id, final BackupCreateConfig config, final String[] include, final String[] exclude) {
      this.id = id;
      this.config = config;
      this.include = include;
      this.exclude = exclude;
    }


    @java.lang.SuppressWarnings("all")
    public static class BackupCreateBuilder {
      @java.lang.SuppressWarnings("all")
      private String id;
      @java.lang.SuppressWarnings("all")
      private BackupCreateConfig config;
      @java.lang.SuppressWarnings("all")
      private String[] include;
      @java.lang.SuppressWarnings("all")
      private String[] exclude;

      @java.lang.SuppressWarnings("all")
      BackupCreateBuilder() {
      }

      /**
       * @return {@code this}.
       */
      @java.lang.SuppressWarnings("all")
      public BackupCreator.BackupCreate.BackupCreateBuilder id(final String id) {
        this.id = id;
        return this;
      }

      /**
       * @return {@code this}.
       */
      @java.lang.SuppressWarnings("all")
      public BackupCreator.BackupCreate.BackupCreateBuilder config(final BackupCreateConfig config) {
        this.config = config;
        return this;
      }

      /**
       * @return {@code this}.
       */
      @java.lang.SuppressWarnings("all")
      public BackupCreator.BackupCreate.BackupCreateBuilder include(final String[] include) {
        this.include = include;
        return this;
      }

      /**
       * @return {@code this}.
       */
      @java.lang.SuppressWarnings("all")
      public BackupCreator.BackupCreate.BackupCreateBuilder exclude(final String[] exclude) {
        this.exclude = exclude;
        return this;
      }

      @java.lang.SuppressWarnings("all")
      public BackupCreator.BackupCreate build() {
        return new BackupCreator.BackupCreate(this.id, this.config, this.include, this.exclude);
      }

      @java.lang.Override
      @java.lang.SuppressWarnings("all")
      public java.lang.String toString() {
        return "BackupCreator.BackupCreate.BackupCreateBuilder(id=" + this.id + ", config=" + this.config + ", include=" + java.util.Arrays.deepToString(this.include) + ", exclude=" + java.util.Arrays.deepToString(this.exclude) + ")";
      }
    }

    @java.lang.SuppressWarnings("all")
    public static BackupCreator.BackupCreate.BackupCreateBuilder builder() {
      return new BackupCreator.BackupCreate.BackupCreateBuilder();
    }

    @java.lang.SuppressWarnings("all")
    public String getId() {
      return this.id;
    }

    @java.lang.SuppressWarnings("all")
    public BackupCreateConfig getConfig() {
      return this.config;
    }

    @java.lang.SuppressWarnings("all")
    public String[] getInclude() {
      return this.include;
    }

    @java.lang.SuppressWarnings("all")
    public String[] getExclude() {
      return this.exclude;
    }
  }


  public static class BackupCreateConfig {
    @java.lang.SuppressWarnings("all")
    BackupCreateConfig() {
    }


    @java.lang.SuppressWarnings("all")
    public static class BackupCreateConfigBuilder {
      @java.lang.SuppressWarnings("all")
      BackupCreateConfigBuilder() {
      }

      @java.lang.SuppressWarnings("all")
      public BackupCreator.BackupCreateConfig build() {
        return new BackupCreator.BackupCreateConfig();
      }

      @java.lang.Override
      @java.lang.SuppressWarnings("all")
      public java.lang.String toString() {
        return "BackupCreator.BackupCreateConfig.BackupCreateConfigBuilder()";
      }
    }

    @java.lang.SuppressWarnings("all")
    public static BackupCreator.BackupCreateConfig.BackupCreateConfigBuilder builder() {
      return new BackupCreator.BackupCreateConfig.BackupCreateConfigBuilder();
    }
    // TBD
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy