Skip to content

Commit

Permalink
Remove availability to use token store outside transaction
Browse files Browse the repository at this point in the history
Having the ability to use the token store outside of a transaction makes
it hard to do property level security and currently we don't really have
a use case for it. So until we do it is no longer allowed.
  • Loading branch information
pontusmelke committed Mar 2, 2018
1 parent 90d31ed commit b30b310
Show file tree
Hide file tree
Showing 19 changed files with 186 additions and 138 deletions.
Expand Up @@ -41,8 +41,6 @@ public interface Session extends AutoCloseable
*/
Transaction beginTransaction( Transaction.Type type ) throws KernelException;

Token token();

@Override
void close();
}
Expand Up @@ -98,6 +98,11 @@ enum Type
*/
TokenWrite tokenWrite();

/**
* @return Token read and write operations
*/
Token token();

/**
* @return The schema index read operations of the graph, used for finding indexes.
*/
Expand Down
Expand Up @@ -81,7 +81,7 @@ public void setupGraph() throws IOException, KernelException
session = kernel.beginSession( LoginContext.AUTH_DISABLED );
cursors = new ManagedTestCursors( kernel.cursors() );
tx = session.beginTransaction( Transaction.Type.explicit );
token = session.token();
token = tx.token();
read = tx.dataRead();
indexRead = tx.indexRead();
schemaRead = tx.schemaRead();
Expand Down
Expand Up @@ -72,7 +72,7 @@ public void shouldSeeNewLabeledNodeInTransaction() throws Exception
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
labelId = session.token().labelGetOrCreateForName( labelName );
labelId = tx.token().labelGetOrCreateForName( labelName );
tx.dataWrite().nodeAddLabel( nodeId, labelId );

try ( NodeCursor node = cursors.allocateNodeCursor() )
Expand Down Expand Up @@ -108,8 +108,8 @@ public void shouldSeeLabelChangesInTransaction() throws Exception
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
toRetain = session.token().labelGetOrCreateForName( toRetainName );
toDelete = session.token().labelGetOrCreateForName( toDeleteName );
toRetain = tx.token().labelGetOrCreateForName( toRetainName );
toDelete = tx.token().labelGetOrCreateForName( toDeleteName );
tx.dataWrite().nodeAddLabel( nodeId, toRetain );
tx.dataWrite().nodeAddLabel( nodeId, toDelete );
tx.success();
Expand All @@ -124,7 +124,7 @@ public void shouldSeeLabelChangesInTransaction() throws Exception

try ( Transaction tx = session.beginTransaction() )
{
toAdd = session.token().labelGetOrCreateForName( toAddName );
toAdd = tx.token().labelGetOrCreateForName( toAddName );
tx.dataWrite().nodeAddLabel( nodeId, toAdd );
tx.dataWrite().nodeRemoveLabel( nodeId, toDelete );

Expand Down Expand Up @@ -197,8 +197,8 @@ public void shouldSeeNewNodePropertyInTransaction() throws Exception
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
int prop1 = session.token().propertyKeyGetOrCreateForName( propKey1 );
int prop2 = session.token().propertyKeyGetOrCreateForName( propKey2 );
int prop1 = tx.token().propertyKeyGetOrCreateForName( propKey1 );
int prop2 = tx.token().propertyKeyGetOrCreateForName( propKey2 );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, prop1, stringValue( "hello" ) ), NO_VALUE );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, prop2, stringValue( "world" ) ), NO_VALUE );

Expand Down Expand Up @@ -240,7 +240,7 @@ public void shouldSeeAddedPropertyFromExistingNodeWithoutPropertiesInTransaction
// When/Then
try ( Transaction tx = session.beginTransaction() )
{
int propToken = session.token().propertyKeyGetOrCreateForName( propKey );
int propToken = tx.token().propertyKeyGetOrCreateForName( propKey );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );

try ( NodeCursor node = cursors.allocateNodeCursor();
Expand Down Expand Up @@ -280,15 +280,15 @@ public void shouldSeeAddedPropertyFromExistingNodeWithPropertiesInTransaction()
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
propToken1 = session.token().propertyKeyGetOrCreateForName( propKey1 );
propToken1 = tx.token().propertyKeyGetOrCreateForName( propKey1 );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken1, stringValue( "hello" ) ), NO_VALUE );
tx.success();
}

// When/Then
try ( Transaction tx = session.beginTransaction() )
{
propToken2 = session.token().propertyKeyGetOrCreateForName( propKey2 );
propToken2 = tx.token().propertyKeyGetOrCreateForName( propKey2 );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken2, stringValue( "world" ) ), NO_VALUE );

try ( NodeCursor node = cursors.allocateNodeCursor();
Expand Down Expand Up @@ -334,7 +334,7 @@ public void shouldSeeUpdatedPropertyFromExistingNodeWithPropertiesInTransaction(
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
propToken = session.token().propertyKeyGetOrCreateForName( propKey );
propToken = tx.token().propertyKeyGetOrCreateForName( propKey );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );
tx.success();
}
Expand Down Expand Up @@ -380,7 +380,7 @@ public void shouldSeeRemovedPropertyInTransaction() throws Exception
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
propToken = session.token().propertyKeyGetOrCreateForName( propKey );
propToken = tx.token().propertyKeyGetOrCreateForName( propKey );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );
tx.success();
}
Expand Down Expand Up @@ -420,7 +420,7 @@ public void shouldSeeRemovedThenAddedPropertyInTransaction() throws Exception
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
propToken = session.token().propertyKeyGetOrCreateForName( propKey );
propToken = tx.token().propertyKeyGetOrCreateForName( propKey );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );
tx.success();
}
Expand Down
Expand Up @@ -145,7 +145,7 @@ public void shouldAddLabelNode() throws Exception
// When
try ( Transaction tx = session.beginTransaction() )
{
labelId = session.token().labelGetOrCreateForName( labelName );
labelId = tx.token().labelGetOrCreateForName( labelName );
tx.dataWrite().nodeAddLabel( node, labelId );
tx.success();
}
Expand All @@ -172,7 +172,7 @@ public void shouldAddLabelNodeOnce() throws Exception

try ( Transaction tx = session.beginTransaction() )
{
labelId = session.token().labelGetOrCreateForName( labelName );
labelId = tx.token().labelGetOrCreateForName( labelName );
assertFalse( tx.dataWrite().nodeAddLabel( node, labelId ) );
tx.success();
}
Expand All @@ -198,7 +198,7 @@ public void shouldRemoveLabel() throws Exception

try ( Transaction tx = session.beginTransaction() )
{
labelId = session.token().labelGetOrCreateForName( labelName );
labelId = tx.token().labelGetOrCreateForName( labelName );
tx.dataWrite().nodeRemoveLabel( nodeId, labelId );
tx.success();
}
Expand All @@ -217,7 +217,7 @@ public void shouldNotAddLabelToNonExistingNode() throws Exception
final String labelName = "Town";
try ( Transaction tx = session.beginTransaction() )
{
labelId = session.token().labelGetOrCreateForName( labelName );
labelId = tx.token().labelGetOrCreateForName( labelName );
exception.expect( KernelException.class );
tx.dataWrite().nodeAddLabel( node, labelId );
}
Expand All @@ -238,14 +238,14 @@ public void shouldRemoveLabelOnce() throws Exception

try ( Transaction tx = session.beginTransaction() )
{
labelId = session.token().labelGetOrCreateForName( labelName );
labelId = tx.token().labelGetOrCreateForName( labelName );
assertTrue( tx.dataWrite().nodeRemoveLabel( nodeId, labelId ) );
tx.success();
}

try ( Transaction tx = session.beginTransaction() )
{
labelId = session.token().labelGetOrCreateForName( labelName );
labelId = tx.token().labelGetOrCreateForName( labelName );
assertFalse( tx.dataWrite().nodeRemoveLabel( nodeId, labelId ) );
tx.success();
}
Expand All @@ -271,7 +271,7 @@ public void shouldAddPropertyToNode() throws Exception
// When
try ( Transaction tx = session.beginTransaction() )
{
int token = session.token().propertyKeyGetOrCreateForName( propertyKey );
int token = tx.token().propertyKeyGetOrCreateForName( propertyKey );
assertThat( tx.dataWrite().nodeSetProperty( node, token, stringValue( "hello" ) ), equalTo( NO_VALUE ) );
tx.success();
}
Expand Down Expand Up @@ -300,7 +300,7 @@ public void shouldUpdatePropertyToNode() throws Exception
// When
try ( Transaction tx = session.beginTransaction() )
{
int token = session.token().propertyKeyGetOrCreateForName( propertyKey );
int token = tx.token().propertyKeyGetOrCreateForName( propertyKey );
assertThat( tx.dataWrite().nodeSetProperty( node, token, stringValue( "hello" ) ),
equalTo( intValue( 42 ) ) );
tx.success();
Expand Down Expand Up @@ -330,7 +330,7 @@ public void shouldRemovePropertyFromNode() throws Exception
// When
try ( Transaction tx = session.beginTransaction() )
{
int token = session.token().propertyKeyGetOrCreateForName( propertyKey );
int token = tx.token().propertyKeyGetOrCreateForName( propertyKey );
assertThat( tx.dataWrite().nodeRemoveProperty( node, token ),
equalTo( intValue( 42 ) ) );
tx.success();
Expand Down Expand Up @@ -358,7 +358,7 @@ public void shouldRemoveNonExistingPropertyFromNode() throws Exception
// When
try ( Transaction tx = session.beginTransaction() )
{
int token = session.token().propertyKeyGetOrCreateForName( propertyKey );
int token = tx.token().propertyKeyGetOrCreateForName( propertyKey );
assertThat( tx.dataWrite().nodeRemoveProperty( node, token ),
equalTo( NO_VALUE ) );
tx.success();
Expand Down Expand Up @@ -388,7 +388,7 @@ public void shouldRemovePropertyFromNodeTwice() throws Exception
// When
try ( Transaction tx = session.beginTransaction() )
{
int token = session.token().propertyKeyGetOrCreateForName( propertyKey );
int token = tx.token().propertyKeyGetOrCreateForName( propertyKey );
assertThat( tx.dataWrite().nodeRemoveProperty( node, token ),
equalTo( intValue( 42 ) ) );
assertThat( tx.dataWrite().nodeRemoveProperty( node, token ),
Expand Down Expand Up @@ -418,7 +418,7 @@ public void shouldUpdatePropertyToNodeInTransaction() throws Exception
// When
try ( Transaction tx = session.beginTransaction() )
{
int token = session.token().propertyKeyGetOrCreateForName( propertyKey );
int token = tx.token().propertyKeyGetOrCreateForName( propertyKey );
assertThat( tx.dataWrite().nodeSetProperty( node, token, stringValue( "hello" ) ), equalTo( NO_VALUE ) );
assertThat( tx.dataWrite().nodeSetProperty( node, token, stringValue( "world" ) ), equalTo( stringValue( "hello" ) ) );
assertThat( tx.dataWrite().nodeSetProperty( node, token, intValue( 1337 ) ), equalTo( stringValue( "world" ) ) );
Expand Down
Expand Up @@ -128,26 +128,26 @@ static StartNode dense( GraphDatabaseService graphDb )
}

static Map<String,Integer> count(
Session session,
org.neo4j.internal.kernel.api.Transaction transaction,
RelationshipTraversalCursor relationship ) throws KernelException
{
HashMap<String,Integer> counts = new HashMap<>();
while ( relationship.next() )
{
String key = computeKey( session, relationship );
String key = computeKey( transaction, relationship );
counts.compute( key, ( k, value ) -> value == null ? 1 : value + 1 );
}
return counts;
}

static void assertCount(
Session session,
org.neo4j.internal.kernel.api.Transaction transaction,
RelationshipTraversalCursor relationship,
Map<String,Integer> expectedCounts,
int expectedType,
Direction direction ) throws KernelException
{
String key = computeKey( session.token().relationshipTypeName( expectedType ), direction );
String key = computeKey( transaction.token().relationshipTypeName( expectedType ), direction );
int expectedCount = expectedCounts.getOrDefault( key, 0 );
int count = 0;

Expand Down Expand Up @@ -223,7 +223,7 @@ private static String computeKey( StartRelationship r )
return computeKey( r.type.name(), r.direction );
}

private static String computeKey( Session session, RelationshipTraversalCursor r ) throws KernelException
private static String computeKey( org.neo4j.internal.kernel.api.Transaction transaction, RelationshipTraversalCursor r ) throws KernelException
{
Direction d;
if ( r.sourceNodeReference() == r.targetNodeReference() )
Expand All @@ -239,7 +239,7 @@ else if ( r.sourceNodeReference() == r.originNodeReference() )
d = Direction.INCOMING;
}

return computeKey( session.token().relationshipTypeName( r.label() ), d );
return computeKey( transaction.token().relationshipTypeName( r.label() ), d );
}

static String computeKey( String type, Direction direction )
Expand Down

0 comments on commit b30b310

Please sign in to comment.