
org.neo4j.kernel.impl.store.HighestTransactionId Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neo4j-kernel Show documentation
Show all versions of neo4j-kernel Show documentation
Neo4j kernel is a lightweight, embedded Java database designed to
store data structured as graphs rather than tables. For more
information, see http://neo4j.org.
/*
* Copyright (c) 2002-2015 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.neo4j.kernel.impl.store;
import java.util.concurrent.atomic.AtomicReference;
/**
* Can accept offerings about {@link TransactionId}, but will always only keep the highest one,
* always available in {@link #get()}.
*/
public class HighestTransactionId
{
private final AtomicReference highest = new AtomicReference<>();
public HighestTransactionId( long initialTransactionId, long initialChecksum )
{
set( initialTransactionId, initialChecksum );
}
/**
* Offers a transaction id. Will be accepted if this is higher than the current highest.
* This method is thread-safe.
*
* @param transactionId transaction id to compare for highest.
* @param checksum checksum of the transaction.
* @return {@code true} if the given transaction id was higher than the current highest,
* {@code false}.
*/
public boolean offer( long transactionId, long checksum )
{
TransactionId high = highest.get();
if ( transactionId < high.transactionId() )
{ // a higher id has already been offered
return false;
}
TransactionId update = new TransactionId( transactionId, checksum );
while ( !highest.compareAndSet( high, update ) )
{
high = highest.get();
if ( high.transactionId() >= transactionId )
{ // apparently someone else set a higher id while we were trying to set this id
return false;
}
}
// we set our id as the highest
return true;
}
/**
* Overrides the highest transaction id value, no matter what it currently is. Used for initialization purposes.
*
* @param transactionId id of the transaction.
* @param checksum checksum of the transaction.
*/
public void set( long transactionId, long checksum )
{
highest.set( new TransactionId( transactionId, checksum ) );
}
/**
* @return the currently highest transaction together with its checksum.
*/
public TransactionId get()
{
return highest.get();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy