Skip to content

Commit

Permalink
Define token type specifically rather than deriving it from the
Browse files Browse the repository at this point in the history
class name
  • Loading branch information
Mark Needham committed Feb 23, 2016
1 parent c0743a0 commit c0f6f45
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 15 deletions.
Expand Up @@ -27,4 +27,10 @@ public DelegatingLabelTokenHolder( TokenCreator tokenCreator )
{ {
super( tokenCreator, new Token.Factory() ); super( tokenCreator, new Token.Factory() );
} }

@Override
protected String tokenType()
{
return "Label";
}
} }
Expand Up @@ -27,4 +27,10 @@ public DelegatingPropertyKeyTokenHolder( TokenCreator tokenCreator )
{ {
super( tokenCreator, new Token.Factory() ); super( tokenCreator, new Token.Factory() );
} }

@Override
protected String tokenType()
{
return "PropertyKey";
}
} }
Expand Up @@ -25,4 +25,10 @@ public DelegatingRelationshipTypeTokenHolder( TokenCreator tokenCreator )
{ {
super( tokenCreator, new RelationshipTypeToken.Factory() ); super( tokenCreator, new RelationshipTypeToken.Factory() );
} }

@Override
protected String tokenType()
{
return "RelationshipType";
}
} }
Expand Up @@ -33,9 +33,12 @@
* When asked for a token that isn't in the cache, delegates to a TokenCreator to create the token, * When asked for a token that isn't in the cache, delegates to a TokenCreator to create the token,
* then stores it in the cache. * then stores it in the cache.
*/ */
public class DelegatingTokenHolder<TOKEN extends Token> extends LifecycleAdapter implements TokenHolder<TOKEN> public abstract class DelegatingTokenHolder<TOKEN extends Token> extends LifecycleAdapter implements TokenHolder<TOKEN>
{ {
protected InMemoryTokenCache<TOKEN> tokenCache = new InMemoryTokenCache<>( this.getClass() ); protected InMemoryTokenCache<TOKEN> tokenCache = new InMemoryTokenCache<>( tokenType() );

protected abstract String tokenType();

private final TokenCreator tokenCreator; private final TokenCreator tokenCreator;
private final TokenFactory<TOKEN> tokenFactory; private final TokenFactory<TOKEN> tokenFactory;


Expand Down
Expand Up @@ -30,11 +30,11 @@ public class InMemoryTokenCache<TOKEN extends Token>
{ {
private final Map<String, Integer> nameToId = new CopyOnWriteHashMap<>(); private final Map<String, Integer> nameToId = new CopyOnWriteHashMap<>();
private final Map<Integer, TOKEN> idToToken = new CopyOnWriteHashMap<>(); private final Map<Integer, TOKEN> idToToken = new CopyOnWriteHashMap<>();
private final Class<? extends TokenHolder> owningClass; private final String tokenType;


public InMemoryTokenCache( Class<? extends TokenHolder> owningClass ) public InMemoryTokenCache( String tokenType )
{ {
this.owningClass = owningClass; this.tokenType = tokenType;
} }


public void clear() public void clear()
Expand All @@ -43,12 +43,12 @@ public void clear()
idToToken.clear(); idToToken.clear();
} }


private static void putAndEnsureUnique( Map<String,Integer> nameToId, Token token, Class<? extends TokenHolder> owningClass ) private static void putAndEnsureUnique( Map<String,Integer> nameToId, Token token, String tokenType )
{ {
Integer previous; Integer previous;
if ( (previous = nameToId.put( token.name(), token.id() )) != null && previous != token.id() ) if ( (previous = nameToId.put( token.name(), token.id() )) != null && previous != token.id() )
{ {
throw new NonUniqueTokenException( owningClass, token.name(), token.id(), previous ); throw new NonUniqueTokenException( tokenType, token.name(), token.id(), previous );
} }
} }


Expand All @@ -59,7 +59,7 @@ public void putAll( List<TOKEN> tokens ) throws NonUniqueTokenException


for ( TOKEN token : tokens ) for ( TOKEN token : tokens )
{ {
putAndEnsureUnique( newNameToId, token, owningClass ); putAndEnsureUnique( newNameToId, token, tokenType );
newIdToToken.put( token.id(), token ); newIdToToken.put( token.id(), token );
} }


Expand All @@ -69,7 +69,7 @@ public void putAll( List<TOKEN> tokens ) throws NonUniqueTokenException


public void put( TOKEN token ) throws NonUniqueTokenException public void put( TOKEN token ) throws NonUniqueTokenException
{ {
putAndEnsureUnique( nameToId, token, owningClass ); putAndEnsureUnique( nameToId, token, tokenType );
idToToken.put( token.id(), token ); idToToken.put( token.id(), token );
} }


Expand Down
Expand Up @@ -26,9 +26,9 @@
*/ */
public class NonUniqueTokenException extends RuntimeException public class NonUniqueTokenException extends RuntimeException
{ {
public NonUniqueTokenException( Class<? extends TokenHolder> holder, String tokenName, int tokenId, int existingId ) public NonUniqueTokenException( String tokenType, String tokenName, int tokenId, int existingId )
{ {
super( String.format( "The %s \"%s\" is not unique, it existed with id=%d before being added with id=%d.", super( String.format( "The %s \"%s\" is not unique, it existed with id=%d before being added with id=%d.",
holder.getSimpleName().replace( "Delegating", "" ).replace( "TokenHolder", "" ), tokenName, existingId, tokenId ) ); tokenType, tokenName, existingId, tokenId ) );
} }
} }
Expand Up @@ -38,7 +38,13 @@ public void shouldClearTokensAsPartOfInitialTokenLoading() throws Exception
{ {
// GIVEN // GIVEN
TokenCreator creator = mock( TokenCreator.class ); TokenCreator creator = mock( TokenCreator.class );
TokenHolder<Token> holder = new DelegatingTokenHolder<Token>( creator, new Token.Factory() ) {}; TokenHolder<Token> holder = new DelegatingTokenHolder<Token>( creator, new Token.Factory() ) {
@Override
protected String tokenType()
{
return "Dummy";
}
};
holder.setInitialTokens( holder.setInitialTokens(
asList( token( "one", 1 ), asList( token( "one", 1 ),
token( "two", 2 ) )); token( "two", 2 ) ));
Expand Down
Expand Up @@ -38,6 +38,12 @@ public ReplicatedLabelTokenHolder( Replicator replicator, IdGeneratorFactory idG
dependencies, new Token.Factory(), TokenType.LABEL, timeoutMillis, logProvider ); dependencies, new Token.Factory(), TokenType.LABEL, timeoutMillis, logProvider );
} }


@Override
protected String tokenType()
{
return "Label";
}

@Override @Override
protected void createToken( TransactionState txState, String tokenName, int tokenId ) protected void createToken( TransactionState txState, String tokenName, int tokenId )
{ {
Expand Down
Expand Up @@ -38,6 +38,12 @@ public ReplicatedPropertyKeyTokenHolder( Replicator replicator, IdGeneratorFacto
dependencies, new Token.Factory(), TokenType.PROPERTY, timeoutMillis, logProvider ); dependencies, new Token.Factory(), TokenType.PROPERTY, timeoutMillis, logProvider );
} }


@Override
protected String tokenType()
{
return "PropertyKey";
}

@Override @Override
protected void createToken( TransactionState txState, String tokenName, int tokenId ) protected void createToken( TransactionState txState, String tokenName, int tokenId )
{ {
Expand Down
Expand Up @@ -38,6 +38,12 @@ public ReplicatedRelationshipTypeTokenHolder( Replicator replicator, IdGenerator
new RelationshipTypeToken.Factory(), TokenType.RELATIONSHIP, timeoutMillis, logProvider ); new RelationshipTypeToken.Factory(), TokenType.RELATIONSHIP, timeoutMillis, logProvider );
} }


@Override
protected String tokenType()
{
return "RelationshipType";
}

@Override @Override
protected void createToken( TransactionState txState, String tokenName, int tokenId ) protected void createToken( TransactionState txState, String tokenName, int tokenId )
{ {
Expand Down
Expand Up @@ -43,13 +43,11 @@
import org.neo4j.kernel.impl.core.TokenHolder; import org.neo4j.kernel.impl.core.TokenHolder;
import org.neo4j.kernel.impl.core.TokenNotFoundException; import org.neo4j.kernel.impl.core.TokenNotFoundException;
import org.neo4j.kernel.impl.locking.LockGroup; import org.neo4j.kernel.impl.locking.LockGroup;
import org.neo4j.kernel.impl.store.TokenStore;
import org.neo4j.kernel.impl.store.id.IdGeneratorFactory; import org.neo4j.kernel.impl.store.id.IdGeneratorFactory;
import org.neo4j.kernel.impl.store.id.IdType; import org.neo4j.kernel.impl.store.id.IdType;
import org.neo4j.kernel.impl.store.record.TokenRecord; import org.neo4j.kernel.impl.store.record.TokenRecord;
import org.neo4j.kernel.impl.transaction.command.Command; import org.neo4j.kernel.impl.transaction.command.Command;
import org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation; import org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation;
import org.neo4j.kernel.impl.transaction.state.RecordAccess;
import org.neo4j.kernel.impl.transaction.tracing.CommitEvent; import org.neo4j.kernel.impl.transaction.tracing.CommitEvent;
import org.neo4j.kernel.impl.util.Dependencies; import org.neo4j.kernel.impl.util.Dependencies;
import org.neo4j.kernel.impl.util.collection.NoSuchEntryException; import org.neo4j.kernel.impl.util.collection.NoSuchEntryException;
Expand Down Expand Up @@ -95,10 +93,12 @@ public ReplicatedTokenHolder( Replicator replicator, IdGeneratorFactory idGenera
this.tokenFactory = tokenFactory; this.tokenFactory = tokenFactory;
this.type = type; this.type = type;
this.timeoutMillis = timeoutMillis; this.timeoutMillis = timeoutMillis;
this.tokenCache = new InMemoryTokenCache<>( this.getClass() ); this.tokenCache = new InMemoryTokenCache<>(tokenType() );
this.log = logProvider.getLog( getClass() ); this.log = logProvider.getLog( getClass() );
} }


protected abstract String tokenType();

@Override @Override
public void start() public void start()
{ {
Expand Down

0 comments on commit c0f6f45

Please sign in to comment.