Skip to content

Commit

Permalink
Simplified initialization of default graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorcorrea committed Jan 4, 2016
1 parent 98b9e9a commit a6468b3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
57 changes: 29 additions & 28 deletions ldp/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ func (node Node) String() string {
return node.uri
}

func (node *Node) appendTriple(predicate, object string) {
node.graph.AppendTriple2("<"+node.uri+">", predicate, object)
}

func (node *Node) setETag() {
node.graph.SetObject("<"+node.uri+">", etagPredicate, calculateEtag())
}

func GetNode(settings Settings, path string) (Node, error) {
node := newNode(settings, path)
err := node.loadNode(true)
Expand Down Expand Up @@ -268,41 +276,36 @@ func (node *Node) loadMeta() error {
return err
}

graph, err := rdf.StringToGraph(meta, node.uri)
node.graph, err = rdf.StringToGraph(meta, node.uri)
if err != nil {
return err
}

if graph.IsRdfSource("<" + node.uri + ">") {
node.setAsRdf(graph)
if node.graph.IsRdfSource("<" + node.uri + ">") {
node.setAsRdf()
} else {
node.setAsNonRdf(graph)
node.setAsNonRdf()
}
return nil
}

func (node *Node) writeNonRdfToDisk(reader io.ReadCloser) error {
subject := "<" + node.uri + ">"
graph := rdf.RdfGraph{}
graph.SetObject(subject, etagPredicate, calculateEtag())

graph.AppendTriple(rdf.NewTriple(subject, rdfTypePredicate, "<"+rdf.LdpResourceUri+">"))
graph.AppendTriple(rdf.NewTriple(subject, rdfTypePredicate, "<"+rdf.LdpNonRdfSourceUri+">"))

node.setAsNonRdf(graph)
node.graph = rdf.RdfGraph{}
node.setETag()
node.appendTriple(rdfTypePredicate, "<"+rdf.LdpResourceUri+">")
node.appendTriple(rdfTypePredicate, "<"+rdf.LdpNonRdfSourceUri+">")
node.setAsNonRdf()
return node.writeToDisk(reader)
}

func (node *Node) writeRdfToDisk(graph rdf.RdfGraph) error {
subject := "<" + node.uri + ">"
graph.SetObject(subject, etagPredicate, calculateEtag())

graph.AppendTriple(rdf.NewTriple(subject, rdfTypePredicate, "<"+rdf.LdpResourceUri+">"))
graph.AppendTriple(rdf.NewTriple(subject, rdfTypePredicate, "<"+rdf.LdpRdfSourceUri+">"))
graph.AppendTriple(rdf.NewTriple(subject, rdfTypePredicate, "<"+rdf.LdpContainerUri+">"))
graph.AppendTriple(rdf.NewTriple(subject, rdfTypePredicate, "<"+rdf.LdpBasicContainerUri+">"))

node.setAsRdf(graph)
node.graph = graph
node.setETag()
node.appendTriple(rdfTypePredicate, "<"+rdf.LdpResourceUri+">")
node.appendTriple(rdfTypePredicate, "<"+rdf.LdpRdfSourceUri+">")
node.appendTriple(rdfTypePredicate, "<"+rdf.LdpContainerUri+">")
node.appendTriple(rdfTypePredicate, "<"+rdf.LdpBasicContainerUri+">")
node.setAsRdf()
return node.writeToDisk(nil)
}

Expand Down Expand Up @@ -330,14 +333,13 @@ func (node *Node) writeToDisk(reader io.ReadCloser) error {
return err
}

func (node *Node) setAsRdf(graph rdf.RdfGraph) {
func (node *Node) setAsRdf() {
subject := "<" + node.uri + ">"
node.isRdf = true
node.graph = graph
node.headers = make(map[string][]string)
node.headers["Content-Type"] = []string{rdf.TurtleContentType}

if graph.IsBasicContainer(subject) {
if node.graph.IsBasicContainer(subject) {
// Is there a way to indicate that PUT is allowed
// for creation only (and not to overwrite?)
node.headers["Allow"] = []string{"GET, HEAD, POST, PUT, PATCH"}
Expand All @@ -351,23 +353,22 @@ func (node *Node) setAsRdf(graph rdf.RdfGraph) {

links := make([]string, 0)
links = append(links, rdf.LdpResourceLink)
if graph.IsBasicContainer(subject) {
if node.graph.IsBasicContainer(subject) {
node.isBasicContainer = true
links = append(links, rdf.LdpContainerLink)
links = append(links, rdf.LdpBasicContainerLink)
// TODO: validate membershipResource is a sub-URI of rootURI
node.membershipResource, node.hasMemberRelation, node.isDirectContainer = graph.GetDirectContainerInfo()
node.membershipResource, node.hasMemberRelation, node.isDirectContainer = node.graph.GetDirectContainerInfo()
if node.isDirectContainer {
links = append(links, rdf.LdpDirectContainerLink)
}
}
node.headers["Link"] = links
}

func (node *Node) setAsNonRdf(graph rdf.RdfGraph) {
func (node *Node) setAsNonRdf() {
// TODO Figure out a way to pass the binary as a stream
node.isRdf = false
node.graph = graph
node.binary = ""
node.headers = make(map[string][]string)
node.headers["Link"] = []string{rdf.LdpNonRdfSourceLink}
Expand Down
4 changes: 4 additions & 0 deletions rdf/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (graph *RdfGraph) AppendTriple(t Triple) bool {
return graph.appendTriple(t.subject, t.predicate, t.object, true)
}

func (graph *RdfGraph) AppendTriple2(subject, predicate, object string) bool {
return graph.appendTriple(subject, predicate, object, true)
}

func (graph RdfGraph) SubjectIs(subject, object string) bool {
if graph.HasTriple(subject, "a", object) {
return true
Expand Down
3 changes: 3 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"ldpserver/ldp"
"ldpserver/rdf"
"ldpserver/util"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -56,6 +57,7 @@ func TestCreateRdf(t *testing.T) {
func TestReplaceRdf(t *testing.T) {
triples := "<> xx:version \"version1\" ."
node, err := theServer.ReplaceRdfSource(triples, "/", "rdf-test", "ignore-etag")
log.Printf("1. %s", node.Content())
if err != nil {
t.Errorf("Error creating a new RDF node with replace: %s", err)
}
Expand All @@ -64,6 +66,7 @@ func TestReplaceRdf(t *testing.T) {
etag := node.Etag()
triples = "<> xx:version \"version2\" ."
node, err = theServer.ReplaceRdfSource(triples, "/", path, etag)
log.Printf("2. %s", node.Content())
if err != nil {
t.Errorf("Error replacing RDF node: %s", err)
}
Expand Down

0 comments on commit a6468b3

Please sign in to comment.