Last active
February 3, 2026 20:26
-
-
Save dacr/7c67f98d17a1348f6d0b52c351ba3416 to your computer and use it in GitHub Desktop.
check neo4j identifiers / published by https://github.com/dacr/code-examples-manager #36ab980c-dbfe-4024-8233-92277a007edc/7151ac77d68ff7654b0db9f04c4309afacc1cd46
This file contains hidden or 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
| // summary : check neo4j identifiers | |
| // keywords : scala, neo4j, cypher, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 36ab980c-dbfe-4024-8233-92277a007edc | |
| // created-on : 2024-06-19T17:54:42+02:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala "3.4.2" | |
| //> using dep "org.neo4j.test:neo4j-harness:5.20.0" | |
| //> using dep "org.neo4j.driver:neo4j-java-driver:5.21.0" | |
| //> using dep "org.wvlet.airframe::airframe-ulid:24.6.0" | |
| // --------------------- | |
| // :WARNING: | |
| // A node's id is unique, but note the following: Neo4j reuses its internal ids when nodes and relationships are deleted, | |
| // which means it's bad practice to refer to them this way. Instead, use application generated ids. | |
| // So don't use the neo4j's id in your system. | |
| // | |
| // => The function id() is deprecated. Use the function elementId() instead. | |
| import org.neo4j.driver.{AuthTokens, GraphDatabase} | |
| import org.neo4j.harness.Neo4jBuilders | |
| import wvlet.airframe.ulid.ULID | |
| import scala.util.Using | |
| import scala.util.Random.* | |
| import scala.util.chaining.* | |
| import scala.jdk.CollectionConverters.* | |
| val fixtureAdmin = "CREATE INDEX person_index FOR (n:Person) ON (n.myid)" | |
| val timestamps = shuffle(0.to(10000).by(100).toList) | |
| val fixtureInject = | |
| timestamps | |
| .map(ts => (ts, ULID.ofMillis(ts))) | |
| .map((ts, ulid) => s"CREATE (:Person {myid: '$ulid', name: 'joe#$ts'})") | |
| .mkString("\n") | |
| val builder = | |
| Neo4jBuilders | |
| .newInProcessBuilder() | |
| .withFixture(fixtureAdmin) | |
| .withFixture(fixtureInject) | |
| Using(builder.build()) { embedded => | |
| Using(GraphDatabase.driver(embedded.boltURI(), AuthTokens.none())) { driver => | |
| Using(driver.session()) { session => | |
| val query = "MATCH (n) RETURN n.myid AS myid, id(n) AS id, elementId(n) AS eid ORDER BY myid" | |
| val response = session.run(query) | |
| val identifiers = response.list().asScala.toList.map { record => | |
| (record.get("myid").asString(), record.get("id").asInt(), record.get("eid").asString()) | |
| } | |
| identifiers.foreach((myid, id, eid) => println(s"myid=$myid eid=$eid id=$id")) | |
| val ulids = identifiers.map((myid, id, eid) => ULID(myid)) | |
| val epochs = ulids.map(_.epochMillis) | |
| if (epochs != timestamps.sorted) throw Exception("INVALID ORDER") | |
| println(s"'$query' : count = ${identifiers.size}") | |
| println("Warning : neo4j records identifiers are managed by neo4j and should not be used") | |
| println("Warning : neo4j id() is deprecated use elementId() instead") | |
| }.tap(r => println(r)) | |
| }.tap(r => println(r)) | |
| }.tap(r => println(r)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment