Package org.kiji.schema.layout.impl

Examples of org.kiji.schema.layout.impl.ColumnId


public class TestColumnId {
  @Test
  public void testNegativeId() {
    try {
      new ColumnId(-1).toByteArray();
      fail("Should fail with IllegalArgumentException.");
    } catch (IllegalArgumentException ise) {
      assertEquals("id may not be negative", ise.getMessage());
    }
  }
View Full Code Here


    }
  }

  @Test
  public void testToByteArray() {
    assertArrayEquals(Bytes.toBytes("A"), new ColumnId(0).toByteArray());
    assertArrayEquals(Bytes.toBytes("B"), new ColumnId(1).toByteArray());
    assertArrayEquals(Bytes.toBytes("C"), new ColumnId(2).toByteArray());
    assertArrayEquals(Bytes.toBytes("/"), new ColumnId(63).toByteArray());
    assertArrayEquals(Bytes.toBytes("AB"), new ColumnId(64).toByteArray());
    assertArrayEquals(Bytes.toBytes("BB"), new ColumnId(65).toByteArray());
    assertArrayEquals(Bytes.toBytes("CB"), new ColumnId(66).toByteArray());
    assertArrayEquals(Bytes.toBytes("oP"), new ColumnId(1000).toByteArray());
    assertArrayEquals(Bytes.toBytes("AAB"), new ColumnId(64 * 64).toByteArray());
  }
View Full Code Here

        getHBaseAdmin().disableTable(hbaseTableName.toString());
      }

      for (HColumnDescriptor newColumnDescriptor : newTableDescriptor.getFamilies()) {
        final String columnName = Bytes.toString(newColumnDescriptor.getName());
        final ColumnId columnId = ColumnId.fromString(columnName);
        final String lgName = newLayout.getLocalityGroupIdNameMap().get(columnId);
        final HColumnDescriptor currentColumnDescriptor =
            currentTableDescriptor.getFamily(newColumnDescriptor.getName());
        if (null == currentColumnDescriptor) {
          if (dryRun) {
View Full Code Here

    } catch (NoSuchColumnException e) {
      throw new IllegalArgumentException(
          String.format("No such column '%s' in table %s.", column, tableURI));
    }

    final ColumnId localityGroupId =
        layout.getFamilyMap().get(column.getFamily()).getLocalityGroup().getId();
    final CassandraTableName table =
        CassandraTableName.getLocalityGroupTableName(tableURI, localityGroupId);

    if (column.isFullyQualified()) {
View Full Code Here

      if (isRename && (refLGLayout == null)) {
        throw new InvalidLayoutException(String.format(
            "Invalid rename: cannot find reference locality group '%s'.", refLGName));
      }

      final ColumnId refLGId = refLGIdMap.remove(refLGName);

      if (lgDesc.getDelete()) {
        // This locality group is deleted:
        if (refLGId == null) {
          throw new InvalidLayoutException(String.format(
              "Attempting to delete locality group '%s' unknown in reference layout.",
              refLGName));
        }
        itLGDesc.remove();
        continue;
      }

      // BloomType, block_size were introduced in version 1.2.
      if (Versions.BLOCK_SIZE_LAYOUT_VERSION.compareTo(mLayoutVersion) > 0) {
        if (lgDesc.getBlockSize() != null) {
          // Cannot use max_filesize if this is the case.
          throw new InvalidLayoutException(
            "Support for specifying block_size begins with layout version "
              + Versions.BLOCK_SIZE_LAYOUT_VERSION);
        }
        if (lgDesc.getBloomType() != null) {
          // Cannot use bloom_type if this is the case.
          throw new InvalidLayoutException(
            "Support for specifying bloom_type begins with layout version "
              + Versions.BLOCK_SIZE_LAYOUT_VERSION);
        }
      } else {
        if (lgDesc.getBlockSize() != null && lgDesc.getBlockSize() <= 0) {
          throw new InvalidLayoutException("block_size must be greater than 0");
        }
      }

      final LocalityGroupLayout lgLayout = new LocalityGroupLayout(lgDesc, refLGLayout);
      localityGroups.add(lgLayout);
      for (String lgName : lgLayout.getNames()) {
        Preconditions.checkState(lgMap.put(lgName, lgLayout) == null,
            "Duplicate locality group name: " + lgName);
      }

      if (lgLayout.getId() != null) {
        final String previous = idMap.put(lgLayout.getId(), lgLayout.getName());
        Preconditions.checkState(previous == null,
            String.format("Duplicate locality group ID '%s' associated to '%s' and '%s'.",
                lgLayout.getId(), lgLayout.getName(), previous));
      } else {
        unassigned.add(lgLayout);
      }

      families.addAll(lgLayout.getFamilies());
      for (FamilyLayout familyLayout : lgLayout.getFamilies()) {
        for (String familyName : familyLayout.getNames()) {
          if (null != familyMap.put(familyName, familyLayout)) {
              throw new InvalidLayoutException(String.format(
                  "Layout for table '%s' contains duplicate family name '%s'.",
                  getName(), familyName));
          }
        }

        if (familyLayout.isMapType()) {
          Preconditions.checkState(
              columnNames.add(KijiColumnName.create(familyLayout.getName(), null)));
        }

        for (ColumnLayout columnLayout: familyLayout.getColumns()) {
          for (String columnName : columnLayout.getNames()) {
            final KijiColumnName column = KijiColumnName.create(familyLayout.getName(), columnName);
            if (null != columnMap.put(column, columnLayout)) {
              throw new InvalidLayoutException(String.format(
                  "Layout for table '%s' contains duplicate column '%s'.",
                  getName(), column));
            }
          }
          Preconditions.checkState(columnNames.add(
              KijiColumnName.create(familyLayout.getName(), columnLayout.getName())));
        }
      }
    }

    if (!refLGIdMap.isEmpty()) {
      throw new InvalidLayoutException(String.format(
          "Missing descriptor(s) for locality group(s): %s.",
          Joiner.on(",").join(refLGIdMap.keySet())));
    }

    mLocalityGroups = ImmutableList.copyOf(localityGroups);
    mLocalityGroupMap = ImmutableMap.copyOf(lgMap);

    mFamilies = ImmutableList.copyOf(families);
    mFamilyMap = ImmutableMap.copyOf(familyMap);

    mColumnNames = ImmutableSet.copyOf(columnNames);

    // Assign IDs to locality groups:
    int nextColumnId = 1;
    for (LocalityGroupLayout localityGroup : unassigned) {
      Preconditions.checkState(localityGroup.getId() == null);
      while (true) {
        final ColumnId columnId = new ColumnId(nextColumnId);
        nextColumnId += 1;
        if (!idMap.containsKey(columnId)) {
          localityGroup.setId(columnId);
          idMap.put(columnId, localityGroup.getName());
          break;
View Full Code Here

              "Invalid locality group alias: '%s'.", name));
        }
      }

      if (lgDesc.getId() > 0) {
        mId = new ColumnId(lgDesc.getId());
      }

      if (mDesc.getTtlSeconds() <= 0) {
        throw new InvalidLayoutException(String.format(
            "Invalid TTL seconds for locality group '%s': TTL must be positive, got %d.",
            getName(), mDesc.getTtlSeconds()));
      }
      if (mDesc.getMaxVersions() <= 0) {
        throw new InvalidLayoutException(String.format(
            "Invalid max versions for locality group '%s': max versions must be positive, got %d.",
            getName(), mDesc.getMaxVersions()));
      }

      if (reference != null) {
        if ((mId != null) && !mId.equals(reference.getId())) {
          throw new InvalidLayoutException(String.format(
              "Descriptor for locality group '%s' has ID %s but reference ID is %s.",
              getName(), mId, reference.getId()));
        }
        mId = reference.getId();
        lgDesc.setId(mId.getId());
      }

      // Build families:

      /**
       * Map of the family IDs from the reference layout.
       * Entries are removed as they are linked to the families in the descriptor.
       * Eventually, this map must become empty.
       */
      final BiMap<String, ColumnId> refFIdMap = (reference != null)
          ? HashBiMap.create(reference.getFamilyIdNameMap().inverse())
          : HashBiMap.<String, ColumnId>create();

      final List<FamilyLayout> families = Lists.newArrayList();
      final Map<String, FamilyLayout> familyMap = Maps.newHashMap();

      /** Map of families in the new layout. */
      final BiMap<ColumnId, String> idMap = HashBiMap.create();

      /** Families with no ID assigned yet. */
      final List<FamilyLayout> unassigned = Lists.newArrayList();

      final Iterator<FamilyDesc> itFamilyDesc = lgDesc.getFamilies().iterator();
      while (itFamilyDesc.hasNext()) {
        final FamilyDesc familyDesc = itFamilyDesc.next();
        final boolean isRename = (familyDesc.getRenamedFrom() != null);
        final String refFName = isRename ? familyDesc.getRenamedFrom() : familyDesc.getName();
        familyDesc.setRenamedFrom(null);
        if (isRename && (reference == null)) {
          throw new InvalidLayoutException(String.format(
              "Invalid rename: no reference locality group '%s' for family '%s'.",
              getName(), refFName));
        }
        final FamilyLayout refFLayout =
            (reference != null) ? reference.getFamilyMap().get(refFName) : null;
        if (isRename && (refFLayout == null)) {
          throw new InvalidLayoutException(String.format(
              "Invalid rename: cannot find reference family '%s' in locality group '%s'.",
              refFName, getName()));
        }

        final ColumnId refFId = refFIdMap.remove(refFName);

        if (familyDesc.getDelete()) {
          if (refFId == null) {
            throw new InvalidLayoutException(String.format(
                "Deleted family '%s' unknown in reference locality group '%s'.",
                refFName, getName()));
          }
          itFamilyDesc.remove();
          continue;
        }

        final FamilyLayout fLayout = new FamilyLayout(familyDesc, refFLayout);
        families.add(fLayout);
        for (String familyName : fLayout.getNames()) {
          Preconditions.checkState(familyMap.put(familyName, fLayout) == null,
              "Duplicate family name: " + familyName);
        }
        if (fLayout.getId() != null) {
          final String previous = idMap.put(fLayout.getId(), fLayout.getName());
          Preconditions.checkState(previous == null,
              String.format("Duplicate family ID '%s' associated to '%s' and '%s'.",
                  fLayout.getId(), fLayout.getName(), previous));
        } else {
          unassigned.add(fLayout);
        }
      }

      if (!refFIdMap.isEmpty()) {
        throw new InvalidLayoutException(String.format(
            "Descriptor for locality group '%s' is missing families: %s",
            lgDesc.getName(), Joiner.on(",").join(refFIdMap.keySet())));
      }

      mFamilies = ImmutableList.copyOf(families);
      mFamilyMap = ImmutableMap.copyOf(familyMap);

      // Assign IDs to families:
      int nextFamilyId = 1;
      for (FamilyLayout fLayout : unassigned) {
        Preconditions.checkState(fLayout.getId() == null);
        while (true) {
          final ColumnId fId = new ColumnId(nextFamilyId);
          nextFamilyId += 1;
          if (!idMap.containsKey(fId)) {
            fLayout.setId(fId);
            idMap.put(fId, fLayout.getName());
            break;
View Full Code Here

                "Invalid family alias: '%s'.", name));
          }
        }

        if (familyDesc.getId() > 0) {
          mId = new ColumnId(familyDesc.getId());
        }

        if (reference != null) {
          if ((mId != null) && !mId.equals(reference.getId())) {
            throw new InvalidLayoutException(String.format(
                "Descriptor for family '%s' has ID %s but reference ID is %s.",
                getName(), mId, reference.getId()));
          }
          mId = reference.getId();
          familyDesc.setId(mId.getId());

          // Cannot change family type (group-type vs map-type):
          if (reference.isMapType() != this.isMapType()) {
            throw new InvalidLayoutException(String.format(
                "Invalid layout update for family '%s' from reference type %s to type %s.",
                getName(),
                reference.isMapType() ? "map" : "group",
                this.isMapType() ? "map" : "group"));
          }
        }

        if (this.isMapType()) {
          // Force validation of schema:
          final CellSchema referenceSchema =
              (null != reference) ? reference.getDesc().getMapSchema() : null;
          validateCellSchema(mLayoutVersion, mDesc.getMapSchema(), referenceSchema);
        }

        // Build columns:

        /**
         * Map of columns from the reference layout.
         * Entries are removed as they are processed and linked to column descriptors.
         * At the end of the process, this map must be empty.
         */
        final BiMap<String, ColumnId> refCIdMap = (reference != null)
            ? HashBiMap.create(reference.getColumnIdNameMap().inverse())
            : HashBiMap.<String, ColumnId>create();

        final List<ColumnLayout> columns = Lists.newArrayList();
        final Map<String, ColumnLayout> columnMap = Maps.newHashMap();

        /** Map of columns in the new layout. */
        final BiMap<ColumnId, String> idMap = HashBiMap.create();

        /** Columns with no ID assigned yet. */
        final List<ColumnLayout> unassigned = Lists.newArrayList();

        final Iterator<ColumnDesc> itColumnDesc = familyDesc.getColumns().iterator();
        while (itColumnDesc.hasNext()) {
          final ColumnDesc columnDesc = itColumnDesc.next();
          final boolean isRename = (columnDesc.getRenamedFrom() != null);
          final String refCName = isRename ? columnDesc.getRenamedFrom() : columnDesc.getName();
          columnDesc.setRenamedFrom(null);
          if (isRename && (reference == null)) {
            throw new InvalidLayoutException(String.format(
                "Invalid renaming: cannot find reference family for column '%s:%s'.",
                getName(), refCName));
          }
          final ColumnLayout refCLayout =
              (reference != null) ? reference.getColumnMap().get(refCName) : null;
          if (isRename && (refCLayout == null)) {
            throw new InvalidLayoutException(String.format(
                "Invalid renaming: cannot find column '%s:%s' in reference family.",
                getName(), refCName));
          }

          final ColumnId refCId = refCIdMap.remove(refCName);

          if (columnDesc.getDelete()) {
            if (refCId == null) {
              throw new InvalidLayoutException(String.format(
                  "Deleted column '%s:%s' does not exist in reference layout.",
                  mDesc.getName(), refCName));
            }
            itColumnDesc.remove();
            continue;
          }

          final ColumnLayout cLayout = new ColumnLayout(columnDesc, refCLayout);
          columns.add(cLayout);
          for (String columnName : cLayout.getNames()) {
            if (null != columnMap.put(columnName, cLayout)) {
                throw new InvalidLayoutException(String.format(
                    "Family '%s' contains duplicate column qualifier '%s'.",
                    getName(), columnName));
            }
          }
          if (cLayout.getId() != null) {
            final String previous = idMap.put(cLayout.getId(), cLayout.getName());
            Preconditions.checkState(previous == null,
                String.format("Duplicate column ID '%s' associated to '%s' and '%s'.",
                    cLayout.getId(), cLayout.getName(), previous));
          } else {
            unassigned.add(cLayout);
          }
        }

        if (!refCIdMap.isEmpty()) {
          throw new InvalidLayoutException(String.format(
              "Descriptor for family '%s' is missing columns: %s.",
              getName(), Joiner.on(",").join(refCIdMap.keySet())));
        }

        mColumns = ImmutableList.copyOf(columns);
        mColumnMap = ImmutableMap.copyOf(columnMap);

        // Assign IDs to columns, build ID maps
        int nextColumnId = 1;
        for (ColumnLayout column : unassigned) {
          Preconditions.checkState(column.getId() == null);
          while (true) {
            final ColumnId columnId = new ColumnId(nextColumnId);
            nextColumnId += 1;
            if (!idMap.containsKey(columnId)) {
              column.setId(columnId);
              idMap.put(columnId, column.getName());
              break;
View Full Code Here

      throw new NoSuchColumnException(
          String.format("No locality group for Cassandra table %s in Kiji table %s.",
              localityGroupTable, mLayout.getName()));
    }

    final ColumnId familyID = ColumnId.fromByteArray(cassandraColumnName.getFamily());
    final FamilyLayout family =
        localityGroup.getFamilyMap().get(localityGroup.getFamilyIdNameMap().get(familyID));
    if (family == null) {
      throw new NoSuchColumnException(String.format(
          "No family with ID %s in locality group %s of table %s.",
          familyID.getId(), localityGroup.getName(), mLayout.getName()));
    }

    final KijiColumnName kijiColumnName;
    if (family.isGroupType()) {
      // Group type family.
      final ColumnId qualifierID = ColumnId.fromByteArray(cassandraColumnName.getQualifier());
      final ColumnLayout qualifier =
          family.getColumnMap().get(family.getColumnIdNameMap().get(qualifierID));
      if (qualifier == null) {
        throw new NoSuchColumnException(String.format(
            "No column with ID %s in family %s of table %s.",
            qualifierID.getId(), family.getName(), mLayout.getName()));
      }
      kijiColumnName = KijiColumnName.create(family.getName(), qualifier.getName());
    } else {
      // Map type family.
      assert(family.isMapType());
View Full Code Here

    final FamilyLayout family = mLayout.getFamilyMap().get(familyName);
    if (family == null) {
      throw new NoSuchColumnException(kijiColumnName.toString());
    }

    final ColumnId familyID = family.getId();

    final byte[] familyBytes = familyID.toByteArray();

    if (qualifierName == null) {
      // Unqualified column
      return new CassandraColumnName(familyBytes, null);
    } else if (family.isGroupType()) {
      // Group type family.
      final ColumnId qualifierID = family.getColumnIdNameMap().inverse().get(qualifierName);
      final byte[] qualifierBytes = qualifierID.toByteArray();
      return new CassandraColumnName(familyBytes, qualifierBytes);
    } else {
      // Map type family.
      assert family.isMapType();
      final byte[] qualifierBytes = Bytes.toBytes(qualifierName);
View Full Code Here

  public void deleteRow(final EntityId entityId) throws IOException {
    synchronized (mMonitor) {
      final KijiTableLayout layout = mCapsule.getLayout();
      final CassandraKijiTable tableURI = mTable;
      for (LocalityGroupLayout localityGroup : layout.getLocalityGroups()) {
        final ColumnId localityGroupId = localityGroup.getId();
        final CassandraTableName table =
            CassandraTableName.getLocalityGroupTableName(tableURI.getURI(), localityGroupId);

        final Statement delete =
            CQLUtils.getLocalityGroupDeleteStatement(layout, table, entityId);
View Full Code Here

TOP

Related Classes of org.kiji.schema.layout.impl.ColumnId

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.