Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
XDI4j Tutorial 8
{{#eclipseproject:technology.higgins|eclipse_custom_style.css}}
This tutorial explains how to work with persistent graphs.
Persistent graphs
So far we have only been working with the MemoryGraphFactory, which creates XDI graphs stored in memory.
Working with persistent XDI graphs is as simple as using a different graph factory, for example:
- XMLGraphFactory - Creates a persistent XDI graph backed by a file in XDI/XML serialization format.
- BDBGraphFactory - Creates a persistent XDI graph backed by a BDB database.
- HibernateGraphFactory - Creates a persistent XDI graph backed by Hibernate
Just like the MemoryGraphFactory, all of the above can be created and used either programmatically, or in the XDI4j server via the GraphMessagingTarget.
Example Code:
package org.eclipse.higgins.xdi4j.tutorial; import org.eclipse.higgins.xdi4j.Graph; import org.eclipse.higgins.xdi4j.addressing.Addressing; import org.eclipse.higgins.xdi4j.impl.xml.XMLGraphFactory; import org.eclipse.higgins.xdi4j.xri3.impl.XRI3; import org.eclipse.higgins.xdi4j.xri3.impl.XRI3Segment; /** * This example app shows how to work with an XDI graph backed * by an XDI/XML file. */ public class Tutorial8 { public static void main(String[] args) throws Exception { XMLGraphFactory factory = new XMLGraphFactory(); factory.setFilename("mygraph.xml"); Graph graph = factory.openGraph(); // see if the graph is filled already String data = Addressing.findLiteralData(graph, new XRI3("=alice/+name")); if (data == null) { graph.createStatement(new XRI3Segment("=alice"), new XRI3Segment("+name"), "Alice X."); System.out.println("Data created in graph."); } else { System.out.println("Data found in graph: " + data); } // don't forget to close the graph graph.close(); } }
To run the XDI4j server with an XDI endpoint backed by a BDB database, the following WEB-INF/application.xml file could be used:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> <bean id="graphfactory" class="org.eclipse.higgins.xdi4j.impl.keyvalue.bdb.BDBGraphFactory"> <property name="databasePath" value="./my-graph/" /> </bean> <bean id="mygraph" factory-bean="graphfactory" factory-method="openGraph" /> <bean name="/myendpoint" class="org.eclipse.higgins.xdi4j.messaging.server.impl.graph.GraphMessagingTarget"> <property name="graph" ref="mygraph" /> </bean> </beans>