Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 65 additions & 137 deletions dotCMS/src/main/java/com/dotmarketing/common/reindex/ReindexEntry.java
Original file line number Diff line number Diff line change
@@ -1,167 +1,95 @@
package com.dotmarketing.common.reindex;

import com.dotcms.annotations.Nullable;
import java.util.Date;

public class ReindexEntry {

private long id;
private String identToIndex;
private int priority;
private boolean delete;
private String serverId;
private String lastResult;
private Date timeEntered;

public Date getTimeEntered() {
return timeEntered;
}

public ReindexEntry setTimeEntered(Date timeEntered) {
this.timeEntered = timeEntered;
return this;
}

public ReindexEntry() {}

public ReindexEntry(long id, String objectToIndex, int priority) {
this.id = id;
this.identToIndex = objectToIndex;
this.priority = priority;
}

public String getLastResult() {
return lastResult;
}

public ReindexEntry setLastResult(String lastResult) {
this.lastResult = lastResult;
return this;
}
import org.immutables.value.Value;

/**
* Immutable value object representing a single entry in the distributed reindex journal
* ({@code dist_reindex_journal}).
*
* <h3>Equality</h3>
* <p>Two entries are equal when they share the same {@code identToIndex}, {@code priority},
* {@code delete} flag, and {@code serverId} — matching the original deduplication semantics
* of the mutable POJO. {@code id}, {@code lastResult}, and {@code timeEntered} are
* {@link Value.Auxiliary auxiliary} and therefore excluded from {@code equals}/{@code hashCode}.</p>
*
* <h3>Construction</h3>
* <pre>{@code
* ReindexEntry entry = ImmutableReindexEntry.builder()
* .id(42L)
* .identToIndex("abc123")
* .priority(0)
* .build(); // isDelete defaults to false
* }</pre>
*/
@Value.Immutable
public abstract class ReindexEntry {

// ── Identity / routing ────────────────────────────────────────────────────

/** DB row id — excluded from equals/hashCode (auxiliary). */
@Value.Auxiliary
public abstract long getId();

/** Contentlet identifier to reindex. */
public abstract String getIdentToIndex();

/** Priority level; also encodes error-retry count (see {@link #errorCount()}). */
public abstract int getPriority();

/**
* @return the id
* {@code true} when the document should be deleted from the index.
* Defaults to {@code false} — failed-reindex records are never deletes.
*/
public long getId() {
return id;
}
@Value.Default
public boolean isDelete() { return false; }

/**
* @param id the id to set
*/
public ReindexEntry setId(long id) {
this.id = id;
return this;
}
/** Server that owns this entry; {@code null} when not assigned to a specific node. */
@Nullable
public abstract String getServerId();

/**
* @return the priority
*/
public int getPriority() {
return priority;
}
// ── Metadata (auxiliary — excluded from equals/hashCode) ─────────────────

/**
* @param priority the priority to set
*/
public ReindexEntry setPriority(int priority) {
this.priority = priority;
return this;
}
/** Result of the last indexing attempt; {@code null} on first attempt. */
@Nullable
@Value.Auxiliary
public abstract String getLastResult();

/**
* @return the delete
*/
public boolean isDelete() {
return delete;
}
/** When this entry was created; {@code null} when populated from the in-memory queue. */
@Nullable
@Value.Auxiliary
public abstract Date getTimeEntered();

/**
* @param delete the delete to set
*/
public ReindexEntry setDelete(boolean delete) {
this.delete = delete;
return this;
}
// ── Derived ───────────────────────────────────────────────────────────────

/**
* @return the identToIndex
* Returns {@code true} when the priority indicates a full reindex operation
* rather than a single-content update.
*/
public String getIdentToIndex() {
return identToIndex;
}

public boolean isReindex() {
return getPriority() >= ReindexQueueFactory.Priority.REINDEX.dbValue();
}

/**
* @param identToIndex the identToIndex to set
*/
public ReindexEntry setIdentToIndex(String identToIndex) {
this.identToIndex = identToIndex;
return this;
}

/**
* @return the serverId
* Returns the number of previous failed attempts encoded in the priority value.
*/
public String getServerId() {
return serverId;
}

/**
* @param serverId the serverId to set
*/
public ReindexEntry setServerId(String serverId) {
this.serverId = serverId;
return this;
}

public int errorCount() {
return this.getPriority() % 100;
return getPriority() % 100;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (delete ? 1231 : 1237);
result = prime * result + ((identToIndex == null) ? 0 : identToIndex.hashCode());
result = prime * result + (int) (priority ^ (priority >>> 32));
result = prime * result + ((serverId == null) ? 0 : serverId.hashCode());
return result;
public String toString() {
return "IndexJournal [id=" + getId() + ", identToIndex=" + getIdentToIndex()
+ ", priority=" + getPriority() + ", delete=" + isDelete()
+ ", serverId=" + getServerId() + "]";
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ReindexEntry other = (ReindexEntry) obj;
if (delete != other.delete)
return false;
if (identToIndex == null) {
if (other.identToIndex != null)
return false;
} else if (!identToIndex.equals(other.identToIndex))
return false;
if (priority != other.priority)
return false;
if (serverId == null) {
if (other.serverId != null)
return false;
} else if (!serverId.equals(other.serverId))
return false;
return true;
}
// ── Factory ───────────────────────────────────────────────────────────────

@Override
public String toString() {
return "IndexJournal [id=" + id + ", identToIndex=" + identToIndex + ", priority=" + priority + ", delete=" + delete + ", serverId="
+ serverId + "]";
/** Returns a new builder for constructing a {@link ReindexEntry}. */
public static ImmutableReindexEntry.Builder builder() {
return ImmutableReindexEntry.builder();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,13 @@ protected List<ReindexEntry> getFailedReindexRecords() throws DotDataException {
priority = Integer.parseInt(map.get("priority").toString());
}

final ReindexEntry ridx = new ReindexEntry()
.setId(identifier)
.setIdentToIndex((String) map.get("ident_to_index"))
.setPriority(priority)
.setTimeEntered((Date) map.get("time_entered"))
.setLastResult(indexVal);
final ReindexEntry ridx = ReindexEntry.builder()
.id(identifier)
.identToIndex((String) map.get("ident_to_index"))
.priority(priority)
.timeEntered((Date) map.get("time_entered"))
.lastResult(indexVal)
.build();
failed.add(ridx);
}
return failed;
Expand Down Expand Up @@ -352,16 +353,13 @@ private void loadUpLocalQueue() throws DotDataException {
}
}

private ReindexEntry mapToReindexEntry(Map<String, Object> map) {
final ReindexEntry entry = new ReindexEntry();
entry.setId(((Number) map.get("id")).longValue());
String identifier = (String) map.get("ident_to_index");
entry.setIdentToIndex(identifier);
entry.setPriority(((Number) (map.get("priority"))).intValue());
entry.setDelete(
((Number) (map.get("dist_action"))).intValue() == ReindexAction.DELETE.ordinal());
return entry;

private ReindexEntry mapToReindexEntry(final Map<String, Object> map) {
return ReindexEntry.builder()
.id(((Number) map.get("id")).longValue())
.identToIndex((String) map.get("ident_to_index"))
.priority(((Number) map.get("priority")).intValue())
.isDelete(((Number) map.get("dist_action")).intValue() == ReindexAction.DELETE.ordinal())
.build();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public void test_getFailedReindexRecords_shouldReturnFailedRecords() throws DotD
final ReindexEntry currentEntry = getReindexEntry(recordId);

//marks the entry as failed and verifies it is returned
currentEntry.setPriority(REINDEX_MAX_FAILURE_ATTEMPTS);
reindexQueueAPI.markAsFailed(currentEntry, "Testing failed record");
reindexQueueAPI.markAsFailed(
ImmutableReindexEntry.builder().from(currentEntry).priority(REINDEX_MAX_FAILURE_ATTEMPTS).build(),
"Testing failed record");

failedRecords = reindexQueueAPI.getFailedReindexRecords();
assertTrue(UtilMethods.isSet(failedRecords));
Expand Down Expand Up @@ -91,8 +92,9 @@ public void test_getFailedReindexRecords_shouldNotReturnAnyRecord() throws DotDa
ReindexEntry currentEntry = getReindexEntry(recordId);

//forces the priority to be set to Priority.REINDEX
currentEntry.setPriority(Priority.REINDEX.ordinal() - 1);
reindexQueueAPI.markAsFailed(currentEntry, "Testing failed record");
reindexQueueAPI.markAsFailed(
ImmutableReindexEntry.builder().from(currentEntry).priority(Priority.REINDEX.ordinal() - 1).build(),
"Testing failed record");
failedRecords = reindexQueueAPI.getFailedReindexRecords();
assertFalse(UtilMethods.isSet(failedRecords));

Expand Down
Loading