directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r1544180 - in /directory/shared/trunk/ldap/model/src: main/java/org/apache/directory/api/ldap/model/ldif/ test/java/org/apache/directory/api/ldap/model/ldif/
Date Thu, 21 Nov 2013 13:52:31 GMT
Author: kayyagari
Date: Thu Nov 21 13:52:30 2013
New Revision: 1544180

URL: http://svn.apache.org/r1544180
Log:
updated the LDIF entry to store length and offset (DIRAPI-158)

Modified:
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifEntry.java
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
    directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifEntry.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifEntry.java?rev=1544180&r1=1544179&r2=1544180&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifEntry.java
(original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifEntry.java
Thu Nov 21 13:52:30 2013
@@ -97,7 +97,16 @@ public class LdifEntry implements Clonea
     /** The controls */
     private Map<String, LdifControl> controls;
 
-
+    /** The lengthBeforeParsing of the entry at the time of parsing. This includes
+     *  the lengthBeforeParsing of the comments present in entry at the time of parsing
+     *  so this lengthBeforeParsing may not always match with the lengthBeforeParsing of
the entry
+     *  data present in memory.
+     */
+    private int lengthBeforeParsing = 0;
+
+    /** the position of the entry in the file or given input string*/
+    private long offset = 0;
+    
     /**
      * Creates a new LdifEntry object.
      */
@@ -829,6 +838,45 @@ public class LdifEntry implements Clonea
     }
 
 
+    /** 
+     *  Returns the lengthBeforeParsing of the entry at the time of parsing. This includes
+     *  the lengthBeforeParsing of the comments present in entry at the time of parsing
+     *  so this lengthBeforeParsing may not always match with the lengthBeforeParsing of
the entry
+     *  data present in memory.
+     */
+    public int getLengthBeforeParsing()
+    {
+        return lengthBeforeParsing;
+    }
+
+
+    /**
+     * @param lengthBeforeParsing the lengthBeforeParsing to set
+     */
+    /**No qualifier*/ void setLengthBeforeParsing( int length )
+    {
+        this.lengthBeforeParsing = length;
+    }
+
+
+    /**
+     * @return the offset
+     */
+    public long getOffset()
+    {
+        return offset;
+    }
+
+
+    /**
+     * @param offset the offset to set
+     */
+    /**No qualifier*/ void setOffset( long offset )
+    {
+        this.offset = offset;
+    }
+
+
     /**
      * @return a String representing the Entry, as a LDIF 
      */

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java?rev=1544180&r1=1544179&r2=1544180&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
(original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
Thu Nov 21 13:52:30 2013
@@ -224,7 +224,15 @@ public class LdifReader implements Itera
      */
     protected Exception error;
 
+    /** total length of an LDIF entry including the comments */
+    protected int entryLen = 0;
 
+    /** the parsed entry's starting position */
+    protected long entryOffset = 0;
+    
+    /** the current offset of the reader */
+    private long offset = 0;
+    
     /**
      * Constructors
      */
@@ -1304,6 +1312,9 @@ public class LdifReader implements Itera
 
         // Ok, we have found a Dn
         LdifEntry entry = new LdifEntry();
+        entry.setLengthBeforeParsing( entryLen );
+        entry.setOffset( entryOffset );
+        
         entry.setDn( dn );
 
         // We remove this dn from the lines
@@ -1489,7 +1500,12 @@ public class LdifReader implements Itera
             // and read the next lines if the current buffer is empty
             if ( lines.size() == 0 )
             {
+                // include the version line as part of the first entry
+                int tmpEntryLen = entryLen;
+                
                 readLines();
+                
+                entryLen += tmpEntryLen;
             }
         }
         else
@@ -1528,6 +1544,9 @@ public class LdifReader implements Itera
         boolean isFirstLine = true;
 
         lines.clear();
+        entryLen = 0;
+        entryOffset = offset;
+        
         StringBuffer sb = new StringBuffer();
 
         try
@@ -1589,6 +1608,11 @@ public class LdifReader implements Itera
                         insideComment = false;
                         break;
                 }
+                
+                byte[] data = line.getBytes();
+                // FIXME might fail on windows in the new line issue, yet to check
+                offset += ( data.length + 1 );
+                entryLen += ( data.length + 1 );
             }
         }
         catch ( IOException ioe )
@@ -1945,6 +1969,7 @@ public class LdifReader implements Itera
             reader.close();
             containsEntries = false;
             containsChanges = false;
+            offset = entryOffset = 0;
         }
     }
 }

Modified: directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java?rev=1544180&r1=1544179&r2=1544180&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java
(original)
+++ directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java
Thu Nov 21 13:52:30 2013
@@ -30,7 +30,9 @@ import static org.junit.Assert.fail;
 import java.io.DataOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.FileWriter;
 import java.io.IOException;
+import java.io.RandomAccessFile;
 import java.io.UnsupportedEncodingException;
 import java.util.List;
 
@@ -205,6 +207,8 @@ public class LdifReaderTest
 
         assertTrue( entry.isLdifContent() );
 
+        assertEquals( ldif.length(), entry.getLengthBeforeParsing() );
+        
         assertEquals( "cn=app1,ou=applications,ou=conf,dc=apache,dc=org", entry.getDn().getName()
);
 
         Attribute attr = entry.get( "displayname" );
@@ -2081,4 +2085,101 @@ public class LdifReaderTest
         assertNotNull( modification.getAttribute().get() );
         assertNull( modification.getAttribute().getBytes() );
     }
+    
+    /**
+     * Test lengths when multiple entries are present
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testLdifParserLengthAndOffset() throws Exception
+    {
+        String ldif1 =  "dn: cn=app1,ou=applications,ou=conf,dc=apache,dc=org\n" +
+                        "cn: app1\n" +
+                        "objectClass: top\n" +
+                        "objectClass: apApplication\n" +
+                        "displayName:   app1   \n" +
+                        "dependencies:\n" +
+                        "envVars:\n";
+
+        String comment = "# This comment was copied. Delete an entry. The operation will
attach the LDAPv3\n" +
+                         "# Tree Delete Control defined in [9]. The criticality\n" +
+                         "# field is \"true\" and the controlValue field is\n" +
+                         "# absent, as required by [9].\n";
+        
+        String version = "version:   1\n";
+        
+        String ldif =
+                version +
+                ldif1 + 
+                "\n" +
+                comment +
+                ldif1 + "\n";
+
+        LdifReader reader = new LdifReader();
+
+        List<LdifEntry> lstEntries = null;
+        
+        try
+        {
+            lstEntries = reader.parseLdif( ldif );
+        }
+        catch ( Exception ne )
+        {
+            fail();
+        }
+        finally
+        {
+            reader.close();
+        }
+        
+        LdifEntry entry1 = lstEntries.get( 0 );
+        
+        assertEquals( version.length() + ldif1.length(), entry1.getLengthBeforeParsing()
);
+        
+        LdifEntry entry2 = lstEntries.get( 1 );
+        
+        assertEquals( ldif1.length() + comment.length(), entry2.getLengthBeforeParsing()
);
+        
+        byte[] data = ldif.getBytes();
+        
+        String ldif1Bytes = new String( data, ( int ) entry1.getOffset(), entry1.getLengthBeforeParsing()
);
+        assertNotNull( reader.parseLdif( ldif1Bytes ).get( 0 ) );
+        
+        String ldif2Bytes = new String( data, ( int ) entry2.getOffset(), entry2.getLengthBeforeParsing()
);
+        assertNotNull( reader.parseLdif( ldif2Bytes ).get( 0 ) );
+        
+        File file = File.createTempFile( "offsetTest", "ldif" );
+        file.deleteOnExit();
+        FileWriter fw = new FileWriter( file );
+        fw.write( ldif );
+        fw.close();
+
+        RandomAccessFile raf = new RandomAccessFile( file, "r" );
+        
+        LdifReader fileReader = new LdifReader( file );
+        
+        LdifEntry rafEntry1 = fileReader.next();
+        
+        data = new byte[rafEntry1.getLengthBeforeParsing()];
+        raf.read( data, ( int ) rafEntry1.getOffset(), data.length );
+
+        reader = new LdifReader();
+        LdifEntry reReadeRafEntry1 = reader.parseLdif( new String( data ) ).get( 0 );
+        assertNotNull( reReadeRafEntry1 );
+        assertEquals( rafEntry1.getOffset(), reReadeRafEntry1.getOffset() );
+        assertEquals( rafEntry1.getLengthBeforeParsing(), reReadeRafEntry1.getLengthBeforeParsing()
);
+        reader.close();
+        
+        LdifEntry rafEntry2 = fileReader.next();
+        
+        data = new byte[rafEntry2.getLengthBeforeParsing()];
+        raf.readFully( data, ( int ) 0, data.length );
+
+        reader = new LdifReader();
+        LdifEntry reReadeRafEntry2 = reader.parseLdif( new String( data ) ).get( 0 );
+        assertNotNull( reReadeRafEntry2 );
+        assertEquals( rafEntry2.getLengthBeforeParsing(), reReadeRafEntry2.getLengthBeforeParsing()
);
+        reader.close();
+    }
 }



Mime
View raw message