Last active
April 2, 2023 10:11
-
-
Save dacr/aa6a36634327e60326668e98ca8e4add to your computer and use it in GitHub Desktop.
arangodb primes number queries / published by https://github.com/dacr/code-examples-manager #516f62c0-c91b-4b22-808b-46926dc5d3b2/da0f36f1781ccdca7e0db1727568b1bbca2e9b18
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 : arangodb primes number queries | |
// keywords : arangodb, graphdb, javadriver, primes-number, json4s, @testable | |
// publish : gist, corporate | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 516f62c0-c91b-4b22-808b-46926dc5d3b2 | |
// created-on : 2021-03-05T09:25:00Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' | |
import $ivy.`com.arangodb:arangodb-java-driver:6.6.3` | |
import $ivy.`org.json4s::json4s-native:3.6.9` | |
import $ivy.`org.json4s::json4s-ext:3.6.9` | |
import $ivy.`org.slf4j:slf4j-nop:1.7.30` // to avoid missing logging impl warning at startup | |
import com.arangodb._ | |
import com.arangodb.entity._ | |
import com.arangodb.model.AqlQueryOptions | |
import com.arangodb.util.MapBuilder | |
import org.json4s.DefaultFormats | |
import org.json4s.native.Serialization.{read, write} | |
import org.json4s.native.JsonMethods.{parse} | |
import org.json4s.Extraction.decompose | |
import org.json4s.JsonDSL._ | |
import scala.util.Properties.envOrElse | |
import scala.util.{Try, Success, Failure} | |
import scala.jdk.CollectionConverters._ | |
implicit val formats = DefaultFormats.lossless // for milliseconds in iso8601 dates... | |
def now(): Long = System.currentTimeMillis() | |
def howLong[T](processing: => T): (Try[T], Long) = { | |
val started = now() | |
(Try { | |
processing | |
}, now() - started) | |
} | |
def howLongReport[T](text: String)(processing: => T): Unit = { | |
val (response, duration) = howLong(processing) | |
response match { | |
case Success(result) => println(s"Success after ${duration}ms : $result $text") | |
case Failure(exception) => println(s"Failure after ${duration}ms : $text") | |
} | |
} | |
val arango = { | |
new ArangoDB | |
.Builder() | |
.host(envOrElse("ARANGODB_HOST", "127.0.0.1"), envOrElse("ARANGODB_PORT", "8529").toInt) | |
.user(envOrElse("ARANGODB_USERNAME", "")) | |
.password(envOrElse("ARANGODB_PASSWORD", "")) | |
.build() | |
} | |
val db = arango.db("primes") | |
val primesNumberCollection = db.collection("primes_number") | |
assert(db.exists(), "please create and feed the primes database first") | |
assert(primesNumberCollection.exists(), "please create and feed the primes number collection first") | |
val count = primesNumberCollection.count() | |
def vars(args:(String, Object)*):java.util.Map[String,Object] = args.toMap.asJava | |
case class CheckedValue(value: Long, isPrime: Boolean, digitCount: Long, nth: Long) | |
// -------------------------------------------------------------------------------------- | |
howLongReport("primes number in the database") { | |
val response: ArangoCursor[String] = db.query( | |
""" | |
|FOR p IN primes_number | |
|FILTER p.isPrime == true | |
|RETURN p | |
|""".stripMargin, | |
vars(), | |
classOf[String] | |
) | |
// This is an iterator, so take care | |
val checkedValues = { | |
response | |
.asInstanceOf[java.util.Iterator[String]] | |
.asScala | |
.map(json => parse(json).extract[CheckedValue]) | |
} | |
checkedValues.size | |
} | |
// -------------------------------------------------------------------------------------- | |
howLongReport("primes number < 10000") { | |
val response: ArangoCursor[String] = db.query( | |
""" | |
|FOR p IN primes_number | |
|FILTER p.isPrime == true and p.value < 10000 | |
|RETURN p | |
|""".stripMargin, | |
vars(), | |
classOf[String] | |
) | |
// This is an iterator, so take care | |
val checkedValues = { | |
response | |
.asInstanceOf[java.util.Iterator[String]] | |
.asScala | |
.map(json => parse(json).extract[CheckedValue]) | |
} | |
checkedValues.size | |
} | |
// -------------------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment