public DelegatingTokenHolder( TokenCreator tokenCreator, String tokenType ) { super( new TokenRegistry( tokenType ) ); this.tokenCreator = tokenCreator; }
/** * Create and put new token in cache. * * @param name token name * @return newly created token id * @throws KernelException */ @Override protected synchronized int createToken( String name ) throws KernelException { Integer id = tokenRegistry.getId( name ); if ( id != null ) { return id; } id = tokenCreator.createToken( name ); try { tokenRegistry.put( new NamedToken( name, id ) ); } catch ( NonUniqueTokenException e ) { throw new IllegalStateException( "Newly created token should be unique.", e ); } return id; }
@Override public NamedToken getTokenById( int id ) throws TokenNotFoundException { NamedToken result = tokenRegistry.getToken( id ); if ( result == null ) { throw new TokenNotFoundException( "Token for id " + id ); } return result; }
@Test public void keepOriginalTokenWhenAddDuplicate() { TokenRegistry tokenCache = createTokenCache(); tokenCache.put( new NamedToken( INBOUND1_TYPE, 1 ) ); tokenCache.put( new NamedToken( INBOUND2_TYPE, 2 ) ); tryToAddDuplicate( tokenCache ); assertEquals( 1, tokenCache.getId( INBOUND1_TYPE ).intValue() ); assertEquals( 2, tokenCache.getId( INBOUND2_TYPE ).intValue() ); assertNull( tokenCache.getToken( 3 ) ); }
@Override public int getIdByName( String name ) { Integer id = tokenRegistry.getId( name ); if ( id == null ) { return NO_TOKEN; } return id; }
@Override public void addToken( NamedToken token ) throws NonUniqueTokenException { tokenRegistry.put( token ); }
public void putAll( List<NamedToken> tokens ) throws NonUniqueTokenException { Map<String, Integer> newNameToId = new HashMap<>(); Map<Integer, NamedToken> newIdToToken = new HashMap<>(); for ( NamedToken token : tokens ) { newIdToToken.put( token.id(), token ); putAndEnsureUnique( newNameToId, token ); } idToToken.putAll( newIdToToken ); nameToId.putAll( newNameToId ); }
public void setInitialTokens( List<NamedToken> tokens ) { nameToId.clear(); idToToken.clear(); putAll( tokens ); }
@Override public int size() { return tokenRegistry.size(); }
@Override public Iterable<NamedToken> getAllTokens() { return tokenRegistry.allTokens(); }
@Override public void setInitialTokens( List<NamedToken> tokens ) throws NonUniqueTokenException { tokenRegistry.setInitialTokens( tokens ); }
boolean resolveIds( String[] names, int[] ids, IntPredicate unresolvedIndexCheck ) { boolean foundUnresolvable = false; for ( int i = 0; i < ids.length; i++ ) { Integer id = tokenRegistry.getId( names[i] ); if ( id != null ) { ids[i] = id; } else { foundUnresolvable = true; if ( unresolvedIndexCheck.test( i ) ) { // If the check returns `true`, it's a signal that we should stop early. break; } } } return foundUnresolvable; } }
private void tryToAddDuplicate( TokenRegistry tokenCache ) { try { tokenCache.put( new NamedToken( INBOUND1_TYPE, 3 ) ); } catch ( NonUniqueTokenException ignored ) { } }
public void put( NamedToken token ) throws NonUniqueTokenException { idToToken.put( token.id(), token ); putAndEnsureUnique( nameToId, token ); }
private synchronized void createMissingTokens( String[] names, int[] ids ) { // We redo the resolving under the lock, to make sure that these ids are really missing, and won't be // created concurrently with us. MutableIntSet unresolvedIndexes = new IntHashSet(); resolveIds( names, ids, i -> !unresolvedIndexes.add( i ) ); if ( !unresolvedIndexes.isEmpty() ) { // We still have unresolved ids to create. ObjectIntHashMap<String> createdTokens = createUnresolvedTokens( unresolvedIndexes, names, ids ); List<NamedToken> createdTokensList = new ArrayList<>( createdTokens.size() ); createdTokens.forEachKeyValue( ( name, index ) -> createdTokensList.add( new NamedToken( name, ids[index] ) ) ); tokenRegistry.putAll( createdTokensList ); } }
@Override public int size() { return tokenRegistry.size(); }
@Override public Iterable<NamedToken> getAllTokens() { return tokenRegistry.allTokens(); }
@Override public void setInitialTokens( List<NamedToken> tokens ) throws NonUniqueTokenException { tokenRegistry.setInitialTokens( tokens ); }
/** * Create and put new token in cache. * * @param name token name * @return newly created token id * @throws KernelException */ @Override protected synchronized int createToken( String name ) throws KernelException { Integer id = tokenRegistry.getId( name ); if ( id != null ) { return id; } id = tokenCreator.createToken( name ); try { tokenRegistry.put( new NamedToken( name, id ) ); } catch ( NonUniqueTokenException e ) { throw new IllegalStateException( "Newly created token should be unique.", e ); } return id; }
@Override public int getOrCreateId( String name ) { Integer id = tokenRegistry.getId( name ); if ( id != null ) { return id; } // Let's create it try { return createToken( name ); } catch ( ReadOnlyDbException e ) { throw new TransactionFailureException( e.getMessage(), e ); } catch ( Throwable e ) { throw new TransactionFailureException( "Could not create token.", e ); } }