You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2011/11/11 15:36:19 UTC

svn commit: r1200902 - /incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/tdbindex.java

Author: andy
Date: Fri Nov 11 14:36:18 2011
New Revision: 1200902

URL: http://svn.apache.org/viewvc?rev=1200902&view=rev
Log:
Work-in-progress

Added:
    incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/tdbindex.java

Added: incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/tdbindex.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/tdbindex.java?rev=1200902&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/tdbindex.java (added)
+++ incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/tdbindex.java Fri Nov 11 14:36:18 2011
@@ -0,0 +1,131 @@
+/*
+ * 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 dev;
+
+import java.util.List ;
+
+import tdb.cmdline.CmdTDB ;
+import arq.cmd.CmdException ;
+
+import com.hp.hpl.jena.tdb.index.TupleIndex ;
+import com.hp.hpl.jena.tdb.nodetable.NodeTupleTable ;
+import com.hp.hpl.jena.tdb.store.DatasetGraphTDB ;
+import com.hp.hpl.jena.tdb.store.bulkloader.BuilderSecondaryIndexes ;
+import com.hp.hpl.jena.tdb.store.bulkloader.BuilderSecondaryIndexesSequential ;
+import com.hp.hpl.jena.tdb.store.bulkloader.BulkLoader ;
+import com.hp.hpl.jena.tdb.store.bulkloader.LoadMonitor ;
+import com.hp.hpl.jena.tdb.sys.Names ;
+
+/** Create a TDB index.
+ *  Defaults to assuming the "primary" index (SPO or GSPO) already exists.
+ */
+public class tdbindex extends CmdTDB
+{
+    
+    protected tdbindex(String[] argv)
+    {
+        super(argv) ;
+    }        
+    
+    @Override
+    protected String getSummary()
+    {
+        return getCommandName()+" [--desc DATASET | -loc DIR] [SrcIndex] DestIndex" ;
+    }
+
+    @Override
+    protected void exec()
+    {
+        List<String> args = super.getPositional() ;
+
+        if ( args.size() == 0 || args.size() > 2 )
+            throw new CmdException("Wrong number of arguments (expected 1 or 2; got "+args.size()) ; 
+
+        String srcIndex ;
+        String destIndex ;
+        if ( args.size() == 1 )
+        {
+            destIndex = args.get(0) ;
+            if ( destIndex.length() != 3  || destIndex.length() != 4 )
+                throw new CmdException("Index '"+destIndex+"' must be of length 3 or 4") ;
+            if ( destIndex.length() == 3 )
+                srcIndex = Names.primaryIndexTriples ;
+            else
+                srcIndex = Names.primaryIndexQuads ;
+        }
+        else
+        {
+            srcIndex = args.get(0) ;
+            destIndex = args.get(1) ;
+        }
+
+        srcIndex = srcIndex.toUpperCase() ;
+        destIndex = destIndex.toUpperCase() ;
+        
+        DatasetGraphTDB dsg = super.getDatasetGraph() ;
+        copyIndex(dsg, srcIndex, destIndex) ;
+    }
+
+    private static void copyIndex(DatasetGraphTDB dsg, String srcIndex, String destIndex)
+    {
+        LoadMonitor loadMonitor = BulkLoader.createLoadMonitor(dsg, "TDB", false) ;
+        
+        BuilderSecondaryIndexes builder = new BuilderSecondaryIndexesSequential(loadMonitor) ;
+        
+        if ( srcIndex.length() != 3  || srcIndex.length() != 4 )
+            throw new CmdException("Source index '"+srcIndex+"' must be of length 3 or 4") ;
+        
+        if ( destIndex.length() != 3  || destIndex.length() != 4 )
+            throw new CmdException("Destination index '"+destIndex+"' must be of length 3 or 4") ;
+        
+        if ( srcIndex .length() != destIndex.length() )
+            throw new CmdException("Source and destination indexes must be the same tuple length") ;
+
+        NodeTupleTable ntt =
+            ( srcIndex .length() == 3 ) ? dsg.getTripleTable().getNodeTupleTable() : dsg.getQuadTable().getNodeTupleTable() ;
+
+        TupleIndex[] indexes = ntt.getTupleTable().getIndexes() ;
+        
+        TupleIndex srcIdx = find(indexes, srcIndex) ;
+        TupleIndex dstIdx = find(indexes, destIndex) ;
+        
+        if ( srcIdx == null )
+            throw new CmdException("No such index: "+srcIndex) ;
+        if ( dstIdx != null )
+            throw new CmdException("Index already exists: "+destIndex) ;
+        
+        if ( true) throw new RuntimeException("BANG") ;
+        
+        //dstIndex = new Index
+        
+        // What about a builder that knows how to copy from one index to another while exploiting semi-locality?   
+        builder.createSecondaryIndexes(srcIdx, new TupleIndex[] {dstIdx}) ;
+    }
+
+    private static TupleIndex find(TupleIndex[] indexes, String srcIndex)
+    {
+        for ( TupleIndex idx : indexes )
+        {
+            if ( idx.getLabel().equals(srcIndex) )
+                return idx ;
+        }
+        return null ;
+    }
+}
+