jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1234198 - in /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model: ChildNodeEntriesBucket.java ChildNodeEntriesInlined.java ChildNodeEntriesTree.java
Date Fri, 20 Jan 2012 23:07:14 GMT
Author: stefan
Date: Fri Jan 20 23:07:13 2012
New Revision: 1234198

URL: http://svn.apache.org/viewvc?rev=1234198&view=rev
Log:
support for flat hierarchies (WIP)

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesBucket.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesInlined.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesTree.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesBucket.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesBucket.java?rev=1234198&r1=1234197&r2=1234198&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesBucket.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesBucket.java
Fri Jan 20 23:07:13 2012
@@ -16,57 +16,32 @@
  */
 package org.apache.jackrabbit.mk.model;
 
-import java.util.Iterator;
+import java.util.HashMap;
 
 /**
- * todo implement using HashMap (insertion order not required)
+ *
  */
-public class ChildNodeEntriesBucket implements ChildNodeEntries {
-    public int getCount() {
-        return 0;  //To change body of implemented methods use File | Settings | File Templates.
-    }
-
-    public ChildNodeEntry get(String name) {
-        return null;  //To change body of implemented methods use File | Settings | File
Templates.
-    }
-
-    public Iterator<String> getNames(int offset, int count) {
-        return null;  //To change body of implemented methods use File | Settings | File
Templates.
-    }
-
-    public Iterator<ChildNodeEntry> getEntries(int offset, int count) {
-        return null;  //To change body of implemented methods use File | Settings | File
Templates.
-    }
-
-    public ChildNodeEntry add(ChildNodeEntry entry) {
-        return null;  //To change body of implemented methods use File | Settings | File
Templates.
-    }
-
-    public ChildNodeEntry remove(String name) {
-        return null;  //To change body of implemented methods use File | Settings | File
Templates.
-    }
-
-    public ChildNodeEntry rename(String oldName, String newName) {
-        return null;  //To change body of implemented methods use File | Settings | File
Templates.
-    }
-
-    public ChildNodeEntry moveAfter(String name, String sibling) {
-        return null;  //To change body of implemented methods use File | Settings | File
Templates.
-    }
+public class ChildNodeEntriesBucket extends ChildNodeEntriesInlined {
 
-    public ChildNodeEntry moveBefore(String name, String sibling) {
-        return null;  //To change body of implemented methods use File | Settings | File
Templates.
+    ChildNodeEntriesBucket() {
+        // using stock HashMap since keeping insertion-order is not required
+        entries = new HashMap<String, ChildNodeEntry>();
     }
 
-    public Iterator<ChildNodeEntry> getAdded(ChildNodeEntries other) {
-        return null;  //To change body of implemented methods use File | Settings | File
Templates.
-    }
+    //------------------------------------------------------------< overrides >
 
-    public Iterator<ChildNodeEntry> getRemoved(ChildNodeEntries other) {
-        return null;  //To change body of implemented methods use File | Settings | File
Templates.
+    @Override
+    public boolean equals(Object obj) {
+        if (obj instanceof ChildNodeEntriesBucket) {
+            return entries.equals(((ChildNodeEntriesBucket) obj).entries);
+        }
+        return false;
     }
 
-    public Iterator<ChildNodeEntry> getModified(ChildNodeEntries other) {
-        return null;  //To change body of implemented methods use File | Settings | File
Templates.
+    @Override
+    protected Object clone()  {
+        ChildNodeEntriesBucket clone = (ChildNodeEntriesBucket) super.clone();
+        clone.entries = (HashMap<String, ChildNodeEntry>) entries.clone();
+        return clone;
     }
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesInlined.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesInlined.java?rev=1234198&r1=1234197&r2=1234198&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesInlined.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesInlined.java
Fri Jan 20 23:07:13 2012
@@ -20,6 +20,7 @@ import org.apache.jackrabbit.mk.util.Abs
 import org.apache.jackrabbit.mk.util.EmptyIterator;
 import org.apache.jackrabbit.mk.util.RangeIterator;
 
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -31,7 +32,7 @@ public class ChildNodeEntriesInlined imp
 
     protected static final Iterator<ChildNodeEntry> EMPTY_ITER = new EmptyIterator<ChildNodeEntry>();
     
-    protected LinkedHashMap<String, ChildNodeEntry> entries;
+    protected HashMap<String, ChildNodeEntry> entries;
     
     ChildNodeEntriesInlined() {
         entries = new LinkedHashMap<String, ChildNodeEntry>();
@@ -77,7 +78,7 @@ public class ChildNodeEntriesInlined imp
         if (offset == 0 && count == -1) {
             return entries.keySet().iterator();
         } else {
-            if (offset >= entries.size()) {
+            if (offset >= entries.size() || count == 0) {
                 return new EmptyIterator<String>();
             }
             if (count == -1 || (offset + count) > entries.size()) {
@@ -94,8 +95,8 @@ public class ChildNodeEntriesInlined imp
         if (offset == 0 && count == -1) {
             return entries.values().iterator();
         } else {
-            if (offset >= entries.size()) {
-                return new EmptyIterator<ChildNodeEntry>();
+            if (offset >= entries.size() || count == 0) {
+                return EMPTY_ITER;
             }
             if (count == -1 || (offset + count) > entries.size()) {
                 count = entries.size() - offset;
@@ -118,8 +119,11 @@ public class ChildNodeEntriesInlined imp
         if (oldName.equals(newName)) {
             return entries.get(oldName);
         }
-        LinkedHashMap<String, ChildNodeEntry> clone =
-                (LinkedHashMap<String, ChildNodeEntry>) entries.clone();
+        if (entries.get(oldName) == null) {
+            return null;
+        }
+        HashMap<String, ChildNodeEntry> clone =
+                (HashMap<String, ChildNodeEntry>) entries.clone();
         entries.clear();
         ChildNodeEntry oldCNE = null;
         for (Map.Entry<String, ChildNodeEntry> entry : clone.entrySet()) {
@@ -144,8 +148,8 @@ public class ChildNodeEntriesInlined imp
             entries.put(target.getName(), target);
             return target;
         } else {
-            LinkedHashMap<String, ChildNodeEntry> clone =
-                    (LinkedHashMap<String, ChildNodeEntry>) entries.clone();
+            HashMap<String, ChildNodeEntry> clone =
+                    (HashMap<String, ChildNodeEntry>) entries.clone();
             entries.clear();
             for (Map.Entry<String, ChildNodeEntry> entry : clone.entrySet()) {
                 entries.put(entry.getKey(), entry.getValue());
@@ -163,8 +167,8 @@ public class ChildNodeEntriesInlined imp
             return null;
         }
 
-        LinkedHashMap<String, ChildNodeEntry> clone =
-                (LinkedHashMap<String, ChildNodeEntry>) entries.clone();
+        HashMap<String, ChildNodeEntry> clone =
+                (HashMap<String, ChildNodeEntry>) entries.clone();
         entries.clear();
         if (sibling == null) {
             // move to top

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesTree.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesTree.java?rev=1234198&r1=1234197&r2=1234198&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesTree.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/ChildNodeEntriesTree.java
Fri Jan 20 23:07:13 2012
@@ -16,8 +16,8 @@
  */
 package org.apache.jackrabbit.mk.model;
 
+import org.apache.jackrabbit.mk.util.AbstractRangeIterator;
 import org.apache.jackrabbit.mk.util.EmptyIterator;
-import org.apache.jackrabbit.mk.util.RangeIterator;
 
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -81,69 +81,160 @@ public class ChildNodeEntriesTree implem
             throw new IllegalArgumentException();
         }
 
-        if (offset >= count) {
+        if (offset >= count || cnt == 0) {
             return new EmptyIterator<String>();
         }
 
-        int n = 0;
+        int skipped = 0;
         if (cnt == -1 || (offset + cnt) > count) {
             cnt = count - offset;
         }
-        ArrayList<String> names = new ArrayList<String>(cnt);
+
+        return new AbstractRangeIterator<String>(getEntries(offset, cnt), 0, -1) {
+            @Override
+            protected String doNext() {
+                ChildNodeEntry cne = (ChildNodeEntry) it.next();
+                return cne.getName();
+            }
+        };
+    }
+
+    public Iterator<ChildNodeEntry> getEntries(int offset, int cnt) {
+        if (offset < 0 || cnt < -1) {
+            throw new IllegalArgumentException();
+        }
+
+        if (offset >= count || cnt == 0) {
+            return EMPTY_ITER;
+        }
+
+        int skipped = 0;
+        if (cnt == -1 || (offset + cnt) > count) {
+            cnt = count - offset;
+        }
+        ArrayList<ChildNodeEntry> list = new ArrayList<ChildNodeEntry>(cnt);
         for (IndexEntry e : index) {
             if (e != null) {
-                if (n + e.getSize() > offset) {
+                if (skipped + e.getSize() <= offset) {
+                    skipped += e.getSize();
+                } else {
                     if (e instanceof NodeInfo) {
-                        names.add(((NodeInfo) e).getName());
+                        list.add((NodeInfo) e);
                     } else {
                         BucketInfo bi = (BucketInfo) e;
                         ChildNodeEntriesBucket bucket = retrieveBucket(bi.id);
-                        // todo implement
+                        for (Iterator<ChildNodeEntry> it =
+                                     bucket.getEntries(offset - skipped, cnt - list.size());
+                             it.hasNext(); ) {
+                            list.add(it.next());
+                        }
+                        skipped = offset;
+                    }
+                    if (list.size() == cnt) {
+                        break;
                     }
                 }
-                n += e.getSize();
             }
         }
-        
-        return names.iterator();
-    }
 
-    public Iterator<ChildNodeEntry> getEntries(int offset, int cnt) {
-        if (offset < 0 || cnt < -1) {
-            throw new IllegalArgumentException();
-        }
-
-        // todo implement
-        return null;
+        return list.iterator();
     }
 
     //------------------------------------------------------------< write ops >
 
     public ChildNodeEntry add(ChildNodeEntry entry) {
-        // todo implement
-        return null;
+        int idx = keyToIndex(entry.getName());
+        IndexEntry ie = index[idx];
+        if (ie == null) {
+            index[idx] = new NodeInfo(entry.getName(), entry.getId());
+            count++;
+            return null;
+        }
+        if (ie instanceof ChildNodeEntry) {
+            ChildNodeEntry existing = (ChildNodeEntry) ie;
+            if (existing.getName().equals(entry.getName())) {
+                index[idx] = new NodeInfo(entry.getName(), entry.getId());
+                return existing;
+            } else {
+                ChildNodeEntriesBucket bucket = new ChildNodeEntriesBucket();
+                bucket.add(existing);
+                bucket.add(entry);
+                BucketInfo bi = new BucketInfo();
+                bi.size = 2;
+                // todo store bucket and update index entry with new bucket id (content hash)
+                //bi.id = <content hash of bucket>;
+                index[idx] = bi;
+                count++;
+                return null;
+            }
+        } else {
+            BucketInfo bi = (BucketInfo) ie;
+            ChildNodeEntries entries = retrieveBucket(bi.id);
+            ChildNodeEntry existing = entries.add(entry);
+            if (existing == null) {
+                count++;
+                bi.size++;
+            }
+            // todo store bucket and update index entry with new bucket id (content hash)
+            //bi.id = <content hash of bucket>;
+            return existing;
+        }
     }
 
     public ChildNodeEntry remove(String name) {
-        // todo implement
-        return null;
+        int idx = keyToIndex(name);
+        IndexEntry ie = index[idx];
+        if (ie == null) {
+            return null;
+        }
+        if (ie instanceof ChildNodeEntry) {
+            ChildNodeEntry existing = (ChildNodeEntry) ie;
+            if (existing.getName().equals(name)) {
+                index[idx] = null;
+                count--;
+                return existing;
+            } else {
+                return null;
+            }
+        } else {
+            BucketInfo bi = (BucketInfo) ie;
+            ChildNodeEntries entries = retrieveBucket(bi.id);
+            ChildNodeEntry existing = entries.remove(name);
+            if (existing == null) {
+                return null;
+            }
+            if (entries.getCount() == 0) {
+                index[idx] = null;
+            } else {
+                bi.size--;
+                // todo store bucket and update index entry with new bucket id (content hash)
+                //bi.id = <content hash of bucket>;
+            }
+            count--;
+            return existing;
+        }
     }
 
     public ChildNodeEntry rename(String oldName, String newName) {
-        // todo implement
-        return null;
+        if (oldName.equals(newName)) {
+            return get(oldName);
+        }
+        ChildNodeEntry old = remove(oldName);
+        if (old == null) {
+            return null;
+        }
+        add(new ChildNodeEntry(newName, old.getId()));
+        return old;
     }
 
     public ChildNodeEntry moveAfter(String name, String sibling) {
         // not supported, silently ignore
-        // todo implement
-        return null;
+        return get(name);
     }
 
     public ChildNodeEntry moveBefore(String name, String sibling) {
         // not supported, silently ignore
-        // todo implement
-        return null;
+        return get(name);
     }
 
     //-------------------------------------------------------------< diff ops >



Mime
View raw message