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

swim.db.Commit Maven / Gradle / Ivy

Go to download

Lock-free document store—optimized for high rate atomic state changes—that concurrently commits and compacts on-disk log-structured storage files without blocking parallel in-memory updates to associative B-tree maps, spatial Q-tree maps, sequential S-tree lists, and singleton U-tree values

There is a newer version: 4.3.15
Show newest version
// Copyright 2015-2020 SWIM.AI inc.
//
// 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 swim.db;

import swim.collections.FingerTrieSeq;
import swim.concurrent.Cont;
import swim.concurrent.Conts;

public final class Commit {

  private static Commit closed;
  private static Commit forced;
  final boolean isClosed;
  final boolean isForced;
  final boolean isShifted;
  final FingerTrieSeq> conts;

  public Commit(boolean isClosed, boolean isForced, boolean isShifted,
                FingerTrieSeq> conts) {
    this.isClosed = isClosed;
    this.isForced = isForced;
    this.isShifted = isShifted;
    this.conts = conts;
  }

  public Commit(boolean isClosed, boolean isForced, boolean isShifted) {
    this(isClosed, isForced, isShifted, FingerTrieSeq.>empty());
  }

  public static Commit closed() {
    if (closed == null) {
      closed = new Commit(true, true, false);
    }
    return closed;
  }

  public static Commit forced() {
    if (forced == null) {
      forced = new Commit(false, true, false);
    }
    return forced;
  }

  public boolean isClosed() {
    return this.isClosed;
  }

  public Commit isClosed(boolean isClosed) {
    return new Commit(isClosed, this.isForced, this.isShifted, this.conts);
  }

  public boolean isForced() {
    return this.isForced;
  }

  public Commit isForced(boolean isForced) {
    return new Commit(this.isClosed, isForced, this.isShifted, this.conts);
  }

  public boolean isShifted() {
    return this.isShifted;
  }

  public Commit isShifted(boolean isShifted) {
    return new Commit(this.isClosed, this.isForced, isShifted, this.conts);
  }

  public FingerTrieSeq> conts() {
    return this.conts;
  }

  public Commit andThen(Cont cont) {
    return new Commit(this.isClosed, this.isForced, this.isShifted, this.conts.appended(cont));
  }

  public void bind(Chunk chunk) {
    for (Cont cont : conts) {
      try {
        cont.bind(chunk);
      } catch (Throwable cause) {
        if (Conts.isNonFatal(cause)) {
          cont.trap(cause);
        } else {
          throw cause;
        }
      }
    }
  }

  public void trap(Throwable error) {
    for (Cont cont : conts) {
      try {
        cont.trap(error);
      } catch (Throwable cause) {
        if (Conts.isNonFatal(cause)) {
          cause.printStackTrace(); // swallow
        } else {
          throw cause;
        }
      }
    }
  }

  public Commit merged(Commit that) {
    final boolean isClosed = this.isClosed || that.isClosed;
    final boolean isForced = this.isForced || that.isForced;
    final boolean isShifted = this.isShifted || that.isShifted;
    final FingerTrieSeq> conts = this.conts.appended(that.conts);
    return new Commit(isClosed, isForced, isShifted, conts);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy