PropertiesRep( PropertyContainer entity, long id ) { this.entityId = id; this.entityToString = entity.toString(); for ( String key : entity.getPropertyKeys() ) { Serializable value = (Serializable) entity.getProperty( key, null ); // We do this because the node may have changed since we did getPropertyKeys() if ( value != null ) { if ( value.getClass().isArray() ) { props.put( key, new ArrayList<>( Arrays.asList( IoPrimitiveUtils.asArray( value ) ) ) ); } else { props.put( key, value ); } } } }
public static byte[] readBytes( ReadableByteChannel channel, byte[] array, int length ) throws IOException { return readAndFlip( channel, ByteBuffer.wrap( array ), length ) ? array : null; }
public static Map<String, String> read2bMap( ReadableChannel channel ) throws IOException { short size = channel.getShort(); Map<String, String> map = new HashMap<>( size ); for ( int i = 0; i < size; i++ ) { String key = read2bLengthAndString( channel ); String value = read2bLengthAndString( channel ); map.put( key, value ); } return map; }
public static String readLengthAndString( ReadableByteChannel channel, ByteBuffer buffer ) throws IOException { Integer length = readInt( channel, buffer ); if ( length != null ) { char[] chars = new char[length]; chars = readCharArray( channel, buffer, chars ); return chars == null ? null : new String( chars ); } return null; }
private Object readIndexValue( byte valueType, ReadableChannel channel ) throws IOException { switch ( valueType ) { case IndexCommand.VALUE_TYPE_NULL: return null; case IndexCommand.VALUE_TYPE_SHORT: return channel.getShort(); case IndexCommand.VALUE_TYPE_INT: return channel.getInt(); case IndexCommand.VALUE_TYPE_LONG: return channel.getLong(); case IndexCommand.VALUE_TYPE_FLOAT: return channel.getFloat(); case IndexCommand.VALUE_TYPE_DOUBLE: return channel.getDouble(); case IndexCommand.VALUE_TYPE_STRING: return read3bLengthAndString( channel ); default: throw new RuntimeException( "Unknown value type " + valueType ); } } }
@Test public void shouldReadALongString() throws IOException { // given // build a string longer than 32k int stringSize = 32 * 1024 + 1; StringBuilder sb = new StringBuilder(); for ( int i = 0; i < stringSize; i++ ) { sb.append( "x" ); } String lengthyString = sb.toString(); // we need 3 more bytes for writing the string length InMemoryClosableChannel channel = new InMemoryClosableChannel( stringSize + 3 ); IoPrimitiveUtils.write3bLengthAndString( channel, lengthyString ); // when String stringFromChannel = IoPrimitiveUtils.read3bLengthAndString( channel ); // then assertEquals( lengthyString, stringFromChannel ); } }
private Command visitIndexCreateCommand( ReadableChannel channel ) throws IOException { IndexCommandHeader header = readIndexCommandHeader( channel ); Map<String,String> config = read2bMap( channel ); CreateCommand command = new CreateCommand(); command.init( header.indexNameId, header.entityType, config ); return command; }
write3bLengthAndString( channel, value.toString() ); break; case IndexCommand.VALUE_TYPE_SHORT:
public static byte[] readBytes( ReadableByteChannel channel, byte[] array ) throws IOException { return readBytes( channel, array, array.length ); }
private String readNextString( ReadableByteChannel channel ) throws IOException { return IoPrimitiveUtils.readLengthAndString( channel, buffer( 100 ) ); }
private Integer readNextInt( ReadableByteChannel channel ) throws IOException { return IoPrimitiveUtils.readInt( channel, buffer( 4 ) ); }
private Object readIndexValue( byte valueType, ReadableChannel channel ) throws IOException { switch ( valueType ) { case IndexCommand.VALUE_TYPE_NULL: return null; case IndexCommand.VALUE_TYPE_SHORT: return channel.getShort(); case IndexCommand.VALUE_TYPE_INT: return channel.getInt(); case IndexCommand.VALUE_TYPE_LONG: return channel.getLong(); case IndexCommand.VALUE_TYPE_FLOAT: return channel.getFloat(); case IndexCommand.VALUE_TYPE_DOUBLE: return channel.getDouble(); case IndexCommand.VALUE_TYPE_STRING: return read3bLengthAndString( channel ); default: throw new RuntimeException( "Unknown value type " + valueType ); } } }
private Command visitIndexCreateCommand( ReadableChannel channel ) throws IOException { IndexCommandHeader header = readIndexCommandHeader( channel ); Map<String,String> config = read2bMap( channel ); CreateCommand command = new CreateCommand(); command.init( header.indexNameId, header.entityType, config ); return command; }
public static String readLengthAndString( ReadableByteChannel channel, ByteBuffer buffer ) throws IOException { Integer length = readInt( channel, buffer ); if ( length != null ) { char[] chars = new char[length]; chars = readCharArray( channel, buffer, chars ); return chars == null ? null : new String( chars ); } return null; }
write3bLengthAndString( channel, value.toString() ); break; case IndexCommand.VALUE_TYPE_SHORT:
private Integer tryToReadVersion( ReadableByteChannel channel ) throws IOException { byte[] array = IoPrimitiveUtils.readBytes( channel, new byte[MAGIC.length] ); if ( !Arrays.equals( MAGIC, array ) ) { return null; } return array != null ? readNextInt( channel ) : null; }
private String readNextString( ReadableByteChannel channel ) throws IOException { return IoPrimitiveUtils.readLengthAndString( channel, buffer( 100 ) ); }
private Integer readNextInt( ReadableByteChannel channel ) throws IOException { return IoPrimitiveUtils.readInt( channel, buffer( 4 ) ); }
private Object readIndexValue( byte valueType, ReadableChannel channel ) throws IOException { switch ( valueType ) { case IndexCommand.VALUE_TYPE_NULL: return null; case IndexCommand.VALUE_TYPE_SHORT: return channel.getShort(); case IndexCommand.VALUE_TYPE_INT: return channel.getInt(); case IndexCommand.VALUE_TYPE_LONG: return channel.getLong(); case IndexCommand.VALUE_TYPE_FLOAT: return channel.getFloat(); case IndexCommand.VALUE_TYPE_DOUBLE: return channel.getDouble(); case IndexCommand.VALUE_TYPE_STRING: return read3bLengthAndString( channel ); default: throw new RuntimeException( "Unknown value type " + valueType ); } } }
private void addSingleProperty( long entityId, Document document, String key, Object value ) { for ( Object oneValue : IoPrimitiveUtils.asArray(value) ) { boolean isValueContext = oneValue instanceof ValueContext; oneValue = isValueContext ? ((ValueContext) oneValue).getCorrectValue() : oneValue.toString(); type.addToDocument( document, key, oneValue ); if ( createdNow ) { // If we know that the index was created this session // then we can go ahead and add stuff to the cache directly // when adding to the index. addToCache( entityId, key, oneValue ); } } }