Skip to content

Commit

Permalink
Implement interval based identifier tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Hafner committed Dec 4, 2024
1 parent 2d137a7 commit 8d37e34
Show file tree
Hide file tree
Showing 10 changed files with 370 additions and 156 deletions.

This file was deleted.

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;
}
}
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.polypheny.db.ddl.DdlManager.FieldInformation;
import org.polypheny.db.type.PolyType;

public class EntityIdentifierUtils {
public class IdentifierUtils {

public static final String IDENTIFIER_KEY = "_eid";

Expand Down
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());
}

}
Loading

0 comments on commit 8d37e34

Please sign in to comment.