List of usage examples for org.apache.lucene.store RAMDirectory RAMDirectory
public RAMDirectory()
From source file:org.apache.jackrabbit.core.query.lucene.ChainedTermEnumTest.java
License:Apache License
protected TermEnum createTermEnum(String prefix, int numTerms) throws IOException { Directory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(Version.LUCENE_36, new StandardAnalyzer(Version.LUCENE_36))); try {// www .j av a 2s . c o m for (int i = 0; i < numTerms; i++) { Document doc = new Document(); doc.add(new Field("field", true, prefix + i, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO)); writer.addDocument(doc); } } finally { writer.close(); } IndexReader reader = IndexReader.open(dir); try { TermEnum terms = reader.terms(); if (terms.term() == null) { // position at first term terms.next(); } return terms; } finally { reader.close(); } }
From source file:org.apache.jackrabbit.core.query.lucene.directory.IndexInputStreamTest.java
License:Apache License
private void checkStream(int size, int buffer) throws IOException { Random rand = new Random(); byte[] data = new byte[size]; rand.nextBytes(data);// w ww . ja v a 2s .c o m Directory dir = new RAMDirectory(); IndexOutput out = dir.createOutput("test"); out.writeBytes(data, data.length); out.close(); InputStream in = new IndexInputStream(dir.openInput("test")); if (buffer != 0) { in = new BufferedInputStream(in, buffer); } byte[] buf = new byte[3]; int len; int pos = 0; while ((len = in.read(buf)) > -1) { for (int i = 0; i < len; i++, pos++) { assertEquals(data[pos], buf[i]); } } in.close(); // assert length assertEquals(data.length, pos); }
From source file:org.apache.jackrabbit.core.query.lucene.directory.IndexOutputStreamTest.java
License:Apache License
private void checkStream(int size, int buffer) throws IOException { Random rand = new Random(); byte[] data = new byte[size]; rand.nextBytes(data);//from w ww .j ava 2 s . c om Directory dir = new RAMDirectory(); OutputStream out = new IndexOutputStream(dir.createOutput("test")); if (buffer != 0) { out = new BufferedOutputStream(out, buffer); } out.write(data); out.close(); byte[] buf = new byte[3]; int pos = 0; IndexInput in = dir.openInput("test"); for (;;) { int len = (int) Math.min(buf.length, in.length() - pos); in.readBytes(buf, 0, len); for (int i = 0; i < len; i++, pos++) { assertEquals(data[pos], buf[i]); } if (len == 0) { // EOF break; } } in.close(); // assert length assertEquals(data.length, pos); }
From source file:org.apache.jackrabbit.core.query.lucene.directory.RAMDirectoryManager.java
License:Apache License
/** * {@inheritDoc}//w ww. j a v a2s . c o m */ public Directory getDirectory(String name) { synchronized (directories) { Directory dir = directories.get(name); if (dir == null) { dir = new RAMDirectory(); directories.put(name, dir); } return dir; } }
From source file:org.apache.jackrabbit.core.query.lucene.IndexMigrationTest.java
License:Apache License
public void testMigration() throws Exception { List<Document> docs = new ArrayList<Document>(); docs.add(createDocument("ab", "a")); docs.add(createDocument("a", "b")); docs.add(createDocument("abcd", "c")); docs.add(createDocument("abc", "d")); DirectoryManager dirMgr = new RAMDirectoryManager(); PersistentIndex idx = new PersistentIndex("index", new StandardAnalyzer(Version.LUCENE_36), Similarity.getDefault(), new DocNumberCache(100), new IndexingQueue(new IndexingQueueStore(new RAMDirectory())), dirMgr, 0); idx.addDocuments(docs.toArray(new Document[docs.size()])); idx.commit();/*w ww .j a v a 2 s.c o m*/ IndexMigration.migrate(idx, dirMgr, SEP_CHAR); }
From source file:org.apache.jackrabbit.core.query.lucene.VolatileIndex.java
License:Apache License
/** * Creates a new <code>VolatileIndex</code> using an <code>analyzer</code>. * * @param analyzer the analyzer to use./* w w w . j ava 2s. c o m*/ * @param similarity the similarity implementation. * @param indexingQueue the indexing queue. * @throws IOException if an error occurs while opening the index. */ VolatileIndex(Analyzer analyzer, Similarity similarity, IndexingQueue indexingQueue) throws IOException { super(analyzer, similarity, new RAMDirectory(), null, indexingQueue); }
From source file:org.apache.jackrabbit.oak.plugins.index.lucene.IndexCopierTest.java
License:Apache License
@Test public void basicTest() throws Exception { Directory baseDir = new RAMDirectory(); IndexDefinition defn = new IndexDefinition(root, builder.getNodeState()); IndexCopier c1 = new RAMIndexCopier(baseDir, sameThreadExecutor(), getWorkDir()); Directory remote = new RAMDirectory(); Directory wrapped = c1.wrapForRead("/foo", defn, remote); byte[] t1 = writeFile(remote, "t1"); byte[] t2 = writeFile(remote, "t2"); assertEquals(2, wrapped.listAll().length); assertTrue(wrapped.fileExists("t1")); assertTrue(wrapped.fileExists("t2")); assertEquals(t1.length, wrapped.fileLength("t1")); assertEquals(t2.length, wrapped.fileLength("t2")); readAndAssert(wrapped, "t1", t1); //t1 should now be added to testDir assertTrue(baseDir.fileExists("t1")); }
From source file:org.apache.jackrabbit.oak.plugins.index.lucene.IndexCopierTest.java
License:Apache License
@Test public void basicTestWithPrefetch() throws Exception { final List<String> syncedFiles = Lists.newArrayList(); Directory baseDir = new RAMDirectory() { @Override//ww w . j a va 2s .c o m public void sync(Collection<String> names) throws IOException { syncedFiles.addAll(names); super.sync(names); } }; IndexDefinition defn = new IndexDefinition(root, builder.getNodeState()); IndexCopier c1 = new RAMIndexCopier(baseDir, sameThreadExecutor(), getWorkDir(), true); Directory remote = new RAMDirectory(); byte[] t1 = writeFile(remote, "t1"); byte[] t2 = writeFile(remote, "t2"); Directory wrapped = c1.wrapForRead("/foo", defn, remote); assertEquals(2, wrapped.listAll().length); assertThat(syncedFiles, containsInAnyOrder("t1", "t2")); assertTrue(wrapped.fileExists("t1")); assertTrue(wrapped.fileExists("t2")); assertTrue(baseDir.fileExists("t1")); assertTrue(baseDir.fileExists("t2")); assertEquals(t1.length, wrapped.fileLength("t1")); assertEquals(t2.length, wrapped.fileLength("t2")); readAndAssert(wrapped, "t1", t1); }
From source file:org.apache.jackrabbit.oak.plugins.index.lucene.IndexCopierTest.java
License:Apache License
@Test public void nonExistentFile() throws Exception { Directory baseDir = new RAMDirectory(); IndexDefinition defn = new IndexDefinition(root, builder.getNodeState()); CollectingExecutor executor = new CollectingExecutor(); IndexCopier c1 = new RAMIndexCopier(baseDir, executor, getWorkDir(), true); Directory remote = new RAMDirectory(); Directory wrapped = c1.wrapForRead("/foo", defn, remote); try {//from ww w .j ava2s.com wrapped.openInput("foo.txt", IOContext.DEFAULT); fail(); } catch (FileNotFoundException ignore) { } assertEquals(0, executor.commands.size()); }
From source file:org.apache.jackrabbit.oak.plugins.index.lucene.IndexCopierTest.java
License:Apache License
@Test public void basicTestWithFS() throws Exception { IndexDefinition defn = new IndexDefinition(root, builder.getNodeState()); IndexCopier c1 = new IndexCopier(sameThreadExecutor(), getWorkDir()); Directory remote = new RAMDirectory(); Directory wrapped = c1.wrapForRead("/foo", defn, remote); byte[] t1 = writeFile(remote, "t1"); byte[] t2 = writeFile(remote, "t2"); assertEquals(2, wrapped.listAll().length); assertTrue(wrapped.fileExists("t1")); assertTrue(wrapped.fileExists("t2")); assertEquals(t1.length, wrapped.fileLength("t1")); assertEquals(t2.length, wrapped.fileLength("t2")); readAndAssert(wrapped, "t1", t1); //t1 should now be added to testDir File indexDir = c1.getIndexDir(defn, "/foo"); assertTrue(new File(indexDir, "t1").exists()); TabularData td = c1.getIndexPathMapping(); assertEquals(1, td.size());/* w w w . jav a2s.co m*/ }