Skip to content

Commit

Permalink
Support offset test graphs on block format (5.22+)
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwalker committed Jul 15, 2024
1 parent e2d9548 commit 04b79e3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@
import java.util.stream.Stream;

import static java.lang.String.format;
import static org.neo4j.gds.compat.InternalReadOps.countByIdGenerator;

public final class Neo4jProxy {

Expand Down Expand Up @@ -519,12 +518,15 @@ public static Long pageCacheMemoryValue(String value) {
}

public static long getHighestPossibleNodeCount(IdGeneratorFactory idGeneratorFactory) {
return countByIdGenerator(
idGeneratorFactory,
RecordIdType.NODE,
BlockFormat.INSTANCE.nodeType,
BlockFormat.INSTANCE.dynamicNodeType
);
return InternalReadOps.findValidIdGeneratorsStream(
idGeneratorFactory,
RecordIdType.NODE,
BlockFormat.INSTANCE.nodeType,
BlockFormat.INSTANCE.dynamicNodeType
)
.mapToLong(IdGenerator::getHighId)
.max()
.orElseThrow(InternalReadOps::unsupportedStoreFormatException);
}

public static long getHighestPossibleRelationshipCount(Read read) {
Expand Down Expand Up @@ -668,7 +670,13 @@ public static long transactionId(KernelTransaction kernelTransaction) {
}

public static void reserveNeo4jIds(IdGeneratorFactory generatorFactory, int size, CursorContext cursorContext) {
IdGenerator idGenerator = generatorFactory.get(RecordIdType.NODE);
var idGenerator = InternalReadOps.findValidIdGeneratorsStream(
generatorFactory,
RecordIdType.NODE,
BlockFormat.INSTANCE.nodeType,
BlockFormat.INSTANCE.dynamicNodeType
)
.findFirst().orElseThrow(InternalReadOps::unsupportedStoreFormatException);

idGenerator.nextConsecutiveIdRange(size, false, cursorContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,33 @@
import org.neo4j.internal.id.IdGeneratorFactory;
import org.neo4j.internal.id.IdType;

import java.util.OptionalLong;
import java.util.Arrays;
import java.util.stream.Stream;

public final class InternalReadOps {

public static long countByIdGenerator(
public static Stream<IdGenerator> findValidIdGeneratorsStream(
@Nullable IdGeneratorFactory idGeneratorFactory,
IdType... idTypes
) {
long highestId = Long.MIN_VALUE;
for (IdType idType : idTypes) {
final OptionalLong count = countByIdGenerator(idGeneratorFactory, idType);
if (count.isPresent()) {
highestId = Math.max(highestId, count.getAsLong());
}
}
if (highestId == Long.MIN_VALUE) {
throw new IllegalStateException(
"Unsupported store format for GDS; GDS cannot read data from this database. " +
"Please try to use Cypher projection instead.");
if (idGeneratorFactory == null || idTypes.length == 0) {
return Stream.empty();
}
return highestId;
}

private static OptionalLong countByIdGenerator(@Nullable IdGeneratorFactory idGeneratorFactory, @Nullable IdType idType) {
if (idGeneratorFactory != null && idType != null) {
return Arrays.stream(idTypes).mapMulti((idType, downstream) -> {
try {
final IdGenerator idGenerator = idGeneratorFactory.get(idType);
var idGenerator = idGeneratorFactory.get(idType);
if (idGenerator != null) {
// getHighId returns the highestId + 1, which is actually a count
return OptionalLong.of(idGenerator.getHighId());
downstream.accept(idGenerator);
}
} catch (Exception ignored) {
}
}
return OptionalLong.empty();
});
}

public static IllegalStateException unsupportedStoreFormatException() {
return new IllegalStateException(
"Unsupported store format for GDS; GDS cannot read data from this database. " +
"Please try to use Cypher projection instead.");
}

private InternalReadOps() {
Expand Down

0 comments on commit 04b79e3

Please sign in to comment.