Skip to content

Commit

Permalink
Fix read bug in inline UTF-8 encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd committed Jun 25, 2017
1 parent 2c1f5d4 commit a019ae3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
Expand Up @@ -26,6 +26,7 @@
import org.neo4j.values.Values; import org.neo4j.values.Values;


import static org.neo4j.impl.store.prototype.neole.ReadStore.combineReference; import static org.neo4j.impl.store.prototype.neole.ReadStore.combineReference;
import static org.neo4j.impl.store.prototype.neole.ShortStringEncoding.ENCODINGS;


class PropertyCursor extends org.neo4j.impl.store.prototype.PropertyCursor<ReadStore> class PropertyCursor extends org.neo4j.impl.store.prototype.PropertyCursor<ReadStore>
{ {
Expand Down Expand Up @@ -108,6 +109,11 @@ void init( StoreFile properties, long reference )
@Override @Override
public boolean next() public boolean next()
{ {
if ( block == Integer.MIN_VALUE )
{
return false;
}

int nextBlock = block + blocksUsedByCurrent(); int nextBlock = block + blocksUsedByCurrent();
while ( nextBlock >= 0 && nextBlock < 4 ) while ( nextBlock >= 0 && nextBlock < 4 )
{ {
Expand Down Expand Up @@ -284,8 +290,13 @@ private int blocksUsedByCurrent()
{ {
int encoding = shortStringEncoding( valueBytes ); int encoding = shortStringEncoding( valueBytes );
int stringLength = shortStringLength( valueBytes ); int stringLength = shortStringLength( valueBytes );
return ShortStringEncoding.calculateNumberOfBlocksUsed( ShortStringEncoding.ENCODINGS[ encoding-1 ],
stringLength ); if ( encoding == ShortStringEncoding.ENCODING_UTF8 || encoding == ShortStringEncoding.ENCODING_LATIN1 )
{
return ShortStringEncoding.numberOfBlocksUsedUTF8OrLatin1( stringLength );
}

return ShortStringEncoding.numberOfBlocksUsed( ENCODINGS[ encoding - 1 ], stringLength );
} }
return 1; return 1;
} }
Expand Down
Expand Up @@ -5,17 +5,17 @@
* This file is part of Neo4j. * This file is part of Neo4j.
* *
* Neo4j is free software: you can redistribute it and/or modify * Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU Affero General Public License as
* the Free Software Foundation, either version 3 of the License, or * published by the Free Software Foundation, either version 3 of the
* (at your option) any later version. * License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU Affero General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.neo4j.impl.store.prototype.neole; package org.neo4j.impl.store.prototype.neole;


Expand Down Expand Up @@ -784,14 +784,19 @@ private long rightOverflowMask( int step )
return mask; return mask;
} }


public static int calculateNumberOfBlocksUsed( ShortStringEncoding encoding, int length ) public static int numberOfBlocksUsedUTF8OrLatin1( int length )
{
return totalBits( length * 8 );
}

public static int numberOfBlocksUsed( ShortStringEncoding encoding, int length )
{ {
return totalBits( length * encoding.step ); return totalBits( length * encoding.step );
} }


private static int totalBits( int bitsForCharacters ) private static int totalBits( int bitsForCharacters )
{ {
int bitsInTotal = 24 + 4 + 5 + 6 + bitsForCharacters; int bitsInTotal = HEADER_SIZE + bitsForCharacters;
return ((bitsInTotal - 1) >> 6) + 1; // /64 return ((bitsInTotal - 1) >> 6) + 1; // /64
} }
} }
Expand Up @@ -39,8 +39,9 @@
public class PropertyCursorTest public class PropertyCursorTest
{ {
private static long bare, byteProp, shortProp, intProp, inlineLongProp, longProp, private static long bare, byteProp, shortProp, intProp, inlineLongProp, longProp,
floatProp, doubleProp, trueProp, falseProp, charProp, shortStringProp, allProps; floatProp, doubleProp, trueProp, falseProp, charProp, shortStringProp, utf8Prop, allProps;


private static String chinese = "造Unicode之";
@ClassRule @ClassRule
public static final GraphSetup graph = new GraphSetup() public static final GraphSetup graph = new GraphSetup()
{ {
Expand All @@ -65,6 +66,7 @@ protected void create( GraphDatabaseService graphDb )


charProp = createNodeWithProperty( graphDb, "charProp", 'x' ); charProp = createNodeWithProperty( graphDb, "charProp", 'x' );
shortStringProp = createNodeWithProperty( graphDb, "shortStringProp", "hello" ); shortStringProp = createNodeWithProperty( graphDb, "shortStringProp", "hello" );
utf8Prop = createNodeWithProperty( graphDb, "utf8Prop", chinese );


Node all = graphDb.createNode(); Node all = graphDb.createNode();
// first property record // first property record
Expand All @@ -83,6 +85,7 @@ protected void create( GraphDatabaseService graphDb )


all.setProperty( "charProp", 'x' ); all.setProperty( "charProp", 'x' );
all.setProperty( "shortStringProp", "hello" ); all.setProperty( "shortStringProp", "hello" );
all.setProperty( "utf8Prop", chinese );


allProps = all.getId(); allProps = all.getId();


Expand Down Expand Up @@ -135,6 +138,7 @@ public void shouldAccessSingleProperty() throws Exception
assertAccessSingleProperty( falseProp, false ); assertAccessSingleProperty( falseProp, false );
assertAccessSingleProperty( charProp, 'x' ); assertAccessSingleProperty( charProp, 'x' );
assertAccessSingleProperty( shortStringProp, "hello" ); assertAccessSingleProperty( shortStringProp, "hello" );
assertAccessSingleProperty( utf8Prop, chinese );
} }


@Test @Test
Expand Down Expand Up @@ -167,8 +171,9 @@ public void shouldAccessAllNodeProperties() throws Exception
assertTrue( "falseProp", values.contains( false ) ); assertTrue( "falseProp", values.contains( false ) );
assertTrue( "charProp", values.contains( 'x' ) ); assertTrue( "charProp", values.contains( 'x' ) );
assertTrue( "shortStringProp", values.contains( "hello" ) ); assertTrue( "shortStringProp", values.contains( "hello" ) );
assertTrue( "utf8Prop", values.contains( chinese ) );


assertEquals( "number of values", 11, values.size() ); assertEquals( "number of values", 12, values.size() );
} }
} }


Expand Down

0 comments on commit a019ae3

Please sign in to comment.