io.antmedia.datastore.db.InMemoryDataStore Maven / Gradle / Ivy
package io.antmedia.datastore.db;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang3.RandomStringUtils;
import io.antmedia.datastore.db.IDataStore;
import io.antmedia.datastore.db.types.Broadcast;
import io.antmedia.datastore.db.types.Endpoint;
public class InMemoryDataStore implements IDataStore {
public LinkedHashMap broadcastMap = new LinkedHashMap();
public InMemoryDataStore(String dbName) {
}
@Override
public String save(Broadcast broadcast) {
String streamId = null;
if (broadcast != null) {
try {
if (broadcast.getStreamId() == null) {
streamId = RandomStringUtils.randomNumeric(24);
broadcast.setStreamId(streamId);
}
streamId = broadcast.getStreamId();
String rtmpURL = broadcast.getRtmpURL();
if (rtmpURL != null) {
rtmpURL += streamId;
}
broadcast.setRtmpURL(rtmpURL);
broadcastMap.put(streamId, broadcast);
}
catch (Exception e) {
e.printStackTrace();
streamId = null;
}
}
return streamId;
}
@Override
public Broadcast get(String id) {
return broadcastMap.get(id);
}
@Override
public boolean updateName(String id, String name, String description) {
Broadcast broadcast = broadcastMap.get(id);
boolean result = false;
if (broadcast != null) {
broadcast.setName(name);
broadcast.setDescription(description);
broadcastMap.put(id, broadcast);
result = true;
}
return result;
}
@Override
public boolean updateStatus(String id, String status) {
Broadcast broadcast = broadcastMap.get(id);
boolean result = false;
if (broadcast != null) {
broadcast.setStatus(status);
broadcastMap.put(id, broadcast);
result = true;
}
return result;
}
@Override
public boolean updateDuration(String id, long duration) {
Broadcast broadcast = broadcastMap.get(id);
boolean result = false;
if (broadcast != null) {
broadcast.setDuration(duration);
broadcastMap.put(id, broadcast);
result = true;
}
return result;
}
@Override
public boolean updatePublish(String id, boolean publish) {
Broadcast broadcast = broadcastMap.get(id);
boolean result = false;
if (broadcast != null) {
broadcast.setPublish(publish);
broadcastMap.put(id, broadcast);
result = true;
}
return result;
}
@Override
public boolean addEndpoint(String id, Endpoint endpoint) {
Broadcast broadcast = broadcastMap.get(id);
boolean result = false;
if (broadcast != null && endpoint != null) {
List endPointList = broadcast.getEndPointList();
if (endPointList == null) {
endPointList = new ArrayList<>();
}
endPointList.add(endpoint);
broadcast.setEndPointList(endPointList);
broadcastMap.put(id, broadcast);
result = true;
}
return result;
}
@Override
public boolean removeEndpoint(String id, Endpoint endpoint) {
boolean result = false;
Broadcast broadcast = broadcastMap.get(id);
if (broadcast != null && endpoint != null) {
List endPointList = broadcast.getEndPointList();
if (endPointList != null) {
for (Iterator iterator = endPointList.iterator(); iterator.hasNext();)
{
Endpoint endpointItem = (Endpoint) iterator.next();
if (endpointItem.rtmpUrl.equals(endpoint.rtmpUrl))
{
iterator.remove();
result = true;
break;
}
}
}
}
return result;
}
@Override
public long getBroadcastCount() {
return broadcastMap.size();
}
@Override
public boolean delete(String id) {
Broadcast broadcast = broadcastMap.get(id);
boolean result = false;
if (broadcast != null) {
broadcastMap.remove(id);
result = true;
}
return result;
}
@Override
public List getBroadcastList(int offset, int size) {
Collection values = broadcastMap.values();
int t = 0;
int itemCount = 0;
if (size > 50) {
size = 50;
}
if (offset < 0){
offset = 0;
}
List list = new ArrayList();
for (Broadcast broadcast : values) {
if (t < offset) {
t++;
continue;
}
list.add(broadcast);
itemCount++;
if (itemCount >= size) {
break;
}
}
return list;
}
@Override
public boolean removeAllEndpoints(String id)
{
boolean result = false;
Broadcast broadcast = broadcastMap.get(id);
if (broadcast != null) {
broadcast.setEndPointList(null);
result = true;
}
return result;
}
}