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

com.scalar.db.storage.dynamo.bytes.TextBytesEncoder Maven / Gradle / Ivy

Go to download

A universal transaction manager that achieves database-agnostic transactions and distributed transactions that span multiple databases

There is a newer version: 3.14.0
Show newest version
package com.scalar.db.storage.dynamo.bytes;

import static com.scalar.db.storage.dynamo.bytes.BytesUtils.mask;

import com.scalar.db.api.Scan.Ordering.Order;
import com.scalar.db.io.TextValue;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import javax.annotation.concurrent.ThreadSafe;

@ThreadSafe
public class TextBytesEncoder implements BytesEncoder {
  private static final byte TERM = 0x00;

  TextBytesEncoder() {}

  @Override
  public int encodedLength(TextValue value, Order order) {
    assert value.getAsString().isPresent();
    return value.getAsString().get().getBytes(StandardCharsets.UTF_8).length + 1;
  }

  @Override
  public void encode(TextValue value, Order order, ByteBuffer dst) {
    assert value.getAsString().isPresent();
    if (value.getAsString().get().contains("\u0000")) {
      throw new IllegalArgumentException("Can't encode a Text value containing '\\u0000'");
    }

    byte[] bytes = value.getAsString().get().getBytes(StandardCharsets.UTF_8);
    for (byte b : bytes) {
      dst.put(mask(b, order));
    }
    dst.put(mask(TERM, order));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy