Commit f91ae821 authored by David Novak's avatar David Novak
Browse files

radical change of locking for insert/delete/search/serialization in the...

radical change of locking for insert/delete/search/serialization in the voronoi tree; it's faster now
parent 6d9b8b2c
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@

    <groupId>mindex</groupId>
    <artifactId>ppp-codes</artifactId>
    <version>3.0.1-DEVEL</version>
    <version>3.0.2-DEVEL</version>
    <packaging>jar</packaging>

    <name>ppp-codes</name>
@@ -40,7 +40,7 @@
                                    <type>${project.packaging}</type>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>/Users/david/ximilar-deploy/version3/common/jars</outputDirectory>
                            <outputDirectory>/Users/david/ximilar-deploy/ximilar-search/common/jars</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
@@ -85,7 +85,7 @@
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>mindex</artifactId>
            <version>3.0.0-DEVEL</version>
            <version>[3.0.2,)</version>
        </dependency>
    </dependencies>
    <description>PPP-Codes index and search algorithm for approximate metric-based search. 
+0 −40
Original line number Diff line number Diff line
@@ -48,9 +48,6 @@ public class PPPCodeIndex extends VoronoiTreeIndex<PPPCodeObject> {
    /** PPP Code serializer - it can be null */
    protected transient PPPCodeReadWriter pppCodeSerializer;

    /** Random generator - it can be null. */
    //protected transient Random random;

    /** The string-integer locator transformer used when the standard data object is transformed to {@link PPPCodeObject}. */
    protected IDString2IntConverter idConverter;

@@ -107,12 +104,6 @@ public class PPPCodeIndex extends VoronoiTreeIndex<PPPCodeObject> {
//        return config.getLongProperty(MIndexProperty.MAX_OBJECT_NUMBER);
    }

//    public Random getRandom() {
//        if (random == null) {
//            random = new Random(System.currentTimeMillis());
//        }
//        return random;
//    }
    // endregion


@@ -210,37 +201,6 @@ public class PPPCodeIndex extends VoronoiTreeIndex<PPPCodeObject> {
    }

    
    // *******************************   Methods processing operations *************************************** //
//
//    /** Pre-created list of supported operations. */
//    private final static List<Class<? extends AbstractOperation>> supportedOperations =
//            Collections.unmodifiableList((List) Arrays.asList(BulkInsertOperation.class, InsertOperation.class, DeleteOperation.class));
//
//    @Override
//    public List<Class<? extends AbstractOperation>> getSupportedOpList() {
//        return PPPCodeIndex.supportedOperations;
//    }
//
//    /**
//     * Method that creates specific implementation of NavigationProcessor for  each type of operation - a single
//     *  PPP-Code index can process only the insert/delete/update operations.
//     * @param operation to be processed
//     * @return a specific implementation of NavigationProcessor
//     */
//    @Override
//    public NavigationProcessor<? extends AbstractOperation> getNavigationProcessor(AbstractOperation operation) {
//        try {
//            // For InsertOperation, BulkInsertOperation and DeleteOperation
//            if (operation instanceof DataManipulationOperation) {
//                return DataManipulationNavigationProcessor.getInstance(operation, this);
//            }
//        } catch (AlgorithmMethodException e) {
//            throw new IllegalStateException(e);
//        }
//        throw new UnsupportedOperationException("PPP-Code single algorithm does not support processing of operation " + operation.getClass());
//    }

    
    // *********************************************   Auxilionary methods   ********************************* //

    /** 
+7 −1
Original line number Diff line number Diff line
@@ -105,4 +105,10 @@ public class PPPCodeSingleAlgorithm extends VoronoiAlgorithm {
        return (PPPCodeIndex) mIndex;
    }

    @Override
    public void beforeStoreToFile(String filepath) {
        super.beforeStoreToFile(filepath);
        getPPPCodeIndex().consolidateTreeData();
    }

}
+10 −10
Original line number Diff line number Diff line
@@ -53,12 +53,12 @@ public class PPPCodeInternalCell extends VoronoiInternalCell<PPPCodeObject> {
     * @throws IOException 
     */
    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
        writeLock();
        try {
        //writeLock();
        //try {
            out.defaultWriteObject();
        } finally {
            writeUnLock();
        }
//        } finally {
//            writeUnLock();
//        }
    }    

    // ************************   Sub-level manipulation  *********************************** //
@@ -93,8 +93,8 @@ public class PPPCodeInternalCell extends VoronoiInternalCell<PPPCodeObject> {
    // ************************    Data manipulation     *************************** //
    
    public void consolidateData(byte [] temporary) {
        readLock();
        try {
//        readLock();
//        try {
            for (Iterator<Map.Entry<Short, VoronoiCell<PPPCodeObject>>> childNodes = getChildNodes(); childNodes.hasNext(); ) {
                VoronoiCell child = childNodes.next().getValue();
                if (child instanceof PPPCodeInternalCell) {
@@ -103,9 +103,9 @@ public class PPPCodeInternalCell extends VoronoiInternalCell<PPPCodeObject> {
                    ((PPPCodeLeafCell) child).consolidateData(temporary);
                }
            }
        } finally {
            readUnLock();
        }
//        } finally {
//            readUnLock();
//        }
    }

    // **********************    For testing purposes    ********************* //
+21 −38
Original line number Diff line number Diff line
@@ -98,9 +98,12 @@ public class PPPCodeLeafCell extends VoronoiLeafCell<PPPCodeObject> implements M
    }
    
    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
        writeLock();
        try {
            if (!isValid()) {
                throw new IOException("Trying to serialize leaf cell which is not valid");
            }
            // consolidation is not actually done in case the leaf node was not modified
            consolidateData(null);
            // write position in bits
            out.writeInt(getOccupationInBits());
@@ -111,6 +114,9 @@ public class PPPCodeLeafCell extends VoronoiLeafCell<PPPCodeObject> implements M
                    out.write(data);
                }
            }
        } finally {
            writeUnlock();
        }
    }

    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
@@ -256,7 +262,7 @@ public class PPPCodeLeafCell extends VoronoiLeafCell<PPPCodeObject> implements M
     * @throws AlgorithmMethodException 
     */
    @Override
    public synchronized SplitConfiguration insertObjects(List<PPPCodeObject> objects) throws AlgorithmMethodException {
    public SplitConfiguration insertObjects(List<PPPCodeObject> objects) throws AlgorithmMethodException {
        if (! ensureWriteOf(objects, getLevel())) {
            return new SplitConfiguration(false, Collections.EMPTY_LIST, objects, null);
        }
@@ -462,7 +468,7 @@ public class PPPCodeLeafCell extends VoronoiLeafCell<PPPCodeObject> implements M
     * @throws AlgorithmMethodException 
     */
    @Override
    public synchronized Collection<PPPCodeObject> deleteObjects(List<PPPCodeObject> objects, int deleteLimit, boolean checkLocator) throws AlgorithmMethodException {
    public Collection<PPPCodeObject> deleteObjects(List<PPPCodeObject> objects, int deleteLimit, boolean checkLocator) throws AlgorithmMethodException {
        // create a map between the PPPs and sets of locators to be deleted for each of the PPP
        Map<ShortArray,Set<Integer>> locatorsToDelete = initPPPLocatorMap(objects);

@@ -565,29 +571,6 @@ public class PPPCodeLeafCell extends VoronoiLeafCell<PPPCodeObject> implements M
        }
    }

    /** 
     * Given a list map of not-deleted PPPs+IDs, this method finds corresponding PPPCOdeObjects from
     *  within the given list.
     */
//    private Collection<PPPCodeObject> findNotDeletedObjects(Map<ShortArray, Set<Integer>> locatorsToDelete, List<PPPCodeObject> objects) {
//        if (locatorsToDelete.isEmpty()) {
//            return Collections.emptyList();
//        }
//        // return the not deleted objects
//        Collection<PPPCodeObject> notDeleted = new ArrayList<>();
//        for (Set<Integer> notDeletedIDs : locatorsToDelete.values()) {
//            for (PPPCodeObject obj : objects) {
//                for (int id : obj.getLocators()) {
//                    if (notDeletedIDs.contains(id)) {
//                        notDeleted.add(obj);
//                        break;
//                    }
//                }
//            }
//        }
//        return notDeleted;
//    }
//
    /**
     * Filters out the deleted locators out from given object with locators. 
     * @param firstLocatorIndex index of the first locator in the passed object within all leaf objects
@@ -703,7 +686,7 @@ public class PPPCodeLeafCell extends VoronoiLeafCell<PPPCodeObject> implements M
     * @param temporary a buffer to be used for the consolidation (so that it does not have to be created for each leaf)
     * @return true, if the consolidation actually took place
     */
    protected synchronized boolean consolidateData(byte [] temporary) {
    protected boolean consolidateData(byte [] temporary) {
        // if the node is either empty, or it was not modified, it does not need any consolidation
        if (data == null || (writeBufferBits == null && deletedIndexes == null)) {
            return false;
Loading