Java Code Examples for org.apache.hadoop.hbase.Tag

The following examples show how to use org.apache.hadoop.hbase.Tag. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: hbase   Source File: VisibilityController.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether cell contains any tag with type as VISIBILITY_TAG_TYPE. This
 * tag type is reserved and should not be explicitly set by user. There are
 * two versions of this method one that accepts pair and other without pair.
 * In case of preAppend and preIncrement the additional operations are not
 * needed like checking for STRING_VIS_TAG_TYPE and hence the API without pair
 * could be used.
 *
 * @param cell
 * @throws IOException
 */
private boolean checkForReservedVisibilityTagPresence(Cell cell) throws IOException {
  // Bypass this check when the operation is done by a system/super user.
  // This is done because, while Replication, the Cells coming to the peer
  // cluster with reserved
  // typed tags and this is fine and should get added to the peer cluster
  // table
  if (isSystemOrSuperUser()) {
    return true;
  }
  Iterator<Tag> tagsItr = PrivateCellUtil.tagsIterator(cell);
  while (tagsItr.hasNext()) {
    if (RESERVED_VIS_TAG_TYPES.contains(tagsItr.next().getType())) {
      return false;
    }
  }
  return true;
}
 
Example 2
Source Project: hbase   Source File: DefaultVisibilityLabelServiceImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean matchVisibility(List<Tag> putVisTags, Byte putTagsFormat, List<Tag> deleteVisTags,
    Byte deleteTagsFormat) throws IOException {
  // Early out if there are no tags in both of cell and delete
  if (putVisTags.isEmpty() && deleteVisTags.isEmpty()) {
    return true;
  }
  // Early out if one of the tags is empty
  if (putVisTags.isEmpty() ^ deleteVisTags.isEmpty()) {
    return false;
  }
  if ((deleteTagsFormat != null && deleteTagsFormat == SORTED_ORDINAL_SERIALIZATION_FORMAT)
      && (putTagsFormat == null || putTagsFormat == SORTED_ORDINAL_SERIALIZATION_FORMAT)) {
    if (putTagsFormat == null) {
      return matchUnSortedVisibilityTags(putVisTags, deleteVisTags);
    } else {
      return matchOrdinalSortedVisibilityTags(putVisTags, deleteVisTags);
    }
  }
  throw new IOException("Unexpected tag format passed for comparison, deleteTagsFormat : "
      + deleteTagsFormat + ", putTagsFormat : " + putTagsFormat);
}
 
Example 3
Source Project: hbase   Source File: DefaultVisibilityLabelServiceImpl.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param putVisTags Visibility tags in Put Mutation
 * @param deleteVisTags Visibility tags in Delete Mutation
 * @return true when all the visibility tags in Put matches with visibility tags in Delete.
 * This is used when both the set of tags are sorted based on the label ordinal.
 */
private static boolean matchOrdinalSortedVisibilityTags(List<Tag> putVisTags,
    List<Tag> deleteVisTags) {
  boolean matchFound = false;
  // If the size does not match. Definitely we are not comparing the equal tags.
  if ((deleteVisTags.size()) == putVisTags.size()) {
    for (Tag tag : deleteVisTags) {
      matchFound = false;
      for (Tag givenTag : putVisTags) {
        if (Tag.matchingValue(tag, givenTag)) {
          matchFound = true;
          break;
        }
      }
      if (!matchFound) break;
    }
  }
  return matchFound;
}
 
Example 4
Source Project: hbase   Source File: AccessController.java    License: Apache License 2.0 6 votes vote down vote up
private static void addCellPermissions(final byte[] perms, Map<byte[], List<Cell>> familyMap) {
  // Iterate over the entries in the familyMap, replacing the cells therein
  // with new cells including the ACL data
  for (Map.Entry<byte[], List<Cell>> e: familyMap.entrySet()) {
    List<Cell> newCells = Lists.newArrayList();
    for (Cell cell: e.getValue()) {
      // Prepend the supplied perms in a new ACL tag to an update list of tags for the cell
      List<Tag> tags = new ArrayList<>();
      tags.add(new ArrayBackedTag(PermissionStorage.ACL_TAG_TYPE, perms));
      Iterator<Tag> tagIterator = PrivateCellUtil.tagsIterator(cell);
      while (tagIterator.hasNext()) {
        tags.add(tagIterator.next());
      }
      newCells.add(PrivateCellUtil.createCell(cell, tags));
    }
    // This is supposed to be safe, won't CME
    e.setValue(newCells);
  }
}
 
Example 5
Source Project: hbase   Source File: AccessController.java    License: Apache License 2.0 6 votes vote down vote up
private void checkForReservedTagPresence(User user, Mutation m) throws IOException {
  // No need to check if we're not going to throw
  if (!authorizationEnabled) {
    m.setAttribute(TAG_CHECK_PASSED, TRUE);
    return;
  }
  // Superusers are allowed to store cells unconditionally.
  if (Superusers.isSuperUser(user)) {
    m.setAttribute(TAG_CHECK_PASSED, TRUE);
    return;
  }
  // We already checked (prePut vs preBatchMutation)
  if (m.getAttribute(TAG_CHECK_PASSED) != null) {
    return;
  }
  for (CellScanner cellScanner = m.cellScanner(); cellScanner.advance();) {
    Iterator<Tag> tagsItr = PrivateCellUtil.tagsIterator(cellScanner.current());
    while (tagsItr.hasNext()) {
      if (tagsItr.next().getType() == PermissionStorage.ACL_TAG_TYPE) {
        throw new AccessDeniedException("Mutation contains cell with reserved type tag");
      }
    }
  }
  m.setAttribute(TAG_CHECK_PASSED, TRUE);
}
 
Example 6
Source Project: hbase   Source File: MobUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Get the table name from when this cell was written into a mob hfile as a TableName.
 * @param cell to extract tag from
 * @return name of table as a TableName. empty if the tag is not found.
 */
public static Optional<TableName> getTableName(Cell cell) {
  Optional<Tag> maybe = getTableNameTag(cell);
  Optional<TableName> name = Optional.empty();
  if (maybe.isPresent()) {
    final Tag tag = maybe.get();
    if (tag.hasArray()) {
      name = Optional.of(TableName.valueOf(tag.getValueArray(), tag.getValueOffset(),
          tag.getValueLength()));
    } else {
      // TODO ByteBuffer handling in tags looks busted. revisit.
      ByteBuffer buffer = tag.getValueByteBuffer().duplicate();
      buffer.mark();
      buffer.position(tag.getValueOffset());
      buffer.limit(tag.getValueOffset() + tag.getValueLength());
      name = Optional.of(TableName.valueOf(buffer));
    }
  }
  return name;
}
 
Example 7
Source Project: hbase   Source File: WALPrettyPrinter.java    License: Apache License 2.0 6 votes vote down vote up
public static Map<String, Object> toStringMap(Cell cell) {
  Map<String, Object> stringMap = new HashMap<>();
  stringMap.put("row",
      Bytes.toStringBinary(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
  stringMap.put("type", cell.getType());
  stringMap.put("family", Bytes.toStringBinary(cell.getFamilyArray(), cell.getFamilyOffset(),
              cell.getFamilyLength()));
  stringMap.put("qualifier",
      Bytes.toStringBinary(cell.getQualifierArray(), cell.getQualifierOffset(),
          cell.getQualifierLength()));
  stringMap.put("timestamp", cell.getTimestamp());
  stringMap.put("vlen", cell.getValueLength());
  if (cell.getTagsLength() > 0) {
    List<String> tagsString = new ArrayList<>();
    Iterator<Tag> tagsIterator = PrivateCellUtil.tagsIterator(cell);
    while (tagsIterator.hasNext()) {
      Tag tag = tagsIterator.next();
      tagsString
          .add((tag.getType()) + ":" + Bytes.toStringBinary(Tag.cloneValue(tag)));
    }
    stringMap.put("tag", tagsString);
  }
  return stringMap;
}
 
Example 8
Source Project: hbase   Source File: HMobStore.java    License: Apache License 2.0 6 votes vote down vote up
public HMobStore(final HRegion region, final ColumnFamilyDescriptor family,
    final Configuration confParam, boolean warmup) throws IOException {
  super(region, family, confParam, warmup);
  this.family = family;
  this.mobFileCache = region.getMobFileCache();
  this.homePath = MobUtils.getMobHome(conf);
  this.mobFamilyPath = MobUtils.getMobFamilyPath(conf, this.getTableName(),
      family.getNameAsString());
  List<Path> locations = new ArrayList<>(2);
  locations.add(mobFamilyPath);
  TableName tn = region.getTableDescriptor().getTableName();
  locations.add(HFileArchiveUtil.getStoreArchivePath(conf, tn, MobUtils.getMobRegionInfo(tn)
      .getEncodedName(), family.getNameAsString()));
  map.put(tn, locations);
  List<Tag> tags = new ArrayList<>(2);
  tags.add(MobConstants.MOB_REF_TAG);
  Tag tableNameTag = new ArrayBackedTag(TagType.MOB_TABLE_NAME_TAG_TYPE,
      getTableName().getName());
  tags.add(tableNameTag);
  this.refCellTags = TagUtil.fromList(tags);
}
 
Example 9
@Override
public List<Tag> createVisibilityExpTags(String visExpression, boolean withSerializationFormat,
    boolean checkAuths) throws IOException {
  ExpressionNode node = null;
  try {
    node = this.expressionParser.parse(visExpression);
  } catch (ParseException e) {
    throw new IOException(e);
  }
  node = this.expressionExpander.expand(node);
  List<Tag> tags = new ArrayList<>();
  if (withSerializationFormat) {
    tags.add(STRING_SERIALIZATION_FORMAT_TAG);
  }
  if (node instanceof NonLeafExpressionNode
      && ((NonLeafExpressionNode) node).getOperator() == Operator.OR) {
    for (ExpressionNode child : ((NonLeafExpressionNode) node).getChildExps()) {
      tags.add(createTag(child));
    }
  } else {
    tags.add(createTag(node));
  }
  return tags;
}
 
Example 10
private static boolean checkForMatchingVisibilityTagsWithSortedOrder(List<Tag> putVisTags,
    List<Tag> deleteVisTags) {
  // Early out if there are no tags in both of cell and delete
  if (putVisTags.isEmpty() && deleteVisTags.isEmpty()) {
    return true;
  }
  boolean matchFound = false;
  // If the size does not match. Definitely we are not comparing the equal
  // tags.
  if ((deleteVisTags.size()) == putVisTags.size()) {
    for (Tag tag : deleteVisTags) {
      matchFound = false;
      for (Tag givenTag : putVisTags) {
        if (Tag.matchingValue(tag, givenTag)) {
          matchFound = true;
          break;
        }
      }
      if (!matchFound)
        break;
    }
  }
  return matchFound;
}
 
Example 11
Source Project: hbase   Source File: TestVisibilityLabelsReplication.java    License: Apache License 2.0 6 votes vote down vote up
protected static void doAssert(byte[] row, String visTag) throws Exception {
  if (VisibilityReplicationEndPointForTest.lastEntries == null) {
    return; // first call
  }
  Assert.assertEquals(1, VisibilityReplicationEndPointForTest.lastEntries.size());
  List<Cell> cells = VisibilityReplicationEndPointForTest.lastEntries.get(0).getEdit().getCells();
  Assert.assertEquals(4, cells.size());
  boolean tagFound = false;
  for (Cell cell : cells) {
    if ((Bytes.equals(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength(), row, 0,
        row.length))) {
      List<Tag> tags = PrivateCellUtil.getTags(cell);
      for (Tag tag : tags) {
        if (tag.getType() == TagType.STRING_VIS_TAG_TYPE) {
          assertEquals(visTag, Tag.getValueAsString(tag));
          tagFound = true;
          break;
        }
      }
    }
  }
  assertTrue(tagFound);
}
 
Example 12
Source Project: hbase   Source File: TestVisibilityLabelsReplication.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void prePut(ObserverContext<RegionCoprocessorEnvironment> e, Put m, WALEdit edit,
    Durability durability) throws IOException {
  byte[] attribute = m.getAttribute(NON_VISIBILITY);
  byte[] cf = null;
  List<Cell> updatedCells = new ArrayList<>();
  if (attribute != null) {
    for (List<? extends Cell> edits : m.getFamilyCellMap().values()) {
      for (Cell cell : edits) {
        KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
        if (cf == null) {
          cf = CellUtil.cloneFamily(kv);
        }
        Tag tag = new ArrayBackedTag((byte) NON_VIS_TAG_TYPE, attribute);
        List<Tag> tagList = new ArrayList<>(PrivateCellUtil.getTags(cell).size() + 1);
        tagList.add(tag);
        tagList.addAll(PrivateCellUtil.getTags(cell));
        Cell newcell = PrivateCellUtil.createCell(kv, tagList);
        ((List<Cell>) updatedCells).add(newcell);
      }
    }
    m.getFamilyCellMap().remove(cf);
    // Update the family map
    m.getFamilyCellMap().put(cf, updatedCells);
  }
}
 
Example 13
Source Project: hbase   Source File: HFileTestUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * This verifies that each cell has a tag that is equal to its rowkey name.  For this to work
 * the hbase instance must have HConstants.RPC_CODEC_CONF_KEY set to
 * KeyValueCodecWithTags.class.getCanonicalName());
 * @param table table containing tagged cells
 * @throws IOException if problems reading table
 */
public static void verifyTags(Table table) throws IOException {
  ResultScanner s = table.getScanner(new Scan());
  for (Result r : s) {
    for (Cell c : r.listCells()) {
      Optional<Tag> tag = PrivateCellUtil.getTag(c, TagType.MOB_TABLE_NAME_TAG_TYPE);
      if (!tag.isPresent()) {
        fail(c.toString() + " has null tag");
        continue;
      }
      Tag t = tag.get();
      byte[] tval = Tag.cloneValue(t);
      assertArrayEquals(c.toString() + " has tag" + Bytes.toString(tval),
          r.getRow(), tval);
    }
  }
}
 
Example 14
private void writeStoreFile(final StoreFileWriter writer) throws IOException {
  byte[] fam = Bytes.toBytes("f");
  byte[] qualifier = Bytes.toBytes("q");
  long now = System.currentTimeMillis();
  byte[] b = Bytes.toBytes("k1");
  Tag t1 = new ArrayBackedTag((byte) 1, "tag1");
  Tag t2 = new ArrayBackedTag((byte) 2, "tag2");
  Tag t3 = new ArrayBackedTag((byte) 3, "tag3");
  try {
    writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t1 }));
    b = Bytes.toBytes("k3");
    writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t2, t1 }));
    b = Bytes.toBytes("k4");
    writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t3 }));
    b = Bytes.toBytes("k5");
    writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t3 }));
  } finally {
    writer.close();
  }
}
 
Example 15
Source Project: hbase   Source File: TestDataBlockEncoders.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Test data block encoding of empty KeyValue.
 *
 * @throws IOException
 *           On test failure.
 */
@Test
public void testEmptyKeyValues() throws IOException {
  List<KeyValue> kvList = new ArrayList<>();
  byte[] row = new byte[0];
  byte[] family = new byte[0];
  byte[] qualifier = new byte[0];
  byte[] value = new byte[0];
  if (!includesTags) {
    kvList.add(new KeyValue(row, family, qualifier, 0L, value));
    kvList.add(new KeyValue(row, family, qualifier, 0L, value));
  } else {
    byte[] metaValue1 = Bytes.toBytes("metaValue1");
    byte[] metaValue2 = Bytes.toBytes("metaValue2");
    kvList.add(new KeyValue(row, family, qualifier, 0L, value,
        new Tag[] { new ArrayBackedTag((byte) 1, metaValue1) }));
    kvList.add(new KeyValue(row, family, qualifier, 0L, value,
        new Tag[] { new ArrayBackedTag((byte) 1, metaValue2) }));
  }
  testEncodersOnDataset(kvList, includesMemstoreTS, includesTags);
}
 
Example 16
Source Project: hbase   Source File: TestDataBlockEncoders.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Test KeyValues with negative timestamp.
 *
 * @throws IOException
 *           On test failure.
 */
@Test
public void testNegativeTimestamps() throws IOException {
  List<KeyValue> kvList = new ArrayList<>();
  byte[] row = new byte[0];
  byte[] family = new byte[0];
  byte[] qualifier = new byte[0];
  byte[] value = new byte[0];
  if (includesTags) {
    byte[] metaValue1 = Bytes.toBytes("metaValue1");
    byte[] metaValue2 = Bytes.toBytes("metaValue2");
    kvList.add(new KeyValue(row, family, qualifier, 0L, value,
        new Tag[] { new ArrayBackedTag((byte) 1, metaValue1) }));
    kvList.add(new KeyValue(row, family, qualifier, 0L, value,
        new Tag[] { new ArrayBackedTag((byte) 1, metaValue2) }));
  } else {
    kvList.add(new KeyValue(row, family, qualifier, -1L, Type.Put, value));
    kvList.add(new KeyValue(row, family, qualifier, -2L, Type.Put, value));
  }
  testEncodersOnDataset(kvList, includesMemstoreTS, includesTags);
}
 
Example 17
Source Project: hbase   Source File: TestDataBlockEncoders.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testZeroByte() throws IOException {
  List<KeyValue> kvList = new ArrayList<>();
  byte[] row = Bytes.toBytes("abcd");
  byte[] family = new byte[] { 'f' };
  byte[] qualifier0 = new byte[] { 'b' };
  byte[] qualifier1 = new byte[] { 'c' };
  byte[] value0 = new byte[] { 'd' };
  byte[] value1 = new byte[] { 0x00 };
  if (includesTags) {
    kvList.add(new KeyValue(row, family, qualifier0, 0, value0,
        new Tag[] { new ArrayBackedTag((byte) 1, "value1") }));
    kvList.add(new KeyValue(row, family, qualifier1, 0, value1,
        new Tag[] { new ArrayBackedTag((byte) 1, "value1") }));
  } else {
    kvList.add(new KeyValue(row, family, qualifier0, 0, Type.Put, value0));
    kvList.add(new KeyValue(row, family, qualifier1, 0, Type.Put, value1));
  }
  testEncodersOnDataset(kvList, includesMemstoreTS, includesTags);
}
 
Example 18
Source Project: hbase   Source File: Mutation.java    License: Apache License 2.0 6 votes vote down vote up
private static Map<String, Object> cellToStringMap(Cell c) {
  Map<String, Object> stringMap = new HashMap<>();
  stringMap.put("qualifier", Bytes.toStringBinary(c.getQualifierArray(), c.getQualifierOffset(),
              c.getQualifierLength()));
  stringMap.put("timestamp", c.getTimestamp());
  stringMap.put("vlen", c.getValueLength());
  List<Tag> tags = PrivateCellUtil.getTags(c);
  if (tags != null) {
    List<String> tagsString = new ArrayList<>(tags.size());
    for (Tag t : tags) {
      tagsString
          .add((t.getType()) + ":" + Bytes.toStringBinary(Tag.cloneValue(t)));
    }
    stringMap.put("tag", tagsString);
  }
  return stringMap;
}
 
Example 19
Source Project: hbase   Source File: PerformanceEvaluation.java    License: Apache License 2.0 6 votes vote down vote up
@Override
void testRow(final int i) throws IOException {
  byte[] row = getRandomRow(this.rand, this.totalRows);
  Put put = new Put(row);
  byte[] value = generateData(this.rand, ROW_LENGTH);
  if (useTags) {
    byte[] tag = generateData(this.rand, TAG_LENGTH);
    Tag[] tags = new Tag[noOfTags];
    for (int n = 0; n < noOfTags; n++) {
      Tag t = new ArrayBackedTag((byte) n, tag);
      tags[n] = t;
    }
    KeyValue kv = new KeyValue(row, FAMILY_NAME, QUALIFIER_NAME, HConstants.LATEST_TIMESTAMP,
        value, tags);
    put.add(kv);
  } else {
    put.addColumn(FAMILY_NAME, QUALIFIER_NAME, value);
  }
  put.setDurability(writeToWAL ? Durability.SYNC_WAL : Durability.SKIP_WAL);
  mutator.mutate(put);
}
 
Example 20
Source Project: hbase   Source File: PerformanceEvaluation.java    License: Apache License 2.0 6 votes vote down vote up
@Override
void testRow(final int i) throws IOException {
  byte[] row = format(i);
  Put put = new Put(row);
  byte[] value = generateData(this.rand, ROW_LENGTH);
  if (useTags) {
    byte[] tag = generateData(this.rand, TAG_LENGTH);
    Tag[] tags = new Tag[noOfTags];
    for (int n = 0; n < noOfTags; n++) {
      Tag t = new ArrayBackedTag((byte) n, tag);
      tags[n] = t;
    }
    KeyValue kv = new KeyValue(row, FAMILY_NAME, QUALIFIER_NAME, HConstants.LATEST_TIMESTAMP,
        value, tags);
    put.add(kv);
  } else {
    put.addColumn(FAMILY_NAME, QUALIFIER_NAME, value);
  }
  put.setDurability(writeToWAL ? Durability.SYNC_WAL : Durability.SKIP_WAL);
  mutator.mutate(put);
}
 
Example 21
Source Project: hbase   Source File: VisibilityController.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether cell contains any tag with type as VISIBILITY_TAG_TYPE. This
 * tag type is reserved and should not be explicitly set by user.
 *
 * @param cell The cell under consideration
 * @param pair An optional pair of type {@code <Boolean, Tag>} which would be reused if already
 *     set and new one will be created if NULL is passed
 * @return If the boolean is false then it indicates that the cell has a RESERVERD_VIS_TAG and
 *     with boolean as true, not null tag indicates that a string modified tag was found.
 */
private Pair<Boolean, Tag> checkForReservedVisibilityTagPresence(Cell cell,
    Pair<Boolean, Tag> pair) throws IOException {
  if (pair == null) {
    pair = new Pair<>(false, null);
  } else {
    pair.setFirst(false);
    pair.setSecond(null);
  }
  // Bypass this check when the operation is done by a system/super user.
  // This is done because, while Replication, the Cells coming to the peer cluster with reserved
  // typed tags and this is fine and should get added to the peer cluster table
  if (isSystemOrSuperUser()) {
    // Does the cell contain special tag which indicates that the replicated
    // cell visiblilty tags
    // have been modified
    Tag modifiedTag = null;
    Iterator<Tag> tagsIterator = PrivateCellUtil.tagsIterator(cell);
    while (tagsIterator.hasNext()) {
      Tag tag = tagsIterator.next();
      if (tag.getType() == TagType.STRING_VIS_TAG_TYPE) {
        modifiedTag = tag;
        break;
      }
    }
    pair.setFirst(true);
    pair.setSecond(modifiedTag);
    return pair;
  }
  Iterator<Tag> tagsItr = PrivateCellUtil.tagsIterator(cell);
  while (tagsItr.hasNext()) {
    if (RESERVED_VIS_TAG_TYPES.contains(tagsItr.next().getType())) {
      return pair;
    }
  }
  pair.setFirst(true);
  return pair;
}
 
Example 22
Source Project: hbase   Source File: VisibilityController.java    License: Apache License 2.0 5 votes vote down vote up
private void removeReplicationVisibilityTag(List<Tag> tags) throws IOException {
  Iterator<Tag> iterator = tags.iterator();
  while (iterator.hasNext()) {
    Tag tag = iterator.next();
    if (tag.getType() == TagType.STRING_VIS_TAG_TYPE) {
      iterator.remove();
      break;
    }
  }
}
 
Example 23
Source Project: hbase   Source File: VisibilityController.java    License: Apache License 2.0 5 votes vote down vote up
private Cell createNewCellWithTags(Mutation mutation, Cell newCell) throws IOException {
  List<Tag> tags = Lists.newArrayList();
  CellVisibility cellVisibility = null;
  try {
    cellVisibility = mutation.getCellVisibility();
  } catch (DeserializationException e) {
    throw new IOException(e);
  }
  if (cellVisibility == null) {
    return newCell;
  }
  // Prepend new visibility tags to a new list of tags for the cell
  // Don't check user auths for labels with Mutations when the user is super user
  boolean authCheck = authorizationEnabled && checkAuths && !(isSystemOrSuperUser());
  tags.addAll(this.visibilityLabelService.createVisibilityExpTags(cellVisibility.getExpression(),
      true, authCheck));
  // Carry forward all other tags
  Iterator<Tag> tagsItr = PrivateCellUtil.tagsIterator(newCell);
  while (tagsItr.hasNext()) {
    Tag tag = tagsItr.next();
    if (tag.getType() != TagType.VISIBILITY_TAG_TYPE
        && tag.getType() != TagType.VISIBILITY_EXP_SERIALIZATION_FORMAT_TAG_TYPE) {
      tags.add(tag);
    }
  }

  return PrivateCellUtil.createCell(newCell, tags);
}
 
Example 24
Source Project: hbase   Source File: VisibilityController.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ReturnCode filterCell(final Cell cell) throws IOException {
  List<Tag> putVisTags = new ArrayList<>();
  Byte putCellVisTagsFormat = VisibilityUtils.extractVisibilityTags(cell, putVisTags);
  if (putVisTags.isEmpty() && deleteCellVisTags.isEmpty()) {
    // Early out if there are no tags in the cell
    return ReturnCode.INCLUDE;
  }
  boolean matchFound = VisibilityLabelServiceManager
      .getInstance().getVisibilityLabelService()
      .matchVisibility(putVisTags, putCellVisTagsFormat, deleteCellVisTags,
          deleteCellVisTagsFormat);
  return matchFound ? ReturnCode.INCLUDE : ReturnCode.SKIP;
}
 
Example 25
Source Project: hbase   Source File: VisibilityUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Extract the visibility tags of the given Cell into the given List
 * @param cell - the cell
 * @param tags - the array that will be populated if visibility tags are present
 * @return The visibility tags serialization format
 */
public static Byte extractVisibilityTags(Cell cell, List<Tag> tags) {
  Byte serializationFormat = null;
  Iterator<Tag> tagsIterator = PrivateCellUtil.tagsIterator(cell);
  while (tagsIterator.hasNext()) {
    Tag tag = tagsIterator.next();
    if (tag.getType() == TagType.VISIBILITY_EXP_SERIALIZATION_FORMAT_TAG_TYPE) {
      serializationFormat = Tag.getValueAsByte(tag);
    } else if (tag.getType() == VISIBILITY_TAG_TYPE) {
      tags.add(tag);
    }
  }
  return serializationFormat;
}
 
Example 26
Source Project: phoenix   Source File: MetaDataUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testConditionallyAddTagsToPutCells( ) {
    List<Tag> tags = TagUtil.asList(VIEW_MODIFIED_PROPERTY_BYTES, 0, VIEW_MODIFIED_PROPERTY_BYTES.length);
    assertEquals(tags.size(), 1);
    Tag expectedTag = tags.get(0);

    String version = VersionInfo.getVersion();
    KeyValueBuilder builder = KeyValueBuilder.get(version);
    KeyValue kv = builder.buildPut(wrap(ROW), wrap(TABLE_FAMILY_BYTES), wrap(UPDATE_CACHE_FREQUENCY_BYTES), wrap(
            PLong.INSTANCE.toBytes(0)));
    Put put = new Put(ROW);
    KeyValueBuilder.addQuietly(put, kv);

    ExtendedCellBuilder cellBuilder = (ExtendedCellBuilder) RawCellBuilderFactory.create();
    MetaDataUtil.conditionallyAddTagsToPutCells(put, TABLE_FAMILY_BYTES, UPDATE_CACHE_FREQUENCY_BYTES, cellBuilder,
            PInteger.INSTANCE.toBytes(1), VIEW_MODIFIED_PROPERTY_BYTES);

    Cell cell = put.getFamilyCellMap().get(TABLE_FAMILY_BYTES).get(0);

    // To check the cell tag whether view has modified this property
    assertTrue(Bytes.compareTo(expectedTag.getValueArray(), TagUtil.concatTags(EMPTY_BYTE_ARRAY, cell)) == 0);
    assertTrue(Bytes.contains(TagUtil.concatTags(EMPTY_BYTE_ARRAY, cell), expectedTag.getValueArray()));

    // To check tag data can be correctly deserialized
    Iterator<Tag> tagIterator = PrivateCellUtil.tagsIterator(cell);
    assertTrue(tagIterator.hasNext());
    Tag actualTag = tagIterator.next();
    assertTrue(Bytes.compareTo(actualTag.getValueArray(), actualTag.getValueOffset(), actualTag.getValueLength(),
            expectedTag.getValueArray(), expectedTag.getValueOffset(), expectedTag.getValueLength()) == 0);
    assertFalse(tagIterator.hasNext());
}
 
Example 27
Source Project: hbase   Source File: VisibilityUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isVisibilityTagsPresent(Cell cell) {
  Iterator<Tag> tagsIterator = PrivateCellUtil.tagsIterator(cell);
  while (tagsIterator.hasNext()) {
    Tag tag = tagsIterator.next();
    if (tag.getType() == VISIBILITY_TAG_TYPE) {
      return true;
    }
  }
  return false;
}
 
Example 28
Source Project: hbase   Source File: VisibilityNewVersionBehaivorTracker.java    License: Apache License 2.0 5 votes vote down vote up
private boolean tagMatched(Cell put, TagInfo delInfo) throws IOException {
  List<Tag> putVisTags = new ArrayList<>();
  Byte putCellVisTagsFormat = VisibilityUtils.extractVisibilityTags(put, putVisTags);
  return putVisTags.isEmpty() == delInfo.tags.isEmpty() && (
      (putVisTags.isEmpty() && delInfo.tags.isEmpty()) || VisibilityLabelServiceManager
          .getInstance().getVisibilityLabelService()
          .matchVisibility(putVisTags, putCellVisTagsFormat, delInfo.tags, delInfo.format));
}
 
Example 29
Source Project: hbase   Source File: DefaultVisibilityLabelServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<Tag> createVisibilityExpTags(String visExpression, boolean withSerializationFormat,
    boolean checkAuths) throws IOException {
  Set<Integer> auths = new HashSet<>();
  if (checkAuths) {
    User user = VisibilityUtils.getActiveUser();
    auths.addAll(this.labelsCache.getUserAuthsAsOrdinals(user.getShortName()));
    auths.addAll(this.labelsCache.getGroupAuthsAsOrdinals(user.getGroupNames()));
  }
  return VisibilityUtils.createVisibilityExpTags(visExpression, withSerializationFormat,
      checkAuths, auths, labelsCache);
}
 
Example 30
Source Project: hbase   Source File: DefaultVisibilityLabelServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
private static List<List<Integer>> sortTagsBasedOnOrdinal(List<Tag> tags) throws IOException {
  List<List<Integer>> fullTagsList = new ArrayList<>();
  for (Tag tag : tags) {
    if (tag.getType() == VISIBILITY_TAG_TYPE) {
      getSortedTagOrdinals(fullTagsList, tag);
    }
  }
  return fullTagsList;
}