/** * Constructs a CellSpec instance from a CellSchema descriptor. * * @param cellSchema CellSchema record to initialize the CellSpec from. * @param schemaTable Schema table to resolve schema hashes or UIDs. * @return a new CellSpec initialized from a CellSchema descriptor. * @throws InvalidLayoutException if the cell specification is invalid. */ public static CellSpec fromCellSchema( final CellSchema cellSchema, final FijiSchemaTable schemaTable) throws InvalidLayoutException { return fromCellSchema(cellSchema) .setSchemaTable(schemaTable); }
/** * Constructs a CellSpec instance from a CellSchema descriptor. * * @param cellSchema CellSchema record to initialize the CellSpec from. * @return a new CellSpec initialized from the given CellSchema descriptor. * @throws InvalidLayoutException if the cell specification is invalid. */ public static CellSpec fromCellSchema(final CellSchema cellSchema) throws InvalidLayoutException { return new CellSpec() .setCellSchema(cellSchema); }
/** * Creates a schema decoder. * * @param cellSpec Specification of the cell encoding. * @return a new schema decoder. * @throws IOException on I/O error. */ private static SchemaDecoder createSchemaDecoder(CellSpec cellSpec) throws IOException { switch (cellSpec.getCellSchema().getStorage()) { case HASH: return new SchemaHashDecoder(cellSpec.getSchemaTable()); case UID: return new SchemaIdDecoder(cellSpec.getSchemaTable()); case FINAL: return new FinalSchemaDecoder(cellSpec.getAvroSchema()); default: throw new InternalFijiError( "Unexpected cell storage in cell schema: " + cellSpec.getCellSchema()); } }
} else { cellSpec = CellSpec.copy(cellSpec); if (cellSpec.getSchemaTable() == null) { cellSpec.setSchemaTable(schemaTable); if (cellSpec.getDecoderFactory() == null) { cellSpec.setDecoderFactory(factory); final FijiCellDecoder<?> decoder = cellSpec.getDecoderFactory().create(cellSpec); decoderMap.put(column, decoder);
for (FijiColumnName column : columns) { final CellSpec cellSpec = layout.getCellSpec(column) .setColumnURI( FijiURI.newBuilder(tableURI).withColumnNames(ImmutableList.of(column)).build()) .setSchemaTable(schemaTable) .setEncoderFactory(factory); final FijiCellEncoder encoder = cellSpec.getEncoderFactory().create(cellSpec); encoderMap.put(column.getName(), encoder);
/** * Initializes a new RawBytesCellDecoder. * * @param cellSpec Specification of the cell encoding. */ public RawBytesCellDecoder(CellSpec cellSpec) { Preconditions.checkNotNull(cellSpec); Preconditions.checkArgument( cellSpec.getCellSchema().getType() == SchemaType.RAW_BYTES); }
/** {@inheritDoc} */ @Override public byte[] getHBaseCellValue(FijiColumnName column, DecodedCell<?> fijiCell) throws IOException { final CellSpec cellSpec = mColumnNameTranslator.getTableLayout().getCellSpec(column) .setSchemaTable(mSchemaTable); final FijiCellEncoder encoder = DefaultFijiCellEncoderFactory.get().create(cellSpec); return encoder.encode(fijiCell); } }
/** * Initializes an abstract FijiAvroCellDecoder. * * @param cellSpec Specification of the cell encoding. * @throws IOException on I/O error. */ protected AvroCellDecoder(CellSpec cellSpec) throws IOException { Preconditions.checkNotNull(cellSpec); Preconditions.checkArgument(cellSpec.isAvro()); mSchemaDecoder = createSchemaDecoder(cellSpec); mReaderSchema = cellSpec.getAvroSchema(); }
/** * Creates a new <code>HBaseTableLayoutDatabase</code> instance. * * <p>This class does not take ownership of the HTable. The caller should close it when * it is no longer needed.</p> * * @param fijiURI URI of the Fiji instance this layout database belongs to. * @param htable The HTable used to store the layout data. * @param family The name of the column family within the HTable used to store layout data. * @param schemaTable The Fiji schema table. * @throws IOException on I/O error. */ public HBaseTableLayoutDatabase( FijiURI fijiURI, HTableInterface htable, String family, FijiSchemaTable schemaTable) throws IOException { mFijiURI = fijiURI; mTable = Preconditions.checkNotNull(htable); mFamily = Preconditions.checkNotNull(family); mFamilyBytes = Bytes.toBytes(mFamily); mSchemaTable = Preconditions.checkNotNull(schemaTable); final CellSpec cellSpec = CellSpec.fromCellSchema(CELL_SCHEMA, mSchemaTable); mCellEncoder = new AvroCellEncoder(cellSpec); mCellDecoder = SpecificCellDecoderFactory.get().create(cellSpec); }
/** * Returns the Avro reader schema used to decode cells. * * <p> For Avro-encoded cells only. </p> * * @return the Avro reader schema used to decode cells. * Null means cells are decoded using their writer schema. */ public Schema getAvroSchema() { Preconditions.checkState(isAvro()); switch (mAvroReaderSchema) { case DEFAULT: return getDefaultReaderSchema(); case EXPLICIT: return mReaderSchema; case WRITER: return null; default: throw new InternalFijiError("Invalid Avro reader schema: " + mAvroReaderSchema); } }
/** * Gets the registered writer schemas associated with the provided cell specification. * * @param spec containing registered schemas. * @return the set of writer schemas registered for the provided cell. * Null if validation is disabled. * @throws IOException if there is an error looking up schemas. */ private static Set<Schema> getRegisteredWriters(final CellSpec spec) throws IOException { final List<AvroSchema> writerSchemas = spec.getCellSchema().getWriters(); if (writerSchemas == null) { return null; } final AvroSchemaResolver resolver = new SchemaTableAvroResolver(spec.getSchemaTable()); return Sets.newHashSet(Collections2.transform(writerSchemas, resolver)); }
if (!mRegisteredWriters.contains(writerSchema)) { LOG.info("Writer schema {} is currently not registered for column {}, registering now.", writerSchema, mCellSpec.getColumnURI()); if (mCellSpec.getColumnURI() == null) { throw new InternalFijiError("CellSpec has no column URI: " + mCellSpec); registerWriterSchema(mCellSpec.getColumnURI(), writerSchema); if (mCellSpec.getCellSchema().getStorage() == SchemaStorage.FINAL && !writerSchema.equals(mReaderSchema)) { throw new FijiEncodingException(
/** {@inheritDoc} */ @Override public Schema getReaderSchema(final String family, final String qualifier) throws IOException { return mTableLayout.getCellSpec(FijiColumnName.create(family, qualifier)).getAvroSchema(); }
/** * Returns a new unspecified CellSpec. * * <p> Use the setters to populate the necessary fields. </p> * * @return a new unspecified CellSpec. */ public static CellSpec create() { return new CellSpec(); }
} else { final CellSpec cellSpec = layout.getCellSpec(column); decoderMap.put(column, cellSpec.getDecoderFactory().create(cellSpec));
/** * Initializes a new RawBytesCellEncoder. * * @param cellSpec Specification of the cell to encode. * @throws IOException on I/O error. */ public RawBytesCellEncoder(final CellSpec cellSpec) throws IOException { mCellSpec = Preconditions.checkNotNull(cellSpec); Preconditions.checkArgument(cellSpec.getCellSchema().getType() == SchemaType.RAW_BYTES); }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public <T> void put(EntityId entityId, String family, String qualifier, long timestamp, T value) throws IOException { final FijiColumnName fijiColumn = new FijiColumnName(family, qualifier); final HBaseColumnName hbaseColumn = mColumnNameTranslator.toHBaseColumnName(fijiColumn); final CellSpec cellSpec = mTable.getLayout().getCellSpec(fijiColumn) .setSchemaTable(mFiji.getSchemaTable()); final FijiCellEncoder encoder = DefaultFijiCellEncoderFactory.get().create(cellSpec); final HFileKeyValue mrKey = new HFileKeyValue( entityId.getHBaseRowKey(), hbaseColumn.getFamily(), hbaseColumn.getQualifier(), timestamp, encoder.encode(value)); write(mrKey); }
/** * Creates a new <code>FijiCellEncoder</code> instance. * * @param cellSpec Specification of the cell to encode. * @throws IOException on I/O error. */ public AvroCellEncoder(final CellSpec cellSpec) throws IOException { mCellSpec = Preconditions.checkNotNull(cellSpec); Preconditions.checkArgument(cellSpec.isAvro()); mReaderSchema = mCellSpec.getAvroSchema(); mSchemaEncoder = createSchemaEncoder(mCellSpec); mRegisteredWriters = flattenAvroUnions(getRegisteredWriters(mCellSpec)); mValidationPolicy = getAvroValidationPolicy(mCellSpec); }
/** * Creates a new <code>CassandraTableLayoutDatabase</code> instance. * * <p>This class does not take ownership of the table. The caller should close it when * it is no longer needed.</p> * * @param fijiURI URI of the Fiji instance this layout database belongs to. * @param admin The Cassandra cluster connection. * @param schemaTable The Fiji schema table. * @throws java.io.IOException on I/O error. */ public CassandraTableLayoutDatabase( FijiURI fijiURI, CassandraAdmin admin, FijiSchemaTable schemaTable) throws IOException { mFijiURI = fijiURI; mMetaTableName = CassandraTableName.getMetaLayoutTableName(fijiURI); mAdmin = admin; mSchemaTable = Preconditions.checkNotNull(schemaTable); final CellSpec cellSpec = CellSpec.fromCellSchema(CELL_SCHEMA, mSchemaTable); mCellEncoder = new AvroCellEncoder(cellSpec); mCellDecoder = SpecificCellDecoderFactory.get().create(cellSpec); mGetRowsStatement = getGetRowsStatement(); mRemoveAllTableLayoutVersionsStatement = getRemoveAllTableLayoutVersionsStatement(); mUpdateTableLayoutStatement = getUpdateTableLayoutStatement(); mRemoveRecentTableLayoutVersionsStatement = getRemoveRecentTableLayoutVersionsStatement(); mListTablesStatement = getListTablesStatement(); }