Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Value validation for indexed properties.
Introduce validation of property values for legacy and schema indexes. Create validators for empty/null/allowable cases. Perform validation during property updated and index population.
- Loading branch information
1 parent
9e7c009
commit f814082
Showing
16 changed files
with
683 additions
and
55 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
community/kernel/src/main/java/org/neo4j/kernel/impl/api/IndexSimpleValueValidator.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,45 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "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.kernel.impl.api; | ||
|
||
import org.neo4j.kernel.impl.util.Validator; | ||
|
||
public class IndexSimpleValueValidator implements Validator | ||
{ | ||
|
||
public static IndexSimpleValueValidator INSTANCE = new IndexSimpleValueValidator(); | ||
|
||
private IndexSimpleValueValidator() | ||
{ | ||
} | ||
|
||
@Override | ||
public void validate( Object value ) | ||
{ | ||
if ( value == null ) | ||
{ | ||
throw new IllegalArgumentException( "Null value" ); | ||
} | ||
if ( value instanceof String ) | ||
{ | ||
IndexValueLengthValidator.INSTANCE.validate( ((String) value).getBytes() ); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
community/kernel/src/main/java/org/neo4j/kernel/impl/api/IndexValueLengthValidator.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,49 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "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.kernel.impl.api; | ||
|
||
import org.neo4j.kernel.impl.util.Validator; | ||
|
||
public class IndexValueLengthValidator implements Validator<byte[]> | ||
{ | ||
// Maximum bytes value length that supported by indexes. | ||
// Absolute hard maximum length for a term, in bytes once | ||
// encoded as UTF8. If a term arrives from the analyzer | ||
// longer than this length, an IllegalArgumentException | ||
// when lucene writer trying to add or update document | ||
private static final int MAX_TERM_LENGTH = (1 << 15) - 2; | ||
|
||
public static final IndexValueLengthValidator INSTANCE = new IndexValueLengthValidator(); | ||
|
||
private IndexValueLengthValidator() | ||
{ | ||
} | ||
|
||
@Override | ||
public void validate( byte[] bytes ) | ||
{ | ||
if ( bytes.length > MAX_TERM_LENGTH ) | ||
{ | ||
throw new IllegalArgumentException( "Property value bytes length: " + bytes.length + | ||
" is longer then " + MAX_TERM_LENGTH + ", which is maximum supported length" + | ||
" of indexed property value." ); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
community/kernel/src/main/java/org/neo4j/kernel/impl/api/IndexValueValidator.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,42 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "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.kernel.impl.api; | ||
|
||
import org.neo4j.kernel.api.index.ArrayEncoder; | ||
import org.neo4j.kernel.impl.util.Validator; | ||
|
||
public class IndexValueValidator implements Validator | ||
{ | ||
public static IndexValueValidator INSTANCE = new IndexValueValidator(); | ||
|
||
private IndexValueValidator() | ||
{ | ||
} | ||
|
||
@Override | ||
public void validate( Object value ) | ||
{ | ||
IndexSimpleValueValidator.INSTANCE.validate( value ); | ||
if ( value.getClass().isArray() ) | ||
{ | ||
IndexValueLengthValidator.INSTANCE.validate( ArrayEncoder.encode( value ).getBytes() ); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
community/kernel/src/main/java/org/neo4j/kernel/impl/api/LegacyIndexValueValidator.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,41 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "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.kernel.impl.api; | ||
|
||
import org.neo4j.kernel.impl.util.Validator; | ||
|
||
public class LegacyIndexValueValidator implements Validator | ||
{ | ||
public static LegacyIndexValueValidator INSTANCE = new LegacyIndexValueValidator(); | ||
|
||
private LegacyIndexValueValidator() | ||
{ | ||
} | ||
|
||
@Override | ||
public void validate( Object value ) | ||
{ | ||
IndexSimpleValueValidator.INSTANCE.validate( value ); | ||
if ( !(value instanceof Number) && value.toString() == null ) | ||
{ | ||
throw new IllegalArgumentException( "Value of type " + value.getClass() + " has null toString" ); | ||
} | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
community/kernel/src/test/java/org/neo4j/kernel/impl/api/IndexSimpleValueValidatorTest.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,77 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "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.kernel.impl.api; | ||
|
||
import org.apache.commons.lang3.RandomStringUtils; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import org.neo4j.kernel.impl.util.Validator; | ||
|
||
import static org.neo4j.kernel.impl.api.IndexSimpleValueValidator.INSTANCE; | ||
|
||
public class IndexSimpleValueValidatorTest | ||
{ | ||
|
||
@Rule | ||
public ExpectedException expectedException = ExpectedException.none(); | ||
|
||
@Test | ||
public void nullIsNotAllowed() throws Exception | ||
{ | ||
expectedException.expect( IllegalArgumentException.class ); | ||
expectedException.expectMessage( "Null value" ); | ||
getValidator().validate( null ); | ||
} | ||
|
||
@Test | ||
public void tooLongStringIsNotAllowed() | ||
{ | ||
expectedException.expect( IllegalArgumentException.class ); | ||
expectedException.expectMessage( | ||
"Property value bytes length: 35000 is longer then 32766, " + | ||
"which is maximum supported length of indexed property value." ); | ||
getValidator().validate( RandomStringUtils.randomAlphabetic( 35000 ) ); | ||
} | ||
|
||
@Test | ||
public void numberIsValidValue() | ||
{ | ||
getValidator().validate( 5 ); | ||
getValidator().validate( 5.0d ); | ||
getValidator().validate( 5.0f ); | ||
getValidator().validate( 5L ); | ||
} | ||
|
||
@Test | ||
public void shortStringIsValidValue() | ||
{ | ||
getValidator().validate( RandomStringUtils.randomAlphabetic( 5 ) ); | ||
getValidator().validate( RandomStringUtils.randomAlphabetic( 10 ) ); | ||
getValidator().validate( RandomStringUtils.randomAlphabetic( 250 ) ); | ||
getValidator().validate( RandomStringUtils.randomAlphabetic( 450 ) ); | ||
} | ||
|
||
protected Validator<Object> getValidator() | ||
{ | ||
return INSTANCE; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
community/kernel/src/test/java/org/neo4j/kernel/impl/api/IndexValueLengthValidatorTest.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,52 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "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.kernel.impl.api; | ||
|
||
import org.apache.commons.lang3.RandomUtils; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import static org.neo4j.kernel.impl.api.IndexValueLengthValidator.INSTANCE; | ||
|
||
public class IndexValueLengthValidatorTest | ||
{ | ||
@Rule | ||
public ExpectedException expectedException = ExpectedException.none(); | ||
|
||
@Test | ||
public void tooLongByteArrayIsNotAllowed() | ||
{ | ||
expectedException.expect( IllegalArgumentException.class ); | ||
expectedException.expectMessage( "Property value bytes length: 35000 is longer then 32766, which is maximum supported length of indexed property value." ); | ||
INSTANCE.validate( RandomUtils.nextBytes( 35000 ) ); | ||
} | ||
|
||
@Test | ||
public void shortByteArrayIsValid() | ||
{ | ||
INSTANCE.validate( RandomUtils.nextBytes( 3 ) ); | ||
INSTANCE.validate( RandomUtils.nextBytes( 30 ) ); | ||
INSTANCE.validate( RandomUtils.nextBytes( 300 ) ); | ||
INSTANCE.validate( RandomUtils.nextBytes( 4303 ) ); | ||
INSTANCE.validate( RandomUtils.nextBytes( 13234 ) ); | ||
} | ||
|
||
} |
Oops, something went wrong.