[Nodal-cvs] nodal/j-src/storage/memory memNodeSetContent.java,NONE,1.1 memLiteralMapContent.java,NON
Status: Pre-Alpha
Brought to you by:
leei
Update of /cvsroot/nodal/nodal/j-src/storage/memory In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14652/storage/memory Modified Files: memMapContent.java memSetContent.java Added Files: memNodeSetContent.java memLiteralMapContent.java memNodeMapContent.java memLiteralSetContent.java Log Message: Split implementations of Set and Map between literal and node properties. --- NEW FILE: memNodeSetContent.java --- /* * Distributed under the Apache Software License, Version 1.1 * (see below, or the file LICENSE for terms and conditions) * * Copyright (c) 2004 University of British Columbia. All rights reserved. * * Created on Jun 22, 2004 by leei */ package storage.memory; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.Set; import org.nodal.model.Node; import org.nodal.model.NodeContent; import org.nodal.model.SetContent; import org.nodal.type.SetType; import org.nodal.util.ConstraintFailure; /** * @author leei */ final class memNodeSetContent extends memSetContent { private final HashSet content; private final LinkedList propSeq; private final boolean isNodeValue; protected static memSetContent ingest (SetContent c) { memNodeSetContent mc = new memNodeSetContent((SetType) c.type()); Iterator i = c.properties(); try { while (i.hasNext()) { Object k = i.next(); mc.opSetValue(k, c.value(k), null); } } catch (ConstraintFailure f) { throw new RuntimeException("constraint failure in ingest: " + f); } return mc; } protected static memSetContent create(SetType t) { return new memNodeSetContent(t); } private memNodeSetContent(SetType t) { super(t); content = new HashSet(); propSeq = new LinkedList(); isNodeValue = t.propertyType().isNodeType(); } public Set asSet() { return Collections.unmodifiableSet(content); } public int size() { return content.size(); } public Iterator properties() { return propSeq.iterator(); } /** * Return true if this Set contains the object obj. * * @param obj * an Object * @return true if this Set contains obj */ public boolean contains(Object obj) { return content.contains(obj); } boolean opRemove (Object key, NodeContent.Editor c) { key = checkItemType(key); // Notify interested parties notifyRemove(key); // Actually remove the key boolean isRemoved = content.remove(key); if (isRemoved) { propSeq.remove(key); } return isRemoved; } boolean opAdd(Object val, NodeContent.Editor c) throws ConstraintFailure { val = checkValue(val); val = assignNode(c, val, (Node) val); // Notify any interested values notifyAdd(val); // Every single set goes through here. boolean isAdded = content.add(val); if (isAdded) { propSeq.addLast (val); } return isAdded; } } --- NEW FILE: memLiteralMapContent.java --- /* * Distributed under the Apache Software License, Version 1.1 * (see below, or the file LICENSE for terms and conditions) * * Copyright (c) 2004 University of British Columbia. All rights reserved. * * Created on Jun 22, 2004 by leei */ package storage.memory; import java.util.Collections; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; import org.nodal.model.MapContent; import org.nodal.model.Node; import org.nodal.model.NodeContent; import org.nodal.type.MapType; import org.nodal.util.ConstraintFailure; import org.nodal.util.PropertyConstraintFailure; /** * @author leei */ class memLiteralMapContent extends memMapContent { private TreeMap content; protected static memMapContent create(MapType t) { return new memLiteralMapContent(t); } protected static memMapContent ingest(MapContent c) { memMapContent mc = new memLiteralMapContent((MapType) c.type()); Iterator i = c.properties(); try { while (i.hasNext()) { Object k = i.next(); mc.opSetValue(k, c.value(k), null); } } catch (ConstraintFailure f) { throw new RuntimeException("constraint failure in ingest: " + f); } return mc; } private memLiteralMapContent(MapType t) { super(t); content = new TreeMap(); } private memLiteralMapContent(memLiteralMapContent c) { super(c.mapType); content = (TreeMap) c.content.clone(); } public Iterator properties() { return content.keySet().iterator(); } void opRemoveKey(Object key, NodeContent.Editor c) throws PropertyConstraintFailure { key = checkKeyType(key); // Notify interested parties notifyRemoveKey(key); // Actually remove the key content.remove(key); } void opSetValue(Object key, Object val, NodeContent.Editor c) throws ConstraintFailure { // Check types and convert if necessary key = checkKeyType(key); val = checkValueType(val); if (val == null) { opRemoveKey(key, c); } else { if (isNodeValue) { val = assignNode(c, key, (Node) val); } // Notify any interested values notifySetValue(key, val); // Every single set goes through here. content.put(key, val); } } public Map asMap() { return Collections.unmodifiableMap(content); } public int size() { return content.size(); } protected Object getContent(Object key) { return content.get(key); } } --- NEW FILE: memNodeMapContent.java --- /* * Distributed under the Apache Software License, Version 1.1 * (see below, or the file LICENSE for terms and conditions) * * Copyright (c) 2004 University of British Columbia. All rights reserved. * * Created on Jun 22, 2004 by leei */ package storage.memory; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; import org.nodal.model.MapContent; import org.nodal.model.Node; import org.nodal.model.NodeContent; import org.nodal.type.MapType; import org.nodal.util.ConstraintFailure; import org.nodal.util.PropertyConstraintFailure; /** * @author leei */ class memNodeMapContent extends memMapContent { private LinkedList keySeq; private HashMap content; protected static memMapContent create(MapType t) { return new memNodeMapContent(t); } protected static memMapContent ingest(MapContent c) { memMapContent mc = new memNodeMapContent((MapType) c.type()); Iterator i = c.properties(); try { while (i.hasNext()) { Object k = i.next(); mc.opSetValue(k, c.value(k), null); } } catch (ConstraintFailure f) { throw new RuntimeException("constraint failure in ingest: " + f); } return mc; } private memNodeMapContent(MapType t) { super(t); content = new HashMap(); keySeq = new LinkedList(); } private memNodeMapContent(memNodeMapContent c) { super(c.mapType); content = (HashMap) c.content.clone(); } public Iterator properties() { return keySeq.iterator(); } void opRemoveKey(Object key, NodeContent.Editor c) throws PropertyConstraintFailure { key = checkKeyType(key); key = seatNode (c, (Node) key); // Notify interested parties notifyRemoveKey(key); // Actually remove the key content.remove(key); keySeq.remove(key); } void opSetValue(Object key, Object val, NodeContent.Editor c) throws ConstraintFailure { // Check types and convert if necessary key = checkKeyType(key); val = checkValueType(val); if (val == null) { opRemoveKey(key, c); } else { key = seatNode(c, (Node) key); if (isNodeValue) { val = assignNode(c, key, (Node) val); } // Notify any interested values notifySetValue(key, val); // Every single set goes through here. Object v = content.put(key, val); if (v != null) { // If key not previously set, assign new seq pos to key keySeq.addLast(key); } } } public Map asMap() { return Collections.unmodifiableMap(content); } public int size() { return content.size(); } protected Object getContent(Object key) { return content.get(key); } } Index: memSetContent.java =================================================================== RCS file: /cvsroot/nodal/nodal/j-src/storage/memory/memSetContent.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** memSetContent.java 14 May 2004 17:31:28 -0000 1.4 --- memSetContent.java 28 Jun 2004 19:13:10 -0000 1.5 *************** *** 9,16 **** package storage.memory; - import java.util.Collections; - import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.nodal.model.Getter; import org.nodal.model.Node; --- 9,15 ---- package storage.memory; import java.util.Iterator; import java.util.Set; + import org.nodal.model.Getter; import org.nodal.model.Node; *************** *** 20,23 **** --- 19,23 ---- import org.nodal.model.Setter; import org.nodal.model.TxnOp; + import org.nodal.nav.Path; import org.nodal.nav.PathOperator; *************** *** 41,83 **** * given a Node that is using this class as its memContent. */ ! public final class memSetContent extends memContent { protected final SetType setType; ! protected HashSet content; ! protected CacheMap getters; ! private boolean isNodeValue; static memSetContent ingest(SetContent c) { ! memSetContent mc = new memSetContent((SetType) c.type()); ! Iterator i = c.properties(); ! try { ! while (i.hasNext()) { ! Object k = i.next(); ! mc.opSetValue(k, c.value(k), null); ! } ! } catch (ConstraintFailure f) { ! throw new RuntimeException("constraint failure in ingest: " + f); } - return mc; } ! static memSetContent create(SetType t) { ! return new memSetContent(t); } ! private memSetContent(SetType t) { setType = t; - content = new HashSet(); getters = new CacheMap(); - isNodeValue = t.itemType().isNodeType(); - } - - private memSetContent(memSetContent c) { - setType = c.setType; - content = (HashSet) c.content.clone(); - getters = new CacheMap(); - } - - protected NodeContent createNodeContent(Node n, Capability cap, RepoServices s) { - return new MyNodeContent(n, cap, s); } --- 41,68 ---- * given a Node that is using this class as its memContent. */ ! public abstract class memSetContent extends memContent { protected final SetType setType; ! protected final CacheMap getters; static memSetContent ingest(SetContent c) { ! SetType st = (SetType) c.type(); ! if (st.propertyType().isNodeType()) { ! return memNodeSetContent.ingest(c); ! } else { ! return memLiteralSetContent.ingest(c); } } ! static memSetContent create(SetType st) { ! if (st.propertyType().isNodeType()) { ! return memNodeSetContent.create(st); ! } else { ! return memLiteralSetContent.create(st); ! } } ! protected memSetContent(SetType t) { setType = t; getters = new CacheMap(); } *************** *** 98,129 **** } - // Implementation of Content methods - public Set asSet() { - return Collections.unmodifiableSet(content); - } - public NodeType type() { return setType; } - public int size() { - return content.size(); - } - - public Iterator properties() { - return content.iterator(); - } - - /** - * Return true if this Set contains the object obj. - * - * @param obj - * an Object - * @return true if this Set contains obj - */ - public boolean contains(Object obj) { - return content.contains(obj); - } - /** * Notify all interested parties when we remove a key. --- 83,90 ---- *************** *** 132,136 **** * the key being removed from this Map */ ! private void notifyRemove(Object val) { // Notify any interested NodeContents Iterator i = nodeContents.values(); --- 93,97 ---- * the key being removed from this Map */ ! protected void notifyRemove(Object val) { // Notify any interested NodeContents Iterator i = nodeContents.values(); *************** *** 154,158 **** * the value assigned to this key */ ! private void notifyAdd(Object val) { // Notify any interested NodeContents Iterator i = nodeContents.values(); --- 115,119 ---- * the value assigned to this key */ ! protected void notifyAdd(Object val) { // Notify any interested NodeContents Iterator i = nodeContents.values(); *************** *** 199,203 **** public Object get() { // Every single value access goes through here. ! return (content.contains(key) ? key : null); } --- 160,164 ---- public Object get() { // Every single value access goes through here. ! return (contains(key) ? key : null); } *************** *** 211,218 **** public boolean valueEquals(Object v) throws PropertyConstraintFailure { ! if (isNodeValue && v != null) { ! v = ((Node) v).bareNode(); } - return v.equals(get()); } --- 172,181 ---- public boolean valueEquals(Object v) throws PropertyConstraintFailure { ! Object val = get(); ! if (val != null) { ! return val.equals (v); ! } else { ! return val == v; } } *************** *** 248,285 **** } - boolean opRemoveKey(Object key, NodeContent.Editor c) { - key = checkItemType(key); - // Notify interested parties - notifyRemove(key); - // Actually remove the key - return content.remove(key); - } - - void opSetValue(Object key, Object val, NodeContent.Editor c) - throws ConstraintFailure { - // Check types and convert if necessary - key = checkItemType(key); - val = checkValue(val); - if (val == null) { - opRemoveKey(key, c); - } else if (key == val) { - opAdd(key, c); - } else { - throw new ConstraintFailure("key/value different in Set: " + key + ", " - + val); - } - } - - boolean opAdd(Object val, NodeContent.Editor c) throws ConstraintFailure { - val = checkValue(val); - if (isNodeValue) { - val = assignNode(c, val, (Node) val); - } - // Notify any interested values - notifyAdd(val); - // Every single set goes through here. - return content.add(val); - } - MemOperator invertAdd(Object key, NodeContent c) { Object value = getValue(key, c); --- 211,214 ---- *************** *** 376,380 **** } // Perform the operation ! return memSetContent.this.opRemoveKey(val, MyNodeEditor.this); } --- 305,309 ---- } // Perform the operation ! return memSetContent.this.opRemove(val, MyNodeEditor.this); } *************** *** 387,389 **** --- 316,342 ---- } } + + protected NodeContent createNodeContent(Node n, Capability cap, RepoServices s) { + return new MyNodeContent(n, cap, s); + } + + public abstract boolean contains(Object obj); + public abstract Set asSet(); + + abstract boolean opAdd(Object v, NodeContent.Editor c) throws ConstraintFailure; + abstract boolean opRemove(Object k, NodeContent.Editor c); + + final void opSetValue(Object key, Object val, NodeContent.Editor c) throws ConstraintFailure { + // Check types and convert if necessary + key = checkItemType(key); + val = checkValue(val); + if (val == null) { + opRemove (key, c); + } else if (key == val) { + opAdd(key, c); + } else { + throw new ConstraintFailure("key/value different in Set: " + key + ", " + + val); + } + } } \ No newline at end of file --- NEW FILE: memLiteralSetContent.java --- /* * Distributed under the Apache Software License, Version 1.1 * (see below, or the file LICENSE for terms and conditions) * * Copyright (c) 2004 University of British Columbia. All rights reserved. * * Created on Jun 22, 2004 by leei */ package storage.memory; import java.util.Collections; import java.util.Iterator; import java.util.Set; import java.util.TreeSet; import org.nodal.model.NodeContent; import org.nodal.model.SetContent; import org.nodal.type.SetType; import org.nodal.util.ConstraintFailure; /** * @author leei */ final class memLiteralSetContent extends memSetContent { private final TreeSet content; private final boolean isNodeValue; protected static memSetContent ingest (SetContent c) { memLiteralSetContent mc = new memLiteralSetContent((SetType) c.type()); Iterator i = c.properties(); try { while (i.hasNext()) { Object k = i.next(); mc.opSetValue(k, c.value(k), null); } } catch (ConstraintFailure f) { throw new RuntimeException("constraint failure in ingest: " + f); } return mc; } protected static memSetContent create(SetType t) { return new memLiteralSetContent(t); } private memLiteralSetContent(SetType t) { super(t); content = new TreeSet(); isNodeValue = t.propertyType().isNodeType(); } public Set asSet() { return Collections.unmodifiableSet(content); } public int size() { return content.size(); } public Iterator properties() { return content.iterator(); } /** * Return true if this Set contains the object obj. * * @param obj * an Object * @return true if this Set contains obj */ public boolean contains(Object obj) { return content.contains(obj); } boolean opRemove (Object key, NodeContent.Editor c) { key = checkItemType(key); // Notify interested parties notifyRemove(key); // Actually remove the key return content.remove(key); } boolean opAdd(Object val, NodeContent.Editor c) throws ConstraintFailure { val = checkValue(val); // Notify any interested values notifyAdd(val); // Every single set goes through here. return content.add(val); } } Index: memMapContent.java =================================================================== RCS file: /cvsroot/nodal/nodal/j-src/storage/memory/memMapContent.java,v retrieving revision 1.57 retrieving revision 1.58 diff -C2 -d -r1.57 -r1.58 *** memMapContent.java 14 May 2004 17:31:28 -0000 1.57 --- memMapContent.java 28 Jun 2004 19:13:10 -0000 1.58 *************** *** 9,16 **** package storage.memory; - import java.util.Collections; - import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.nodal.model.Getter; import org.nodal.model.MapContent; --- 9,15 ---- package storage.memory; import java.util.Iterator; import java.util.Map; + import org.nodal.model.Getter; import org.nodal.model.MapContent; *************** *** 41,83 **** * given a Node that is using this class as its memContent. */ ! public final class memMapContent extends memContent { protected final MapType mapType; ! protected HashMap content; ! protected CacheMap getters; ! private boolean isNodeValue; ! private boolean isNodeProperty; ! static memMapContent ingest(MapContent c) { ! memMapContent mc = new memMapContent((MapType) c.type()); ! Iterator i = c.properties(); ! try { ! while (i.hasNext()) { ! Object k = i.next(); ! mc.opSetValue(k, c.value(k), null); ! } ! } catch (ConstraintFailure f) { ! throw new RuntimeException("constraint failure in ingest: " + f); } - return mc; } ! static memMapContent create(MapType t) { ! return new memMapContent(t); } ! private memMapContent(MapType t) { mapType = t; - content = new HashMap(); getters = new CacheMap(); - isNodeProperty = t.keyType().isNodeType(); isNodeValue = t.valueType().isNodeType(); } - private memMapContent(memMapContent c) { - mapType = c.mapType; - content = (HashMap) c.content.clone(); - getters = new CacheMap(); - } - protected NodeContent createNodeContent(Node n, Capability cap, RepoServices s) { return new MyNodeContent(n, cap, s); --- 40,71 ---- * given a Node that is using this class as its memContent. */ ! public abstract class memMapContent extends memContent { protected final MapType mapType; ! protected final CacheMap getters; ! protected final boolean isNodeValue; ! static memMapContent ingest(MapContent c) { ! NodeType nt = (NodeType) c.type(); ! if (nt.propertyType().isNodeType()) { ! return memNodeMapContent.ingest (c); ! } else { ! return memLiteralMapContent.ingest (c); } } ! static memMapContent create(MapType nt) { ! if (nt.propertyType().isNodeType()) { ! return memNodeMapContent.create (nt); ! } else { ! return memLiteralMapContent.create (nt); ! } } ! protected memMapContent(MapType t) { mapType = t; getters = new CacheMap(); isNodeValue = t.valueType().isNodeType(); } protected NodeContent createNodeContent(Node n, Capability cap, RepoServices s) { return new MyNodeContent(n, cap, s); *************** *** 100,120 **** } - // Implementation of Content methods - public Map asMap() { - return Collections.unmodifiableMap(content); - } - public NodeType type() { return mapType; } - public int size() { - return content.size(); - } - - public Iterator properties() { - return content.keySet().iterator(); - } - /** * Notify all interested parties when we remove a key. --- 88,95 ---- *************** *** 123,127 **** * the key being removed from this Map */ ! private void notifyRemoveKey(Object key) { // Notify any interested NodeContents Iterator i = nodeContents.values(); --- 98,102 ---- * the key being removed from this Map */ ! protected void notifyRemoveKey(Object key) { // Notify any interested NodeContents Iterator i = nodeContents.values(); *************** *** 145,149 **** * the value assigned to this key */ ! private void notifySetValue(Object key, Object val) { // Notify any interested NodeContents Iterator i = nodeContents.values(); --- 120,124 ---- * the value assigned to this key */ ! protected void notifySetValue(Object key, Object val) { // Notify any interested NodeContents Iterator i = nodeContents.values(); *************** *** 175,178 **** --- 150,156 ---- } + protected abstract Object getContent (Object key); + public abstract Map asMap(); + /** * Implementation of the Getter associated with a particular key. *************** *** 190,194 **** public Object get() { // Every single value access goes through here. ! return content.get(key); } --- 168,172 ---- public Object get() { // Every single value access goes through here. ! return getContent (key); } *************** *** 240,273 **** } - void opRemoveKey(Object key, NodeContent.Editor c) - throws PropertyConstraintFailure { - key = checkKeyType(key); - // Notify interested parties - notifyRemoveKey(key); - // Actually remove the key - content.remove(key); - } - - void opSetValue(Object key, Object val, NodeContent.Editor c) - throws ConstraintFailure { - // Check types and convert if necessary - key = checkKeyType(key); - val = checkValueType(val); - if (val == null) { - opRemoveKey(key, c); - } else { - if (isNodeProperty) { - key = seatNode(c, (Node) key); - } - if (isNodeValue) { - val = assignNode(c, key, (Node) val); - } - // Notify any interested values - notifySetValue(key, val); - // Every single set goes through here. - content.put(key, val); - } - } - MemOperator invertRemoveKey(Object key, NodeContent c) throws PropertyConstraintFailure { --- 218,221 ---- |