下面列出了org.apache.lucene.index.IndexWriter#getLiveCommitData ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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");
}
}
@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");
}
}
/**
* 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;
}
/**
* 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;
}
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);
}
}