Example usage for org.apache.lucene.store RAMDirectory RAMDirectory

List of usage examples for org.apache.lucene.store RAMDirectory RAMDirectory

Introduction

In this page you can find the example usage for org.apache.lucene.store RAMDirectory RAMDirectory.

Prototype

public RAMDirectory(LockFactory lockFactory) 

Source Link

Document

Constructs an empty Directory with the given LockFactory .

Usage

From source file:TestWang.java

License:Open Source License

public void computeMAP(ImageSearcher searcher, String prefix) throws IOException {
    System.out.println("ComputeMap");
    // copy index to ram to be much faster ...
    IndexReader reader = IndexReader.open(new RAMDirectory(FSDirectory.open(new File(indexPath))), true);

    Pattern p = Pattern.compile("([0-9]+).jpg");
    double map = 0;
    double errorRate = 0d;
    double precision10 = 0d;
    double[] pr10cat = new double[10];
    double[] pr10cnt = new double[10];
    for (int i = 0; i < pr10cat.length; i++) {
        pr10cat[i] = 0d;/*from w  ww .  j a v a2 s  .  c  om*/
        pr10cnt[i] = 0d;
    }
    System.out.println("name\tmap\tp@10\terror rate");
    System.out.println("Anzahl Anfragen: " + sampleQueries.length);
    for (int i = 0; i < sampleQueries.length; i++) {
        int id = sampleQueries[i];
        String file = testExtensive + "/" + id + ".jpg";
        ImageSearchHits hits = searcher.search(findDoc(reader, id + ".jpg"), reader);
        int goodOnes = 0;
        double avgPrecision = 0d;
        double precision10temp = 0d;
        int countResults = 0;
        //            System.out.println("Anzahl Hits: " + hits.length());
        for (int j = 0; j < hits.length(); j++) {
            Document d = hits.doc(j);
            String hitsId = d.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
            Matcher matcher = p.matcher(hitsId);
            if (matcher.find())
                hitsId = matcher.group(1);
            else
                fail("Did not get the number ...");
            int testID = Integer.parseInt(hitsId);
            if (testID != id)
                countResults++;
            if ((testID != id) && ((int) Math.floor(id / 100) == (int) Math.floor(testID / 100))) {
                goodOnes++;
                // Only if there is a change in recall
                avgPrecision += (double) goodOnes / (double) countResults;
                //                    System.out.print("x");
                if (j <= 10) {
                    precision10temp += 1d;
                }
            } else {
                if (j == 1) { // error rate
                    errorRate++;
                }
            }
        } // end for loop iterating results.
        //            if (avgPrecision<=0) {
        //                System.out.println("avgPrecision = " + avgPrecision);
        //                System.out.println("goodOnes = " + goodOnes);
        //            }
        //            assertTrue("Check if average precision is > 0", avgPrecision > 0);
        //            assertTrue("Check if goodOnes is > 0", goodOnes > 0);
        avgPrecision = avgPrecision / goodOnes;
        precision10 += precision10temp / 10d;
        // precision @ 10 for each category ...
        pr10cat[(int) Math.floor(id / 100)] += precision10temp / 10d;
        pr10cnt[(int) Math.floor(id / 100)] += 1d;
        map += avgPrecision;
    }
    map = map / sampleQueries.length;
    errorRate = errorRate / sampleQueries.length;
    precision10 = precision10 / sampleQueries.length;
    System.out.print(prefix + "\t");
    System.out.print(String.format("%.5f ", map) + '\t');
    System.out.print(String.format("%.5f ", precision10) + '\t');
    System.out.print(String.format("%.5f ", errorRate) + '\t');
    // precision@10 per category
    for (int i = 0; i < pr10cat.length; i++) {
        double v = 0;
        if (pr10cnt[i] > 0)
            v = pr10cat[i] / pr10cnt[i];
        //            System.out.print(i + ": ");
        System.out.printf("%.5f\t", v);

    }
    System.out.println();
}

From source file:action.indexing.Fragments.java

License:Apache License

public void dirCopy() throws Exception {
    Directory otherDir = null;//  ww w.j  a v  a2  s . co  m

    // START
    Directory ramDir = new RAMDirectory(otherDir);
    // END
}

From source file:ca.ualberta.entitylinking.common.indexing.AliasLuceneIndex.java

License:Open Source License

@SuppressWarnings("resource")
public static boolean exists(String diskDir) {
    Directory dir = null;/*from   w  ww. j a  va 2 s  .c om*/

    try {
        dir = new RAMDirectory(new MMapDirectory(new File(diskDir)));
        if (!IndexReader.indexExists(dir))
            return false;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:ca.ualberta.entitylinking.common.indexing.AliasLuceneIndex.java

License:Open Source License

public boolean loadIndex(String diskDir) {
    Directory dir = null;//from w ww  .j a v  a  2  s  .  co m

    try {
        dir = new RAMDirectory(new MMapDirectory(new File(diskDir)));
        if (!IndexReader.indexExists(dir))
            return false;

        reader = IndexReader.open(dir);
        String[] keyArray = FieldCache.DEFAULT.getStrings(reader, "docID");
        //   int[] sizeArray= FieldCache.DEFAULT.getInts(reader, "size");
        searcher = new IndexSearcher(reader);

        for (int i = 0; i < keyArray.length; i++)
            docIDMap.put(keyArray[i], i);
    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println("Loading index done22!!");
    return true;
}

From source file:ca.ualberta.entitylinking.common.indexing.WikipediaIndex.java

License:Open Source License

public void loadIndex(String diskDir) {
    Directory dir = null;// www.  j  a va2 s . c  o m

    try {
        dir = new RAMDirectory(new MMapDirectory(new File(diskDir)));
        if (!IndexReader.indexExists(dir))
            return;

        reader = IndexReader.open(dir);
        searcher = new IndexSearcher(reader);
        System.out.println("Loading WikipediaIndex done!!!");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.ifountain.compass.CompositeDirectoryWrapperProvider.java

License:Apache License

public Directory wrap(String subIndex, Directory dir) throws SearchEngineException {
    String subLogPrefix = logPrefix + subIndex + " : ";

    List allClassMappings = DomainClassMappingHelper.getDomainClassMappings();
    try {//from w  w w .j  a  v  a  2  s. c o m
        log.info(subLogPrefix + "Checking storageType");

        String storageType = null;
        CompassClassMapping classMapping = getClassMappingForSubIndex(allClassMappings, subIndex);
        if (classMapping != null) {
            storageType = classMapping.getStorageType();
        }
        log.info(subLogPrefix + "storageType in classMapping is " + storageType);

        if (storageType == null) {
            //search the top parent class and find storage type of it
            if (classMapping != null) {
                log.info(subLogPrefix + "Seaching superclasses becasue storageType is " + storageType);
                Class superClass = classMapping.getMappedClassSuperClass();
                while (superClass != null) {
                    log.info(subLogPrefix + "Seaching superclass " + superClass.getName());
                    CompassClassMapping superClassMapping = getClassMappingForMappedClass(allClassMappings,
                            superClass);
                    if (superClassMapping != null) {
                        storageType = superClassMapping.getStorageType();
                        if (storageType == null) //if not found search another top
                        {
                            superClass = superClassMapping.getMappedClassSuperClass();
                        } else //if found stop search
                        {
                            log.info(subLogPrefix + "Found storageType " + storageType + " in superClass "
                                    + superClass.getName());
                            superClass = null;
                        }
                    }
                }
            }
            //if no storageType found in parents, then return dir
            if (storageType == null) {
                log.info(subLogPrefix
                        + "storageType can not be found in supercclasses, using default- FILE_TYPE (given dir) ");
                return dir;
            }
        }

        if (storageType.equalsIgnoreCase(RAM_DIR_TYPE)) {
            log.info(subLogPrefix + "Creating  storageType " + storageType);
            RAMDirectory ramdir = new RAMDirectory(dir);
            ramdir.setLockFactory(dir.getLockFactory());
            return ramdir;
        } else if (storageType.equalsIgnoreCase(MIRRORED_DIR_TYPE)) {
            log.info(subLogPrefix + "Creating  storageType " + storageType);
            return new MemoryMirrorDirectoryWrapper(dir, awaitTermination, maxNumberOfUnProcessedBytes,
                    minNumberOfUnProcessedBytes, doCreateExecutorService());
        } else if (storageType.equalsIgnoreCase(FILE_DIR_TYPE)) {
            log.info(subLogPrefix + "Creating  storageType " + storageType);
            return dir;
        } else {
            log.warn(subLogPrefix + "StorageType is unknown using default- FILE_TYPE (given dir)");
            return dir;
        }

    } catch (IOException e) {
        throw new SearchEngineException("Failed to wrap directory [" + dir + "] with async memory wrapper", e);
    }
}

From source file:com.ifountain.compass.MemoryMirrorDirectoryWrapper.java

License:Apache License

public MemoryMirrorDirectoryWrapper(Directory dir, long awaitTermination, long maxNumberOfUnProcessedBytes,
        long minNumberOfUnProcessedBytes, ExecutorService executorService) throws IOException {
    log.info("Initializing FileAndMemory storage type for directory " + dir.toString() + " awaitTermination :"
            + awaitTermination + " maxNumberOfUnProcessedBytes:" + maxNumberOfUnProcessedBytes
            + " minNumberOfUnProcessedBytes:" + minNumberOfUnProcessedBytes);
    this.dir = dir;
    this.maxNumberOfUnProcessedBytes = maxNumberOfUnProcessedBytes;
    this.minNumberOfUnProcessedBytes = minNumberOfUnProcessedBytes;
    this.ramDir = new RAMDirectory(dir);
    this.ramDir.setLockFactory(dir.getLockFactory());
    this.executorService = executorService;
    this.awaitTermination = awaitTermination;
}

From source file:com.pongasoft.kiwidoc.index.impl.lucene.impl.RAMDirectoryFactory.java

License:Apache License

/**
 * Opens the directory//from  w w  w.  j a v  a 2 s .  c o m
 *
 * @param create <code>true</code> if the directory needs to be
 *               created/erased
 * @return a directory
 * @throws IOException when there is a problem opening the directory
 */
public Directory openDirectory(boolean create) throws IOException {
    if (_directoryPath == null || create)
        return openEmptyRAMDirectory();

    boolean directoryExists = IndexReader.indexExists(_directoryPath);
    if (!directoryExists)
        return openEmptyRAMDirectory();

    Chronos c = new Chronos();
    RAMDirectory ramDirectory = new RAMDirectory(_directoryPath);
    log.info("Opened RAM directory from FS " + _directoryPath + " in " + c.getElapsedTimeAsHMS());
    return ramDirectory;
}

From source file:de.linguatools.disco.DISCO.java

License:Apache License

/***************************************************************************
 * A complete word space can be loaded into RAM to
 * speed up similarity computations. Make sure that you have enough free
 * memory since word spaces can be very large. Also, remember that loading a
 * huge word space into RAM will take some time.
 * @param idxName the word space directory
 * @param loadIntoRAM if true the word space is loaded into RAM
 * @throws IOException/*from   ww w .j  av  a  2s  .  c om*/
 */
public DISCO(String idxName, boolean loadIntoRAM) throws IOException {

    indexName = idxName;
    analyzer = new WhitespaceAnalyzer(version);
    parser = new QueryParser(version, "word", analyzer);
    if (loadIntoRAM == true) {
        indexRAM = new RAMDirectory(FSDirectory.open(new File(indexName)));
        is = new IndexSearcher(IndexReader.open(indexRAM));
    } else {
        is = new IndexSearcher(IndexReader.open(FSDirectory.open(new File(indexName))));
    }
}

From source file:jetbrains.exodus.lucene.DebugExodusDirectory.java

License:Apache License

public DebugExodusDirectory(@NotNull final ContextualEnvironment env,
        @NotNull final StoreConfig contentsStoreConfig, @NotNull final LockFactory lockFactory)
        throws IOException {
    directory = new ExodusDirectory(env, contentsStoreConfig, lockFactory);
    debugDirectory = new RAMDirectory(directory);
    setLockFactory(lockFactory);//from w w  w . j  ava2 s .c o m
}