Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Broke out StringValue implementations
- Loading branch information
1 parent
21cbefd
commit 1a2447d
Showing
8 changed files
with
171 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
community/values/src/main/java/org/neo4j/values/storable/StringWrappingStringValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.values.storable; | ||
|
||
/** | ||
* Implementation of StringValue that wraps a `java.lang.String` and | ||
* delegates methods to that instance. | ||
*/ | ||
final class StringWrappingStringValue extends StringValue | ||
{ | ||
final String value; | ||
|
||
StringWrappingStringValue( String value ) | ||
{ | ||
assert value != null; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
String value() | ||
{ | ||
return value; | ||
} | ||
|
||
@Override | ||
public int length() | ||
{ | ||
return value.codePointCount( 0, value.length() ); | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
community/values/src/main/java/org/neo4j/values/storable/UTF8StringValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.values.storable; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
|
||
/* | ||
* Just as a normal StringValue but is backed by a byte array and does string | ||
* serialization lazily. | ||
* | ||
* TODO in this implementation most operations will actually load the string | ||
* such as hashCode, length. These could be implemented using | ||
* the byte array directly in later optimizations | ||
*/ | ||
public final class UTF8StringValue extends StringValue | ||
{ | ||
private volatile String value; | ||
private final byte[] bytes; | ||
private final int offset; | ||
private final int length; | ||
|
||
UTF8StringValue( byte[] bytes, int offset, int length ) | ||
{ | ||
assert bytes != null; | ||
this.bytes = bytes; | ||
this.offset = offset; | ||
this.length = length; | ||
} | ||
|
||
@Override | ||
public <E extends Exception> void writeTo( ValueWriter<E> writer ) throws E | ||
{ | ||
writer.writeUTF8( bytes, offset, length ); | ||
} | ||
|
||
@Override | ||
public boolean equals( Value value ) | ||
{ | ||
if ( value instanceof org.neo4j.values.storable.UTF8StringValue ) | ||
{ | ||
return Arrays.equals( bytes, ((org.neo4j.values.storable.UTF8StringValue) value).bytes ); | ||
} | ||
else | ||
{ | ||
return super.equals( value ); | ||
} | ||
} | ||
|
||
@Override | ||
String value() | ||
{ | ||
String s = value; | ||
if ( s == null ) | ||
{ | ||
synchronized ( this ) | ||
{ | ||
s = value; | ||
if ( s == null ) | ||
{ | ||
s = value = new String( bytes, offset, length, StandardCharsets.UTF_8 ); | ||
|
||
} | ||
} | ||
} | ||
return s; | ||
} | ||
|
||
@Override | ||
public int length() | ||
{ | ||
int count = 0, i = offset, len = offset + length; | ||
while ( i < len ) | ||
{ | ||
byte b = bytes[i]; | ||
//If high bit is zero (equivalent to the byte being positive in two's complement) | ||
//we are dealing with an ascii value and use a single byte for storing the value. | ||
if ( b >= 0 ) | ||
{ | ||
i++; | ||
} | ||
|
||
//The number of high bits tells us how many bytes we use to store the value | ||
//e.g. 110xxxx -> need two bytes, 1110xxxx -> need three bytes, 11110xxx -> needs | ||
//four bytes | ||
while ( b < 0 ) | ||
{ | ||
i++; | ||
b = (byte) (b << 1); | ||
} | ||
count++; | ||
} | ||
return count; | ||
} | ||
|
||
public byte[] bytes() | ||
{ | ||
return bytes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters