Author: eevans
Date: Mon Nov 30 03:25:15 2009
New Revision: 885325

URL: http://svn.apache.org/viewvc?rev=885325&view=rev
Log:
JSON importer/exporter tests

Added:
    incubator/cassandra/trunk/test/resources/
    incubator/cassandra/trunk/test/resources/SimpleCF.json
    incubator/cassandra/trunk/test/resources/SuperCF.json
    
incubator/cassandra/trunk/test/unit/org/apache/cassandra/io/SSTableAccessor.java
   (with props)
    incubator/cassandra/trunk/test/unit/org/apache/cassandra/tools/
    
incubator/cassandra/trunk/test/unit/org/apache/cassandra/tools/SSTableExportTest.java
   (with props)
    
incubator/cassandra/trunk/test/unit/org/apache/cassandra/tools/SSTableImportTest.java
   (with props)
Modified:
    incubator/cassandra/trunk/build.xml
    
incubator/cassandra/trunk/src/java/org/apache/cassandra/io/SSTableReader.java
    incubator/cassandra/trunk/test/conf/storage-conf.xml
    incubator/cassandra/trunk/test/unit/org/apache/cassandra/Util.java

Modified: incubator/cassandra/trunk/build.xml
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/build.xml?rev=885325&r1=885324&r2=885325&view=diff
==============================================================================
--- incubator/cassandra/trunk/build.xml (original)
+++ incubator/cassandra/trunk/build.xml Mon Nov 30 03:25:15 2009
@@ -214,6 +214,9 @@
       <classpath refid="cassandra.classpath"/>
       <src path="${test.unit.src}"/>
     </javac>
+    <copy todir="${test.classes}">
+      <fileset dir="${test.dir}"/>
+    </copy>
   </target>
 
    <target name="test" depends="build-test">

Modified: 
incubator/cassandra/trunk/src/java/org/apache/cassandra/io/SSTableReader.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/io/SSTableReader.java?rev=885325&r1=885324&r2=885325&view=diff
==============================================================================
--- 
incubator/cassandra/trunk/src/java/org/apache/cassandra/io/SSTableReader.java 
(original)
+++ 
incubator/cassandra/trunk/src/java/org/apache/cassandra/io/SSTableReader.java 
Mon Nov 30 03:25:15 2009
@@ -186,7 +186,7 @@
         return indexPositions;
     }
 
-    private void loadBloomFilter() throws IOException
+    void loadBloomFilter() throws IOException
     {
         DataInputStream stream = new DataInputStream(new 
FileInputStream(filterFilename()));
         try
@@ -199,7 +199,7 @@
         }
     }
 
-    private void loadIndexFile() throws IOException
+    void loadIndexFile() throws IOException
     {
         BufferedRandomAccessFile input = new 
BufferedRandomAccessFile(indexFilename(), "r");
         try

Modified: incubator/cassandra/trunk/test/conf/storage-conf.xml
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/test/conf/storage-conf.xml?rev=885325&r1=885324&r2=885325&view=diff
==============================================================================
--- incubator/cassandra/trunk/test/conf/storage-conf.xml (original)
+++ incubator/cassandra/trunk/test/conf/storage-conf.xml Mon Nov 30 03:25:15 
2009
@@ -50,6 +50,7 @@
        <ColumnFamily ColumnType="Super" CompareSubcolumnsWith="LongType" 
Name="Super1"/>
        <ColumnFamily ColumnType="Super" CompareSubcolumnsWith="LongType" 
Name="Super2"/>
        <ColumnFamily ColumnType="Super" CompareSubcolumnsWith="LongType" 
Name="Super3"/>
+       <ColumnFamily ColumnType="Super" CompareSubcolumnsWith="UTF8Type" 
Name="Super4"/>
      </Keyspace>
      <Keyspace Name = "Keyspace2">
        <ColumnFamily Name="Standard1"/>

Added: incubator/cassandra/trunk/test/resources/SimpleCF.json
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/test/resources/SimpleCF.json?rev=885325&view=auto
==============================================================================
--- incubator/cassandra/trunk/test/resources/SimpleCF.json (added)
+++ incubator/cassandra/trunk/test/resources/SimpleCF.json Mon Nov 30 03:25:15 
2009
@@ -0,0 +1,4 @@
+{
+ "rowA": [["colAA", "76616c4141", 1, false], ["colAB", "76616c4142", 1, 
false]],
+ "rowB": [["colBA", "76616c4241", 1, false], ["colBB", "76616c4242", 1, false]]
+}

Added: incubator/cassandra/trunk/test/resources/SuperCF.json
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/test/resources/SuperCF.json?rev=885325&view=auto
==============================================================================
--- incubator/cassandra/trunk/test/resources/SuperCF.json (added)
+++ incubator/cassandra/trunk/test/resources/SuperCF.json Mon Nov 30 03:25:15 
2009
@@ -0,0 +1,4 @@
+{
+  "rowA": {"superA": {"deletedAt": -9223372036854775808, "subColumns": 
[["colAA", "76616c75654141", 1, false], ["colAB", "76616c75654142", 1, 
false]]}},
+  "rowB": {"superB": {"deletedAt": -9223372036854775808, "subColumns": 
[["colBA", "76616c75654241", 1, false], ["colBB", "76616c75654242", 1, false]]}}
+}

Modified: incubator/cassandra/trunk/test/unit/org/apache/cassandra/Util.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/test/unit/org/apache/cassandra/Util.java?rev=885325&r1=885324&r2=885325&view=diff
==============================================================================
--- incubator/cassandra/trunk/test/unit/org/apache/cassandra/Util.java 
(original)
+++ incubator/cassandra/trunk/test/unit/org/apache/cassandra/Util.java Mon Nov 
30 03:25:15 2009
@@ -21,6 +21,8 @@
  */
 
 
+import java.io.File;
+import java.io.IOException;
 import java.nio.ByteBuffer;
 
 import org.apache.cassandra.db.Column;
@@ -46,4 +48,11 @@
         bb.putLong(v);
         return bytes;
     }
+    
+    public static File createTemporarySSTable(String keyspace, String colFam) 
throws IOException
+    {
+        File tmpDir = new File(System.getProperty("java.io.tmpdir") + 
File.separator + keyspace);
+        tmpDir.mkdirs();    // Create the per-keyspace temp directory
+        return File.createTempFile(colFam + "-", "-Data.db", tmpDir);
+    }
 }

Added: 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/io/SSTableAccessor.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/test/unit/org/apache/cassandra/io/SSTableAccessor.java?rev=885325&view=auto
==============================================================================
--- 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/io/SSTableAccessor.java
 (added)
+++ 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/io/SSTableAccessor.java
 Mon Nov 30 03:25:15 2009
@@ -0,0 +1,35 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements.  See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership.  The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License.  You may obtain a copy of the License at
+*
+*    http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied.  See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+package org.apache.cassandra.io;
+
+import java.io.IOException;
+
+import org.apache.cassandra.dht.IPartitioner;
+
+public class SSTableAccessor
+{
+    public static SSTableReader getSSTableReader(String filename, 
IPartitioner<?> partitioner)
+    throws IOException
+    {
+        SSTableReader sstable =  new SSTableReader(filename, partitioner, 
null, null, null);
+        sstable.loadBloomFilter();
+        sstable.loadIndexFile();
+        return sstable;
+    }
+}

Propchange: 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/io/SSTableAccessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/tools/SSTableExportTest.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/test/unit/org/apache/cassandra/tools/SSTableExportTest.java?rev=885325&view=auto
==============================================================================
--- 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/tools/SSTableExportTest.java
 (added)
+++ 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/tools/SSTableExportTest.java
 Mon Nov 30 03:25:15 2009
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements.  See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership.  The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License.  You may obtain a copy of the License at
+*
+*    http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied.  See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+package org.apache.cassandra.tools;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.Arrays;
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.db.ColumnFamily;
+import org.apache.cassandra.db.filter.QueryPath;
+import org.apache.cassandra.dht.IPartitioner;
+import org.apache.cassandra.io.DataOutputBuffer;
+import org.apache.cassandra.io.SSTableReader;
+import org.apache.cassandra.io.SSTableWriter;
+import static org.apache.cassandra.Util.createTemporarySSTable;
+import static org.apache.cassandra.utils.FBUtilities.hexToBytes;
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+import org.junit.Test;
+
+public class SSTableExportTest
+{
+    @Test
+    public void testExportSimpleCf() throws IOException
+    {
+        File tempSS = createTemporarySSTable("Keyspace1", "Standard1");
+        ColumnFamily cfamily = ColumnFamily.create("Keyspace1", "Standard1");
+        IPartitioner<?> partitioner = DatabaseDescriptor.getPartitioner();
+        DataOutputBuffer dob = new DataOutputBuffer();
+        SSTableWriter writer = new SSTableWriter(tempSS.getPath(), 2, 
partitioner);
+        
+        // Add rowA
+        cfamily.addColumn(new QueryPath("Standard1", null, "colA".getBytes()), 
"valA".getBytes(), 1, false);
+        ColumnFamily.serializer().serializeWithIndexes(cfamily, dob);
+        writer.append(partitioner.decorateKey("rowA"), dob);
+        dob.reset();
+        cfamily.clear();
+        
+        // Add rowB
+        cfamily.addColumn(new QueryPath("Standard1", null, "colB".getBytes()), 
"valB".getBytes(), 1, false);
+        ColumnFamily.serializer().serializeWithIndexes(cfamily, dob);
+        writer.append(partitioner.decorateKey("rowB"), dob);
+        dob.reset();
+        cfamily.clear();
+     
+        SSTableReader reader = writer.closeAndOpenReader(0);
+        
+        // Export to JSON and verify
+        File tempJson = File.createTempFile("Standard1", ".json");
+        SSTableExport.export(reader, new PrintStream(tempJson.getPath()));
+        
+        JSONObject json = (JSONObject)JSONValue.parse(new 
FileReader(tempJson));
+        
+        JSONArray rowA = (JSONArray)json.get("rowA");
+        JSONArray colA = (JSONArray)rowA.get(0);
+        assert Arrays.equals(hexToBytes((String)colA.get(1)), 
"valA".getBytes());
+        
+        JSONArray rowB = (JSONArray)json.get("rowB");
+        JSONArray colB = (JSONArray)rowB.get(0);
+        assert !(Boolean)colB.get(3);
+    }
+
+    @Test
+    public void testExportSuperCf() throws IOException
+    {
+        File tempSS = createTemporarySSTable("Keyspace1", "Super4");
+        ColumnFamily cfamily = ColumnFamily.create("Keyspace1", "Super4");
+        IPartitioner<?> partitioner = DatabaseDescriptor.getPartitioner();
+        DataOutputBuffer dob = new DataOutputBuffer();
+        SSTableWriter writer = new SSTableWriter(tempSS.getPath(), 2, 
partitioner);
+        
+        // Add rowA
+        cfamily.addColumn(new QueryPath("Super4", "superA".getBytes(), 
"colA".getBytes()), "valA".getBytes(), 1, false);
+        ColumnFamily.serializer().serializeWithIndexes(cfamily, dob);
+        writer.append(partitioner.decorateKey("rowA"), dob);
+        dob.reset();
+        cfamily.clear();
+        
+        // Add rowB
+        cfamily.addColumn(new QueryPath("Super4", "superB".getBytes(), 
"colB".getBytes()), "valB".getBytes(), 1, false);
+        ColumnFamily.serializer().serializeWithIndexes(cfamily, dob);
+        writer.append(partitioner.decorateKey("rowB"), dob);
+        dob.reset();
+        cfamily.clear();
+     
+        SSTableReader reader = writer.closeAndOpenReader(0);
+        
+        // Export to JSON and verify
+        File tempJson = File.createTempFile("Super4", ".json");
+        SSTableExport.export(reader, new PrintStream(tempJson.getPath()));
+        
+        JSONObject json = (JSONObject)JSONValue.parse(new 
FileReader(tempJson));
+        
+        JSONObject rowA = (JSONObject)json.get("rowA");
+        JSONObject superA = 
(JSONObject)rowA.get(cfamily.getComparator().getString("superA".getBytes()));
+        JSONArray subColumns = (JSONArray)superA.get("subColumns");
+        JSONArray colA = (JSONArray)subColumns.get(0);
+        
+        assert Arrays.equals(hexToBytes((String)colA.get(1)), 
"valA".getBytes());
+        assert !(Boolean)colA.get(3);       
+    }
+}

Propchange: 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/tools/SSTableExportTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/tools/SSTableImportTest.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/test/unit/org/apache/cassandra/tools/SSTableImportTest.java?rev=885325&view=auto
==============================================================================
--- 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/tools/SSTableImportTest.java
 (added)
+++ 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/tools/SSTableImportTest.java
 Mon Nov 30 03:25:15 2009
@@ -0,0 +1,67 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements.  See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership.  The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License.  You may obtain a copy of the License at
+*
+*    http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied.  See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+package org.apache.cassandra.tools;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.db.ColumnFamily;
+import org.apache.cassandra.db.IColumn;
+import org.apache.cassandra.db.filter.NamesQueryFilter;
+import org.apache.cassandra.db.filter.QueryPath;
+import org.apache.cassandra.io.SSTableAccessor;
+import org.apache.cassandra.io.SSTableReader;
+import static org.apache.cassandra.utils.FBUtilities.hexToBytes;
+import static org.apache.cassandra.Util.createTemporarySSTable;
+import org.json.simple.parser.ParseException;
+import org.junit.Test;
+
+public class SSTableImportTest
+{   
+    @Test
+    public void testImportSimpleCf() throws IOException, ParseException
+    {
+        // Import JSON to temp SSTable file
+        String jsonUrl = 
getClass().getClassLoader().getResource("resources/SimpleCF.json").getPath();
+        File tempSS = createTemporarySSTable("Keyspace1", "Standard1");
+        SSTableImport.importJson(jsonUrl, "Keyspace1", "Standard1", 
tempSS.getPath());
+
+        // Verify results
+        SSTableReader reader = 
SSTableAccessor.getSSTableReader(tempSS.getPath(), 
DatabaseDescriptor.getPartitioner());
+        NamesQueryFilter qf = new NamesQueryFilter("rowA", new 
QueryPath("Standard1", null, null), "colAA".getBytes());
+        ColumnFamily cf = 
qf.getSSTableColumnIterator(reader).getColumnFamily();
+        assert Arrays.equals(cf.getColumn("colAA".getBytes()).value(), 
hexToBytes("76616c4141"));
+    }
+
+    @Test
+    public void testImportSuperCf() throws IOException, ParseException
+    {
+        String jsonUrl = 
getClass().getClassLoader().getResource("resources/SuperCF.json").getPath();
+        File tempSS = createTemporarySSTable("Keyspace1", "Super4");
+        SSTableImport.importJson(jsonUrl, "Keyspace1", "Super4", 
tempSS.getPath());
+        
+        // Verify results
+        SSTableReader reader = 
SSTableAccessor.getSSTableReader(tempSS.getPath(), 
DatabaseDescriptor.getPartitioner());
+        NamesQueryFilter qf = new NamesQueryFilter("rowA", new 
QueryPath("Super4", null, null), "superA".getBytes());
+        ColumnFamily cf = 
qf.getSSTableColumnIterator(reader).getColumnFamily();
+        IColumn superCol = cf.getColumn("superA".getBytes());
+        assert 
Arrays.equals(superCol.getSubColumn("colAA".getBytes()).value(), 
hexToBytes("76616c75654141"));
+    }
+}

Propchange: 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/tools/SSTableImportTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to