Skip to content

Commit

Permalink
Simplify, by decoupling TransactionType from AccessMode
Browse files Browse the repository at this point in the history
  • Loading branch information
craigtaverner committed Feb 27, 2016
1 parent 72b028b commit 77f9f8b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 59 deletions.
Expand Up @@ -21,6 +21,7 @@

import org.neo4j.graphdb.NotInTransactionException;
import org.neo4j.graphdb.TransactionTerminatedException;
import org.neo4j.graphdb.security.AuthorizationViolationException;
import org.neo4j.kernel.api.DataWriteOperations;
import org.neo4j.kernel.api.ReadOperations;
import org.neo4j.kernel.api.SchemaWriteOperations;
Expand Down Expand Up @@ -58,7 +59,11 @@ public KernelStatement( KernelTransactionImplementation transaction,
@Override
public ReadOperations readOperations()
{
transaction.verifyReadTransaction();
if( !transaction.mode().allowsReads() )
{
throw new AuthorizationViolationException(
String.format( "Read operations are not allowed for `%s` transactions.", transaction.mode().name() ) );
}
return facade;
}

Expand All @@ -72,15 +77,25 @@ public TokenWriteOperations tokenWriteOperations()
public DataWriteOperations dataWriteOperations()
throws InvalidTransactionTypeKernelException
{
transaction.verifyDataWriteTransaction();
if( !transaction.mode().allowsWrites() )
{
throw new AuthorizationViolationException(
String.format( "Write operations are not allowed for `%s` transactions.", transaction.mode().name() ) );
}
transaction.upgradeToDataTransaction();
return facade;
}

@Override
public SchemaWriteOperations schemaWriteOperations()
throws InvalidTransactionTypeKernelException
{
transaction.verifySchemaWriteTransaction();
if( !transaction.mode().allowsSchemaWrites() )
{
throw new AuthorizationViolationException(
String.format( "Schema operations are not allowed for `%s` transactions.", transaction.mode().name() ) );
}
transaction.upgradeToSchemaTransaction();
return facade;
}

Expand Down
Expand Up @@ -24,7 +24,6 @@
import java.util.function.Supplier;

import org.neo4j.collection.pool.Pool;
import org.neo4j.graphdb.security.AuthorizationViolationException;
import org.neo4j.helpers.Clock;
import org.neo4j.kernel.api.AccessMode;
import org.neo4j.kernel.api.KernelTransaction;
Expand Down Expand Up @@ -75,52 +74,34 @@ public class KernelTransactionImplementation implements KernelTransaction, TxSta

private enum TransactionType
{
NONE,
READ_ONLY,
READ_AND_DATA_WRITE
READ,
DATA
{
@Override
TransactionType enableSchemaWriteTransaction() throws InvalidTransactionTypeKernelException
TransactionType upgradeToSchemaTransaction() throws InvalidTransactionTypeKernelException
{
throw new InvalidTransactionTypeKernelException(
"Cannot perform schema updates in a transaction that has performed data updates." );
}

@Override
TransactionType enableReadTransaction()
{
return READ_AND_DATA_WRITE;
}
},
READ_AND_SCHEMA_WRITE
SCHEMA
{
@Override
TransactionType enableDataWriteTransaction() throws InvalidTransactionTypeKernelException
TransactionType upgradeToDataTransaction() throws InvalidTransactionTypeKernelException
{
throw new InvalidTransactionTypeKernelException(
"Cannot perform data updates in a transaction that has performed schema updates." );
}

@Override
TransactionType enableReadTransaction()
{
return READ_AND_SCHEMA_WRITE;
}
};

TransactionType enableReadTransaction()
{
return READ_ONLY;
}

TransactionType enableDataWriteTransaction() throws InvalidTransactionTypeKernelException
TransactionType upgradeToDataTransaction() throws InvalidTransactionTypeKernelException
{
return READ_AND_DATA_WRITE;
return DATA;
}

TransactionType enableSchemaWriteTransaction() throws InvalidTransactionTypeKernelException
TransactionType upgradeToSchemaTransaction() throws InvalidTransactionTypeKernelException
{
return READ_AND_SCHEMA_WRITE;
return SCHEMA;
}
}

Expand All @@ -146,11 +127,11 @@ TransactionType enableSchemaWriteTransaction() throws InvalidTransactionTypeKern
// whereas others, such as timestamp or txId when transaction starts, even locks, needs to be set in #initialize().
private TransactionState txState;
private LegacyIndexTransactionState legacyIndexTransactionState;
private TransactionType transactionType; // Tracks current state of transaction, which will upgrade to WRITE or READ_AND_SCHEMA_WRITE mode when necessary
private TransactionType transactionType; // Tracks current state of transaction, which will upgrade to WRITE or SCHEMA mode when necessary
private TransactionHooks.TransactionHooksState hooksState;
private KernelStatement currentStatement;
private CloseListener closeListener;
private AccessMode accessMode; // Defines whether a transaction is allowed to upgrade to WRITE or READ_AND_SCHEMA_WRITE mode
private AccessMode accessMode; // Defines whether a transaction/statement is allowed to perform read, write or schema commands
private Locks.Client locks;
private boolean beforeHookInvoked;
private boolean closing, closed;
Expand Down Expand Up @@ -199,7 +180,7 @@ public KernelTransactionImplementation initialize(
this.type = type;
this.locks = locks;
this.closing = closed = failure = success = terminated = beforeHookInvoked = false;
this.transactionType = TransactionType.NONE;
this.transactionType = TransactionType.READ;
this.startTimeMillis = clock.currentTimeMillis();
this.lastTransactionIdWhenStarted = lastCommittedTx;
this.transactionEvent = tracer.beginTransaction();
Expand Down Expand Up @@ -271,36 +252,15 @@ public void releaseStatement( Statement statement )
currentStatement = null;
}

public void verifyReadTransaction()
public void upgradeToDataTransaction() throws InvalidTransactionTypeKernelException
{
if( !accessMode.allowsReads() )
{
throw new AuthorizationViolationException(
String.format( "Read operations are not allowed for `%s` transactions.", accessMode.name() ) );
}

transactionType.enableReadTransaction();
}

public void verifyDataWriteTransaction() throws InvalidTransactionTypeKernelException
{
if( !accessMode.allowsWrites() )
{
throw new AuthorizationViolationException(
String.format( "Write operations are not allowed for `%s` transactions.", accessMode.name() ) );
}
transactionType = transactionType.enableDataWriteTransaction();
transactionType = transactionType.upgradeToDataTransaction();
}

public void verifySchemaWriteTransaction() throws InvalidTransactionTypeKernelException
public void upgradeToSchemaTransaction() throws InvalidTransactionTypeKernelException
{
if( !accessMode.allowsSchemaWrites() )
{
throw new AuthorizationViolationException(
String.format( "Schema write operations are not allowed for `%s` transactions.", accessMode.name() ) );
}
doUpgradeToSchemaTransaction();
transactionType = transactionType.enableSchemaWriteTransaction();
transactionType = transactionType.upgradeToSchemaTransaction();
}

public void doUpgradeToSchemaTransaction() throws InvalidTransactionTypeKernelException
Expand Down

0 comments on commit 77f9f8b

Please sign in to comment.