Search in sources :

Example 1 with NodeRevisionBackup

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;
}
Also used : EntityType(org.sagebionetworks.repo.model.EntityType) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipEntry(java.util.zip.ZipEntry) NodeBackup(org.sagebionetworks.repo.model.NodeBackup) NodeRevisionBackup(org.sagebionetworks.repo.model.NodeRevisionBackup) FileInputStream(java.io.FileInputStream)

Example 2 with NodeRevisionBackup

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;
}
Also used : NodeRevisionBackup(org.sagebionetworks.repo.model.NodeRevisionBackup)

Example 3 with NodeRevisionBackup

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());
}
Also used : AccessControlList(org.sagebionetworks.repo.model.AccessControlList) ResourceAccess(org.sagebionetworks.repo.model.ResourceAccess) DocumentFields(org.sagebionetworks.repo.model.search.DocumentFields) EntityPath(org.sagebionetworks.repo.model.EntityPath) AdapterFactoryImpl(org.sagebionetworks.schema.adapter.org.json.AdapterFactoryImpl) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Reference(org.sagebionetworks.repo.model.Reference) JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) Node(org.sagebionetworks.repo.model.Node) NodeRevisionBackup(org.sagebionetworks.repo.model.NodeRevisionBackup) Document(org.sagebionetworks.repo.model.search.Document) Date(java.util.Date) NamedAnnotations(org.sagebionetworks.repo.model.NamedAnnotations) Annotations(org.sagebionetworks.repo.model.Annotations) ACCESS_TYPE(org.sagebionetworks.repo.model.ACCESS_TYPE) NamedAnnotations(org.sagebionetworks.repo.model.NamedAnnotations) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with NodeRevisionBackup

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);
}
Also used : NamedAnnotations(org.sagebionetworks.repo.model.NamedAnnotations) Annotations(org.sagebionetworks.repo.model.Annotations) Set(java.util.Set) HashSet(java.util.HashSet) Node(org.sagebionetworks.repo.model.Node) NodeRevisionBackup(org.sagebionetworks.repo.model.NodeRevisionBackup) NamedAnnotations(org.sagebionetworks.repo.model.NamedAnnotations) Date(java.util.Date) Test(org.junit.Test)

Example 5 with NodeRevisionBackup

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;
}
Also used : EntityPath(org.sagebionetworks.repo.model.EntityPath) Node(org.sagebionetworks.repo.model.Node) NodeBackup(org.sagebionetworks.repo.model.NodeBackup) NodeRevisionBackup(org.sagebionetworks.repo.model.NodeRevisionBackup) Document(org.sagebionetworks.repo.model.search.Document)

Aggregations

NodeRevisionBackup (org.sagebionetworks.repo.model.NodeRevisionBackup)42 Test (org.junit.Test)16 NamedAnnotations (org.sagebionetworks.repo.model.NamedAnnotations)12 Node (org.sagebionetworks.repo.model.Node)11 NodeBackup (org.sagebionetworks.repo.model.NodeBackup)9 Date (java.util.Date)7 Annotations (org.sagebionetworks.repo.model.Annotations)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)4 Set (java.util.Set)4 StringWriter (java.io.StringWriter)3 HashMap (java.util.HashMap)3 Before (org.junit.Before)3 AccessControlList (org.sagebionetworks.repo.model.AccessControlList)3 EntityPath (org.sagebionetworks.repo.model.EntityPath)3 EntityType (org.sagebionetworks.repo.model.EntityType)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 ZipEntry (java.util.zip.ZipEntry)2 Project (org.sagebionetworks.repo.model.Project)2