Java Code Examples for org.apache.lucene.index.IndexWriter#getLiveCommitData()

The following examples show how to use org.apache.lucene.index.IndexWriter#getLiveCommitData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeleteUserCommitData.java    From clue with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Namespace args, PrintStream out) throws Exception {
	IndexWriter writer = luceneContext.getIndexWriter();
	if (writer != null) {
		String key = args.get("key");
		Iterable<Map.Entry<String, String>> commitData = writer.getLiveCommitData();
		List<Map.Entry<String, String>> commitList = new LinkedList<>();
		for (Map.Entry<String, String> dataEntry : commitData) {
			if (!dataEntry.equals(key)) {
				commitList.add(dataEntry);
			}
		}
	    if (commitList.size() > 0) {
		  writer.setLiveCommitData(commitList);
		  writer.commit();
			luceneContext.refreshReader();
		  out.println("commit data: " + key +" removed.");
	    } else {
		  out.println("no commit data found, no action taken");
	    }
	} else {
		out.println("unable to open writer, index is in readonly mode");
	}
}
 
Example 2
Source File: SaveUserCommitData.java    From clue with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Namespace args, PrintStream out) throws Exception {
	IndexWriter writer = ctx.getIndexWriter();
	String key = args.getString("key");
	String val = args.getString("value");
	if (writer != null) {
		Iterable<Map.Entry<String, String>> commitData = writer.getLiveCommitData();
		HashMap<String, String> commitMap = new HashMap<>();
		if (commitData != null) {
			for (Map.Entry<String, String> entry : commitData) {
				commitMap.put(entry.getKey(), entry.getValue());
			}
		}
		commitMap.put(key, val);
		writer.setLiveCommitData(commitMap.entrySet());
		writer.commit();
		ctx.refreshReader();
		out.println(String.format("commit data key: %s, val: %s  saved.", key, val));
	} else {
		out.println("unable to open writer, index is in readonly mode");
	}
}
 
Example 3
Source File: test.java    From vscode-extension with MIT License 5 votes vote down vote up
/**
 * Gets the commit data from {@link IndexWriter} as a map.
 */
private static Map<String, String> commitDataAsMap(final IndexWriter indexWriter) {
    Map<String, String> commitData = new HashMap<>(6);
    for (Map.Entry<String, String> entry : indexWriter.getLiveCommitData()) {
        commitData.put(entry.getKey(), entry.getValue());
    }
    return commitData;
}
 
Example 4
Source File: InternalEngine.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the commit data from {@link IndexWriter} as a map.
 */
private static Map<String, String> commitDataAsMap(final IndexWriter indexWriter) {
    Map<String, String> commitData = new HashMap<>(6);
    for (Map.Entry<String, String> entry : indexWriter.getLiveCommitData()) {
        commitData.put(entry.getKey(), entry.getValue());
    }
    return commitData;
}
 
Example 5
Source File: PrimaryNode.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public PrimaryNode(IndexWriter writer, int id, long primaryGen, long forcePrimaryVersion,
                   SearcherFactory searcherFactory, PrintStream printStream) throws IOException {
  super(id, writer.getDirectory(), searcherFactory, printStream);
  message("top: now init primary");
  this.writer = writer;
  this.primaryGen = primaryGen;

  try {
    // So that when primary node's IndexWriter finishes a merge, but before it cuts over to the merged segment,
    // it copies it out to the replicas.  This ensures the whole system's NRT latency remains low even when a
    // large merge completes:
    writer.getConfig().setMergedSegmentWarmer(new PreCopyMergedSegmentWarmer(this));

    message("IWC:\n" + writer.getConfig());
    message("dir:\n" + writer.getDirectory());
    message("commitData: " + writer.getLiveCommitData());

    // Record our primaryGen in the userData, and set initial version to 0:
    Map<String,String> commitData = new HashMap<>();
    Iterable<Map.Entry<String,String>> iter = writer.getLiveCommitData();
    if (iter != null) {
      for(Map.Entry<String,String> ent : iter) {
        commitData.put(ent.getKey(), ent.getValue());
      }
    }
    commitData.put(PRIMARY_GEN_KEY, Long.toString(primaryGen));
    if (commitData.get(VERSION_KEY) == null) {
      commitData.put(VERSION_KEY, "0");
      message("add initial commitData version=0");
    } else {
      message("keep current commitData version=" + commitData.get(VERSION_KEY));
    }
    writer.setLiveCommitData(commitData.entrySet(), false);

    // We forcefully advance the SIS version to an unused future version.  This is necessary if the previous primary crashed and we are
    // starting up on an "older" index, else versions can be illegally reused but show different results:
    if (forcePrimaryVersion != -1) {
      message("now forcePrimaryVersion to version=" + forcePrimaryVersion);
      writer.advanceSegmentInfosVersion(forcePrimaryVersion);
    }

    mgr = new SearcherManager(writer, true, true, searcherFactory);
    setCurrentInfos(Collections.<String>emptySet());
    message("init: infos version=" + curInfos.getVersion());

  } catch (Throwable t) {
    message("init: exception");
    t.printStackTrace(printStream);
    throw new RuntimeException(t);
  }
}