-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from dc2f/nodetypeapi
Userfriendly API for Node Types solves #1
- Loading branch information
Showing
15 changed files
with
536 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
12 changes: 10 additions & 2 deletions
12
src/main/java/com/dc2f/dstore/hierachynodestore/WorkingTree.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
package com.dc2f.dstore.hierachynodestore; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import com.dc2f.dstore.hierachynodestore.nodetype.NodeTypeAccessor; | ||
|
||
|
||
public interface WorkingTree { | ||
WorkingTreeNode getRootNode(); | ||
public static final @Nonnull String NAME_NODETYPE = ":nodetype"; | ||
|
||
@Nonnull WorkingTreeNode getRootNode(); | ||
|
||
Commit commit(String message); | ||
@Nonnull Commit commit(String message); | ||
|
||
@Nonnull NodeTypeAccessor getNodeTypeAccessor(); | ||
} |
This file contains 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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/dc2f/dstore/hierachynodestore/exception/NodeStoreException.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.dc2f.dstore.hierachynodestore.exception; | ||
|
||
/** | ||
* generic exception from your node store. | ||
*/ | ||
public class NodeStoreException extends RuntimeException { | ||
private static final long serialVersionUID = 1L; | ||
|
||
|
||
public NodeStoreException(String message) { | ||
this(message, null); | ||
} | ||
public NodeStoreException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains 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
This file contains 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
96 changes: 96 additions & 0 deletions
96
src/main/java/com/dc2f/dstore/hierachynodestore/impl/nodetype/NodeTypeAccessorImpl.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.dc2f.dstore.hierachynodestore.impl.nodetype; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
import com.dc2f.dstore.hierachynodestore.WorkingTree; | ||
import com.dc2f.dstore.hierachynodestore.WorkingTreeNode; | ||
import com.dc2f.dstore.hierachynodestore.exception.NodeStoreException; | ||
import com.dc2f.dstore.hierachynodestore.impl.WorkingTreeImpl; | ||
import com.dc2f.dstore.hierachynodestore.nodetype.NodeTypeAccessor; | ||
import com.dc2f.dstore.hierachynodestore.nodetype.NodeTypeDefinition; | ||
import com.dc2f.dstore.storage.Property; | ||
import com.google.common.collect.Iterables; | ||
|
||
import static com.dc2f.utils.NullUtils.assertNotNull; | ||
|
||
// FIXME: We currently create a new NodeTypeDefinitionImpl instance for each accessor call, maybe we should cache it internally? | ||
public class NodeTypeAccessorImpl implements NodeTypeAccessor { | ||
private @Nonnull WorkingTreeImpl workingTreeImpl; | ||
private @Nullable WorkingTreeNode rootNode; | ||
|
||
public NodeTypeAccessorImpl(@Nonnull WorkingTreeImpl workingTreeImpl) { | ||
this.workingTreeImpl = workingTreeImpl; | ||
} | ||
|
||
@Override | ||
public @Nonnull Collection<NodeTypeDefinition> listNodeTypeDefinitions() { | ||
WorkingTreeNode child = getNodeTypeRootNode(); | ||
|
||
List<NodeTypeDefinition> nodeTypeDefs = new ArrayList<>(); | ||
for (WorkingTreeNode nodeTypeChild : child.getChildrenByNodeType(NodeTypeDefinition.NODETYPE_NAME_NODETYPEDEFINTION)) { | ||
nodeTypeDefs.add(new NodeTypeDefinitionImpl(assertNotNull(nodeTypeChild))); | ||
} | ||
return nodeTypeDefs; | ||
} | ||
|
||
private @Nonnull WorkingTreeNode getNodeTypeRootNode() { | ||
WorkingTreeNode nodeTypeRoot = rootNode; | ||
if (nodeTypeRoot == null) { | ||
WorkingTreeNode rootNode = workingTreeImpl.getRootNode(); | ||
Iterable<WorkingTreeNode> children = rootNode.getChildrenByProperty(Property.PROPERTY_NAME, WorkingTree.NAME_NODETYPE); | ||
try { | ||
nodeTypeRoot = Iterables.getOnlyElement(children); | ||
} catch (IllegalArgumentException e) { | ||
// iterable contained too many items? | ||
throw new NodeStoreException("Too many root nodes with node type name.", e); | ||
} catch (NoSuchElementException e) { | ||
// returned iterable was empty | ||
// we have to create a new root node. | ||
} | ||
if (nodeTypeRoot == null) { | ||
nodeTypeRoot = rootNode.addChild(WorkingTree.NAME_NODETYPE); | ||
} | ||
rootNode = nodeTypeRoot; | ||
} | ||
return nodeTypeRoot; | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public NodeTypeDefinition addNodeTypeDefinition(@Nonnull String nodeTypeName) { | ||
if (getNodeTypeDefinitionByName(nodeTypeName) != null) { | ||
throw new IllegalArgumentException("Nodetype already exists."); | ||
} | ||
WorkingTreeNode root = getNodeTypeRootNode(); | ||
WorkingTreeNode child = root.addChild(); | ||
child.setProperty(Property.PROPERTY_NAME, new Property(nodeTypeName)); | ||
child.setProperty(WorkingTree.NAME_NODETYPE, new Property(NodeTypeDefinition.NODETYPE_NAME_NODETYPEDEFINTION)); | ||
NodeTypeDefinitionImpl nodeTypeDefinition = new NodeTypeDefinitionImpl(child); | ||
return nodeTypeDefinition; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public NodeTypeDefinition getNodeTypeDefinitionByName( | ||
@Nonnull String nodeTypeName) { | ||
WorkingTreeNode root = getNodeTypeRootNode(); | ||
Iterable<WorkingTreeNode> children = root.getChildrenByProperty(Property.PROPERTY_NAME, new Property(nodeTypeName)); | ||
try { | ||
WorkingTreeNode node = Iterables.getOnlyElement(children); | ||
return new NodeTypeDefinitionImpl(assertNotNull(node)); | ||
} catch (IllegalArgumentException e) { | ||
throw new NodeStoreException("There are two node types of this name.", e); | ||
} catch (NoSuchElementException e) { | ||
// empty iterable, no such child. | ||
return null; | ||
} | ||
} | ||
|
||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/dc2f/dstore/hierachynodestore/impl/nodetype/NodeTypeDefinitionImpl.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.dc2f.dstore.hierachynodestore.impl.nodetype; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import com.dc2f.dstore.hierachynodestore.WorkingTree; | ||
import com.dc2f.dstore.hierachynodestore.WorkingTreeNode; | ||
import com.dc2f.dstore.hierachynodestore.nodetype.NodeTypeDefinition; | ||
import com.dc2f.dstore.hierachynodestore.nodetype.PropertyDefinition; | ||
import com.dc2f.dstore.storage.Property; | ||
|
||
import static com.dc2f.utils.NullUtils.assertNotNull; | ||
|
||
public class NodeTypeDefinitionImpl implements NodeTypeDefinition { | ||
public static final @Nonnull String PROPERTY_NAME = ":name"; | ||
|
||
private @Nonnull WorkingTreeNode node; | ||
|
||
public NodeTypeDefinitionImpl(@Nonnull WorkingTreeNode node) { | ||
// TODO assert node type? | ||
this.node = node; | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public Collection<PropertyDefinition> listPropertyDefinitions() { | ||
List<PropertyDefinition> ret = new ArrayList<>(); | ||
for (WorkingTreeNode child : node.getChildren()) { | ||
ret.add(new PropertyDefinitionImpl(assertNotNull(child))); | ||
} | ||
return assertNotNull(Collections.unmodifiableCollection(ret)); | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public PropertyDefinition addPropertyDefinition(@Nonnull String name) { | ||
WorkingTreeNode propertyNode = node.addChild(); | ||
propertyNode.setProperty(PROPERTY_NAME, new Property(name)); | ||
propertyNode.setProperty(WorkingTree.NAME_NODETYPE, new Property(NodeTypeDefinition.NODETYPE_NAME_PROPERTYDEFINITION)); | ||
return new PropertyDefinitionImpl(propertyNode); | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public Collection<NodeTypeDefinition> listAllowedChildNodeTypes() { | ||
// FIXME implement me :) | ||
return new ArrayList<>(); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.