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() 

Source Link

Document

Constructs an empty Directory .

Usage

From source file:org.apache.blur.lucene.index.BlurIndexWriterTest.java

License:Apache License

@Test
public void testIndexRelocationFencing()
        throws CorruptIndexException, LockObtainFailedException, IOException, InterruptedException {
    final AtomicBoolean fail1 = new AtomicBoolean();
    final AtomicBoolean fail2 = new AtomicBoolean();
    final Path hdfsDirPath = new Path(toUri("./target/tmp/BlurIndexWriterTest"));
    final Directory directory = new RAMDirectory();
    Thread thread1 = new Thread(new Runnable() {
        @Override//from  w  w  w  .  ja va2s  .co  m
        public void run() {
            BlurIndexWriter writer = null;
            try {
                BlurLockFactory blurLockFactory = new BlurLockFactory(_configuration, hdfsDirPath, "node1",
                        "1");
                directory.setLockFactory(blurLockFactory);
                IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_43, new KeywordAnalyzer());
                conf.setInfoStream(getInfoStream());
                writer = new BlurIndexWriter(directory, conf);
                writer.addIndexes(addDir("1"));
                waitToLooseLock();
                writer.prepareCommit();
                fail1.set(true);
            } catch (IOException e) {
                e.printStackTrace();
                if (writer != null) {
                    try {
                        writer.rollback();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    });

    Thread thread2 = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                waitForDirInThread1ToBeAdded(directory);
                BlurLockFactory blurLockFactory = new BlurLockFactory(_configuration, hdfsDirPath, "node2",
                        "2");
                directory.setLockFactory(blurLockFactory);
                IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_43, new KeywordAnalyzer());
                conf.setInfoStream(getInfoStream());
                BlurIndexWriter writer = new BlurIndexWriter(directory, conf);
                obtainLock();
                writer.addIndexes(addDir("2"));
                writer.commit();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
                fail2.set(true);
            }
        }

    });
    thread1.start();
    thread2.start();
    thread1.join();
    thread2.join();

    if (fail1.get()) {
        fail();
    }

    if (fail2.get()) {
        fail();
    }

    DirectoryReader reader = DirectoryReader.open(directory);
    List<AtomicReaderContext> leaves = reader.leaves();
    assertEquals(leaves.size(), 1);
    assertEquals(reader.numDocs(), 1);
    Document document = reader.document(0);
    assertEquals("2", document.get("f"));
    reader.close();
}

From source file:org.apache.blur.lucene.index.BlurIndexWriterTest.java

License:Apache License

private Directory addDir(String v) throws IOException {
    RAMDirectory directory = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_43, new KeywordAnalyzer());
    IndexWriter writer = new IndexWriter(directory, config);
    writer.addDocument(getDoc(v));//  w  w w  .j  av  a 2 s  . c o  m
    writer.close();

    return directory;
}

From source file:org.apache.blur.lucene.search.FacetQueryTest.java

License:Apache License

private IndexReader createIndex(int docCount, int facetFields, boolean ram)
        throws CorruptIndexException, LockObtainFailedException, IOException {
    Directory directory;//from   ww  w  .j  av a  2s .  c  o m
    if (ram) {
        directory = new RAMDirectory();
    } else {
        File dir = new File("./target/tmp/facet_tmp");
        if (dir.exists()) {
            directory = FSDirectory.open(dir);
            if (DirectoryReader.indexExists(directory)) {
                DirectoryReader reader = DirectoryReader.open(directory);
                if (reader.numDocs() == docCount) {
                    return reader;
                }
                reader.close();
                directory.close();
            }
        }
        rmr(dir);
        directory = FSDirectory.open(dir);
    }
    IndexWriterConfig conf = new IndexWriterConfig(LUCENE_VERSION, new KeywordAnalyzer());
    IndexWriter writer = new IndexWriter(directory, conf);
    FieldType fieldType = new FieldType();
    fieldType.setStored(true);
    fieldType.setIndexed(true);
    fieldType.setOmitNorms(true);
    long start = System.nanoTime();
    for (int i = 0; i < docCount; i++) {
        long now = System.nanoTime();
        if (start + TimeUnit.SECONDS.toNanos(5) < now) {
            System.out.println("Indexing doc " + i + " of " + docCount);
            start = System.nanoTime();
        }
        Document document = new Document();
        document.add(new Field("f1", "value", fieldType));
        document.add(new Field("f2", "v" + i, fieldType));
        for (int f = 0; f < facetFields; f++) {
            document.add(new Field("facet" + f, "value", fieldType));
        }
        writer.addDocument(document);
    }
    writer.close();
    return DirectoryReader.open(directory);
}

From source file:org.apache.blur.lucene.search.RandomSuperQueryTest.java

License:Apache License

private Directory createIndex(Random random, Collection<Query> sampler)
        throws CorruptIndexException, LockObtainFailedException, IOException {
    Directory directory = new RAMDirectory();
    String[] columnFamilies = genWords(random, MIN_NUM_COL_FAM, MAX_NUM_COL_FAM, "colfam");
    Map<String, String[]> columns = new HashMap<String, String[]>();
    for (int i = 0; i < columnFamilies.length; i++) {
        columns.put(columnFamilies[i], genWords(random, MIN_NUM_COLS, MAX_NUM_COLS, "col"));
    }//from  w  w w. j a v a  2  s .  c o m
    IndexWriter writer = new IndexWriter(directory,
            new IndexWriterConfig(LUCENE_VERSION, new StandardAnalyzer(LUCENE_VERSION)));
    int numberOfDocs = random.nextInt(MAX_NUM_OF_DOCS) + 1;
    for (int i = 0; i < numberOfDocs; i++) {
        writer.addDocuments(generatSuperDoc(random, columns, sampler));
    }
    writer.close();
    return directory;
}

From source file:org.apache.blur.lucene.search.SuperQueryTest.java

License:Apache License

public static Directory createIndex() throws CorruptIndexException, LockObtainFailedException, IOException {
    Directory directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory,
            new IndexWriterConfig(LUCENE_VERSION, new StandardAnalyzer(LUCENE_VERSION)));
    writer.addDocuments(/*from  w  w w. j av  a 2s.c om*/
            addPrime(Arrays.asList(newDocument(newStringField(ROW_ID, "1"), newStringField(PERSON_NAME, NAME1)),
                    newDocument(newStringField(ROW_ID, "1"), newStringField(PERSON_NAME, NAME1)),
                    newDocument(newStringField(ROW_ID, "1"), newStringField(ADDRESS_STREET, STREET1)))));
    writer.addDocuments(
            addPrime(Arrays.asList(newDocument(newStringField(ROW_ID, "2"), newStringField(PERSON_NAME, NAME2)),
                    newDocument(newStringField(ROW_ID, "2"), newStringField(ADDRESS_STREET, STREET1)))));
    writer.addDocuments(
            addPrime(Arrays.asList(newDocument(newStringField(ROW_ID, "3"), newStringField(PERSON_NAME, NAME1)),
                    newDocument(newStringField(ROW_ID, "3"), newStringField(ADDRESS_STREET, STREET1)),
                    newDocument(newStringField(ROW_ID, "3"), newStringField(ADDRESS_STREET, STREET2)))));
    writer.close();
    return directory;
}

From source file:org.apache.blur.lucene.search.TestingPagingCollector.java

License:Apache License

private static IndexReader getReaderFlatScore(int length) throws Exception {
    _directory = new RAMDirectory();
    IndexWriter indexWriter = new IndexWriter(_directory,
            new IndexWriterConfig(LUCENE_VERSION, new KeywordAnalyzer()));
    for (int i = 0; i < length; i++) {
        Document document = new Document();
        document.add(new StringField("f1", "value", Store.NO));
        document.add(new IntDocValuesField("index", i));
        document.add(new IntField("index", i, Store.YES));
        indexWriter.addDocument(document);
    }/*from  w w w . j  a v a2s.c om*/
    indexWriter.close();
    return DirectoryReader.open(_directory);
}

From source file:org.apache.blur.lucene.security.IndexSearcherTest.java

License:Apache License

private void runTest(int expected, Collection<String> readAuthorizations,
        Collection<String> discoverAuthorizations, Collection<String> discoverableFields)
        throws IOException, ParseException {
    IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_43, new StandardAnalyzer(Version.LUCENE_43));
    Directory dir = new RAMDirectory();
    {//  w ww.ja  v a 2 s .  c  om
        IndexWriter writer = new IndexWriter(dir, conf);
        writer.addDocument(getEmpty());
        writer.commit();
        writer.addDocument(getDoc(0, "(a&b)|d", null, "f1", "f2"));
        writer.addDocument(getDoc(1, "a&b&c", null, "f1", "f2"));
        writer.addDocument(getDoc(2, "a&b&c&e", "a&b&c", "f1", "f2"));
        writer.addDocument(getDoc(3, null, null, "f1", "f2"));// can't find
        writer.close(false);
    }
    DirectoryReader reader = DirectoryReader.open(dir);
    validate(expected, 2, readAuthorizations, discoverAuthorizations, discoverableFields, dir, reader);
    {
        IndexWriter writer = new IndexWriter(dir, conf);
        writer.deleteDocuments(new Term("id", "0"));
        writer.addDocument(getDoc(0, "(a&b)|d", null, "f1", "f2"));
        writer.close(false);
    }
    reader = DirectoryReader.openIfChanged(reader);
    validate(expected, 3, readAuthorizations, discoverAuthorizations, discoverableFields, dir, reader);
    {
        IndexWriter writer = new IndexWriter(dir, conf);
        writer.deleteDocuments(new Term("id", "1"));
        writer.addDocument(getDoc(1, "a&b&c", null, "f1", "f2"));
        writer.close(false);
    }
    reader = DirectoryReader.openIfChanged(reader);
    validate(expected, 4, readAuthorizations, discoverAuthorizations, discoverableFields, dir, reader);
}

From source file:org.apache.blur.manager.indexserver.LocalIndexServer.java

License:Apache License

private Map<String, BlurIndex> openFromDisk() throws IOException {
    String table = _tableContext.getDescriptor().getName();
    Path tablePath = _tableContext.getTablePath();
    File tableFile = new File(tablePath.toUri());
    if (tableFile.isDirectory()) {
        Map<String, BlurIndex> shards = new ConcurrentHashMap<String, BlurIndex>();
        int shardCount = _tableContext.getDescriptor().getShardCount();
        for (int i = 0; i < shardCount; i++) {
            Directory directory;//from ww w. j  a va 2 s  .  c  o  m
            String shardName = ShardUtil.getShardName(BlurConstants.SHARD_PREFIX, i);
            if (_ramDir) {
                directory = new RAMDirectory();
            } else {
                File file = new File(tableFile, shardName);
                file.mkdirs();
                directory = new MMapDirectory(file);
            }
            if (!DirectoryReader.indexExists(directory)) {
                new IndexWriter(directory, new IndexWriterConfig(LUCENE_VERSION, new KeywordAnalyzer()))
                        .close();
            }
            shards.put(shardName, openIndex(table, shardName, directory));
        }
        return shards;
    }
    throw new IOException("Table [" + table + "] not found.");
}

From source file:org.apache.blur.manager.writer.MutatableActionTest.java

License:Apache License

@Test
public void testReplaceRow() throws IOException {
    RAMDirectory directory = new RAMDirectory();
    DirectoryReader reader = getIndexReader(directory);
    IndexWriter writer = new IndexWriter(directory, _conf.clone());
    assertEquals(0, reader.numDocs());/*from  w  ww  .j  a  v  a 2s  .c o  m*/

    Row row = genRow();
    _action.replaceRow(row);
    _action.performMutate(getSearcher(reader, directory), writer);
    reader = commitAndReopen(reader, writer);
    assertEquals(1, reader.numDocs());

    Row row2 = new Row(row);
    List<Column> cols = new ArrayList<Column>();
    cols.add(new Column("n", "v"));
    row2.addToRecords(new Record("1", "fam", cols));

    _action.replaceRow(row2);
    _action.performMutate(getSearcher(reader, directory), writer);
    reader = commitAndReopen(reader, writer);
    assertEquals(2, reader.numDocs());
}

From source file:org.apache.blur.manager.writer.MutatableActionTest.java

License:Apache License

@Test
public void testDeleteRecord() throws IOException {
    RAMDirectory directory = new RAMDirectory();
    DirectoryReader reader = getIndexReader(directory);
    IndexWriter writer = new IndexWriter(directory, _conf.clone());
    assertEquals(0, reader.numDocs());/*  w ww. j a v  a 2s. c  o  m*/

    Row row = genRow();
    List<Column> cols = new ArrayList<Column>();
    cols.add(new Column("n", "v"));
    row.addToRecords(new Record("1", "fam", cols));

    _action.replaceRow(row);
    _action.performMutate(getSearcher(reader, directory), writer);
    reader = commitAndReopen(reader, writer);
    assertEquals(2, reader.numDocs());

    _action.deleteRecord(row.getId(), "1");
    _action.performMutate(getSearcher(reader, directory), writer);
    reader = commitAndReopen(reader, writer);
    assertEquals(1, reader.numDocs());
}