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

com.clickzetta.platform.catalyst.data.BinarySection Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package com.clickzetta.platform.catalyst.data;

import com.clickzetta.platform.catalyst.memory.MemorySegment;
import com.clickzetta.platform.catalyst.memory.MemorySegmentUtils;
import com.google.common.base.Preconditions;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public abstract class BinarySection implements Serializable {
  private static final long serialVersionUID = 1L;

  public static final int MAX_FIX_PART_DATA_SIZE = 7;

  public static final long HIGHEST_FIRST_BIT = 0x80L << 56;

  public static final long HIGHEST_SECOND_TO_EIGHTH_BIT = 0x7FL << 56;

  protected transient MemorySegment[] segments;
  protected transient int offset;
  protected transient int sizeInBytes;

  public BinarySection() {
  }

  public BinarySection(MemorySegment[] segments, int offset, int sizeInBytes) {
    Preconditions.checkArgument(segments != null);
    this.segments = segments;
    this.offset = offset;
    this.sizeInBytes = sizeInBytes;
  }

  public final void pointTo(MemorySegment segment, int offset, int sizeInBytes) {
    pointTo(new MemorySegment[]{segment}, offset, sizeInBytes);
  }

  public void pointTo(MemorySegment[] segments, int offset, int sizeInBytes) {
    Preconditions.checkArgument(segments != null);
    this.segments = segments;
    this.offset = offset;
    this.sizeInBytes = sizeInBytes;
  }

  public MemorySegment[] getSegments() {
    return segments;
  }

  public int getOffset() {
    return offset;
  }

  public int getSizeInBytes() {
    return sizeInBytes;
  }

  public byte[] toBytes() {
    return MemorySegmentUtils.getBytes(segments, offset, sizeInBytes);
  }

  private void writeObject(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();
    byte[] bytes = toBytes();
    out.writeInt(bytes.length);
    out.write(bytes);
  }

  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    byte[] bytes = new byte[in.readInt()];
    IOUtils.readFully(in, bytes);
    pointTo(MemorySegment.wrap(bytes), 0, bytes.length);
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    final BinarySection that = (BinarySection) o;
    return sizeInBytes == that.sizeInBytes
        && MemorySegmentUtils.equals(
        segments, offset, that.segments, that.offset, sizeInBytes);
  }

  @Override
  public int hashCode() {
    return MemorySegmentUtils.hash(segments, offset, sizeInBytes);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy