Search in sources :

Example 61 with MetacardImpl

use of ddf.catalog.data.impl.MetacardImpl in project ddf by codice.

the class MetacardResourceStatusTest method getBasicMetacard.

private MetacardImpl getBasicMetacard(String sourceId, String resourceUri) throws URISyntaxException {
    MetacardImpl metacard = new MetacardImpl();
    metacard.setId(METACARD_ID);
    if (sourceId != null) {
        metacard.setSourceId(sourceId);
    }
    metacard.setResourceSize(RESOURCE_SIZE);
    if (resourceUri != null) {
        metacard.setResourceURI(new URI(resourceUri));
    }
    return metacard;
}
Also used : URI(java.net.URI) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 62 with MetacardImpl

use of ddf.catalog.data.impl.MetacardImpl in project ddf by codice.

the class TestMetacardGroomerPlugin method copy.

private Metacard copy(Metacard inputMetacard) {
    MetacardImpl newMetacard = new MetacardImpl(getHybridMetacardType());
    newMetacard.setSourceId(inputMetacard.getSourceId());
    newMetacard.setType(inputMetacard.getMetacardType());
    for (AttributeDescriptor ad : inputMetacard.getMetacardType().getAttributeDescriptors()) {
        newMetacard.setAttribute(inputMetacard.getAttribute(ad.getName()));
    }
    return newMetacard;
}
Also used : AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 63 with MetacardImpl

use of ddf.catalog.data.impl.MetacardImpl in project ddf by codice.

the class MigrationFileWriterTest method testWriteMetacardsWriteObjectFails.

@Test(expected = IOException.class)
public void testWriteMetacardsWriteObjectFails() throws Exception {
    whenNew(ObjectOutputStream.class).withAnyArguments().thenReturn(mockObjectOutputStream);
    doThrow(IOException.class).when(mockObjectOutputStream).writeObject(anyObject());
    List<Result> results = new ArrayList<>();
    results.add(new ResultImpl(new MetacardImpl()));
    mockFile = testFolder.newFile("mockFile");
    fileWriter.writeMetacards(mockFile, results);
}
Also used : ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 64 with MetacardImpl

use of ddf.catalog.data.impl.MetacardImpl in project ddf by codice.

the class SolrCatalogProvider method update.

@Override
public UpdateResponse update(UpdateRequest updateRequest) throws IngestException {
    if (updateRequest == null) {
        throw new IngestException(REQUEST_MUST_NOT_BE_NULL_MESSAGE);
    }
    List<Entry<Serializable, Metacard>> updates = updateRequest.getUpdates();
    // the list of updates, both new and old metacards
    ArrayList<Update> updateList = new ArrayList<>();
    String attributeName = updateRequest.getAttributeName();
    // need an attribute name in order to do query
    if (attributeName == null) {
        throw new IngestException("Attribute name cannot be null. " + "Please provide the name of the attribute.");
    }
    List<String> identifiers = new ArrayList<>();
    // if we have nothing to update, send the empty list
    if (updates == null || updates.size() == 0) {
        return new UpdateResponseImpl(updateRequest, null, new ArrayList<>());
    }
    // Loop to get all identifiers
    for (Entry<Serializable, Metacard> updateEntry : updates) {
        identifiers.add(updateEntry.getKey().toString());
    }
    /* 1a. Create the old Metacard Query */
    String attributeQuery = getQuery(attributeName, identifiers);
    SolrQuery query = new SolrQuery(attributeQuery);
    // Set number of rows to the result size + 1.  The default row size in Solr is 10, so this
    // needs to be set in situations where the number of metacards to update is > 10.  Since there
    // could be more results in the query response than the number of metacards in the update request,
    // 1 is added to the row size, so we can still determine whether we found more metacards than
    // updated metacards provided
    query.setRows(updates.size() + 1);
    QueryResponse idResults = null;
    /* 1b. Execute Query */
    try {
        idResults = solr.query(query, METHOD.POST);
    } catch (SolrServerException | IOException e) {
        LOGGER.info("Failed to query for metacard(s) before update.", e);
    }
    // map of old metacards to be populated
    Map<Serializable, Metacard> idToMetacardMap = new HashMap<>();
    /* 1c. Populate list of old metacards */
    if (idResults != null && idResults.getResults() != null && idResults.getResults().size() != 0) {
        LOGGER.debug("Found {} current metacard(s).", idResults.getResults().size());
        // CHECK updates size assertion
        if (idResults.getResults().size() > updates.size()) {
            throw new IngestException("Found more metacards than updated metacards provided. Please ensure your attribute values match unique records.");
        }
        for (SolrDocument doc : idResults.getResults()) {
            Metacard old;
            try {
                old = client.createMetacard(doc);
            } catch (MetacardCreationException e) {
                LOGGER.info("Unable to create metacard(s) from Solr responses during update.", e);
                throw new IngestException("Could not create metacard(s).");
            }
            if (!idToMetacardMap.containsKey(old.getAttribute(attributeName).getValue())) {
                idToMetacardMap.put(old.getAttribute(attributeName).getValue(), old);
            } else {
                throw new IngestException("The attribute value given [" + old.getAttribute(attributeName).getValue() + "] matched multiple records. Attribute values must at most match only one unique Metacard.");
            }
        }
    }
    if (Metacard.ID.equals(attributeName)) {
        idToMetacardMap.putAll(pendingNrtIndex.getAllPresent(identifiers));
    }
    if (idToMetacardMap.size() == 0) {
        LOGGER.debug("No results found for given attribute values.");
        // return an empty list
        return new UpdateResponseImpl(updateRequest, null, new ArrayList<>());
    }
    /* 2. Update the cards */
    List<Metacard> newMetacards = new ArrayList<>();
    for (Entry<Serializable, Metacard> updateEntry : updates) {
        String localKey = updateEntry.getKey().toString();
        /* 2a. Prepare new Metacard */
        MetacardImpl newMetacard = new MetacardImpl(updateEntry.getValue());
        // Find the exact oldMetacard that corresponds with this newMetacard
        Metacard oldMetacard = idToMetacardMap.get(localKey);
        // matched but another did not
        if (oldMetacard != null) {
            // overwrite the id, in case it has not been done properly/already
            newMetacard.setId(oldMetacard.getId());
            newMetacard.setSourceId(getId());
            newMetacards.add(newMetacard);
            updateList.add(new UpdateImpl(newMetacard, oldMetacard));
        }
    }
    try {
        client.add(newMetacards, isForcedAutoCommit());
    } catch (SolrServerException | SolrException | IOException | MetacardCreationException e) {
        LOGGER.info("Failed to update metacard(s) with Solr.", e);
        throw new IngestException("Failed to update metacard(s).");
    }
    pendingNrtIndex.putAll(updateList.stream().collect(Collectors.toMap(u -> u.getNewMetacard().getId(), u -> copyMetacard(u.getNewMetacard()))));
    return new UpdateResponseImpl(updateRequest, updateRequest.getProperties(), updateList);
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) Serializable(java.io.Serializable) HashMap(java.util.HashMap) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Entry(java.util.Map.Entry) SolrDocument(org.apache.solr.common.SolrDocument) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl) IngestException(ddf.catalog.source.IngestException) SolrException(org.apache.solr.common.SolrException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) IOException(java.io.IOException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Metacard(ddf.catalog.data.Metacard) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse)

Example 65 with MetacardImpl

use of ddf.catalog.data.impl.MetacardImpl in project ddf by codice.

the class SolrProviderTest method testNumericalFields.

@Test
public void testNumericalFields() throws Exception {
    deleteAllIn(provider);
    /* SETUP */
    String doubleField = "hertz";
    double doubleFieldValue = 16065.435;
    String floatField = "inches";
    float floatFieldValue = 4.435f;
    String intField = "count";
    int intFieldValue = 4;
    String longField = "milliseconds";
    long longFieldValue = 9876543293L;
    String shortField = "daysOfTheWeek";
    short shortFieldValue = 1;
    Set<AttributeDescriptor> descriptors = numericalDescriptors(doubleField, floatField, intField, longField, shortField);
    MetacardTypeImpl mType = new MetacardTypeImpl("numberMetacardType", descriptors);
    MetacardImpl customMetacard1 = new MetacardImpl(mType);
    customMetacard1.setAttribute(Metacard.ID, "");
    customMetacard1.setAttribute(doubleField, doubleFieldValue);
    customMetacard1.setAttribute(floatField, floatFieldValue);
    customMetacard1.setAttribute(intField, intFieldValue);
    customMetacard1.setAttribute(longField, longFieldValue);
    customMetacard1.setAttribute(shortField, shortFieldValue);
    create(Arrays.asList((Metacard) customMetacard1));
    // searching double field with int value
    greaterThanQueryAssertion(doubleField, intFieldValue, 1);
    // searching float field with double value
    greaterThanQueryAssertion(floatField, 4.0, 1);
    // searching long field with int value
    greaterThanQueryAssertion(longField, intFieldValue, 1);
    // searching int field with long value
    greaterThanQueryAssertion(intField, 3L, 1);
    // searching int field with long value
    greaterThanQueryAssertion(shortField, 0L, 1);
}
Also used : Metacard(ddf.catalog.data.Metacard) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) Matchers.containsString(org.hamcrest.Matchers.containsString) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Test(org.junit.Test)

Aggregations

MetacardImpl (ddf.catalog.data.impl.MetacardImpl)384 Test (org.junit.Test)247 Metacard (ddf.catalog.data.Metacard)144 ArrayList (java.util.ArrayList)102 Result (ddf.catalog.data.Result)62 HashMap (java.util.HashMap)59 Date (java.util.Date)52 ResultImpl (ddf.catalog.data.impl.ResultImpl)51 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)49 URI (java.net.URI)38 Matchers.anyString (org.mockito.Matchers.anyString)38 QueryRequest (ddf.catalog.operation.QueryRequest)36 QueryResponse (ddf.catalog.operation.QueryResponse)35 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)35 Serializable (java.io.Serializable)33 HashSet (java.util.HashSet)31 QueryImpl (ddf.catalog.operation.impl.QueryImpl)29 MetacardTypeImpl (ddf.catalog.data.impl.MetacardTypeImpl)28 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)27 List (java.util.List)27