Last active
April 2, 2023 10:12
-
-
Save dacr/de1cba07c5372ebade3fc182f430c00f to your computer and use it in GitHub Desktop.
Create arangodb primes number dataset / published by https://github.com/dacr/code-examples-manager #5ab01592-db02-466f-bed3-282c1375d573/f49b7a06e5fe29734480ec5b0e754a46fd3c2aad
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 : Create arangodb primes number dataset | |
// 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 : 5ab01592-db02-466f-bed3-282c1375d573 | |
// 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.`fr.janalyse::primes:1.2.2` | |
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.Extraction.decompose | |
import org.json4s.JsonDSL._ | |
import scala.util.Properties.envOrElse | |
import fr.janalyse.primes._ | |
import scala.util.{Try,Success,Failure} | |
def now():Long = System.currentTimeMillis() | |
def howLong[T](processing: => T):(Try[T],Long) = { | |
val started = now() | |
(Try {processing}, now()-started) | |
} | |
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") | |
if (db.exists()) db.drop() | |
db.create() | |
implicit val formats = DefaultFormats.lossless // for milliseconds in iso8601 dates... | |
val primesNumberCollection = db.collection("primes_number") | |
if (!primesNumberCollection.exists()) primesNumberCollection.create() | |
val generator = new PrimesGenerator[Long] | |
val insertCount = 100_000 | |
val (result, duration) = howLong { | |
for { | |
v <- generator.checkedValues.take(insertCount) | |
} { | |
val jsonValue = | |
("_key" -> v.value.toString) ~ | |
("digitCount" -> v.digitCount) ~ | |
("value" -> v.value) ~ | |
("isPrime" -> v.isPrime) ~ | |
("nth" -> v.nth) | |
// https://github.com/json4s/json4s/issues/702 | |
// val jsonValue = decompose(v).merge(decompose("_key" -> v.value.toString)) | |
primesNumberCollection.insertDocument(write(jsonValue)) | |
} | |
val insertedRecords = primesNumberCollection.count().getCount() | |
assert( insertedRecords == insertCount, s"Some records are missing, only $insertedRecords records inserted for $insertCount expected") | |
} | |
System.out.println(s"$insertCount records inserted in $duration milliseconds") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment