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

br.com.objectos.git.GitWorker Maven / Gradle / Ivy

Go to download

ObjectosGit is a pure Java library that provides a limited set of GIT operations.

The newest version!
/*
 * Copyright (C) 2020-2022 Objectos Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package br.com.objectos.git;

import br.com.objectos.collections.set.ImmutableSet;
import br.com.objectos.concurrent.Computation;
import br.com.objectos.concurrent.CpuWorker;
import br.com.objectos.concurrent.IoWorker;
import br.com.objectos.fs.Directory;
import br.com.objectos.git.GitEngine.Option;

final class GitWorker {

  private final CpuWorker cpuWorker;

  private final GitEngine[] engines;

  GitWorker(CpuWorker cpuWorker, GitEngine[] engines) {
    this.cpuWorker = cpuWorker;

    this.engines = engines;
  }

  public static GitWorker create(
      CpuWorker cpuWorker, IoWorker ioWorker, int enginesPerWorker, Option[] engineOptions) {
    GitEngine[] engines;
    engines = new GitEngine[enginesPerWorker];

    for (int i = 0; i < enginesPerWorker; i++) {
      engines[i] = GitEngine.create(ioWorker, engineOptions);
    }

    return new GitWorker(cpuWorker, engines);
  }

  public final Computation> copyObjects(
      Repository source, ImmutableSet objectsToCopy, Repository destination) {
    GitEngine e;
    e = randomEngine();

    GitTask> task;
    task = e.copyObjects(source, objectsToCopy, destination);

    return create(task);
  }

  public final Computation materializeEntry(
      Repository repository, Entry entry, Directory directory) {
    GitEngine e;
    e = randomEngine();

    GitTask task;
    task = e.materializeEntry(repository, entry, directory);

    return create(task);
  }

  public final Computation openRepository(Directory directory) {
    GitEngine e;
    e = randomEngine();

    GitTask task;
    task = e.openRepository(directory);

    return create(task);
  }

  public final Computation readBlob(Repository repository, ObjectId id) {
    GitEngine e;
    e = randomEngine();

    GitTask task;
    task = e.readBlob(repository, id);

    return create(task);
  }

  public final Computation readCommit(Repository repository, ObjectId id) {
    GitEngine e;
    e = randomEngine();

    GitTask task;
    task = e.readCommit(repository, id);

    return create(task);
  }

  public final Computation readTree(Repository repository, ObjectId id) {
    GitEngine e;
    e = randomEngine();

    GitTask task;
    task = e.readTree(repository, id);

    return create(task);
  }

  public final Computation resolve(Repository repository, RefName ref) {
    GitEngine e;
    e = randomEngine();

    GitTask task;
    task = e.resolve(repository, ref);

    return create(task);
  }

  public final Computation updateRef(
      Repository repository, RefName ref, ObjectId newValue) {
    GitEngine e;
    e = randomEngine();

    GitTask task;
    task = e.updateRef(repository, ref, newValue);

    return create(task);
  }

  public final Computation writeCommit(
      Repository repository, MutableCommit commit) {
    GitEngine e;
    e = randomEngine();

    GitTask task;
    task = e.writeCommit(repository, commit);

    return create(task);
  }

  public final Computation writeTree(
      GitCommand command, Repository repository, MutableTree tree) {
    GitEngine e;
    e = randomEngine();

    GitTask task;
    task = e.writeTree(repository, tree);

    return create(command, task);
  }

  private  Computation create(GitCommand command, GitTask task) {
    AbstractGitTask o;
    o = (AbstractGitTask) task;

    o.setCommand(command);

    return create(task);
  }

  private  Computation create(GitTask task) {
    if (cpuWorker.offer(task)) {
      return task;
    } else {
      return new CpuTaskRejected(task);
    }
  }

  private GitEngine randomEngine() {
    int index;
    index = Git.randomIndex(engines);

    return engines[index];
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy