|
| 1 | +/* |
| 2 | + * Copyright 2014 JBoss, by Red Hat, Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.uberfire.ext.metadata.io; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.URI; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Random; |
| 27 | + |
| 28 | +import org.apache.commons.io.FileUtils; |
| 29 | +import org.junit.AfterClass; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.BeforeClass; |
| 32 | +import org.junit.Test; |
| 33 | +import org.uberfire.ext.metadata.backend.lucene.LuceneConfig; |
| 34 | +import org.uberfire.ext.metadata.backend.lucene.LuceneConfigBuilder; |
| 35 | +import org.uberfire.ext.metadata.search.ClusterSegment; |
| 36 | +import org.uberfire.io.IOService; |
| 37 | +import org.uberfire.java.nio.base.FileSystemId; |
| 38 | +import org.uberfire.java.nio.base.SegmentedPath; |
| 39 | +import org.uberfire.java.nio.file.Path; |
| 40 | + |
| 41 | +import static org.junit.Assert.*; |
| 42 | + |
| 43 | +public class LuceneSearchIndexTest { |
| 44 | + |
| 45 | + private int seed = new Random( 10L ).nextInt(); |
| 46 | + |
| 47 | + protected boolean created = false; |
| 48 | + private Map<String, Path> basePaths = new HashMap<String, Path>(); |
| 49 | + |
| 50 | + private LuceneConfig config; |
| 51 | + private IOService ioService = null; |
| 52 | + |
| 53 | + protected static final List<File> tempFiles = new ArrayList<File>(); |
| 54 | + |
| 55 | + @AfterClass |
| 56 | + @BeforeClass |
| 57 | + public static void cleanup() { |
| 58 | + for ( final File tempFile : tempFiles ) { |
| 59 | + FileUtils.deleteQuietly( tempFile ); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + protected IOService ioService() { |
| 64 | + if ( ioService == null ) { |
| 65 | + config = new LuceneConfigBuilder() |
| 66 | + .withInMemoryMetaModelStore() |
| 67 | + .useDirectoryBasedIndex() |
| 68 | + .useInMemoryDirectory() |
| 69 | + .build(); |
| 70 | + |
| 71 | + ioService = new IOServiceIndexedImpl( config.getIndexEngine() ); |
| 72 | + } |
| 73 | + return ioService; |
| 74 | + } |
| 75 | + |
| 76 | + protected static File createTempDirectory() throws IOException { |
| 77 | + final File temp = File.createTempFile( "temp", Long.toString( System.nanoTime() ) ); |
| 78 | + if ( !( temp.delete() ) ) { |
| 79 | + throw new IOException( "Could not delete temp file: " + temp.getAbsolutePath() ); |
| 80 | + } |
| 81 | + if ( !( temp.mkdir() ) ) { |
| 82 | + throw new IOException( "Could not create temp directory: " + temp.getAbsolutePath() ); |
| 83 | + } |
| 84 | + tempFiles.add( temp ); |
| 85 | + return temp; |
| 86 | + } |
| 87 | + |
| 88 | + @Before |
| 89 | + public void setup() throws IOException { |
| 90 | + if ( !created ) { |
| 91 | + final String path = createTempDirectory().getAbsolutePath(); |
| 92 | + System.setProperty( "org.uberfire.nio.git.dir", |
| 93 | + path ); |
| 94 | + System.out.println( ".niogit: " + path ); |
| 95 | + |
| 96 | + for ( String repositoryName : getRepositoryNames() ) { |
| 97 | + |
| 98 | + final URI newRepo = URI.create( "git://" + repositoryName ); |
| 99 | + |
| 100 | + try { |
| 101 | + ioService().newFileSystem( newRepo, |
| 102 | + new HashMap<String, Object>() ); |
| 103 | + |
| 104 | + //Don't ask, but we need to write a single file first in order for indexing to work |
| 105 | + final Path basePath = getDirectoryPath( repositoryName ).resolveSibling( "someNewOtherPath" ); |
| 106 | + ioService().write( basePath.resolve( "dummy" ), |
| 107 | + "<none>" ); |
| 108 | + basePaths.put( repositoryName, |
| 109 | + basePath ); |
| 110 | + |
| 111 | + } catch ( final Exception ex ) { |
| 112 | + ex.fillInStackTrace(); |
| 113 | + System.out.println( ex.getMessage() ); |
| 114 | + } finally { |
| 115 | + created = true; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private String[] getRepositoryNames() { |
| 122 | + return new String[]{ this.getClass().getSimpleName() + "_1", this.getClass().getSimpleName() + "_2" }; |
| 123 | + } |
| 124 | + |
| 125 | + private Path getBasePath( final String repositoryName ) { |
| 126 | + return basePaths.get( repositoryName ); |
| 127 | + } |
| 128 | + |
| 129 | + private Path getDirectoryPath( final String repositoryName ) { |
| 130 | + final Path dir = ioService().get( URI.create( "git://" + repositoryName + "/_someDir" + seed ) ); |
| 131 | + ioService().deleteIfExists( dir ); |
| 132 | + return dir; |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testClusterSegments() throws IOException, InterruptedException { |
| 137 | + //Add test files |
| 138 | + final Path path1 = getBasePath( this.getClass().getSimpleName() + "_1" ).resolve( "indexedFile1.txt" ); |
| 139 | + ioService().write( path1, |
| 140 | + "content1" ); |
| 141 | + final Path path2 = getBasePath( this.getClass().getSimpleName() + "_2" ).resolve( "indexedFile2.txt" ); |
| 142 | + ioService().write( path2, |
| 143 | + "content2" ); |
| 144 | + |
| 145 | + //Setup ClusterSegments |
| 146 | + final ClusterSegment cs1 = new ClusterSegment() { |
| 147 | + @Override |
| 148 | + public String getClusterId() { |
| 149 | + return ( (FileSystemId) getBasePath( LuceneSearchIndexTest.this.getClass().getSimpleName() + "_1" ).getFileSystem() ).id(); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public String[] segmentIds() { |
| 154 | + return new String[]{ ( (SegmentedPath) getBasePath( LuceneSearchIndexTest.this.getClass().getSimpleName() + "_1" ) ).getSegmentId() }; |
| 155 | + } |
| 156 | + }; |
| 157 | + final ClusterSegment cs2 = new ClusterSegment() { |
| 158 | + @Override |
| 159 | + public String getClusterId() { |
| 160 | + return ( (FileSystemId) getBasePath( LuceneSearchIndexTest.this.getClass().getSimpleName() + "_2" ).getFileSystem() ).id(); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public String[] segmentIds() { |
| 165 | + return new String[]{ ( (SegmentedPath) getBasePath( LuceneSearchIndexTest.this.getClass().getSimpleName() + "_2" ) ).getSegmentId() }; |
| 166 | + } |
| 167 | + }; |
| 168 | + |
| 169 | + Thread.sleep( 5000 ); //wait for events to be consumed from jgit -> (notify changes -> watcher -> index) -> lucene index |
| 170 | + |
| 171 | + final Map<String, Object> attributes = new HashMap<String, Object>() {{ |
| 172 | + put( "filename", |
| 173 | + "*.txt" ); |
| 174 | + }}; |
| 175 | + |
| 176 | + { |
| 177 | + final int hits = config.getSearchIndex().searchByAttrsHits( attributes ); |
| 178 | + assertEquals( 2, |
| 179 | + hits ); |
| 180 | + } |
| 181 | + |
| 182 | + { |
| 183 | + final int hits = config.getSearchIndex().searchByAttrsHits( attributes, |
| 184 | + cs1 ); |
| 185 | + assertEquals( 1, |
| 186 | + hits ); |
| 187 | + } |
| 188 | + |
| 189 | + { |
| 190 | + final int hits = config.getSearchIndex().searchByAttrsHits( attributes, |
| 191 | + cs2 ); |
| 192 | + assertEquals( 1, |
| 193 | + hits ); |
| 194 | + } |
| 195 | + |
| 196 | + { |
| 197 | + final int hits = config.getSearchIndex().searchByAttrsHits( attributes, |
| 198 | + cs1, |
| 199 | + cs2 ); |
| 200 | + assertEquals( 2, |
| 201 | + hits ); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | +} |
0 commit comments