Skip to content

Commit

Permalink
introduced imixs.property 'snapshot.history'
Browse files Browse the repository at this point in the history
issue #9
  • Loading branch information
rsoika committed Nov 5, 2017
1 parent 1e2b654 commit 47ed6fe
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
11 changes: 11 additions & 0 deletions imixs-archive-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ always the $UniqueID from the origin-workitem suffixed with a timestamp.
During the snapshot creation the snapshot $UniqueID is stored into the origin-workitem.


### Snapshot History

The snapshot-service will hold a snapshot history. The snaphsot history can be configured by the imixs property

snapshot.history=1

The _snapshot.history_ defines how many snapshots will be stored into the local database. The default setting is '1' which means that only the latest snapshot will be stored. A setting of '10' will store the latest 10 snaphsot-workitems.
When the history is set to '0', no snapshot-workitems will be removed by the service. This setting is used for external archive systems.




### CDI Events

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.imixs.workflow.WorkflowKernel;
import org.imixs.workflow.engine.DocumentEvent;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.engine.PropertyService;
import org.imixs.workflow.exceptions.QueryException;

/**
Expand Down Expand Up @@ -77,10 +78,14 @@ public class SnapshotService {
@EJB
DocumentService documentService;

@EJB
PropertyService propertyService;

private static Logger logger = Logger.getLogger(SnapshotService.class.getName());

public static String SNAPSHOTID = "$snapshotid";
public static String TYPE_PRAFIX = "snapshot-";
public static String PROPERTY_SNAPSHOT_HISTORY = "snapshot.history";

/**
* The snapshot-workitem is created immediately after the workitem was processed
Expand Down Expand Up @@ -141,7 +146,7 @@ public void onSave(@Observes DocumentEvent documentEvent) {
logger.info("migrating file content from blobWorkitem for document '"
+ documentEvent.getDocument().getUniqueID() + "' ....");
copyFilesFromItemCollection(blobWorkitem, snapshot);
}
}
}

// 5. remove file content form the origin-workitem
Expand All @@ -166,15 +171,15 @@ public void onSave(@Observes DocumentEvent documentEvent) {
// 6. store the snapshot uniqeId into the origin-workitem ($snapshotID)
documentEvent.getDocument().replaceItemValue(SNAPSHOTID, snapshot.getUniqueID());

// 7. remove deprecated snapshots - note: this method should not be called in
// Hadoop mode!
removeDeprecatedSnaphosts(snapshot.getUniqueID());

// save the snapshot immutable and without indexing....
// 7. save the snapshot immutable and without indexing....
snapshot.replaceItemValue(DocumentService.NOINDEX, true);
snapshot.replaceItemValue(DocumentService.IMMUTABLE, true);

documentService.save(snapshot);

// 8. remove deprecated snapshots
cleanSnaphostHistory(snapshot.getUniqueID());

}

/**
Expand Down Expand Up @@ -203,27 +208,45 @@ ItemCollection findLastSnapshot(String uniqueid) {
}

/**
* This method removes all snapshots older than the given current snapshot id
* This method removes all snapshots older than the defined by the imixs
* property 'snapshot.hystory'. If the snapshot.history is set to '0' no
* snapshot-workitems will be removed.
*/
void removeDeprecatedSnaphosts(String snapshotID) {
void cleanSnaphostHistory(String snapshotID) {

if (snapshotID == null || snapshotID.isEmpty()) {
throw new SnapshotException(DocumentService.INVALID_UNIQUEID, "undefined $snapshotID");
throw new SnapshotException(DocumentService.INVALID_UNIQUEID, "invalid " + SNAPSHOTID);
}

// get snapshot-history
int iSnapshotHistory = 1;
try {
iSnapshotHistory = Integer
.parseInt(propertyService.getProperties().getProperty(PROPERTY_SNAPSHOT_HISTORY, "1"));
} catch (NumberFormatException nfe) {
throw new SnapshotException(DocumentService.INVALID_PARAMETER,
"imixs.properties '" + PROPERTY_SNAPSHOT_HISTORY + "' must be a integer value.");
}

logger.fine(PROPERTY_SNAPSHOT_HISTORY + " = " + iSnapshotHistory);

// skip if history = 0
if (iSnapshotHistory == 0) {
return;
}

logger.fine("cleanSnaphostHistory for $snapshotid: " + snapshotID);
String snapshtIDPfafix = snapshotID.substring(0, snapshotID.lastIndexOf('-'));
String query = "SELECT document FROM Document AS document WHERE document.id > '" + snapshtIDPfafix
+ "-' AND document.id < '" + snapshotID + "'";
List<ItemCollection> result = documentService.getDocumentsByQuery(query);
+ "-' AND document.id < '" + snapshotID + "' ORDER BY document.id ASC";

if (result.size() > 0) {
logger.fine("searching deprecated snapshots before snapshot: '" + snapshotID + "'....");
for (ItemCollection oldSnapshot : result) {
logger.fine("remove deprecated snapshot: " + oldSnapshot.getUniqueID());
documentService.remove(oldSnapshot);
}
List<ItemCollection> result = documentService.getDocumentsByQuery(query);
while (result.size() >= iSnapshotHistory) {
ItemCollection oldSnapshot = result.get(0);
logger.fine("remove deprecated snapshot: " + oldSnapshot.getUniqueID());
documentService.remove(oldSnapshot);
result.remove(0);
}

}

/**
Expand Down

0 comments on commit 47ed6fe

Please sign in to comment.