Skip to content

Commit

Permalink
Implemented SegmentMetadataStore and fixed bug where assertion would …
Browse files Browse the repository at this point in the history
…prevent initialization of metadata.
  • Loading branch information
Spiess committed Oct 26, 2021
1 parent d09040f commit b78d67b
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class MetadataStore
/// <summary>
/// Actual internal storage of metadata
/// </summary>
protected Dictionary<string, Dictionary<string, string>> _storage =
protected Dictionary<string, Dictionary<string, string>> Storage =
new Dictionary<string, Dictionary<string, string>>();

public bool Initialized { get; protected set; }
Expand All @@ -26,19 +26,19 @@ public async Task<Dictionary<string, Dictionary<string, string>>> GetAll()
await InitializeAsync();
}

return _storage;
return Storage;
}

public bool DomainExists(string domain)
{
Assert.IsTrue(Initialized);
return _storage.ContainsKey(domain);
return Storage.ContainsKey(domain);
}

public string Get(string domain, string key)
{
Assert.IsTrue(Initialized);
return _storage[domain][key];
return Storage[domain][key];
}

/// <summary>
Expand All @@ -51,7 +51,7 @@ public string Get(string str)
var domainAndKey = str.Split('.');
if (domainAndKey.Length >= 1)
{
return _storage[domainAndKey[0]][domainAndKey[1]];
return Storage[domainAndKey[0]][domainAndKey[1]];
}

throw new ArgumentException("Cannot retrieve without domain");
Expand All @@ -64,7 +64,7 @@ public string Get(string str)
public List<(string Key, string Value)> GetDomain(string domain)
{
Assert.IsTrue(Initialized);
var items = _storage[domain];
var items = Storage[domain];
return items.Keys.Select(key => (key, items[key])).ToList();
}

Expand All @@ -75,7 +75,7 @@ public string Get(string str)
public bool Exists(string domain, string key)
{
Assert.IsTrue(Initialized);
return DomainExists(domain) && _storage[domain].ContainsKey(key);
return DomainExists(domain) && Storage[domain].ContainsKey(key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,20 @@ public class ObjectData
/// </summary>
private List<SegmentData> _segments;

public ObjectMetadataStore ObjectMetadata { get; private set; }

/// <summary>
/// Constructs a new instance with the given id, for lazy loading.
/// </summary>
/// <param name="id"></param>
public ObjectData(string id)
{
_id = id;
ObjectMetadata = new ObjectMetadataStore(_id);
}

public ObjectMetadataStore ObjectMetadata { get; private set; }

/// <summary>
/// Constructs a new instance with the given wrapper content.
/// </summary>
/// <param name="descriptor"></param>
public ObjectData(MediaObjectDescriptor descriptor)
{
_id = descriptor.ObjectId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public void Initialize(MediaObjectMetadataQueryResult data)

foreach (var meta in data.Content.Where(meta => meta.ObjectId == ObjectId))
{
if (!DomainExists(meta.Domain))
if (!Storage.ContainsKey(meta.Domain))
{
_storage.Add(meta.Domain, new Dictionary<string, string>());
Storage.Add(meta.Domain, new Dictionary<string, string>());
}

var domain = _storage[meta.Domain];
var domain = Storage[meta.Domain];
domain.Add(meta.Key, meta.Value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,27 @@ public class SegmentData
// TODO: Consider combining lazy loading requests into batch requests every x seconds to reduce request overhead
private static readonly SemaphoreSlim InitLock = new SemaphoreSlim(1, 1);

public SegmentMetadataStore SegmentMetadata { get; private set; }


public SegmentData(string id)
{
_id = id;
SegmentMetadata = new SegmentMetadataStore(_id);
}

public SegmentData(MediaSegmentDescriptor descriptor)
{
_descriptor = descriptor;
_id = descriptor.SegmentId;
SegmentMetadata = new SegmentMetadataStore(_id);
Initialized = true;
}

/// <summary>
/// Runs asynchronous segment ID query to initialize the data for this segment.
/// </summary>
private async Task InitializeAsync()
private async Task InitializeAsync(bool withMetadata = true)
{
await InitLock.WaitAsync();
try
Expand All @@ -63,6 +67,11 @@ private async Task InitializeAsync()

var result = queryResult.Content[0];
Initialize(result);

if (withMetadata)
{
await SegmentMetadata.InitializeAsync();
}
}
finally
{
Expand Down Expand Up @@ -94,6 +103,18 @@ public void Initialize(MediaSegmentDescriptor data)
Initialized = true;
}

public void InitializeMeta(MediaSegmentMetadataQueryResult meta)
{
if (SegmentMetadata.Initialized)
{
Debug.LogWarning("Attempt to initialize already initialized segment metadata for media object with " +
$"id \"{Id}\". Using cached data.");
return;
}

SegmentMetadata.Initialize(meta);
}

/// <summary>
/// ID of the <see cref="MediaSegmentDescriptor"/>
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Org.Vitrivr.CineastApi.Model;
using UnityEngine;

namespace Vitrivr.UnityInterface.CineastApi.Model.Data
{
/// <summary>
/// Access and local representation of metadata for segments.
/// </summary>
[Serializable]
public class SegmentMetadataStore : MetadataStore
{
public SegmentMetadataStore(string id)
{
SegmentId = id;
Initialized = false;
}

public string SegmentId { get; private set; }

public void Initialize(MediaSegmentMetadataQueryResult data)
{
if (Initialized)
{
Debug.LogWarning($"Attempted to initialize already initialized metadata for media object {SegmentId}!");
return;
}

foreach (var meta in data.Content.Where(meta => meta.SegmentId == SegmentId))
{
if (!Storage.ContainsKey(meta.Domain))
{
Storage.Add(meta.Domain, new Dictionary<string, string>());
}

var domain = Storage[meta.Domain];
domain.Add(meta.Key, meta.Value);
}

Initialized = true;
}

public override async Task InitializeAsync()
{
if (Initialized)
{
Debug.LogWarning($"Attempted to initialize already initialized metadata for media object {SegmentId}!");
return;
}

var metadataResult = await CineastWrapper.MetadataApi.FindSegMetaByIdAsync(SegmentId);
if (!Initialized)
{
Initialize(metadataResult);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static void Reset()
}

/// <summary>
/// Whether the given segmentId is known to this registry
/// Whether the given segmentId is known to this registry.
/// </summary>
/// <param name="segmentId"></param>
/// <returns></returns>
Expand All @@ -119,14 +119,11 @@ public static bool Exists(string segmentId)
}

/// <summary>
/// Gets the corresponding object of the segment specified
/// Gets the corresponding object of the segment specified.
/// </summary>
/// <param name="segmentId"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static async Task<ObjectData> GetObjectOf(string segmentId)
{
// TODO might require a dedicated cache
if (!Exists(segmentId))
{
throw new ArgumentException($"Cannot get object of unknown segment with id {segmentId}");
Expand Down

0 comments on commit b78d67b

Please sign in to comment.