org.ojai.store.DocumentMutation Java Examples

The following examples show how to use org.ojai.store.DocumentMutation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ArtistRateDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @param id         identifier of document, which will be updated.
 * @param artistRate artist rate.
 * @return updated artist rate.
 */
@Override
public ArtistRate update(String id, ArtistRate artistRate) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        // Create a DocumentMutation to update non-null fields
        DocumentMutation mutation = connection.newMutation();

        // Update only non-null fields
        if (artistRate.getRating() != null) {
            mutation.set("rating", artistRate.getRating());
        }

        // Update the OJAI Document with specified identifier
        store.update(id, mutation);

        Document updatedOjaiDoc = store.findById(id);

        log.debug("Update document from table '{}' with id: '{}'. Elapsed time: {}", tablePath, id, stopwatch);

        // Map Ojai document to the actual instance of model class
        return mapOjaiDocument(updatedOjaiDoc);
    });
}
 
Example #2
Source File: StatisticDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @param id        identifier of document, which will be updated.
 * @param id        identifier of document, which will be updated.
 * @param statistic statistic.
 * @return updated statistic.
 */
@Override
public Statistic update(String id, Statistic statistic) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        // Create a DocumentMutation to update non-null fields
        DocumentMutation mutation = connection.newMutation();

        // Update only non-null fields
        if (statistic.getDocumentNumber() != null) {
            mutation.set("document_number", statistic.getDocumentNumber());
        }

        // Update the OJAI Document with specified identifier
        store.update(id, mutation);

        Document updatedOjaiDoc = store.findById(id);

        log.debug("Update document from table '{}' with id: '{}'. Elapsed time: {}", tablePath, id, stopwatch);

        // Map Ojai document to the actual instance of model class
        return mapOjaiDocument(updatedOjaiDoc);
    });
}
 
Example #3
Source File: AlbumRateDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @param id        identifier of document, which will be updated.
 * @param albumRate album rate.
 * @return updated album rate.
 */
@Override
public AlbumRate update(String id, AlbumRate albumRate) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        // Create a DocumentMutation to update non-null fields
        DocumentMutation mutation = connection.newMutation();

        // Update only non-null fields
        if (albumRate.getRating() != null) {
            mutation.set("rating", albumRate.getRating());
        }

        // Update the OJAI Document with specified identifier
        store.update(id, mutation);

        Document updatedOjaiDoc = store.findById(id);

        log.debug("Update document from table '{}' with id: '{}'. Elapsed time: {}", tablePath, id, stopwatch);

        // Map Ojai document to the actual instance of model class
        return mapOjaiDocument(updatedOjaiDoc);
    });
}
 
Example #4
Source File: AlbumDao.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes single track according to the specified album identifier and track identifier.
 *
 * @param albumId identifier of album, for which track will be deleted.
 * @param trackId identifier of track, which will be deleted.
 * @return <code>true</code> if track is successfully deleted, <code>false</code> otherwise.
 */
public boolean deleteTrack(String albumId, String trackId) {

    List<Track> existingAlbumTracks = getTracksList(albumId);
    if (existingAlbumTracks == null) {
        return false;
    }

    int trackIndex = getTrackIndexById(existingAlbumTracks, trackId);
    if (trackIndex < 0) {
        return false;
    }

    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        // Delete single track
        DocumentMutation mutation = AlbumMutationBuilder.forConnection(connection)
                .deleteTrack(trackIndex).build();

        // Set update info if available
        getUpdateInfo().ifPresent(updateInfo -> mutation.set("update_info", updateInfo));

        // Update the OJAI Document with specified identifier
        store.update(albumId, mutation);

        log.debug("Deleting album's track with id: '{}' for albumId: '{}' took {}", trackId, albumId, stopwatch);

        return true;
    });

}
 
Example #5
Source File: UserDao.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
@Override
public User update(String id, User user) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        // Create a DocumentMutation to update non-null fields
        DocumentMutation mutation = connection.newMutation();

        // Update only non-null fields
        if (user.getFirstName() != null) {
            mutation.set("first_name", user.getFirstName());
        }

        if (user.getLastName() != null) {
            mutation.set("last_name", user.getLastName());
        }

        // Update the OJAI Document with specified identifier
        store.update(id, mutation);

        Document updatedOjaiDoc = store.findById(id);

        log.debug("Update document from table '{}' with id: '{}'. Elapsed time: {}", tablePath, id, stopwatch);

        // Map Ojai document to the actual instance of model class
        return mapOjaiDocument(updatedOjaiDoc);
    });
}
 
Example #6
Source File: AlbumDao.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @param id    identifier of album, which will be updated.
 * @param album contains album info that will be updated.
 * @return updated album.
 */
@Override
@SuppressWarnings("unchecked")
public Album update(String id, Album album) {

    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        // Update basic fields
        DocumentMutation albumMutation = AlbumMutationBuilder.forConnection(connection)
                .setName(album.getName())
                .setBarcode(album.getBarcode())
                .setCountry(album.getCountry())
                .setLanguage(album.getLanguage())
                .setPackaging(album.getPackaging())
                .setTrackList(album.getTrackList())
                .setArtists(album.getArtists())
                .setFormat(album.getFormat())
                .setDateDay(album.getReleasedDate())
                .setRating(album.getRating())
                .build();

        // Set update info if available
        getUpdateInfo().ifPresent(updateInfo -> albumMutation.set("update_info", updateInfo));

        // Update the OJAI Document with specified identifier
        store.update(id, albumMutation);

        Document updatedOjaiDoc = store.findById(id);

        log.debug("Update document from table '{}' with id: '{}'. Elapsed time: {}", tablePath, id, stopwatch);

        // Map Ojai document to the actual instance of model class
        return mapOjaiDocument(updatedOjaiDoc);
    });
}
 
Example #7
Source File: MapRJsonDocumentLoader.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static void commitMutation(String tableName, String fieldPath, DocumentMutation documentMutation) throws MapRJsonDocumentLoaderException {
  if(isTest) {
    Preconditions.checkNotNull(testDelegate);
    testDelegate.commitMutationInternal(tableName, fieldPath, documentMutation);
  } else {
    Preconditions.checkNotNull(delegate);
    delegate.commitMutationInternal(tableName, fieldPath, documentMutation);
  }
}
 
Example #8
Source File: MapRJsonDocumentLoader.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static DocumentMutation createDocumentMutation() {
  if(isTest) {
    Preconditions.checkNotNull(testDelegate);
    return testDelegate.createDocumentMutationInternal();
  } else {
    Preconditions.checkNotNull(delegate);
    return delegate.createDocumentMutationInternal();
  }
}
 
Example #9
Source File: MapRJson6_0DocumentLoader.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
protected DocumentMutation createDocumentMutationInternal() {
  if(connection == null) {
    startConnection();
  }

  return connection.newMutation();
}
 
Example #10
Source File: MapRJsonTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private DocumentMutation populateDocumentMutation(Record rec) throws OnRecordErrorException {
  DocumentMutation documentMutation = MapRJsonDocumentLoader.createDocumentMutation();
  boolean replace = mapRJsonConfigBean.setOrReplace == SetOrReplace.REPLACE;

  if(rec != null && (rec.get().getType() == Field.Type.LIST_MAP || rec.get().getType() == Field.Type.MAP)) {
    Map<String, Field> fields = rec.get().getValueAsMap();
    for(Map.Entry<String, Field> entry : fields.entrySet()) {
      String path = entry.getKey();
      Field field = entry.getValue();

      //don't add the keyField to the document mutation set. that gets added later
      if(entry.getKey().equals(mapRJsonConfigBean.keyField)) {
        continue;
      }
      switch(field.getType()) {
        case DOUBLE:
          double d = field.getValueAsDouble();
          documentMutation = (replace ? documentMutation.setOrReplace(path, d) : documentMutation.set(path, d));
          break;
        case FLOAT:
          float f = field.getValueAsFloat();
          documentMutation = (replace ? documentMutation.setOrReplace(path, f) : documentMutation.set(path, f));
          break;
        case INTEGER:
          int i = field.getValueAsInteger();
          documentMutation = (replace ? documentMutation.setOrReplace(path, i) : documentMutation.set(path, i));
          break;
        case SHORT:
          short s = field.getValueAsShort();
          documentMutation = (replace ? documentMutation.setOrReplace(path, s) : documentMutation.set(path, s));
          break;
        case LONG:
        case DATE:
        case TIME:
        case DATETIME:
          long l = field.getValueAsLong();
          documentMutation = (replace ? documentMutation.setOrReplace(path, l) : documentMutation.set(path, l));
          break;
        case STRING:
          String st = field.getValueAsString();
          documentMutation = (replace ? documentMutation.setOrReplace(path, st) : documentMutation.set(path, st));
          break;
        case BYTE_ARRAY:
          byte[] ba = field.getValueAsByteArray();
          documentMutation = (replace ? documentMutation.setOrReplace(path, ByteBuffer.wrap(ba)) : documentMutation.set(path, ByteBuffer.wrap(ba)));
          break;
        case BOOLEAN:
        case MAP:
        case LIST:
        case LIST_MAP:
        case CHAR:
        case BYTE:
        default:
          throw new OnRecordErrorException(rec, Errors.MAPR_JSON_14, field.getType().name());
      }
    }
  }

  return documentMutation;
}
 
Example #11
Source File: MapRJsonTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private void performUpdateOperation(Record rec) throws StageException {
  os.reset();
  createJson(os, rec);
  DocumentMutation document = populateDocumentMutation(rec);
  doUpdate(document, rec);
}
 
Example #12
Source File: MapRJson6_0DocumentLoader.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
protected void commitMutationInternal(String tableName, String fieldPath, DocumentMutation documentMutation) throws MapRJsonDocumentLoaderException {
  initTable(tableName, false);
  theStores.get(tableName).update(fieldPath, documentMutation);
}
 
Example #13
Source File: MapRJson5_2DocumentLoader.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
protected void commitMutationInternal(String tableName, String fieldPath, DocumentMutation documentMutation) throws MapRJsonDocumentLoaderException {
  initTable(tableName, false);
  theTables.get(tableName).update(fieldPath, documentMutation);
}
 
Example #14
Source File: MapRJson5_2DocumentLoader.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
protected DocumentMutation createDocumentMutationInternal() {
  return MapRDB.newMutation();
}
 
Example #15
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public boolean checkAndMutate(final String id, final QueryCondition condition, final DocumentMutation m)
    throws StoreException {
  return store.checkAndMutate(id, condition, m);
}
 
Example #16
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public void update(final String id, final DocumentMutation m) throws StoreException {
  store.update(id, m);
}
 
Example #17
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public boolean checkAndMutate(final Value _id, final QueryCondition condition, final DocumentMutation mutation)
    throws StoreException {
  return store.checkAndUpdate(_id, condition, mutation);
}
 
Example #18
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public void update(final Value _id, final DocumentMutation mutation) throws StoreException {
  store.update(_id, mutation);
}
 
Example #19
Source File: JsonDriver.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public DocumentMutation newMutation() {
  throw new UnsupportedOperationException();
}
 
Example #20
Source File: JsonConnection.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public DocumentMutation newMutation() {
  return driver.newMutation();
}
 
Example #21
Source File: AlbumMutationBuilder.java    From mapr-music with Apache License 2.0 4 votes vote down vote up
public DocumentMutation build() {
    return this.mutation;
}
 
Example #22
Source File: ArtistDao.java    From mapr-music with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @param id     identifier of document, which will be updated.
 * @param artist contains artist info that will be updated.
 * @return updated artist.
 */
@Override
public Artist update(String id, Artist artist) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        // Create a DocumentMutation to update non-null fields
        DocumentMutation mutation = connection.newMutation();

        // Update only non-null fields
        if (artist.getName() != null) {
            mutation.set("name", artist.getName());
        }

        if (artist.getGender() != null) {
            mutation.set("gender", artist.getGender());
        }

        if (artist.getBeginDate() != null) {
            mutation.set("begin_date", artist.getBeginDate());
        }

        if (artist.getEndDate() != null) {
            mutation.set("end_date", artist.getEndDate());
        }

        if (artist.getIpi() != null) {
            mutation.set("IPI", artist.getIpi());
        }

        if (artist.getIsni() != null) {
            mutation.set("ISNI", artist.getIsni());
        }

        if (artist.getArea() != null) {
            mutation.set("area", artist.getArea());
        }

        if (artist.getAlbums() != null) {

            List<Map> albumsMapList = artist.getAlbums().stream()
                    .map(album -> mapper.convertValue(album, Map.class))
                    .collect(Collectors.toList());

            mutation.set("albums", albumsMapList);
        }

        if (artist.getProfileImageUrl() != null) {
            mutation.set("profile_image_url", artist.getProfileImageUrl());
        }

        if (artist.getDeleted() != null) {
            mutation.set("deleted", artist.getDeleted());
        }

        if (artist.getRating() != null) {
            mutation.set("rating", artist.getRating());
        }

        // Set update info if available
        getUpdateInfo().ifPresent(updateInfo -> mutation.set("update_info", updateInfo));

        // Update the OJAI Document with specified identifier
        store.update(id, mutation);

        Document updatedOjaiDoc = store.findById(id);

        log.debug("Update document from table '{}' with id: '{}'. Elapsed time: {}", tablePath, id, stopwatch);

        // Map Ojai document to the actual instance of model class
        return mapOjaiDocument(updatedOjaiDoc);
    });
}
 
Example #23
Source File: OjaiCodec.java    From ojai with Apache License 2.0 2 votes vote down vote up
/**
 * Decodes an instance of &lt;T&gt; into an OJAI {@link DocumentMutation}
 *
 * @param encodedMutation an instance of &lt;T&gt; containing encoded OJAI DocumentMutation
 * @return an OJAI {@link DocumentMutation}
 * @throws DecodingException
 */
public DocumentMutation decodeMutation(@NonNullable T encodedMutation) throws DecodingException;
 
Example #24
Source File: OjaiCodec.java    From ojai with Apache License 2.0 2 votes vote down vote up
/**
 * Encodes an OJAI {@link DocumentMutation} into an instance of &lt;T&gt;
 *
 * @param condition OJAI DocumentMutation to encode
 * @return an instance of &lt;T&gt;
 * @throws EncodingException
 */
public T encodeMutation(@NonNullable DocumentMutation mutation) throws EncodingException;
 
Example #25
Source File: MapRJsonDocumentLoader.java    From datacollector with Apache License 2.0 votes vote down vote up
protected abstract DocumentMutation createDocumentMutationInternal(); 
Example #26
Source File: MapRJsonDocumentLoader.java    From datacollector with Apache License 2.0 votes vote down vote up
protected abstract void commitMutationInternal(String tableName, String fieldPath, DocumentMutation documentMutation) throws MapRJsonDocumentLoaderException;