Skip to content

Instantly share code, notes, and snippets.

@mikeycmccarthy
Created March 9, 2012 16:53
Show Gist options
  • Save mikeycmccarthy/2007476 to your computer and use it in GitHub Desktop.
Save mikeycmccarthy/2007476 to your computer and use it in GitHub Desktop.
Uniqueness Problem
// NodeManager
public <T extends PropertyContainer> T indexPutIfAbsent( Index<T> index, T entity, String key, Object value )
{
T existing = index.get( key, value ).getSingle();
if ( existing != null ) return existing;
// Grab lock
IndexLock lock = new IndexLock( index.getName(), key );
LockType.WRITE.acquire( lock, lockManager ); // 1.
try
{
// Check again
existing = index.get( key, value ).getSingle();
if ( existing != null )
{
LockType.WRITE.release( lock, lockManager );
return existing;
}
// Add
index.add( entity, key, value ); // 2
return null;
}
finally
{
if ( existing == null ) LockType.WRITE.unacquire( lock, lockManager, lockReleaser ); // 5
}
// LuceneIndex
public void add( T entity, String key, Object value )
{
LuceneXaConnection connection = getConnection(); // 3
assertValidKey( key );
for ( Object oneValue : IoPrimitiveUtils.asArray( value ) )
{
connection.add( this, entity, key, oneValue ); // 4
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment