jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1185846 - /jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/hierarchy/UniqueIdResolver.java
Date Tue, 18 Oct 2011 20:42:21 GMT
Author: mduerig
Date: Tue Oct 18 20:42:20 2011
New Revision: 1185846

URL: http://svn.apache.org/viewvc?rev=1185846&view=rev
Log:
Microkernel based Jackrabbit prototype (WIP)
- remove unnecessary synchronization
- simplify

Modified:
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/hierarchy/UniqueIdResolver.java

Modified: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/hierarchy/UniqueIdResolver.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/hierarchy/UniqueIdResolver.java?rev=1185846&r1=1185845&r2=1185846&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/hierarchy/UniqueIdResolver.java
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/hierarchy/UniqueIdResolver.java
Tue Oct 18 20:42:20 2011
@@ -38,25 +38,25 @@ import static org.apache.jackrabbit.spi.
 public class UniqueIdResolver implements ItemStateCreationListener, EntryFactory.NodeEntryListener
{
     private static final Logger log = LoggerFactory.getLogger(UniqueIdResolver.class);
 
-    private final ItemStateFactory isf;
+    private final ItemStateFactory itemStateFactory;
 
     /**
      * Maps a String uniqueID to a {@link NodeEntry}.
      */
-    private final Map<String, NodeEntry> lookUp;
+    private final Map<String, NodeEntry> nodeEntryById;
 
     /**
      * Creates a new {@code UniqueIdResolver}.
      */
-    public UniqueIdResolver(ItemStateFactory isf) {
-        lookUp = cast(new ReferenceMap(ReferenceMap.HARD, ReferenceMap.SOFT));
-        this.isf = isf;
-        isf.addCreationListener(this);
+    public UniqueIdResolver(ItemStateFactory itemStateFactory) {
+        nodeEntryById = cast(new ReferenceMap(ReferenceMap.HARD, ReferenceMap.SOFT));
+        this.itemStateFactory = itemStateFactory;
+        itemStateFactory.addCreationListener(this);
     }
 
     public void dispose() {
-        isf.removeCreationListener(this);
-        lookUp.clear();
+        itemStateFactory.removeCreationListener(this);
+        nodeEntryById.clear();
     }
 
     public NodeEntry lookup(String uniqueId) {
@@ -65,13 +65,13 @@ public class UniqueIdResolver implements
             log.error(e.getMessage(), e);
             throw e;
         }
-        return lookUp.get(uniqueId);
+        return nodeEntryById.get(uniqueId);
     }
 
     public NodeEntry resolve(NodeId nodeId, NodeEntry rootEntry) throws RepositoryException
{
         NodeEntry entry = lookup(nodeId.getUniqueID());
         if (entry == null) {
-            NodeState state = isf.createDeepNodeState(nodeId, rootEntry);
+            NodeState state = itemStateFactory.createDeepNodeState(nodeId, rootEntry);
             entry = state.getHierarchyEntry();
         }
         return entry;
@@ -90,25 +90,23 @@ public class UniqueIdResolver implements
      */
     @Override
     public void statusChanged(ItemState<?> state, Status previousStatus) {
-        synchronized (lookUp) {
-            if (Status.isTerminal(state.getStatus())) {
-                if (state.isNode()) {
-                    NodeEntry entry = cast(state.getHierarchyEntry());
-                    String uniqueID = entry.getUniqueID();
-                    if (uniqueID != null) {
-                        NodeEntry mapEntry = lookUp.get(uniqueID);
-                        if (mapEntry == entry) {
-                            lookUp.remove(uniqueID);
-                        } // else: removed entry is not present in lookup but
-                          //       only it's replacement -> ignore
-                    }
+        if (Status.isTerminal(state.getStatus())) {
+            if (state.isNode()) {
+                NodeEntry entry = cast(state.getHierarchyEntry());
+                String uniqueID = entry.getUniqueID();
+                if (uniqueID != null) {
+                    NodeEntry mapEntry = nodeEntryById.get(uniqueID);
+                    if (mapEntry == entry) {
+                        nodeEntryById.remove(uniqueID);
+                    } // else: removed entry is not present in lookup but
+                      //       only it's replacement -> ignore
                 }
-                // stop listening if a state reached status REMOVED.
-                if (Status.REMOVED == state.getStatus()) {
-                    state.removeListener(this);
-                }
-            } // else: any other status than REMOVED -> ignore.
-        }
+            }
+            // stop listening if a state reached status REMOVED.
+            if (Status.REMOVED == state.getStatus()) {
+                state.removeListener(this);
+            }
+        } // else: any other status than REMOVED -> ignore.
     }
 
     //------------------------------------------< ItemStateCreationListener >---
@@ -125,7 +123,7 @@ public class UniqueIdResolver implements
             NodeEntry entry = cast(state.getHierarchyEntry());
             String uniqueID = entry.getUniqueID();
             if (uniqueID != null) {
-                if (!lookUp.containsKey(uniqueID) || lookUp.get(uniqueID) != entry) {
+                if (!nodeEntryById.containsKey(uniqueID) || nodeEntryById.get(uniqueID) !=
entry) {
                     log.error("Created NodeState identified by UniqueID that is not contained
in the lookup.");
                 }
             }
@@ -136,47 +134,43 @@ public class UniqueIdResolver implements
 
     @Override
     public void entryCreated(NodeEntry entry) {
-        synchronized (lookUp) {
-            String uniqueID = entry.getUniqueID();
-            if (uniqueID != null) {
-                putToLookup(uniqueID, entry);
-            }
+        String uniqueID = entry.getUniqueID();
+        if (uniqueID != null) {
+            put(uniqueID, entry);
         }
     }
 
     @Override
     public void uniqueIdChanged(NodeEntry entry, String previousUniqueID) {
-        synchronized (lookUp) {
-            if (previousUniqueID != null) {
-                Object previous = lookUp.get(previousUniqueID);
-                if (previous == entry) {
-                    lookUp.remove(previousUniqueID);
-                } // else: previousUniqueID points to another entry -> ignore
-            }
-            String uniqueID = entry.getUniqueID();
-            if (uniqueID != null) {
-                putToLookup(uniqueID, entry);
-            }
+        if (previousUniqueID != null) {
+            Object previous = nodeEntryById.get(previousUniqueID);
+            if (previous == entry) {
+                nodeEntryById.remove(previousUniqueID);
+            } // else: previousUniqueID points to another entry -> ignore
+        }
+        String uniqueID = entry.getUniqueID();
+        if (uniqueID != null) {
+            put(uniqueID, entry);
         }
     }
 
     //------------------------------------------------------------< private >---
     
-    private void putToLookup(String uniqueID, NodeEntry entry) {
-        NodeEntry previous = lookUp.put(uniqueID, entry);
+    private void put(String uniqueID, NodeEntry entry) {
+        NodeEntry previous = nodeEntryById.put(uniqueID, entry);
         if (previous != null) {
             // some other entry existed before with the same uniqueID
             if (sameEntry(previous, entry)) {
+                // if both represent the same entry the creation of
+                // the entry is the result of gc of the node or any of the
+                // ancestors. in this case there is not need to 'remove'
+                // the previous entry. instead it is just removed from this
+                // cache and left for collection.
                 log.debug("Replacement of NodeEntry identified by UniqueID");
             } else {
-                // if the new entry represents the externally moved/renamed
+                // otherwise the new entry represents the externally moved/renamed
                 // correspondence of the previous the latter needs to marked
                 // removed/stale-destroyed.
-                // otherwise (both represent the same entry) the creation
-                // of entry is the result of gc of the node or any of the
-                // ancestors. in this case there is not need to 'remove'
-                // the previous entry. instead it is just removed from this
-                // cache and left for collection.
                 previous.remove();
             }
         }
@@ -185,21 +179,21 @@ public class UniqueIdResolver implements
     private static boolean sameEntry(NodeEntry previous, NodeEntry entry) {
         if (previous == entry) {
             return true;
-        } else if (Status.REMOVED != previous.getStatus() &&
-                previous.getName().equals(entry.getName())) {
-
-            NodeEntry parent = previous.getParent();
-            NodeEntry parent2 = entry.getParent();
-            if (parent == parent2) {
+        } else if (Status.REMOVED != previous.getStatus() && previous.getName().equals(entry.getName()))
{
+            NodeEntry previousParent = previous.getParent();
+            NodeEntry parent = entry.getParent();
+            if (previousParent == parent) {
                 return true;
             } else {
                 try {
-                    return parent.getPath().equals(parent2.getPath());
+                    return previousParent.getPath().equals(parent.getPath());
                 } catch (RepositoryException e) {
-                    // TODO: add some fallback
+                    return false;
                 }
             }
         }
-        return false;
+        else {
+            return false;
+        }
     }
 }



Mime
View raw message