All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.antmedia.datastore.db.MongoStore Maven / Gradle / Ivy
package io.antmedia.datastore.db;
import java.util.List;
import org.apache.commons.lang3.RandomStringUtils;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Key;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.query.FindOptions;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.mongodb.morphia.query.UpdateResults;
import com.mongodb.MongoClient;
import com.mongodb.WriteResult;
import io.antmedia.datastore.db.types.Broadcast;
import io.antmedia.datastore.db.types.Endpoint;
public class MongoStore implements IDataStore {
private Morphia morphia;
private Datastore datastore;
public MongoStore(String dbName) {
morphia = new Morphia();
morphia.mapPackage("io.antmedia.datastore.db.types");
datastore = morphia.createDatastore(new MongoClient(), dbName);
datastore.ensureIndexes();
}
/* (non-Javadoc)
* @see io.antmedia.datastore.db.IDataStore#save(io.antmedia.datastore.db.types.Broadcast)
*/
@Override
public String save(Broadcast broadcast) {
try {
String streamId = null;
if (broadcast.getStreamId() == null) {
streamId = RandomStringUtils.randomAlphanumeric(12) + System.currentTimeMillis();
broadcast.setStreamId(streamId);
}
streamId = broadcast.getStreamId();
String rtmpURL = broadcast.getRtmpURL();
if (rtmpURL != null) {
rtmpURL += streamId;
}
broadcast.setRtmpURL(rtmpURL);
Key key = datastore.save(broadcast);
return streamId;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
/* (non-Javadoc)
* @see io.antmedia.datastore.db.IDataStore#get(java.lang.String)
*/
@Override
public Broadcast get(String id)
{
try {
return datastore.find(Broadcast.class).field("streamId").equal(id).get();
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
/* (non-Javadoc)
* @see io.antmedia.datastore.db.IDataStore#updateName(java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public boolean updateName(String id, String name, String description)
{
try {
Query query = datastore.createQuery(Broadcast.class).field("streamId").equal(id);
UpdateOperations ops = datastore
.createUpdateOperations(Broadcast.class)
.set("name", name).set("description", description);
UpdateResults update = datastore.update(query, ops);
return update.getUpdatedCount() == 1;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
/* (non-Javadoc)
* @see io.antmedia.datastore.db.IDataStore#updateStatus(java.lang.String, java.lang.String)
*/
@Override
public boolean updateStatus(String id, String status)
{
try {
Query query = datastore.createQuery(Broadcast.class).field("streamId").equal(id);
UpdateOperations ops = datastore
.createUpdateOperations(Broadcast.class)
.set("status", status);
UpdateResults update = datastore.update(query, ops);
return update.getUpdatedCount() == 1;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
/* (non-Javadoc)
* @see io.antmedia.datastore.db.IDataStore#updateDuration(java.lang.String, long)
*/
@Override
public boolean updateDuration(String id, long duration)
{
try {
Query query = datastore.createQuery(Broadcast.class).field("streamId").equal(id);
UpdateOperations ops = datastore
.createUpdateOperations(Broadcast.class)
.set("duration", duration);
UpdateResults update = datastore.update(query, ops);
return update.getUpdatedCount() == 1;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
/* (non-Javadoc)
* @see io.antmedia.datastore.db.IDataStore#updatePublish(java.lang.String, boolean)
*/
@Override
public boolean updatePublish(String id, boolean publish) {
try {
Query query = datastore.createQuery(Broadcast.class).field("dbId").equal(new ObjectId(id));
UpdateOperations ops = datastore
.createUpdateOperations(Broadcast.class)
.set("publish", publish);
UpdateResults update = datastore.update(query, ops);
return update.getUpdatedCount() == 1;
}
catch (Exception e){
e.printStackTrace();
}
return false;
}
/* (non-Javadoc)
* @see io.antmedia.datastore.db.IDataStore#addEndpoint(java.lang.String, io.antmedia.datastore.db.types.Endpoint)
*/
@Override
public boolean addEndpoint(String id, Endpoint endpoint)
{
if (id != null && endpoint != null) {
try {
Query query = datastore.createQuery(Broadcast.class).field("streamId").equal(id);
UpdateOperations ops = datastore
.createUpdateOperations(Broadcast.class)
.push("endPointList", endpoint);
UpdateResults update = datastore.update(query, ops);
return update.getUpdatedCount() == 1;
}
catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
@Override
public boolean removeEndpoint(String id, Endpoint endpoint) {
boolean result = false;
if (id != null && endpoint != null)
{
Query query = datastore.createQuery(Broadcast.class).field("streamId").equal(id);
UpdateOperations ops = datastore
.createUpdateOperations(Broadcast.class)
.removeAll("endPointList", endpoint);
UpdateResults update = datastore.update(query, ops);
return update.getUpdatedCount() == 1;
}
return result;
}
@Override
public boolean removeAllEndpoints(String id) {
boolean result = false;
if (id != null)
{
Query query = datastore.createQuery(Broadcast.class).field("streamId").equal(id);
UpdateOperations ops = datastore
.createUpdateOperations(Broadcast.class).unset("endPointList");
UpdateResults update = datastore.update(query, ops);
return update.getUpdatedCount() == 1;
}
return result;
}
/* (non-Javadoc)
* @see io.antmedia.datastore.db.IDataStore#getBroadcastCount()
*/
@Override
public long getBroadcastCount() {
return datastore.getCount(Broadcast.class);
}
/* (non-Javadoc)
* @see io.antmedia.datastore.db.IDataStore#delete(java.lang.String)
*/
@Override
public boolean delete(String id) {
try {
Query query = datastore.createQuery(Broadcast.class).field("streamId").equal(id);
WriteResult delete = datastore.delete(query);
return delete.getN() == 1;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public List getBroadcastList(int offset, int size) {
return datastore.find(Broadcast.class).asList(
new FindOptions().skip(offset).limit(size));
}
public Datastore getDataStore() {
return datastore;
}
}