| /******************************************************************************* |
| * Copyright (c) 2000, 2016 IBM Corporation and others. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.dltk.core.search.indexing; |
| |
| import java.io.IOException; |
| |
| import org.eclipse.core.runtime.IPath; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.dltk.core.search.index.Index; |
| import org.eclipse.dltk.internal.core.search.processing.JobManager; |
| import org.eclipse.dltk.internal.core.util.Util; |
| |
| |
| /* |
| * Save the index of a project. |
| */ |
| public class SaveIndex extends IndexRequest { |
| public SaveIndex(IPath containerPath, IndexManager manager) { |
| super(containerPath, manager); |
| } |
| @Override |
| public boolean execute(IProgressMonitor progressMonitor) { |
| |
| if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled()) return true; |
| |
| /* ensure no concurrent write access to index */ |
| Index index = this.manager.getIndex(this.containerPath, true /*reuse index file*/, false /*don't create if none*/); |
| if (index == null) return true; |
| ReadWriteMonitor monitor = index.monitor; |
| if (monitor == null) return true; // index got deleted since acquired |
| |
| try { |
| monitor.enterWrite(); // ask permission to write |
| this.manager.saveIndex(index); |
| } catch (IOException e) { |
| if (JobManager.VERBOSE) { |
| Util.verbose("-> failed to save index " + this.containerPath + " because of the following exception:", System.err); //$NON-NLS-1$ //$NON-NLS-2$ |
| e.printStackTrace(); |
| } |
| return false; |
| } finally { |
| monitor.exitWrite(); // free write lock |
| } |
| return true; |
| } |
| @Override |
| public String toString() { |
| return "saving index for " + this.containerPath; //$NON-NLS-1$ |
| } |
| } |