use of org.sagebionetworks.repo.model.NodeRevisionBackup in project Synapse-Repository-Services by Sage-Bionetworks.
the class NodeBackupDriverImpl method restoreFromBackup.
/**
* Restore from the backup.
* @throws InterruptedException
*/
@Override
public boolean restoreFromBackup(File source, Progress progress) throws IOException, InterruptedException {
if (source == null)
throw new IllegalArgumentException("Source file cannot be null");
if (!source.exists())
throw new IllegalArgumentException("Source file dose not exist: " + source.getAbsolutePath());
if (progress == null)
throw new IllegalArgumentException("Progress cannot be null");
FileInputStream fis = new FileInputStream(source);
try {
log.info("Restoring: " + source.getAbsolutePath());
progress.appendLog("Restoring: " + source.getAbsolutePath());
// First clear all data
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fis));
progress.setMessage("Reading: " + source.getAbsolutePath());
progress.setTotalCount(source.length());
// We need to map the node type to the node id.
EntityType nodeType = null;
NodeBackup backup = null;
List<NodeRevisionBackup> revisions = null;
ZipEntry entry;
progress.appendLog("Processing nodes:");
while ((entry = zin.getNextEntry()) != null) {
progress.setMessage(entry.getName());
// Check for termination.
checkForTermination(progress);
// Is this a node or a revision?
if (isNodeBackupFile(entry.getName())) {
// Push the current data
if (backup != null) {
createOrUpdateNodeWithRevisions(backup, revisions);
// clear the current data
backup = null;
}
// This is a backup file.
backup = nodeSerializer.readNodeBackup(zin);
// Append this id to the log.
progress.appendLog(backup.getNode().getId());
revisions = new ArrayList<NodeRevisionBackup>();
try {
nodeType = EntityType.valueOf(backup.getNode().getNodeType());
} catch (IllegalArgumentException e) {
// This was likely a deleted entity type.
nodeType = EntityType.unknown;
backup = null;
// for now skip unknown types
continue;
}
migrationDriver.migrateNodePrincipals(backup);
// Are we restoring the root node?
if (backup.getNode().getParentId() == null) {
// This node is a root. Does it match the current root?
String currentRootId = getCurrentRootId();
if (!backup.getNode().getId().equals(currentRootId)) {
// We are being asked to restore a root node but we already have one.
// Since the current root does not match the ID of the root we were given
// we must clear all data and start with a clean database
backupManager.clearAllData();
}
}
} else if (isNodeRevisionFile(entry.getName())) {
// Skip unknown types.
if (EntityType.unknown == nodeType)
continue;
if (backup == null)
throw new IllegalArgumentException("Found a revsions without a matching entity.");
if (revisions == null)
throw new IllegalArgumentException("Found a revisoin without any matching entity");
// This is a revision file.
NodeRevisionBackup revision = NodeSerializerUtil.readNodeRevision(zin);
// Add this to the list
// Migrate the revision to the current version
nodeType = migrationDriver.migrateToCurrentVersion(revision, nodeType);
// nodeType is changed as needed
backup.getNode().setNodeType(nodeType.name());
// Add this to the list of revisions to be processed
revisions.add(revision);
} else {
throw new IllegalArgumentException("Did not recongnize file name: " + entry.getName());
}
progress.incrementProgressBy(entry.getCompressedSize());
if (log.isTraceEnabled()) {
log.trace(progress.toString());
}
// This is run in a tight loop so to be CPU friendly we should yield
Thread.yield();
}
progress.appendLog("Finished processing nodes.");
if (backup != null) {
// do the final backup
createOrUpdateNodeWithRevisions(backup, revisions);
}
} finally {
if (fis != null) {
fis.close();
}
}
return true;
}
use of org.sagebionetworks.repo.model.NodeRevisionBackup in project Synapse-Repository-Services by Sage-Bionetworks.
the class NodeBackupManagerImpl method getNodeRevision.
@Override
public NodeRevisionBackup getNodeRevision(String nodeId, Long revisionId) throws NotFoundException, DatastoreException {
// Pass it along
NodeRevisionBackup rev = nodeBackupDao.getNodeRevision(nodeId, revisionId);
// Make sure the xml version is set to the current version
rev.setXmlVersion(NodeRevisionBackup.CURRENT_XML_VERSION);
return rev;
}
use of org.sagebionetworks.repo.model.NodeRevisionBackup in project Synapse-Repository-Services by Sage-Bionetworks.
the class SearchDocumentDriverImplAutowireTest method testAnnotations.
/**
* All non-blob annotations belong in the free text annotations field, some
* are _also_ facets in the search index
*
* @throws Exception
*/
@Test
public void testAnnotations() throws Exception {
Node node = new Node();
node.setId("5678");
node.setParentId("1234");
node.setETag("0");
node.setNodeType(EntityType.dataset.name());
node.setDescription("Test annotations");
Long nonexistantPrincipalId = 42L;
node.setCreatedByPrincipalId(nonexistantPrincipalId);
node.setCreatedOn(new Date());
node.setModifiedByPrincipalId(nonexistantPrincipalId);
node.setModifiedOn(new Date());
node.setVersionLabel("versionLabel");
NodeRevisionBackup rev = new NodeRevisionBackup();
NamedAnnotations named = new NamedAnnotations();
Annotations primaryAnnos = named.getAdditionalAnnotations();
primaryAnnos.addAnnotation("species", "Dragon");
primaryAnnos.addAnnotation("numSamples", 999L);
Annotations additionalAnnos = named.getAdditionalAnnotations();
additionalAnnos.addAnnotation("Species", "Unicorn");
additionalAnnos.addAnnotation("stringKey", "a multi-word annotation gets underscores so we can exact-match find it");
additionalAnnos.addAnnotation("longKey", 10L);
additionalAnnos.addAnnotation("number_of_samples", "42");
additionalAnnos.addAnnotation("Tissue_Tumor", "ear lobe");
additionalAnnos.addAnnotation("platform", "synapse");
Date dateValue = new Date();
additionalAnnos.addAnnotation("dateKey", dateValue);
additionalAnnos.addAnnotation("blobKey", new String("bytes").getBytes());
rev.setNamedAnnotations(named);
Set<Reference> references = new HashSet<Reference>();
Map<String, Set<Reference>> referenceMap = new HashMap<String, Set<Reference>>();
referenceMap.put("tooMany", references);
node.setReferences(referenceMap);
for (int i = 0; i <= SearchDocumentDriverImpl.FIELD_VALUE_SIZE_LIMIT + 10; i++) {
Reference ref = new Reference();
ref.setTargetId("123" + i);
ref.setTargetVersionNumber(1L);
references.add(ref);
}
Set<ACCESS_TYPE> rwAccessType = new HashSet<ACCESS_TYPE>();
rwAccessType.add(ACCESS_TYPE.READ);
rwAccessType.add(ACCESS_TYPE.UPDATE);
ResourceAccess rwResourceAccess = new ResourceAccess();
// readWriteTest@sagebase.org
rwResourceAccess.setPrincipalId(123L);
rwResourceAccess.setAccessType(rwAccessType);
Set<ACCESS_TYPE> roAccessType = new HashSet<ACCESS_TYPE>();
roAccessType.add(ACCESS_TYPE.READ);
ResourceAccess roResourceAccess = new ResourceAccess();
// readOnlyTest@sagebase.org
roResourceAccess.setPrincipalId(456L);
roResourceAccess.setAccessType(roAccessType);
Set<ResourceAccess> resourceAccesses = new HashSet<ResourceAccess>();
resourceAccesses.add(rwResourceAccess);
resourceAccesses.add(roResourceAccess);
AccessControlList acl = new AccessControlList();
acl.setResourceAccess(resourceAccesses);
EntityPath fakeEntityPath = createFakeEntityPath();
AdapterFactoryImpl adapterFactoryImpl = new AdapterFactoryImpl();
JSONObjectAdapter adapter = adapterFactoryImpl.createNew();
fakeEntityPath.writeToJSONObject(adapter);
String fakeEntityPathJSONString = adapter.toJSONString();
Document document = searchDocumentDriver.formulateSearchDocument(node, rev, acl, fakeEntityPath);
assertEquals(DocumentTypeNames.add, document.getType());
assertEquals("en", document.getLang());
assertEquals(node.getId(), document.getId());
assertTrue(0 < document.getVersion());
DocumentFields fields = document.getFields();
// Check entity property fields
assertEquals(node.getId(), fields.getId());
assertEquals(node.getETag(), fields.getEtag());
assertEquals(node.getParentId(), fields.getParent_id());
assertEquals(node.getName(), fields.getName());
assertEquals("study", fields.getNode_type());
assertEquals(node.getDescription(), fields.getDescription());
// since the Principal doesn't exist, the 'created by' display name defaults to the principal ID
assertEquals("" + nonexistantPrincipalId, fields.getCreated_by());
assertEquals(new Long(node.getCreatedOn().getTime() / 1000), fields.getCreated_on());
// since the Principal doesn't exist, the 'modified by' display name defaults to the principal ID
assertEquals("" + nonexistantPrincipalId, fields.getModified_by());
assertEquals(new Long(node.getModifiedOn().getTime() / 1000), fields.getModified_on());
// Check boost field
assertTrue(fields.getBoost().contains(node.getId()));
assertTrue(fields.getBoost().contains(node.getName()));
// Check the faceted fields
assertEquals(2, fields.getNum_samples().size());
assertEquals(new Long(42), fields.getNum_samples().get(0));
assertEquals(new Long(999), fields.getNum_samples().get(1));
assertEquals(2, fields.getSpecies().size());
assertEquals("Dragon", fields.getSpecies().get(0));
assertEquals("Unicorn", fields.getSpecies().get(1));
assertEquals("ear lobe", fields.getTissue().get(0));
assertEquals("synapse", fields.getPlatform().get(0));
// Check ACL fields
assertEquals(2, fields.getAcl().size());
assertEquals(1, fields.getUpdate_acl().size());
// Make sure our references were trimmed
assertTrue(10 < fields.getReferences().size());
assertEquals(SearchDocumentDriverImpl.FIELD_VALUE_SIZE_LIMIT, fields.getReferences().size());
}
use of org.sagebionetworks.repo.model.NodeRevisionBackup in project Synapse-Repository-Services by Sage-Bionetworks.
the class NodeDAOImplTest method testCreateRevision.
@Test
public void testCreateRevision() throws Exception {
Long currentVersionNumver = new Long(8);
Node node = privateCreateNew("parent");
// Start with a node already on an advanced version
node.setVersionNumber(currentVersionNumver);
node.setVersionComment("Current comment");
node.setVersionLabel("8.0");
node.setNodeType(EntityType.project.name());
String id = nodeDao.createNew(node);
toDelete.add(id);
assertNotNull(id);
node = nodeDao.getNode(id);
assertEquals("8.0", node.getVersionLabel());
assertEquals(currentVersionNumver, node.getVersionNumber());
// now create a new version number
NodeRevisionBackup newRev = new NodeRevisionBackup();
Annotations annos = new Annotations();
annos.addAnnotation("stringKey", "StringValue");
annos.addAnnotation("dateKey", new Date(1000));
annos.addAnnotation("longKey", new Long(123));
annos.addAnnotation("doubleKey", new Double(4.5));
NamedAnnotations nammed = new NamedAnnotations();
nammed.put("newNamed", annos);
String keyOnNewVersion = "NodeDAOImplTest.testCreateRevision.OnNew";
annos.addAnnotation(keyOnNewVersion, "value on new");
newRev.setNamedAnnotations(nammed);
Long newVersionNumber = new Long(1);
newRev.setRevisionNumber(newVersionNumber);
newRev.setNodeId(id);
newRev.setLabel("1.0");
newRev.setModifiedByPrincipalId(creatorUserGroupId);
newRev.setModifiedOn(new Date());
newRev.setReferences(new HashMap<String, Set<Reference>>());
// This annotation should not be query-able
assertFalse(nodeDao.isStringAnnotationQueryable(id, keyOnNewVersion));
// Now create the version
nodeBackupDao.createNewRevisionFromBackup(newRev);
// This annotation should still not be query-able because it is not on the current version.
assertFalse(nodeDao.isStringAnnotationQueryable(id, keyOnNewVersion));
// Get the older version
NodeRevisionBackup clone = nodeBackupDao.getNodeRevision(id, newVersionNumber);
assertNotNull(clone);
assertEquals("value on new", clone.getNamedAnnotations().getAnnotationsForName("newNamed").getSingleValue(keyOnNewVersion));
assertEquals("1.0", clone.getLabel());
assertEquals(newRev, clone);
}
use of org.sagebionetworks.repo.model.NodeRevisionBackup in project Synapse-Repository-Services by Sage-Bionetworks.
the class SearchDocumentDriverImpl method formulateFromBackup.
/**
* @param backup
* @return
* @throws NotFoundException
*/
public Document formulateFromBackup(NodeBackup backup) throws NotFoundException {
Node node = backup.getNode();
if (node.getId() == null)
throw new IllegalArgumentException("node.id cannot be null");
String benefactorId = backup.getBenefactor();
NodeBackup benefactorBackup = backupManager.getNode(benefactorId);
Long revId = node.getVersionNumber();
NodeRevisionBackup rev = backupManager.getNodeRevision(node.getId(), revId);
// get the path
EntityPath entityPath = getEntityPath(node.getId());
Document document = formulateSearchDocument(node, rev, benefactorBackup.getAcl(), entityPath);
return document;
}
Aggregations