@Override public Iterable<Row<T, K>> execute(final ResultSet rs) throws AvroBaseException, SQLException { List<Row<T, K>> result = new ArrayList<Row<T, K>>(); while(rs.next()) { int schemaId = rs.getInt(1); long version = rs.getLong(2); int format = rs.getInt(3); byte[] bytes = rs.getBytes(4); result.add(new Row<T, K>(readValue(bytes, getSchema(schemaId), AvroFormat.values()[format]), row, version)); } return result; } }.query();
@Override public Row<T, K> get(K row) throws AvroBaseException { Object key = keyMaker.make(row); Element element = cache.get(key); Row<T, K> tkRow; if (element == null) { tkRow = super.get(row); cache.put(new Element(key, tkRow)); invalidate(row); } else { // TODO: until we offer immutable rows, clone the result Serializable value = element.getValue(); tkRow = value == null ? null : ((Row<T, K>) value).clone(); } return tkRow; }
Iterable<Row<T, Long>> execute(ResultSet rs) throws AvroBaseException, SQLException { final List<Row<T,Long>> results = new LinkedList<Row<T,Long>>(); while (rs.next()) { long row = rs.getLong(1); int schema_id = rs.getInt(2); long version = rs.getLong(3); byte[] avro = rs.getBytes(4); Schema schema = getSchema(schema_id); if (schema != null) { results.add(new Row<T, Long>(readValue(avro, schema, format), row, version)); } else { throw new AvroBaseException("Failed to find schema: " + schema_id); } } return results; } }.query();
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 boolean put(K row, T value, long version) throws AvroBaseException { boolean put = super.put(row, value, version); cache.put(new Element(keyMaker.make(row), new Row<T, K>(value, row, version))); invalidate(row); return put; }
@Override public void put(K row, T value) throws AvroBaseException { super.put(row, value); cache.put(new Element(keyMaker.make(row), new Row<T, K>(value, row))); invalidate(row); }
private Row<T, K> newrow(K row, DBObject ro) { T ao = getAvroObject(ro); return new Row<T, K>(ao, row, (Long) ro.get("version")); }
@Override public K create(T value) throws AvroBaseException { K k = super.create(value); Row<T, K> tkRow = new Row<T, K>(value, k); cache.put(new Element(keyMaker.make(k), tkRow)); return k; }
public Row<T, K> execute(ResultSet rs) throws AvroBaseException, SQLException { if (rs.next()) { int schema_id = rs.getInt(1); long version = rs.getLong(2); AvroFormat format = AvroFormat.values()[rs.getByte(3)]; byte[] avro = rs.getBytes(4); Schema schema = getSchema(schema_id); if (schema != null) { return new Row<T, K>(readValue(avro, schema, format), keytx.fromBytes(row), version); } else { throw new AvroBaseException("Failed to find schema: " + schema_id); } } else { return null; } } }.query();
@Override public boolean put(K row, T value, long version) throws AvroBaseException { final boolean rv = delegate().put(row, value, version); if (rv) { index.index(new Row<T,K>(value, row)); } return rv; }
@Override public K create(T value) throws AvroBaseException { final K row = delegate().create(value); indexRow(new Row<T,K>(value, row)); return row; }
@Override public void put(K row, T value) throws AvroBaseException { delegate().put(row, value); indexRow(new Row<T,K>(value, row)); }
@Override public boolean put(K row, T value, long version) throws AvroBaseException { final boolean rv = delegate().put(row, value, version); indexRow(new Row<T,K>(value, row)); return rv; }
@Override public void put(K row, T value) throws AvroBaseException { delegate().put(row, value); index.index(new Row<T,K>(value, row)); }
@Override public K create(T value) throws AvroBaseException { final K row = delegate().create(value); index.index(new Row<T,K>(value, row)); return row; }
@Override public Row<T, K> mutate(K row, Mutator<T> tMutator, Creator<T> tCreator) throws AvroBaseException { Row<T, K> tRow; do { // Grab the current version tRow = get(row); // If it doesn't exist, create a new one if (tRow == null && tCreator != null) { final T newValue = tCreator.create(); if (newValue != null) { tRow = new Row<T,K>(newValue, row, 0); } } if (tRow != null) { T value = tMutator.mutate(tRow.value); // Mutator can abort the mutation if (value == null) return tRow; // Optimistically set the row if (put(row, value, tRow.version)) { return new Row<T, K>(value, row, tRow.version + 1); } } else { return null; } // On failure to set, try again } while (true); }
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, K> mutate(K row, Mutator<T> tMutator) throws AvroBaseException { Lock writeLock = writeLock(row); try { File file = getFile(row, true); FileInputStream fis = new FileInputStream(file); FileChannel channel = fis.getChannel(); FileLock fileLock = channel.lock(); try { Row<T, K> tStringRow = _get(row); if (tStringRow == null) return null; T mutate = tMutator.mutate(tStringRow.value); if (mutate != null) { put(row, mutate); return new Row<T, K>(mutate, row); } return tStringRow; } finally { fileLock.release(); channel.close(); fis.close(); } } catch (FileNotFoundException e) { return null; } catch (IOException e) { throw new AvroBaseException("Failed to delete: " + row, e); } finally { writeLock.unlock(); } }
schema = loadSchema(j.get(schemaId + z).getBytes(), schemaId); return new Row<T, String>(readValue(data.getBytes(), schema, format), row, Long.parseLong(versionStr)); } catch (Exception e) { pool.returnBrokenResource(j);