You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@carbondata.apache.org by ravipesala <gi...@git.apache.org> on 2017/08/14 08:36:12 UTC

[GitHub] carbondata pull request #1240: [CARBONDATA-1365] add RLE codec implementatio...

Github user ravipesala commented on a diff in the pull request:

    https://github.com/apache/carbondata/pull/1240#discussion_r132898313
  
    --- Diff: core/src/main/java/org/apache/carbondata/core/datastore/page/encoding/RLECodec.java ---
    @@ -0,0 +1,417 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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 org.apache.carbondata.core.datastore.page.encoding;
    +
    +import java.io.ByteArrayInputStream;
    +import java.io.ByteArrayOutputStream;
    +import java.io.DataInputStream;
    +import java.io.DataOutputStream;
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.List;
    +
    +import org.apache.carbondata.core.datastore.page.ColumnPage;
    +import org.apache.carbondata.core.datastore.page.ComplexColumnPage;
    +import org.apache.carbondata.core.datastore.page.statistics.SimpleStatsResult;
    +import org.apache.carbondata.core.memory.MemoryException;
    +import org.apache.carbondata.core.metadata.CodecMetaFactory;
    +import org.apache.carbondata.core.metadata.datatype.DataType;
    +
    +/**
    + * RLE encoding implementation for integral column page.
    + * This encoding keeps track of repeated-run and non-repeated-run, and make use
    + * of the highest bit of the length field to indicate the type of run.
    + * The length field is encoded as 32bits value.
    + *
    + * For example: input data {5, 5, 1, 2, 3, 3, 3, 3, 3} will be encoded to
    + * {0x00, 0x00, 0x00, 0x02, 0x05,             (repeated-run, 2 values of 5)
    + *  0x80, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03, (non-repeated-run, 3 values: 1, 2, 3)
    + *  0x00, 0x00, 0x00, 0x04, 0x03}             (repeated-run, 4 values of 3)
    + */
    +public class RLECodec implements ColumnPageCodec {
    +
    +  enum RUN_STATE { INIT, START, REPEATED_RUN, NONREPEATED_RUN }
    +
    +  private DataType dataType;
    +  private int pageSize;
    +
    +  /**
    +   * New RLECodec
    +   * @param dataType data type of the raw column page before encode
    +   * @param pageSize page size of the raw column page before encode
    +   */
    +  RLECodec(DataType dataType, int pageSize) {
    +    this.dataType = dataType;
    +    this.pageSize = pageSize;
    +  }
    +
    +  @Override
    +  public String getName() {
    +    return "RLECodec";
    +  }
    +
    +  @Override
    +  public EncodedColumnPage encode(ColumnPage input) throws MemoryException, IOException {
    +    Encoder encoder = new Encoder();
    +    return encoder.encode(input);
    +  }
    +
    +  @Override
    +  public EncodedColumnPage[] encodeComplexColumn(ComplexColumnPage input) {
    +    throw new UnsupportedOperationException("complex column does not support RLE encoding");
    +  }
    +
    +  @Override
    +  public ColumnPage decode(byte[] input, int offset, int length) throws MemoryException,
    +      IOException {
    +    Decoder decoder = new Decoder(dataType, pageSize);
    +    return decoder.decode(input, offset, length);
    +  }
    +
    +  // This codec supports integral type only
    +  private void validateDataType(DataType dataType) {
    +    switch (dataType) {
    +      case BYTE:
    +      case SHORT:
    +      case INT:
    +      case LONG:
    +        break;
    +      default:
    +        throw new UnsupportedOperationException(dataType + " is not supported for RLE");
    +    }
    +  }
    +
    +  private class Encoder {
    +    // While encoding RLE, this class internally work as a state machine
    +    // INIT state is the initial state before any value comes
    +    // START state is the start for each run
    +    // REPEATED_RUN state means it is collecting repeated values (`lastValue`)
    +    // NONREPEATED_RUN state means it is collecting non-repeated values (`nonRepeatValues`)
    +    private RUN_STATE runState;
    +
    +    // count for each run, either REPEATED_RUN or NONREPEATED_RUN
    +    private int valueCount;
    +
    +    // collected value for REPEATED_RUN
    +    private Object lastValue;
    +
    +    // collected value for NONREPEATED_RUN
    +    private List<Object> nonRepeatValues;
    +
    +    // data type of input page
    +    private DataType dataType;
    +
    +    // output stream for encoded data
    +    private ByteArrayOutputStream bao;
    +    private DataOutputStream stream;
    +
    +    private Encoder() {
    +      this.runState = RUN_STATE.INIT;
    +      this.valueCount = 0;
    +      this.nonRepeatValues = new ArrayList<>();
    +      this.bao = new ByteArrayOutputStream();
    +      this.stream = new DataOutputStream(bao);
    +    }
    +
    +    private EncodedColumnPage encode(ColumnPage input) throws MemoryException, IOException {
    +      validateDataType(input.getDataType());
    +      this.dataType = input.getDataType();
    +      switch (dataType) {
    +        case BYTE:
    +          byte[] bytePage = input.getBytePage();
    +          for (int i = 0; i < bytePage.length; i++) {
    +            putValue(bytePage[i]);
    +          }
    +          break;
    +        case SHORT:
    +          short[] shortPage = input.getShortPage();
    +          for (int i = 0; i < shortPage.length; i++) {
    +            putValue(shortPage[i]);
    +          }
    +          break;
    +        case INT:
    +          int[] intPage = input.getIntPage();
    +          for (int i = 0; i < intPage.length; i++) {
    +            putValue(intPage[i]);
    +          }
    +          break;
    +        case LONG:
    +          long[] longPage = input.getLongPage();
    +          for (int i = 0; i < longPage.length; i++) {
    +            putValue(longPage[i]);
    +          }
    +          break;
    +        default:
    +          throw new UnsupportedOperationException(input.getDataType() +
    +              " does not support RLE encoding");
    +      }
    +      byte[] encoded = collectResult();
    +      SimpleStatsResult stats = (SimpleStatsResult) input.getStatistics();
    +      return new EncodedMeasurePage(
    +          input.getPageSize(),
    +          encoded,
    +          CodecMetaFactory.createMeta(stats, input.getDataType()),
    +          stats.getNullBits());
    +    }
    +
    +    private void putValue(Object value) throws IOException {
    +      if (runState == RUN_STATE.INIT) {
    +        startNewRun(value);
    +      } else {
    +        if (lastValue.equals(value)) {
    +          putRepeatValue(value);
    +        } else {
    +          putNonRepeatValue(value);
    +        }
    +      }
    +    }
    +
    +    // when last row is reached, write out all collected data
    +    private byte[] collectResult() throws IOException {
    +      switch (runState) {
    +        case REPEATED_RUN:
    +          writeRunLength(valueCount);
    +          writeRunValue(lastValue);
    +          break;
    +        case NONREPEATED_RUN:
    +          writeRunLength(valueCount | 0x80000000);
    --- End diff --
    
    I think writing short is enough as of now. Because we are restricting page size in short only


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---