Understand any codebase instantly. System intelligence for codebases, built for humans and AI.
TypeScript aiai-memoryclaude-codeclicode-analysiscode-mappingcode-memorycodebase-understandingcodexdeveloper-toolsllmopenclawpersistent-memoryprogram-analysissoftware-architecturesystem-intelligencesystem-mapping
feat: add ArangoDB client with schema initialization
riley0227acdaefc6f506
diff --git a/memory-layer/src/main/scala/ix/memory/db/ArangoClient.scala b/memory-layer/src/main/scala/ix/memory/db/ArangoClient.scalanew file mode 100644index 00000000..d4672f97--- /dev/null+++ b/memory-layer/src/main/scala/ix/memory/db/ArangoClient.scala@@ -0,0 +1,63 @@+package ix.memory.db++import cats.effect.{IO, Resource}+import com.arangodb.{ArangoDB, ArangoDatabase}+import com.fasterxml.jackson.databind.ObjectMapper+import io.circe.Json+import io.circe.parser.{parse => parseJson}++import scala.jdk.CollectionConverters._++class ArangoClient private (db: ArangoDatabase) {+ private val mapper = new ObjectMapper()++ private def toJson(value: AnyRef): Option[Json] =+ Option(value).flatMap(v => parseJson(mapper.writeValueAsString(v)).toOption)++ def query(aql: String, bindVars: Map[String, AnyRef] = Map.empty): IO[List[Json]] =+ IO.blocking {+ val javaVars = new java.util.HashMap[String, AnyRef]()+ bindVars.foreach { case (k, v) => javaVars.put(k, v) }+ val cursor = db.query(aql, classOf[AnyRef], javaVars)+ cursor.asListRemaining().asScala.toList.flatMap(toJson)+ }++ def queryOne(aql: String, bindVars: Map[String, AnyRef] = Map.empty): IO[Option[Json]] =+ query(aql, bindVars).map(_.headOption)++ def execute(aql: String, bindVars: Map[String, AnyRef] = Map.empty): IO[Unit] =+ IO.blocking {+ val javaVars = new java.util.HashMap[String, AnyRef]()+ bindVars.foreach { case (k, v) => javaVars.put(k, v) }+ db.query(aql, classOf[Void], javaVars)+ ()+ }++ def ensureSchema(): IO[Unit] = ArangoSchema.ensure(db)+}++object ArangoClient {+ def resource(+ host: String,+ port: Int,+ database: String,+ user: String,+ password: String+ ): Resource[IO, ArangoClient] =+ Resource.make(+ IO.blocking {+ val arango = new ArangoDB.Builder()+ .host(host, port)+ .user(user)+ .password(if (password.isEmpty) null else password)+ .build()+ val existingDbs = arango.getDatabases.asScala.toSet+ if (!existingDbs.contains(database)) {+ arango.createDatabase(database)+ }+ (arango, new ArangoClient(arango.db(database)))+ }+ ) { case (arango, _) =>+ IO.blocking(arango.shutdown())+ }.map(_._2)+}diff --git a/memory-layer/src/main/scala/ix/memory/db/ArangoSchema.scala b/memory-layer/src/main/scala/ix/memory/db/ArangoSchema.scalanew file mode 100644index 00000000..9539601c--- /dev/null+++ b/memory-layer/src/main/scala/ix/memory/db/ArangoSchema.scala@@ -0,0 +1,89 @@+package ix.memory.db++import cats.effect.IO+import com.arangodb.ArangoDatabase+import com.arangodb.entity.CollectionType+import com.arangodb.model.{CollectionCreateOptions, PersistentIndexOptions, TtlIndexOptions}++object ArangoSchema {++ def ensure(db: ArangoDatabase): IO[Unit] = IO.blocking {+ // Vertex collections+ ensureCollection(db, "nodes")+ ensureCollection(db, "claims")+ ensureCollection(db, "conflict_sets")+ ensureCollection(db, "patches")+ ensureCollection(db, "revisions")+ ensureCollection(db, "idempotency_keys")++ // Edge collection+ ensureCollection(db, "edges", edge = true)++ // Indexes — nodes+ val nodes = db.collection("nodes")+ nodes.ensurePersistentIndex(+ java.util.Arrays.asList("tenant", "kind"),+ new PersistentIndexOptions()+ )+ nodes.ensurePersistentIndex(+ java.util.Arrays.asList("tenant", "attrs.name"),+ new PersistentIndexOptions().sparse(true)+ )++ // Indexes — edges+ val edges = db.collection("edges")+ edges.ensurePersistentIndex(+ java.util.Arrays.asList("tenant", "src"),+ new PersistentIndexOptions()+ )+ edges.ensurePersistentIndex(+ java.util.Arrays.asList("tenant", "dst"),+ new PersistentIndexOptions()+ )+ edges.ensurePersistentIndex(+ java.util.Arrays.asList("tenant", "predicate"),+ new PersistentIndexOptions()+ )++ // Indexes — claims+ val claims = db.collection("claims")+ claims.ensurePersistentIndex(+ java.util.Arrays.asList("tenant", "entity_id"),+ new PersistentIndexOptions()+ )+ claims.ensurePersistentIndex(+ java.util.Arrays.asList("tenant", "status"),+ new PersistentIndexOptions()+ )++ // Indexes — patches+ val patches = db.collection("patches")+ patches.ensurePersistentIndex(+ java.util.Arrays.asList("tenant", "patch_id"),+ new PersistentIndexOptions().unique(true)+ )++ // Indexes — idempotency_keys+ val idem = db.collection("idempotency_keys")+ idem.ensurePersistentIndex(+ java.util.Arrays.asList("key"),+ new PersistentIndexOptions().unique(true)+ )+ idem.ensureTtlIndex(+ java.util.Arrays.asList("created_at"),+ new TtlIndexOptions().expireAfter(86400)+ )++ ()+ }++ private def ensureCollection(db: ArangoDatabase, name: String, edge: Boolean = false): Unit = {+ if (!db.collection(name).exists()) {+ if (edge) {+ db.createCollection(name, new CollectionCreateOptions().`type`(CollectionType.EDGES))+ } else {+ db.createCollection(name)+ }+ }+ }+}diff --git a/memory-layer/src/test/scala/ix/memory/db/ArangoClientSpec.scala b/memory-layer/src/test/scala/ix/memory/db/ArangoClientSpec.scalanew file mode 100644index 00000000..e9483358--- /dev/null+++ b/memory-layer/src/test/scala/ix/memory/db/ArangoClientSpec.scala@@ -0,0 +1,55 @@+package ix.memory.db++import cats.effect.IO+import cats.effect.testing.scalatest.AsyncIOSpec+import org.scalatest.flatspec.AsyncFlatSpec+import org.scalatest.matchers.should.Matchers+import io.circe.Json++class ArangoClientSpec extends AsyncFlatSpec with AsyncIOSpec with Matchers {++ // Requires ArangoDB running on localhost:8529+ val clientResource = ArangoClient.resource(+ host = "localhost", port = 8529,+ database = "ix_memory_test", user = "root", password = ""+ )++ "ArangoClient" should "connect and ensure schema" in {+ clientResource.use { client =>+ client.ensureSchema().map(_ => succeed)+ }+ }++ it should "execute a simple query" in {+ clientResource.use { client =>+ for {+ _ <- client.ensureSchema()+ results <- client.query("RETURN 1", Map.empty)+ } yield results.size shouldBe 1+ }+ }++ it should "insert and retrieve a document" in {+ clientResource.use { client =>+ for {+ _ <- client.ensureSchema()+ // Clean up any leftover document from previous runs+ _ <- client.execute(+ "FOR n IN nodes FILTER n._key == @key REMOVE n IN nodes",+ Map("key" -> "test1")+ )+ _ <- client.execute(+ "INSERT { _key: @key, name: @name } INTO nodes",+ Map("key" -> "test1", "name" -> "testNode")+ )+ result <- client.queryOne(+ "FOR n IN nodes FILTER n._key == @key RETURN n",+ Map("key" -> "test1")+ )+ } yield {+ result shouldBe defined+ result.get.hcursor.get[String]("name") shouldBe Right("testNode")+ }+ }+ }+}