com.datastax.driver.core.BatchStatement.Type Java Examples

The following examples show how to use com.datastax.driver.core.BatchStatement.Type. 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: CassandraVideoV2Dao.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private ListenableFuture<Set<String>> removeFavoriteTags(UUID placeId, UUID recordingId, Set<String> tags) {
	VideoMetadata metadata = findByPlaceAndId(placeId, recordingId);
	if(metadata != null && metadata.getTags().contains(VideoConstants.TAG_FAVORITE)) {
		BatchStatement stmt = new BatchStatement(BatchStatement.Type.LOGGED);
		addStatementsForRemoveFromFavoriteTables(stmt, metadata);	
		return Futures.transformAsync(
				VideoV2Util.executeAsyncAndUpdateTimer(session, stmt, RemoveTagsTimer),
            (AsyncFunction<ResultSet, Set<String>>) input -> {
            	Set<String> expectedTags = new HashSet<>(metadata.getTags());
   				expectedTags.removeAll(tags);
               return Futures.immediateFuture(expectedTags);
            },
            MoreExecutors.directExecutor()
         );
	}else{
		logger.warn("Can not removeFavoriteTags.  Either recording id [{}] is invalid or video does not contain Favorite tag [{}]", recordingId, metadata.getTags());
		return Futures.immediateFuture(ImmutableSet.<String>of());
		
	}
}
 
Example #2
Source File: CassandraVideoV2Dao.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private void addStatementsForRemoveFromFavoriteTables(BatchStatement stmt, VideoMetadata metadata) {
	UUID placeId = metadata.getPlaceId();
	UUID recordingId = metadata.getRecordingId();
	//Delete placeRecordingIndexFavorite
	for(String tag: metadata.getTags()) {
		stmt.add(placeRecordingIndexFavorite.deleteTag(placeId, recordingId, tag));
	}
	stmt.add(placeRecordingIndexFavorite.deleteVideo(placeId, recordingId, PlaceRecordingIndexV2Table.Type.RECORDING));
	stmt.add(placeRecordingIndexFavorite.deleteCamera(placeId, recordingId, metadata.getCameraId()));
	if(metadata.isDeleted()) {
		stmt.add(placeRecordingIndexFavorite.deleteDeleted(placeId, recordingId));
	}
	//Delete metadata
	stmt.add(recordingMetadataFavoriteTable.deleteRecording(recordingId));
	//Delete recording
	stmt.add(recordingFavoriteTable.deleteRecording(recordingId));
	//Add to purge table
	Date purgeAt = null;
	if(metadata.getDeletionTime() != null && metadata.getDeletionTime().before(new Date())) {
		//already expired
		purgeAt = VideoUtil.getPurgeTimestamp(config.getPurgeDelay(), TimeUnit.MILLISECONDS); 
	}else{
		purgeAt = metadata.getDeletionTime();
	}	 
	addPurgeStatements(stmt, placeId, recordingId, purgeAt, metadata.getDeletionPartition(), metadata.getLoc(), true);
}
 
Example #3
Source File: CassandraVideoV2Dao.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
public ListenableFuture<?> delete(UUID placeId, UUID recordingId, boolean isFavorite, Date purgeTime, int purgePartitionId) {
	
	BatchStatement stmt = new BatchStatement(Type.UNLOGGED);

	addDeleteStatements(stmt, placeId, recordingId, isFavorite, purgeTime, purgePartitionId);
	// Add to Purge table if it's favorite
	if(isFavorite) {
		VideoMetadata metadata = findByPlaceAndId(placeId, recordingId);
		metadata.setDeletionTime(purgeTime);
		metadata.setDeletionPartition(purgePartitionId);
		addStatementsForRemoveFromFavoriteTables(stmt, metadata);
	}

	long startTime = System.nanoTime();
	ResultSetFuture result = session.executeAsync(stmt);
	result.addListener(() -> DeleteTimer.update(System.nanoTime() - startTime, TimeUnit.NANOSECONDS), MoreExecutors.directExecutor());
	return result;
}
 
Example #4
Source File: CassandraVideoV2Dao.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
public Stream<VideoRecordingSize> streamRecordingSizeAsc(UUID placeId, boolean includeFavorites, boolean includeInProgress) {
	Iterator<VideoRecordingSize> it = null;
	if(includeFavorites){		
		List<PeekingIterator<VideoRecordingSize>> idIterators = new ArrayList<>(2);
		idIterators.add( peekingUuidIterator( placeRecordingIndexFavorite.selectRecordingSizeAsc(placeId, PlaceRecordingIndexV2Table.Type.RECORDING), placeRecordingIndexFavorite::getRecordingIdAndSizeAndFavorite ) );
		idIterators.add( peekingUuidIterator( placeRecordingIndex.selectRecordingSizeAsc(placeId, PlaceRecordingIndexV2Table.Type.RECORDING), placeRecordingIndex::getRecordingIdAndSizeAndFavorite ) );
		it = new UnionIterator(idIterators);
	}else{
		Iterator<VideoRecordingSize> favorites = recordingIdsIterator(placeRecordingIndexFavorite.selectRecordingSizeAsc(placeId, PlaceRecordingIndexV2Table.Type.RECORDING), placeRecordingIndexFavorite::getRecordingIdAndSizeAndFavorite);
		Iterator<VideoRecordingSize> allRecordings = recordingIdsIterator(placeRecordingIndex.selectRecordingSizeAsc(placeId, PlaceRecordingIndexV2Table.Type.RECORDING), placeRecordingIndex::getRecordingIdAndSizeAndFavorite );
		it = new DifferenceIterator(allRecordings, favorites);
		
	}
	Spliterator<VideoRecordingSize> matches = Spliterators.spliteratorUnknownSize(it, Spliterator.DISTINCT | Spliterator.SORTED);
	
	Stream<VideoRecordingSize> stream = StreamSupport.stream(matches, false);
	if(!includeInProgress) {
		stream = stream.filter(VideoRecordingSize::isCompletedRecording);
	}
	return stream;
}
 
Example #5
Source File: CassandraVideoV2Dao.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private void addDeleteStatements(BatchStatement stmt, UUID placeId, UUID recordingId, boolean isFavorite, Date purgeTime, int purgePartitionId) {
	boolean expired = false;
	long expiration = 0;
	if(isFavorite) {
		//check to see if data in the normal table has expired
		BoundStatement bs = recordingMetadataTable.selectRecording(recordingId);
		ResultSet rs = session.execute(bs);
		if(!rs.isExhausted()) {
			expiration = rs.one().getLong(VideoMetadataV2Table.COL_EXPIRATION);
		}else{
			expired = true;
		}
	}
	if(!expired) {					
		long actualTtlInSeconds = VideoV2Util.createActualTTL(recordingId, expiration);		
		// Recording metadata Table Mutations
		stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSeconds, MetadataAttribute.DELETED, Boolean.TRUE.toString()));
		// Recording Place Index Mutations
		stmt.add(placeRecordingIndex.insertDeleted(placeId, recordingId, expiration, actualTtlInSeconds));
		stmt.add(placeRecordingIndex.deleteVideo(placeId, recordingId, PlaceRecordingIndexV2Table.Type.RECORDING));
		stmt.add(placeRecordingIndex.deleteVideo(placeId, recordingId, PlaceRecordingIndexV2Table.Type.STREAM));					
	}

}
 
Example #6
Source File: CassandraRepo.java    From monasca-persister with Apache License 2.0 6 votes vote down vote up
public int handleFlush_batch(String id) {
  Statement query;
  int flushedCount = 0;

  BatchStatement batch = new BatchStatement(Type.UNLOGGED);
  while ((query = queue.poll()) != null) {
    flushedCount++;
    batch.add(query);
  }

  executeQuery(id, batch, System.nanoTime());

  metricCompleted.inc(flushedCount);

  return flushedCount;
}
 
Example #7
Source File: CassandraAppHandoffDao.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public String newToken(SessionHandoff handoff) {
	String token = Utils.randomTokenString(tokenLength);
	try(Timer.Context ctx = newToken.time()) {
		BatchStatement batch = new BatchStatement(Type.LOGGED);
		batch.add( insertToken.bind(token, handoff.getPersonId(), handoff.getIp(), handoff.getUrl(), handoff.getUsername()) );
		batch.add( updateToken.bind(token, handoff.getPersonId()) );
		session.execute( batch );
		return token;
	}
}
 
Example #8
Source File: CassandraVideoV2Dao.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private void removeNonFavoriteTags(UUID placeId, UUID recordingId, Set<String> tags) {
	BatchStatement stmt = new BatchStatement(BatchStatement.Type.LOGGED);
	for(String tag: tags) {
		stmt.add(recordingMetadataTable.deleteTag(recordingId, tag));
		stmt.add(placeRecordingIndex.deleteTag(placeId, recordingId, tag));
	}
	executeAndUpdateTimer(session, stmt, RemoveTagsTimer);
}
 
Example #9
Source File: CassandraVideoV2Dao.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private void complete(UUID placeId, UUID recordingId, long expiration, long actualTtlInSeconds, double duration, long size) {
	BatchStatement stmt = new BatchStatement(Type.UNLOGGED); // recording will be atomic, and place_recording will be atomic, but they will be independently atomic to save performance
	stmt.setRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE);
	
	addDurationAndSizeStatements(stmt, placeId, recordingId, duration, size, expiration, actualTtlInSeconds);
	// Recording Metadata Index Mutations
	stmt.add(placeRecordingIndex.insertRecording(placeId, recordingId, size, expiration, actualTtlInSeconds));

	executeAndUpdateTimer(session, stmt, CompleteTimer);		
}
 
Example #10
Source File: CassandraVideoV2Dao.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public void completeAndDelete(UUID placeId, UUID recordingId, double duration, long size, Date purgeTime, int purgePartitionId, long ttlInSeconds) {
	BatchStatement stmt = new BatchStatement(Type.UNLOGGED); // recording will be atomic, and place_recording will be atomic, but they will be independently atomic to save performance
	stmt.setRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE);
	
	addDurationAndSizeStatements(stmt, placeId, recordingId, duration, size, ttlInSeconds);
	addDeleteStatements(stmt, placeId, recordingId, false, purgeTime, purgePartitionId);
	executeAndUpdateTimer(session, stmt, CompleteTimer);	
}
 
Example #11
Source File: CassandraVideoV2Dao.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private Iterator<VideoRecordingSize> queryPlan(VideoQuery query, UUID start, UUID end) {
	// choose the most specific index and then apply additional filters in memory
	
	// there should be at most one per camera, so this should be the most specific index
	if(query.getRecordingType() == VideoType.STREAM) {
		return recordingIdsIterator( placeRecordingIndex.selectIdsByType(query.getPlaceId(), PlaceRecordingIndexV2Table.Type.STREAM, start, end, query.getLimit() + 1), placeRecordingIndex::getRecordingIdAndFavorite );
	}
	
	if(query.getTags() != null && !query.getTags().isEmpty()) {
		return querySet(query.getTags(), (tag) -> addStatementToList(
						placeRecordingIndexFavorite.selectIdsByTag(query.getPlaceId(), tag, start, end, query.getLimit() + 1), 
						placeRecordingIndexFavorite::getRecordingIdAndFavorite,
						placeRecordingIndex.selectIdsByTag(query.getPlaceId(), tag, start, end, query.getLimit() + 1),
						placeRecordingIndex::getRecordingIdAndFavorite));
	}
	
	if(query.getCameras() != null && !query.getCameras().isEmpty()) {
		return querySet(query.getCameras(), (cameraId) -> addStatementToList(
					placeRecordingIndexFavorite.selectIdsByCamera(query.getPlaceId(), cameraId, start, end, query.getLimit() + 1), 
					placeRecordingIndexFavorite::getRecordingIdAndFavorite,
					placeRecordingIndex.selectIdsByCamera(query.getPlaceId(), cameraId, start, end, query.getLimit() + 1),
					placeRecordingIndex::getRecordingIdAndFavorite));
	}
	
	List<PeekingIterator<VideoRecordingSize>> typeIterators = new ArrayList<>(5);
	// type is either ANY or RECORDING, already handled stream above
	typeIterators.add( peekingUuidIterator( placeRecordingIndexFavorite.selectIdsByType(query.getPlaceId(), PlaceRecordingIndexV2Table.Type.RECORDING, start, end, query.getLimit() + 1), placeRecordingIndexFavorite::getRecordingIdAndFavorite ) );
	typeIterators.add( peekingUuidIterator( placeRecordingIndex.selectIdsByType(query.getPlaceId(), PlaceRecordingIndexV2Table.Type.RECORDING, start, end, query.getLimit() + 1), placeRecordingIndex::getRecordingIdAndFavorite ) );
	if(query.getRecordingType() == VideoType.ANY) {
		typeIterators.add( peekingUuidIterator( placeRecordingIndex.selectIdsByType(query.getPlaceId(), PlaceRecordingIndexV2Table.Type.STREAM, start, end, query.getLimit() + 1), placeRecordingIndex::getRecordingIdAndFavorite ) );
	}
	if(query.isListDeleted()) {
		typeIterators.add( peekingUuidIterator( placeRecordingIndexFavorite.selectIdsByDeleted(query.getPlaceId(), start, end, query.getLimit() + 1), placeRecordingIndexFavorite::getRecordingIdAndFavorite ) );
		typeIterators.add( peekingUuidIterator( placeRecordingIndex.selectIdsByDeleted(query.getPlaceId(), start, end, query.getLimit() + 1), placeRecordingIndex::getRecordingIdAndFavorite ) );
	}
	return new UnionIterator( typeIterators );
}
 
Example #12
Source File: CassandraVideoV2Dao.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private void addNonFavoriteTags(UUID placeId, UUID recordingId, Set<String> tags, long ttlInSeconds) {
	BatchStatement stmt = new BatchStatement(BatchStatement.Type.LOGGED);	
	long expiration = VideoV2Util.createExpirationFromTTL(recordingId, ttlInSeconds);
	long actualTtlInSeconds = VideoV2Util.createActualTTL(recordingId, expiration);
	for(String tag: tags) {
		stmt.add(recordingMetadataTable.insertTag(recordingId, expiration, actualTtlInSeconds, tag));
		stmt.add(placeRecordingIndex.insertTag(placeId, recordingId, expiration, actualTtlInSeconds, tag));
	}
	executeAndUpdateTimer(session, stmt, AddTagsTimer);
}
 
Example #13
Source File: CassandraVideoV2Dao.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Override
public void insert(VideoMetadata metadata) {
	BatchStatement stmt = new BatchStatement(Type.UNLOGGED); // recording will be atomic, and place_recording will be atomic, but they will be independently atomic to save performance
	
	UUID placeId = metadata.getPlaceId();
	UUID recordingId = metadata.getRecordingId();
	UUID personId = metadata.getPersonId();
	
	// Recording Metadata Table Mutations
	long expiration = metadata.getExpiration();
	Date purgeAt = new Date(expiration);
	//Make sure expiration value matches with delete time since we add a delay + round to the next hour when calculating the delete time
	//Therefore, the expiration time will be a little longer than what is defined for the service level.
	long actualTtlInSec = VideoV2Util.createActualTTL(recordingId, expiration);
	stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.NAME, metadata.getName()));
	stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.PLACEID, String.valueOf(metadata.getPlaceId())));
	stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.ACCOUNTID, String.valueOf(metadata.getAccountId())));
	stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.CAMERAID, String.valueOf(metadata.getCameraId())));

	if (personId != null) {
		stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.PERSONID, String.valueOf(personId)));
	}

	stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.WIDTH, String.valueOf(metadata.getWidth())));
	stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.HEIGHT, String.valueOf(metadata.getHeight())));
	stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.BANDWIDTH, String.valueOf(metadata.getBandwidth())));
	stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.FRAMERATE, String.valueOf(metadata.getFramerate())));
	stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.PRECAPTURE, String.valueOf(metadata.getPrecapture())));
	stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.LOCATION, String.valueOf(metadata.getLoc())));
	stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.TYPE, metadata.isStream() ? VideoMetadataV2Table.ATTR_TYPE_STREAM : VideoMetadataV2Table.ATTR_TYPE_RECORDING));
	stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.EXPIRATION, String.valueOf(expiration)));		   
	stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.DELETED_TIME, String.valueOf(purgeAt.getTime())));
	
	int partitionId = VideoDao.calculatePartitionId(recordingId, config.getPurgePartitions());
	metadata.setDeletionPartition(partitionId);
     stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.DELETED_PARTITION, String.valueOf(partitionId)));
     
	if(metadata.getVideoCodec() != null) {
		stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.VIDEO_CODEC, metadata.getVideoCodec().name()));
	}

	if(metadata.getAudioCodec() != null) {
		stmt.add(recordingMetadataTable.insertField(recordingId, expiration, actualTtlInSec, MetadataAttribute.AUDIO_CODEC, metadata.getAudioCodec().name()));
	}

	// Recording Table Mutations
	stmt.add(recordingTable.insertField(recordingId, expiration, actualTtlInSec, RecordingTableField.STORAGE, toblob(metadata.getLoc())));
	stmt.add(recordingTable.insertField(recordingId, expiration, actualTtlInSec, RecordingTableField.ACCOUNT, toblob(metadata.getAccountId())));
	stmt.add(recordingTable.insertField(recordingId, expiration, actualTtlInSec, RecordingTableField.PLACE, toblob(metadata.getPlaceId())));
	stmt.add(recordingTable.insertField(recordingId, expiration, actualTtlInSec, RecordingTableField.CAMERA, toblob(metadata.getCameraId())));
	stmt.add(recordingTable.insertField(recordingId, expiration, actualTtlInSec, RecordingTableField.EXPIRATION, toblob(expiration)));

	if (metadata.getPersonId() != null) {
		stmt.add(recordingTable.insertField(recordingId, expiration, actualTtlInSec, RecordingTableField.PERSON, toblob(metadata.getPersonId())));
	}

	stmt.add(recordingTable.insertField(recordingId, expiration, actualTtlInSec, RecordingTableField.WIDTH, toblob(metadata.getWidth())));
	stmt.add(recordingTable.insertField(recordingId, expiration, actualTtlInSec, RecordingTableField.HEIGHT, toblob(metadata.getHeight())));
	stmt.add(recordingTable.insertField(recordingId, expiration, actualTtlInSec, RecordingTableField.BANDWIDTH,toblob(metadata.getBandwidth())));
	stmt.add(recordingTable.insertField(recordingId, expiration, actualTtlInSec, RecordingTableField.FRAMERATE,toblob(metadata.getFramerate())));

	if(metadata.getVideoCodec() != null) {
		stmt.add(recordingTable.insertField(recordingId, expiration, actualTtlInSec, RecordingTableField.VIDEO_CODEC, toblob(metadata.getVideoCodec())));
	}

	if(metadata.getAudioCodec() != null) {
		stmt.add(recordingTable.insertField(recordingId, expiration, actualTtlInSec, RecordingTableField.AUDIO_CODEC, toblob(metadata.getAudioCodec())));
	}

	// Recording Metadata Index Mutations
	stmt.add(placeRecordingIndex.insertCamera(placeId, recordingId, metadata.getCameraId(), expiration, actualTtlInSec));
	stmt.add(placeRecordingIndex.insertVideo(placeId, recordingId, metadata.isStream() ? PlaceRecordingIndexV2Table.Type.STREAM : PlaceRecordingIndexV2Table.Type.RECORDING, expiration, actualTtlInSec));
	
	// Purge table		
	addPurgeStatements(stmt, placeId, recordingId, purgeAt, partitionId, metadata.getLoc(), !metadata.isStream());	
	VideoV2Util.executeAndUpdateTimer(session, stmt, InsertVideoTimer);		
}
 
Example #14
Source File: CQLTransaction.java    From Doradus with Apache License 2.0 4 votes vote down vote up
private void executeUpdatesSynchronous(DBTransaction transaction) {
    BatchStatement batchState = new BatchStatement(Type.UNLOGGED);
    batchState.addAll(getMutations(transaction));
    executeBatch(batchState);
}