Search in sources :

Example 11 with SuppressForbidden

use of org.apache.lucene.util.SuppressForbidden in project lucene-solr by apache.

the class HighFreqTerms method main.

@SuppressForbidden(reason = "System.out required: command line tool")
public static void main(String[] args) throws Exception {
    String field = null;
    int numTerms = DEFAULT_NUMTERMS;
    if (args.length == 0 || args.length > 4) {
        usage();
        System.exit(1);
    }
    Directory dir = FSDirectory.open(Paths.get(args[0]));
    Comparator<TermStats> comparator = new DocFreqComparator();
    for (int i = 1; i < args.length; i++) {
        if (args[i].equals("-t")) {
            comparator = new TotalTermFreqComparator();
        } else {
            try {
                numTerms = Integer.parseInt(args[i]);
            } catch (NumberFormatException e) {
                field = args[i];
            }
        }
    }
    IndexReader reader = DirectoryReader.open(dir);
    TermStats[] terms = getHighFreqTerms(reader, numTerms, field, comparator);
    for (int i = 0; i < terms.length; i++) {
        System.out.printf(Locale.ROOT, "%s:%s \t totalTF = %,d \t docFreq = %,d \n", terms[i].field, terms[i].termtext.utf8ToString(), terms[i].totalTermFreq, terms[i].docFreq);
    }
    reader.close();
}
Also used : IndexReader(org.apache.lucene.index.IndexReader) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) SuppressForbidden(org.apache.lucene.util.SuppressForbidden)

Example 12 with SuppressForbidden

use of org.apache.lucene.util.SuppressForbidden in project lucene-solr by apache.

the class CheckIndex method doMain.

// actual main: returns exit code instead of terminating JVM (for easy testing)
@SuppressForbidden(reason = "System.out required: command line tool")
private static int doMain(String[] args) throws IOException, InterruptedException {
    Options opts;
    try {
        opts = parseOptions(args);
    } catch (IllegalArgumentException e) {
        System.out.println(e.getMessage());
        return 1;
    }
    if (!assertsOn())
        System.out.println("\nNOTE: testing will be more thorough if you run java with '-ea:org.apache.lucene...', so assertions are enabled");
    System.out.println("\nOpening index @ " + opts.indexPath + "\n");
    Directory directory = null;
    Path path = Paths.get(opts.indexPath);
    try {
        if (opts.dirImpl == null) {
            directory = FSDirectory.open(path);
        } else {
            directory = CommandLineUtil.newFSDirectory(opts.dirImpl, path);
        }
    } catch (Throwable t) {
        System.out.println("ERROR: could not open directory \"" + opts.indexPath + "\"; exiting");
        t.printStackTrace(System.out);
        return 1;
    }
    try (Directory dir = directory;
        CheckIndex checker = new CheckIndex(dir)) {
        opts.out = System.out;
        return checker.doCheck(opts);
    }
}
Also used : Path(java.nio.file.Path) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) SuppressForbidden(org.apache.lucene.util.SuppressForbidden)

Example 13 with SuppressForbidden

use of org.apache.lucene.util.SuppressForbidden in project lucene-solr by apache.

the class CheckHdfsIndex method doMain.

// actual main: returns exit code instead of terminating JVM (for easy testing)
@SuppressForbidden(reason = "System.out required: command line tool")
protected static int doMain(String[] args) throws IOException, InterruptedException {
    CheckIndex.Options opts;
    try {
        opts = CheckIndex.parseOptions(args);
    } catch (IllegalArgumentException e) {
        System.out.println(e.getMessage());
        return 1;
    }
    if (!CheckIndex.assertsOn()) {
        System.out.println("\nNOTE: testing will be more thorough if you run java with '-ea:org.apache.lucene...', so assertions are enabled");
    }
    if (opts.getDirImpl() != null) {
        System.out.println("\nIgnoring specified -dir-impl, instead using " + HdfsDirectory.class.getSimpleName());
    }
    System.out.println("\nOpening index @ " + opts.getIndexPath() + "\n");
    Directory directory;
    try {
        directory = new HdfsDirectory(new Path(opts.getIndexPath()), getConf());
    } catch (IOException e) {
        System.out.println("ERROR: could not open hdfs directory \"" + opts.getIndexPath() + "\"; exiting");
        e.printStackTrace(System.out);
        return 1;
    }
    try (Directory dir = directory;
        CheckIndex checker = new CheckIndex(dir)) {
        opts.setOut(System.out);
        return checker.doCheck(opts);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) HdfsDirectory(org.apache.solr.store.hdfs.HdfsDirectory) IOException(java.io.IOException) CheckIndex(org.apache.lucene.index.CheckIndex) HdfsDirectory(org.apache.solr.store.hdfs.HdfsDirectory) Directory(org.apache.lucene.store.Directory) SuppressForbidden(org.apache.lucene.util.SuppressForbidden)

Example 14 with SuppressForbidden

use of org.apache.lucene.util.SuppressForbidden in project lucene-solr by apache.

the class LockVerifyServer method main.

@SuppressForbidden(reason = "System.out required: command line tool")
public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.out.println("Usage: java org.apache.lucene.store.LockVerifyServer bindToIp clients\n");
        System.exit(1);
    }
    int arg = 0;
    final String hostname = args[arg++];
    final int maxClients = Integer.parseInt(args[arg++]);
    try (final ServerSocket s = new ServerSocket()) {
        s.setReuseAddress(true);
        // initially 30 secs to give clients enough time to startup
        s.setSoTimeout(30000);
        s.bind(new InetSocketAddress(hostname, 0));
        final InetSocketAddress localAddr = (InetSocketAddress) s.getLocalSocketAddress();
        System.out.println("Listening on " + localAddr + "...");
        // we set the port as a sysprop, so the ANT task can read it. For that to work, this server must run in-process:
        System.setProperty("lockverifyserver.port", Integer.toString(localAddr.getPort()));
        final Object localLock = new Object();
        final int[] lockedID = new int[1];
        lockedID[0] = -1;
        final CountDownLatch startingGun = new CountDownLatch(1);
        final Thread[] threads = new Thread[maxClients];
        for (int count = 0; count < maxClients; count++) {
            final Socket cs = s.accept();
            threads[count] = new Thread() {

                @Override
                public void run() {
                    try (InputStream in = cs.getInputStream();
                        OutputStream os = cs.getOutputStream()) {
                        final int id = in.read();
                        if (id < 0) {
                            throw new IOException("Client closed connection before communication started.");
                        }
                        startingGun.await();
                        os.write(43);
                        os.flush();
                        while (true) {
                            final int command = in.read();
                            if (command < 0) {
                                // closed
                                return;
                            }
                            synchronized (localLock) {
                                final int currentLock = lockedID[0];
                                if (currentLock == -2) {
                                    // another thread got error, so we exit, too!
                                    return;
                                }
                                switch(command) {
                                    case 1:
                                        // Locked
                                        if (currentLock != -1) {
                                            lockedID[0] = -2;
                                            throw new IllegalStateException("id " + id + " got lock, but " + currentLock + " already holds the lock");
                                        }
                                        lockedID[0] = id;
                                        break;
                                    case 0:
                                        // Unlocked
                                        if (currentLock != id) {
                                            lockedID[0] = -2;
                                            throw new IllegalStateException("id " + id + " released the lock, but " + currentLock + " is the one holding the lock");
                                        }
                                        lockedID[0] = -1;
                                        break;
                                    default:
                                        throw new RuntimeException("Unrecognized command: " + command);
                                }
                                os.write(command);
                                os.flush();
                            }
                        }
                    } catch (RuntimeException | Error e) {
                        throw e;
                    } catch (Exception ioe) {
                        throw new RuntimeException(ioe);
                    } finally {
                        IOUtils.closeWhileHandlingException(cs);
                    }
                }
            };
            threads[count].start();
        }
        // start
        System.out.println("All clients started, fire gun...");
        startingGun.countDown();
        // wait for all threads to finish
        for (Thread t : threads) {
            t.join();
        }
        // cleanup sysprop
        System.clearProperty("lockverifyserver.port");
        System.out.println("Server terminated.");
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) SuppressForbidden(org.apache.lucene.util.SuppressForbidden)

Example 15 with SuppressForbidden

use of org.apache.lucene.util.SuppressForbidden in project lucene-solr by apache.

the class MMapDirectory method unmapHackImpl.

@SuppressForbidden(reason = "Needs access to private APIs in DirectBuffer, sun.misc.Cleaner, and sun.misc.Unsafe to enable hack")
private static Object unmapHackImpl() {
    final Lookup lookup = lookup();
    try {
        try {
            // *** sun.misc.Unsafe unmapping (Java 9+) ***
            final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
            // first check if Unsafe has the right method, otherwise we can give up
            // without doing any security critical stuff:
            final MethodHandle unmapper = lookup.findVirtual(unsafeClass, "invokeCleaner", methodType(void.class, ByteBuffer.class));
            // fetch the unsafe instance and bind it to the virtual MH:
            final Field f = unsafeClass.getDeclaredField("theUnsafe");
            f.setAccessible(true);
            final Object theUnsafe = f.get(null);
            return newBufferCleaner(ByteBuffer.class, unmapper.bindTo(theUnsafe));
        } catch (SecurityException se) {
            // rethrow to report errors correctly (we need to catch it here, as we also catch RuntimeException below!):
            throw se;
        } catch (ReflectiveOperationException | RuntimeException e) {
            // *** sun.misc.Cleaner unmapping (Java 8) ***
            final Class<?> directBufferClass = Class.forName("java.nio.DirectByteBuffer");
            final Method m = directBufferClass.getMethod("cleaner");
            m.setAccessible(true);
            final MethodHandle directBufferCleanerMethod = lookup.unreflect(m);
            final Class<?> cleanerClass = directBufferCleanerMethod.type().returnType();
            /* "Compile" a MH that basically is equivalent to the following code:
         * void unmapper(ByteBuffer byteBuffer) {
         *   sun.misc.Cleaner cleaner = ((java.nio.DirectByteBuffer) byteBuffer).cleaner();
         *   if (Objects.nonNull(cleaner)) {
         *     cleaner.clean();
         *   } else {
         *     noop(cleaner); // the noop is needed because MethodHandles#guardWithTest always needs ELSE
         *   }
         * }
         */
            final MethodHandle cleanMethod = lookup.findVirtual(cleanerClass, "clean", methodType(void.class));
            final MethodHandle nonNullTest = lookup.findStatic(Objects.class, "nonNull", methodType(boolean.class, Object.class)).asType(methodType(boolean.class, cleanerClass));
            final MethodHandle noop = dropArguments(constant(Void.class, null).asType(methodType(void.class)), 0, cleanerClass);
            final MethodHandle unmapper = filterReturnValue(directBufferCleanerMethod, guardWithTest(nonNullTest, cleanMethod, noop)).asType(methodType(void.class, ByteBuffer.class));
            return newBufferCleaner(directBufferClass, unmapper);
        }
    } catch (SecurityException se) {
        return "Unmapping is not supported, because not all required permissions are given to the Lucene JAR file: " + se + " [Please grant at least the following permissions: RuntimePermission(\"accessClassInPackage.sun.misc\") " + " and ReflectPermission(\"suppressAccessChecks\")]";
    } catch (ReflectiveOperationException | RuntimeException e) {
        return "Unmapping is not supported on this platform, because internal Java APIs are not compatible with this Lucene version: " + e;
    }
}
Also used : Method(java.lang.reflect.Method) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) Field(java.lang.reflect.Field) MethodHandle(java.lang.invoke.MethodHandle) SuppressForbidden(org.apache.lucene.util.SuppressForbidden)

Aggregations

SuppressForbidden (org.apache.lucene.util.SuppressForbidden)17 IOException (java.io.IOException)6 Path (java.nio.file.Path)6 Directory (org.apache.lucene.store.Directory)6 FSDirectory (org.apache.lucene.store.FSDirectory)5 ArrayList (java.util.ArrayList)4 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 LineNumberReader (java.io.LineNumberReader)2 OutputStream (java.io.OutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 InetSocketAddress (java.net.InetSocketAddress)2 Socket (java.net.Socket)2 StringTokenizer (java.util.StringTokenizer)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Pattern (java.util.regex.Pattern)2 BufferedOutputStream (java.io.BufferedOutputStream)1