-
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.
finalized first version of node type api and added a simple test. issue
- Loading branch information
1 parent
4c670f9
commit 06c56cf
Showing
11 changed files
with
229 additions
and
11 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
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
76 changes: 74 additions & 2 deletions
76
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 |
---|---|---|
@@ -1,24 +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 { | ||
@SuppressWarnings("unused") | ||
private @Nonnull WorkingTreeImpl workingTreeImpl; | ||
private @Nullable WorkingTreeNode rootNode; | ||
|
||
public NodeTypeAccessorImpl(@Nonnull WorkingTreeImpl workingTreeImpl) { | ||
this.workingTreeImpl = workingTreeImpl; | ||
} | ||
|
||
@Override | ||
public @Nonnull Collection<NodeTypeDefinition> listNodeTypeDefinitions() { | ||
throw new UnsupportedOperationException(); | ||
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; | ||
} | ||
} | ||
|
||
|
||
} |
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
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
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
99 changes: 99 additions & 0 deletions
99
src/test/java/com/dc2f/dstore/test/hierachynodestore/nodetype/NodeTypeApiTest.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,99 @@ | ||
package com.dc2f.dstore.test.hierachynodestore.nodetype; | ||
|
||
import static com.dc2f.dstore.test.TreeAssertions.assertTree; | ||
import static com.dc2f.dstore.test.TreeAssertions.node; | ||
import static com.dc2f.dstore.test.TreeAssertions.properties; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.dc2f.dstore.hierachynodestore.HierarchicalNodeStore; | ||
import com.dc2f.dstore.hierachynodestore.WorkingTree; | ||
import com.dc2f.dstore.hierachynodestore.WorkingTreeUtils; | ||
import com.dc2f.dstore.hierachynodestore.nodetype.NodeTypeAccessor; | ||
import com.dc2f.dstore.hierachynodestore.nodetype.NodeTypeDefinition; | ||
import com.dc2f.dstore.storage.Property.PropertyType; | ||
import com.dc2f.dstore.storage.StorageBackend; | ||
import com.dc2f.dstore.storage.map.HashMapStorage; | ||
import com.dc2f.dstore.test.TreeAssertions.ExpectedNode; | ||
|
||
public class NodeTypeApiTest { | ||
private Logger logger = LoggerFactory.getLogger(NodeTypeApiTest.class); | ||
private HierarchicalNodeStore nodeStore; | ||
|
||
@Before | ||
public void setupDstore() throws IOException { | ||
StorageBackend storageBackend = initStorageBackend(); | ||
nodeStore = new HierarchicalNodeStore(storageBackend); | ||
} | ||
|
||
protected StorageBackend initStorageBackend() { | ||
return new HashMapStorage(); | ||
} | ||
|
||
|
||
@Test | ||
public void testSimpleNodeTypeSetup() { | ||
WorkingTree wt = nodeStore.checkoutBranch("master"); | ||
NodeTypeAccessor nodeTypeAccessor = wt.getNodeTypeAccessor(); | ||
|
||
NodeTypeDefinition entryNodeType = nodeTypeAccessor.addNodeTypeDefinition("com.dc2f.blog.BlogEntry"); | ||
|
||
// we need a 'nodetype' property (no inheritance, grml) | ||
entryNodeType.addPropertyDefinition(WorkingTree.NAME_NODETYPE) | ||
// for now we have no special property type for this.. | ||
.setType(PropertyType.STRING) | ||
.setRequired(true); | ||
|
||
entryNodeType.addPropertyDefinition("title") | ||
.setRequired(true) | ||
.setType(PropertyType.STRING); | ||
|
||
entryNodeType.addPropertyDefinition("body") | ||
.setRequired(true) | ||
.setType(PropertyType.STRING); | ||
|
||
entryNodeType.addPropertyDefinition("slug") | ||
.setRequired(true) | ||
.setType(PropertyType.STRING); | ||
|
||
wt.commit(null); | ||
|
||
logger.info("Got tree: " + WorkingTreeUtils.debugRecursiveTree(wt.getRootNode())); | ||
|
||
ExpectedNode expectedNode = node(properties("name", ""), | ||
node(properties("name", ":nodetype"), | ||
node(properties("name", "com.dc2f.blog.BlogEntry", | ||
":nodetype", | ||
"com.dc2f.nodetype.NodeTypeDefinition"), | ||
|
||
node(properties(":name", ":nodetype", | ||
":nodetype", "com.dc2f.nodetype.PropertyDefinition", | ||
":required", true, | ||
":type", "STRING")), | ||
|
||
node(properties(":name", "title", | ||
":nodetype", "com.dc2f.nodetype.PropertyDefinition", | ||
":required", true, | ||
":type", "STRING")), | ||
|
||
node(properties(":name", "body", | ||
":nodetype", "com.dc2f.nodetype.PropertyDefinition", | ||
":required", true, | ||
":type", "STRING")), | ||
|
||
node(properties(":name", "slug", | ||
":nodetype", "com.dc2f.nodetype.PropertyDefinition", | ||
":required", true, | ||
":type", "STRING")) | ||
|
||
))); | ||
|
||
assertTree(expectedNode, wt.getRootNode()); | ||
} | ||
|
||
} |