@Override public K create(T value) throws AvroBaseException { if (supplier == null) throw new AvroBaseException("No key generator provided"); K row = supplier.get(); put(row, value); return row; }
public LoggingMysqlAB(ExecutorService es, DataSource datasource, String table, String family, String schemaTable, Schema schema, AvroFormat storageFormat, KeyStrategy<K> keytx) throws AvroBaseException { super(es, datasource, table, family, schemaTable, schema, storageFormat, keytx); try { roll(); } catch (SQLException e) { throw new AvroBaseException("Could not roll log table", e); } }
@Override public K lookup(SQ query) { final Iterable<K> results = search(query); try { return Iterables.getOnlyElement(results, null); } catch (IllegalArgumentException e) { throw new AvroBaseException("Too many results"); } }
@Override public void exportSchema(DataOutputStream dos) { try { dos.writeBoolean(true); writeSchemas(dos); dos.writeBoolean(false); } catch (Exception e) { throw new AvroBaseException(e); } }
private Schema loadSchema(int id, byte[] value) throws AvroBaseException { Schema schema; try { schema = Schema.parse(new ByteArrayInputStream(value)); } catch (IOException e) { throw new AvroBaseException("Could not parse the schema", e); } abbrevSchema.put(id, schema); schemaAbbrev.put(schema, id); return schema; }
@Override public int compare(S3Object s3Object, S3Object s3Object1) { try { final byte[] key = Hex.decodeHex(filename(s3Object).toCharArray()); final byte[] key1 = Hex.decodeHex(filename(s3Object1).toCharArray()); return bytesComparator.compare(key, key1); } catch (DecoderException e) { throw new AvroBaseException("Failed to decode filename: " + s3Object.getName(), e); } } });
private Schema loadSchema(int id, byte[] value) throws AvroBaseException { Schema schema; try { schema = Schema.parse(new ByteArrayInputStream(value)); } catch (IOException e) { throw new AvroBaseException("Could not parse the schema", e); } schemas.put(id, schema); schemaIds.put(schema, id); return schema; }
/** * Load a schema from the schema table */ protected Schema loadSchema(byte[] value, String row) throws AvroBaseException { Schema schema; try { schema = Schema.parse(new ByteArrayInputStream(value)); } catch (IOException e) { throw new AvroBaseException("Failed to deserialize schema: " + row, e); } schemaCache.put(row, schema); hashCache.put(schema, row); return schema; }
Row<T, Long> execute(ResultSet rs) throws AvroBaseException, SQLException { if (rs.next()) { int schema_id = rs.getInt(1); long version = rs.getLong(2); byte[] avro = rs.getBytes(3); Schema schema = getSchema(schema_id); if (schema != null) { return new Row<T, Long>(readValue(avro, schema, format), row, version); } else { throw new AvroBaseException("Failed to find schema: " + schema_id); } } else { return null; } } }.query();
@Override public Row<T, K> get(K row) throws AvroBaseException { Lock lock = readLock(row); try { return _get(row); } catch (Exception e) { throw new AvroBaseException("Failed to get row: " + row, e); } finally { lock.unlock(); } }
@Override public Long create(T value) throws AvroBaseException { setTimestamp(value, FIELD_CREATED); final long key = keySupplier.get(); if (!put(key, value, 0)) { throw new AvroBaseException("did not add " + key); } else { return key; } }
private String toString(K row) { if (row == null) return null; byte[] bytes = transformer == null ? (byte[]) row : transformer.apply(row); try { return new String(base32hex.encode(bytes)); } catch (IOException e) { throw new AvroBaseException("Could not encode"); } }
@Override public K create(T value) throws AvroBaseException { final K key = keytx.newKey(); if (!put(key, value, 0)) { throw new AvroBaseException("did not add " + key); } else { return key; } }
@Override public void put(final Long id, final T value) throws AvroBaseException { setTimestamp(value, FIELD_UPDATED); int updated = new Update(upsertStatement) { void setup(PreparedStatement ps) throws AvroBaseException, SQLException { parameterize(ps, id, 1, schemaId(value), value); } }.insert(); if (updated == 0) { throw new AvroBaseException("Failed to save: " + updated); } }
public void addShard(ShardableAvroBase<T, K> avroBase, double weight, boolean wait) throws AvroBaseException { strategy.add(avroBase, weight); if (wait) { try { strategy.waitForBalance(); } catch (InterruptedException e) { throw new AvroBaseException("Shard balancing interrupted", e); } } }
private HTableInterface createSchemaTable() throws AvroBaseException { HTableInterface schemaTable; HColumnDescriptor family = new HColumnDescriptor(AVRO_FAMILY); family.setMaxVersions(1); family.setCompressionType(Compression.Algorithm.LZO); family.setInMemory(true); HTableDescriptor tableDesc = new HTableDescriptor(schemaName); tableDesc.addFamily(family); try { admin.createTable(tableDesc); } catch (IOException e1) { throw new AvroBaseException(e1); } schemaTable = pool.getTable(schemaName); return schemaTable; }
@Override public void delete(byte[] row) throws AvroBaseException { HTableInterface table = getTable(); try { Delete delete = new Delete(row); delete.deleteFamily(family); table.delete(delete); } catch (IOException e) { throw new AvroBaseException("Failed to delete row", e); } finally { pool.putTable(table); } }
public Row<T, K> clone() { Schema schema = value.getSchema(); T newvalue; try { newvalue = (T) Class.forName(schema.getFullName()).newInstance(); } catch (Exception e) { throw new AvroBaseException("Could not clone row", e); } for (Schema.Field field : schema.getFields()) { int pos = field.pos(); newvalue.put(pos, value.get(pos)); } return new Row<T, K>(newvalue, row, version); } }
@Override public Row<T, byte[]> get(byte[] row) throws AvroBaseException { HTableInterface table = getTable(); try { Result result = getHBaseRow(table, row, family); // TODO: This is working around a bug in HBASE 0.89 if (row.length == 0 && !Bytes.equals(row, result.getRow())) { return null; } return getRowResult(result, row); } catch (IOException e) { throw new AvroBaseException(e); } finally { pool.putTable(table); } }
@Override public void put(byte[] row, T value) throws AvroBaseException { HTableInterface table = getTable(); long version; try { do { // FIXME: Spin until success, last one wins. Provably dangerous? version = getVersion(family, row, table); } while (!put(row, value, version)); } catch (IOException e) { throw new AvroBaseException("Failed to retrieve version for row: " + $_(row), e); } finally { pool.putTable(table); } }