public ByteBuffer convert(RecordId recordId) { if (recordId == null) { return null; } return ByteBuffer.wrap(recordId.toBytes()); }
@Override public String getValue(RecordId recordId) { return recordId.toString(); } }
/** * Called for expressions of the kind "+prop". */ public Dep plus(IdGenerator idGenerator, Map<String, String> propsToAdd) { RecordId master = id.getMaster(); Map<String, String> newVariantProperties = Maps.newHashMap(id.getVariantProperties()); Set<String> newVprops = Sets.newHashSet(this.moreDimensionedVariants); for (String key: propsToAdd.keySet()) { String value = propsToAdd.get(key); if (value == null) { newVprops.add(key); } else { newVariantProperties.put(key, value); } } return new Dep(idGenerator.newRecordId(master, newVariantProperties), newVprops); }
private static SortedMap<String, PropertyValue> createVariantProps(RecordId recordId) { if (recordId == null) { return null; } SortedMap<String, PropertyValue> variantProps = null; if (!recordId.isMaster()) { variantProps = new TreeMap<String, PropertyValue>(); for (Map.Entry<String, String> entry : recordId.getVariantProperties().entrySet()) { variantProps.put(entry.getKey(), new PropertyValue(PropertyMode.SET, entry.getValue())); } } return variantProps; }
@Override public DependantRecordIdsIterator findDependantsOf(AbsoluteRecordId parentRecordId, Set<SchemaId> fields, SchemaId vtag) throws IOException { final RecordId master = parentRecordId.getRecordId().getMaster(); final Query query = new Query(); query.addEqualsCondition("dependency_masterrecordid", master.toBytes()); if (vtag != null) { query.addEqualsCondition("dependant_vtag", vtag.getBytes()); } query.setIndexFilter(new DerefMapIndexFilter(parentRecordId.getRecordId().getVariantProperties(), fields)); return new DependantRecordIdsIteratorImpl(backwardDerefIndex.performQuery(query), this.serializationUtil); }
@Override public Set<RecordId> getVariants(RecordId recordId) throws RepositoryException { byte[] masterRecordIdBytes = recordId.getMaster().toBytes(); FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL); filterList.addFilter(new PrefixFilter(masterRecordIdBytes)); filterList.addFilter(REAL_RECORDS_FILTER); Scan scan = new Scan(masterRecordIdBytes, filterList); scan.addColumn(RecordCf.DATA.bytes, RecordColumn.DELETED.bytes); Set<RecordId> recordIds = new HashSet<RecordId>(); try { ResultScanner scanner = recordTable.getScanner(scan); Result result; while ((result = scanner.next()) != null) { RecordId id = idGenerator.fromBytes(result.getRow()); recordIds.add(id); } Closer.close( scanner); // Not closed in finally block: avoid HBase contact when there could be connection problems. } catch (IOException e) { throw new RepositoryException("Error getting list of variants of record " + recordId.getMaster(), e); } return recordIds; }
RecordId masterRecordId = this.masterRecordId == null ? contextRecordId.getMaster() : this.masterRecordId; (this.masterRecordId != null && !this.masterRecordId.isMaster()) || (copyAll && !contextRecordId.isMaster())) { for (Map.Entry<String, String> entry : contextRecordId.getVariantProperties().entrySet()) { if (!varProps.containsKey(entry.getKey())) { varProps.put(entry.getKey(), entry.getValue());
@Override public String getValue(RecordId recordId) throws ShardSelectorException { String propertyValue = recordId.getVariantProperties().get(propertyName); if (propertyValue == null) { throw new ShardSelectorException("Variant property used for sharding has no value. Property " + propertyName + " in record id: " + recordId); } return propertyValue; } }
public RecordVariantFilter(RecordId masterRecordId, Map<String, String> variantProperties) { this.masterRecordId = masterRecordId.getMaster(); this.variantProperties = variantProperties; }
@Override public void follow(IndexUpdateBuilder indexUpdateBuilder, FollowCallback callback) throws RepositoryException, IOException, InterruptedException { RecordContext ctx = indexUpdateBuilder.getRecordContext(); if (ctx.dep.id.isMaster()) { // We're already on a master record, stop here return; } LRepository repository = indexUpdateBuilder.getRepository(); LTable table = repository.getTable(indexUpdateBuilder.getTable()); Dep masterDep = new Dep(ctx.dep.id.getMaster(), Collections.<String>emptySet()); Record master = null; try { master = VersionTag.getIdRecord(masterDep.id, indexUpdateBuilder.getVTag(), table, repository); } catch (RecordNotFoundException e) { // It's ok that the master does not exist } catch (VersionNotFoundException e) { // It's ok that the master does not exist } indexUpdateBuilder.push(master, masterDep); callback.call(); indexUpdateBuilder.pop(); }
@Override public void addDependency(SchemaId field) { RecordContext ctx = contexts.peek(); try { if (!ctx.dep.moreDimensionedVariants.isEmpty() || !ctx.dep.id.equals(recordId)) { // avoid adding unnecesary self-references dependencies.get(DerefMapUtil.newEntry( new AbsoluteRecordIdImpl(table, ctx.dep.id), ctx.dep.moreDimensionedVariants)).add(field); } } catch (ExecutionException ee) { throw new RuntimeException("Failed to update dependencies"); } }
@Override public int hashCode() { int result = masterRecordId != null ? masterRecordId.hashCode() : 0; result = 31 * result + (copyAll ? 1 : 0); result = 31 * result + (variantProps != null ? variantProps.hashCode() : 0); return result; }
@Override public RecordId newRecordId(RecordId masterRecordId, Map<String, String> variantProperties) { ArgumentValidator.notNull(masterRecordId, "masterRecordId"); ArgumentValidator.notNull(variantProperties, "variantProperties"); if (!masterRecordId.isMaster()) { throw new IllegalArgumentException("Specified masterRecordId is a variant record ID."); } if (variantProperties.isEmpty()) { return masterRecordId; } checkReservedCharacters(variantProperties); return new VariantRecordId(masterRecordId, variantProperties, this); }
private IndexEntry createBackwardEntry(AbsoluteRecordId parentRecordId, AbsoluteRecordId dependantRecordId, SchemaId dependantVtagId, Set<SchemaId> fields, Set<String> moreDimensionedVariantProperties) throws IOException { final byte[] serializedVariantPropertiesPattern = this.serializationUtil.serializeVariantPropertiesPattern( this.serializationUtil.createVariantPropertiesPattern(parentRecordId.getRecordId().getVariantProperties(), moreDimensionedVariantProperties)); final IndexEntry bwdEntry = new IndexEntry(backwardDerefIndex.getDefinition()); bwdEntry.addField("dependency_masterrecordid", parentRecordId.getRecordId().getMaster().toBytes()); bwdEntry.addField("dependant_vtag", dependantVtagId.getBytes()); bwdEntry.addField("variant_properties_pattern", serializedVariantPropertiesPattern); // the identifier is the dependant which depends on the dependency bwdEntry.setIdentifier(dependantRecordId.toBytes()); // the fields which the dependant uses of the dependency (null if used for deleting the entry) if (fields != null) { bwdEntry.addData(FIELDS_KEY, this.serializationUtil.serializeFields(fields)); } return bwdEntry; }
@Override public Filter createHBaseFilter(RecordFilter uncastFilter, LRepository repository, HBaseRecordFilterFactory factory) throws RepositoryException, InterruptedException { if (!(uncastFilter instanceof RecordVariantFilter)) { return null; } final RecordVariantFilter filter = (RecordVariantFilter) uncastFilter; if (filter.getMasterRecordId() == null) { throw new IllegalArgumentException("Record ID should be specified in RecordVariantFilter"); } if (filter.getVariantProperties() == null) { throw new IllegalArgumentException("VariantProperties should be specified in RecordVariantFilter"); } return new FilterList(Arrays.<Filter>asList( new PrefixFilter(filter.getMasterRecordId().getMaster().toBytes()), new LilyRecordVariantFilter(filter.getVariantProperties()))); } }
private void evalProps(Map<String, String> resolvedProps, RecordId contextRecordId) { Map<String, String> contextProps = contextRecordId.getVariantProperties(); for (Map.Entry<String, PropertyValue> entry : variantProps.entrySet()) { PropertyValue propValue = entry.getValue(); switch (propValue.mode) { case SET: resolvedProps.put(entry.getKey(), propValue.value); break; case REMOVE: resolvedProps.remove(entry.getKey()); break; case COPY: String value = contextProps.get(entry.getKey()); if (value != null) { resolvedProps.put(entry.getKey(), value); } break; } } }
/** * Calling this resets the state of the variant properties recorded so far. */ public LinkBuilder recordId(RecordId recordId) { if (recordId != null) { this.masterRecordId = recordId.getMaster(); this.variantProps = createVariantProps(recordId); } else { this.masterRecordId = null; this.variantProps = null; } return this; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } VariantRecordId other = (VariantRecordId) obj; if (masterRecordId == null) { if (other.masterRecordId != null) { return false; } } else if (!masterRecordId.equals(other.masterRecordId)) { return false; } if (variantProperties == null) { if (other.variantProperties != null) { return false; } } else if (!variantProperties.equals(other.variantProperties)) { return false; } return true; } }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((masterRecordId == null) ? 0 : masterRecordId.hashCode()); result = prime * result + ((variantProperties == null) ? 0 : variantProperties.hashCode()); return result; }