private List<IMonitoringRecord> decodeMonitoringRecords(final ByteBuffer buffer, final IRegistry<String> stringRegistry, final int endOffset) { final List<IMonitoringRecord> records = new ArrayList<IMonitoringRecord>(); int currentOffset = buffer.position(); final CachedRecordFactoryCatalog recordFactoryCatalog = this.cachedRecordFactoryCatalog; while (currentOffset < endOffset) { final int recordTypeId = buffer.getInt(); final String recordTypeName = stringRegistry.get(recordTypeId); final long loggingTimestamp = buffer.getLong(); final IRecordFactory<? extends IMonitoringRecord> recordFactory = recordFactoryCatalog.get(recordTypeName); final IMonitoringRecord record = recordFactory.create(DefaultValueDeserializer.create(buffer, stringRegistry)); record.setLoggingTimestamp(loggingTimestamp); records.add(record); currentOffset = buffer.position(); } // The record data must end exactly at the given end offset if (currentOffset != endOffset) { throw new InvalidFormatException("Invalid record data found, should have ended at offset " + endOffset + ", but ended at offset " + currentOffset + "."); } return records; }
private void readRegularRecord(final ByteBuffer buffer) { final long registryId = buffer.getLong(); final int classId = buffer.getInt(); final long loggingTimestamp = buffer.getLong(); final ILookup<String> stringRegistry = this.getStringRegistry(registryId); try { final String recordClassName = stringRegistry.get(classId); final IRecordFactory<? extends IMonitoringRecord> recordFactory = this.cachedRecordFactoryCatalog.get(recordClassName); final IMonitoringRecord record = recordFactory.create(DefaultValueDeserializer.create(buffer, stringRegistry)); record.setLoggingTimestamp(loggingTimestamp); this.reader.deliverRecord(record); } catch (final RecordInstantiationException e) { LOG.error("Error instantiating record", e); } }
private void read(final ByteBuffer buffer) { final int clazzId = buffer.getInt(); final long loggingTimestamp = buffer.getLong(); try { // NOCS (Nested try-catch) // final IMonitoringRecord record = AbstractMonitoringRecord.createFromByteBuffer(clazzid, buffer, this.stringRegistry); final String recordClassName = this.stringRegistry.get(clazzId); final IRecordFactory<? extends IMonitoringRecord> recordFactory = this.cachedRecordFactoryCatalog.get(recordClassName); final IMonitoringRecord record = recordFactory.create(DefaultValueDeserializer.create(buffer, this.stringRegistry)); record.setLoggingTimestamp(loggingTimestamp); super.deliver(OUTPUT_PORT_NAME_RECORDS, record); } catch (final RecordInstantiationException ex) { this.log.error("Failed to create record", ex); } }
private boolean onBufferReceived(final ByteBuffer buffer) { // NOCS (hides field) // identify record class if (buffer.remaining() < INT_BYTES) { return false; } final int clazzId = buffer.getInt(); // NOPMD (clazzId must be read before reading timestamp) // identify logging timestamp if (buffer.remaining() < LONG_BYTES) { return false; } final long loggingTimestamp = buffer.getLong(); // NOPMD (timestamp must be read before checking the buffer for record size) final String recordClassName = this.stringRegistry.get(clazzId); // identify record data final IRecordFactory<? extends IMonitoringRecord> recordFactory = this.recordFactories.get(recordClassName); if (buffer.remaining() < recordFactory.getRecordSizeInBytes()) { // includes the case where size is -1 return false; } try { final IMonitoringRecord record = recordFactory.create(DefaultValueDeserializer.create(buffer, this.stringRegistry)); record.setLoggingTimestamp(loggingTimestamp); this.recordReceiver.newMonitoringRecord(record); } catch (final RecordInstantiationException ex) { this.logger.error("Failed to create: " + recordClassName, ex); throw ex; // we cannot continue reading the buffer because we do not know at which position to continue } return true; }
@Override protected final boolean onBufferReceived(final ByteBuffer buffer) { // identify record class if (buffer.remaining() < INT_BYTES) { return false; } final int clazzId = buffer.getInt(); // NOPMD (clazzId must be read before reading timestamp) // identify logging timestamp if (buffer.remaining() < LONG_BYTES) { return false; } final long loggingTimestamp = buffer.getLong(); // NOPMD (timestamp must be read before checking the buffer for record size) final String recordClassName = this.stringRegistry.get(clazzId); // identify record data final IRecordFactory<? extends IMonitoringRecord> recordFactory = this.recordFactories.get(recordClassName); if (buffer.remaining() < recordFactory.getRecordSizeInBytes()) { // includes the case where size is -1 return false; } try { final IMonitoringRecord record = recordFactory.create(DefaultValueDeserializer.create(buffer, this.stringRegistry)); record.setLoggingTimestamp(loggingTimestamp); this.onRecordReceived(record); } catch (final RecordInstantiationException ex) { super.logger.error("Failed to create: " + recordClassName, ex); } return true; }
private boolean deserializeRecord(final int clazzId, final ByteBuffer buffer) { // identify logging timestamp if (buffer.remaining() < LONG_BYTES) { return false; } final long loggingTimestamp = buffer.getLong(); // NOPMD (timestamp must be read before checking the buffer for record size) final String recordClassName = this.readerRegistry.get(clazzId); // identify record data final IRecordFactory<? extends IMonitoringRecord> recordFactory = this.recordFactories.get(recordClassName); if (buffer.remaining() < recordFactory.getRecordSizeInBytes()) { return false; } try { final IMonitoringRecord record = recordFactory.create(DefaultValueDeserializer.create(buffer, this.stringRegistryWrapper)); record.setLoggingTimestamp(loggingTimestamp); this.listener.onRecordReceived(record); } catch (final RecordInstantiationException ex) { super.logger.error("Failed to create: " + recordClassName, ex); } return true; }