-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement interval based identifier tracking
- Loading branch information
Tobias Hafner
committed
Dec 4, 2024
1 parent
2d137a7
commit 8d37e34
Showing
10 changed files
with
370 additions
and
156 deletions.
There are no files selected for viewing
129 changes: 0 additions & 129 deletions
129
core/src/main/java/org/polypheny/db/transaction/locking/EntityIdentifierGenerator.java
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
core/src/main/java/org/polypheny/db/transaction/locking/IdentifierInterval.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2019-2024 The Polypheny Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.polypheny.db.transaction.locking; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class IdentifierInterval implements Comparable<IdentifierInterval> { | ||
private long lowerBound; | ||
private long upperBound; | ||
|
||
IdentifierInterval(long lowerBound, long upperBound) { | ||
if (upperBound < lowerBound) { | ||
throw new IllegalArgumentException("Upper bound must be greater or equal than lower bound"); | ||
} | ||
this.lowerBound = lowerBound; | ||
this.upperBound = upperBound; | ||
} | ||
|
||
@Override | ||
public int compareTo( IdentifierInterval other ) { | ||
return Long.compare( this.lowerBound, other.lowerBound ); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "IdentifierInterval{" + lowerBound + ", " + upperBound + "}"; | ||
} | ||
|
||
public long getNextIdentifier() { | ||
long identifier = lowerBound; | ||
lowerBound++; | ||
return identifier; | ||
} | ||
|
||
public boolean hasNextIdentifier() { | ||
return lowerBound < upperBound; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
core/src/main/java/org/polypheny/db/transaction/locking/IdentifierRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.polypheny.db.transaction.locking; | ||
|
||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
public class IdentifierRegistry { | ||
|
||
private static final Long MAX_IDENTIFIER_VALUE = Long.MAX_VALUE; | ||
public static final IdentifierRegistry INSTANCE = new IdentifierRegistry(MAX_IDENTIFIER_VALUE); | ||
|
||
private final TreeSet<IdentifierInterval> availableIdentifiers; | ||
|
||
IdentifierRegistry( long maxIdentifierValue ) { | ||
this.availableIdentifiers = new TreeSet<>(); | ||
this.availableIdentifiers.add( new IdentifierInterval( 0, maxIdentifierValue ) ); | ||
} | ||
|
||
|
||
public long getEntryIdentifier() { | ||
while ( !availableIdentifiers.first().hasNextIdentifier() ) { | ||
availableIdentifiers.pollFirst(); | ||
if ( availableIdentifiers.isEmpty() ) { | ||
throw new IllegalStateException( "No identifiers available" ); | ||
} | ||
} | ||
return availableIdentifiers.first().getNextIdentifier(); | ||
} | ||
|
||
|
||
public void releaseEntryIdentifiers( Set<Long> identifiers ) { | ||
if ( identifiers.isEmpty() ) { | ||
return; | ||
} | ||
|
||
for ( long currentIdentifier : identifiers ) { | ||
IdentifierInterval newInterval = new IdentifierInterval( currentIdentifier, currentIdentifier + 1 ); | ||
|
||
IdentifierInterval lowerAdjacentInterval = availableIdentifiers.floor( newInterval ); | ||
IdentifierInterval upperAdjacentInterval = availableIdentifiers.ceiling( newInterval ); | ||
|
||
boolean isMergedWithLower = mergeWithLowerInterval( lowerAdjacentInterval, currentIdentifier ); | ||
boolean isMergedWithUpper = mergeWithUpperInterval( lowerAdjacentInterval, upperAdjacentInterval, currentIdentifier, isMergedWithLower ); | ||
|
||
if ( isMergedWithLower || isMergedWithUpper ) { | ||
continue; | ||
} | ||
availableIdentifiers.add( newInterval ); | ||
} | ||
} | ||
|
||
|
||
private boolean mergeWithLowerInterval( IdentifierInterval lowerInterval, long currentIdentifier ) { | ||
if ( lowerInterval == null ) { | ||
return false; | ||
} | ||
if ( lowerInterval.getUpperBound() != currentIdentifier ) { | ||
return false; | ||
} | ||
lowerInterval.setUpperBound( currentIdentifier + 1 ); | ||
return true; | ||
} | ||
|
||
|
||
private boolean mergeWithUpperInterval( IdentifierInterval lowerInterval, IdentifierInterval upperInterval, long currentIdentifier, boolean isMergedWithLower ) { | ||
if ( upperInterval == null ) { | ||
return false; | ||
} | ||
if ( upperInterval.getLowerBound() != currentIdentifier + 1 ) { | ||
return false; | ||
} | ||
if ( !isMergedWithLower ) { | ||
upperInterval.setLowerBound( currentIdentifier ); | ||
return true; | ||
} | ||
if ( lowerInterval == null ) { | ||
return true; | ||
} | ||
lowerInterval.setUpperBound( upperInterval.getUpperBound() ); | ||
availableIdentifiers.remove( upperInterval ); | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
core/src/test/java/org/polypheny/db/transaction/locking/IdentifierIntervalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2019-2024 The Polypheny Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.polypheny.db.transaction.locking; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class IdentifierIntervalTest { | ||
|
||
@Test | ||
void testCreatingIntervalLowerBound() { | ||
IdentifierInterval interval = new IdentifierInterval(10, 20); | ||
assertEquals(10, interval.getLowerBound()); | ||
} | ||
|
||
@Test | ||
void testCreatingIntervalUpperBound() { | ||
IdentifierInterval interval = new IdentifierInterval(10, 20); | ||
assertEquals(20, interval.getUpperBound()); | ||
} | ||
|
||
@Test | ||
void testCreatingIntervalInvalidBounds() { | ||
Exception exception = assertThrows(IllegalArgumentException.class, () -> { | ||
new IdentifierInterval(20, 10); | ||
}); | ||
assertTrue(exception.getMessage().contains("Upper bound must be greater or equal than lower bound")); | ||
} | ||
|
||
@Test | ||
void testHasNextIdentifier() { | ||
IdentifierInterval interval = new IdentifierInterval(10, 20); | ||
assertTrue(interval.hasNextIdentifier()); | ||
} | ||
|
||
@Test | ||
void testRetrievingIdentifiersUntilEmpty() { | ||
IdentifierInterval interval = new IdentifierInterval(10, 12); | ||
assertEquals(10, interval.getNextIdentifier()); | ||
assertEquals(11, interval.getNextIdentifier()); | ||
assertFalse(interval.hasNextIdentifier()); | ||
} | ||
|
||
@Test | ||
void testIntervalComparisonLess() { | ||
IdentifierInterval interval1 = new IdentifierInterval(5, 10); | ||
IdentifierInterval interval2 = new IdentifierInterval(10, 15); | ||
assertTrue(interval1.compareTo(interval2) < 0, "Interval1 should be less than Interval2"); | ||
} | ||
|
||
@Test | ||
void testIntervalComparisonGreater() { | ||
IdentifierInterval interval1 = new IdentifierInterval(5, 10); | ||
IdentifierInterval interval2 = new IdentifierInterval(1, 5); | ||
assertTrue(interval1.compareTo(interval2) > 0, "Interval1 should be greater than Interval2"); | ||
} | ||
|
||
@Test | ||
void testIntervalComparisonEquals() { | ||
IdentifierInterval interval1 = new IdentifierInterval(5, 10); | ||
IdentifierInterval interval2 = new IdentifierInterval(5, 10); | ||
assertEquals(0, interval1.compareTo(interval2)); | ||
} | ||
|
||
@Test | ||
void testIntervalComparisonIncludes() { | ||
IdentifierInterval interval1 = new IdentifierInterval(5, 10); | ||
IdentifierInterval interval2 = new IdentifierInterval(1, 20); | ||
assertTrue( interval1.compareTo( interval2 ) > 0, "Interval1 should be greater than Interval2"); | ||
} | ||
|
||
@Test | ||
void testIntervalToString() { | ||
IdentifierInterval interval1 = new IdentifierInterval(5, 10); | ||
assertEquals("IdentifierInterval{5, 10}", interval1.toString()); | ||
} | ||
|
||
} |
Oops, something went wrong.