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

com.clickzetta.platform.common.AcidSchemaConvert Maven / Gradle / Ivy

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

import cz.proto.FieldRef;
import cz.proto.FieldSchema;
import cz.proto.MetadataEntity;

import java.util.*;

public class AcidSchemaConvert extends SchemaConvert {

  @Override
  public Schema convertToExternalSchema(MetadataEntity.Entity meta) {
    List fields = meta.getTable().getTableSchema().getFieldsList();
    List columns = new ArrayList<>();
    List keyColumns = new ArrayList<>();
    List sortColumns = new ArrayList<>();

    Map fieldIndexMap = new HashMap<>();
    for (FieldSchema field : fields) {
      fieldIndexMap.put(field.getType().getFieldId(), field);
    }

    Set keyIndexSet = new LinkedHashSet<>();
    for (FieldRef pkField : meta.getTable().getPrimaryKeySpec().getPrimaryKey().getFieldsList()) {
      keyIndexSet.add(pkField.getFieldId());
    }
    columns.add(buildVirtualColumnSchema(Constant.VIRTUAL_BUCKET_KEY));
    for (Integer keyIndex : keyIndexSet) {
      columns.add(buildColumnSchema(fieldIndexMap.get(keyIndex), true));
      keyColumns.add(buildColumnSchema(fieldIndexMap.get(keyIndex), true));
    }

    for (FieldSchema field : fields) {
      if (!keyIndexSet.contains(field.getType().getFieldId())) {
        columns.add(buildColumnSchema(field, false));
      }
    }
    return new Schema(columns, keyColumns, sortColumns);
  }

  @Override
  public Schema convertToExternalKeySchema(MetadataEntity.Entity meta) {
    List fields = meta.getTable().getTableSchema().getFieldsList();
    List columns = new ArrayList<>();
    List keyColumns = new ArrayList<>();
    List sortColumns = new ArrayList<>();

    Map fieldIndexMap = new HashMap<>();
    for (FieldSchema field : fields) {
      fieldIndexMap.put(field.getType().getFieldId(), field);
    }

    Set keyIndexSet = new LinkedHashSet<>();
    for (FieldRef pkField : meta.getTable().getPrimaryKeySpec().getPrimaryKey().getFieldsList()) {
      keyIndexSet.add(pkField.getFieldId());
    }
    columns.add(buildVirtualColumnSchema(Constant.VIRTUAL_BUCKET_KEY));
    for (Integer keyIndex : keyIndexSet) {
      columns.add(buildColumnSchema(fieldIndexMap.get(keyIndex), false));
      keyColumns.add(buildColumnSchema(fieldIndexMap.get(keyIndex), false));
    }
    return new Schema(columns, keyColumns, sortColumns);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy