Skip to content

Commit

Permalink
Changed the database and the code to communicate with it.
Browse files Browse the repository at this point in the history
Changed the UpkFile format once again to better keep history.
  • Loading branch information
stricq committed Mar 22, 2018
1 parent 5c7ea61 commit 58c66ec
Show file tree
Hide file tree
Showing 23 changed files with 409 additions and 74 deletions.
4 changes: 1 addition & 3 deletions UpkManager.Domain/Contracts/IUpkFileRemoteRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ public interface IUpkFileRemoteRepository {

Task SaveUpkFile(DomainUpkFile File);

Task SaveUpkFile(List<DomainUpkFile> File);

Task DeleteUpkFile(string Id);
Task SaveUpkFile(List<DomainUpkFile> Files);

}

Expand Down
2 changes: 2 additions & 0 deletions UpkManager.Domain/Contracts/IUpkFileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public interface IUpkFileRepository {

Task<DomainVersion> GetGameVersion(string GamePath);

Task<string> GetGameLocale(string GamePath);

}

}
55 changes: 31 additions & 24 deletions UpkManager.Domain/Models/DomainUpkFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ namespace UpkManager.Domain.Models {
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public sealed class DomainUpkFile {

#region Private Fields

private long filesize;

#endregion Private Fields

#region Constructor

public DomainUpkFile() {
Expand All @@ -26,8 +32,6 @@ public DomainUpkFile() {

public string Id { get; set; }

public long FileSize { get; set; }

public string ContentsRoot { get; set; }

public string Package { get; set; }
Expand All @@ -44,6 +48,13 @@ public DomainUpkFile() {

public DomainVersion CurrentVersion { get; set; }

public string CurrentLocale { get; set; }

public long Filesize {
get => GetCurrentExports()?.Filesize ?? filesize;
set => filesize = value;
}

public string GameFilename { get; set; }

public string Filename => $"{Package}.upk";
Expand All @@ -58,45 +69,37 @@ public DomainUpkFile() {

#region Domain Methods

public List<DomainExportType> GetBestExports() {
return GetBestExports(GetMaxVersion());
public DomainExportVersion GetCurrentExports() {
return GetExports(CurrentVersion, CurrentLocale);
}

public List<DomainExportType> GetBestExports(DomainVersion version) {
public DomainExportVersion GetExports(DomainVersion version, string locale) {
//
// Check for exact version match
//
DomainExportVersion found = Exports.SingleOrDefault(v => v.Version == version);

if (found != null) return found.Types;
//
// Else look for the max of all versions less than the specified version
//
DomainVersion max = Exports.Where(v => v.Version < version).Max(v => v.Version);

if (max != null) {
found = Exports.Single(v => v.Version == max);
DomainExportVersion found = Exports.SingleOrDefault(v => v.Versions.Contains(version) && v.Locale == locale);

return found.Types;
}
if (found != null) return found;
//
// Otherwise just return for the max version
//
max = Exports.Max(v => v.Version);
DomainVersion max = Exports.Where(v => v.Locale == locale).SelectMany(v => v.Versions).Max();

if (max != null) {
found = Exports.Single(v => v.Version == max);
found = Exports.Single(v => v.Versions.Contains(max) && v.Locale == locale);

return found.Types;
return found;
}
//
// Otherwise there is nothing to return
//
return new List<DomainExportType>();
return null;
}

public DomainVersion GetMaxVersion() {
return !Exports.Any() ? CurrentVersion : Exports.Max(e => e.Version);
public DomainVersion GetLeastVersion() {
DomainExportVersion current = Exports.SingleOrDefault(v => v.Versions.Contains(CurrentVersion) && v.Locale == CurrentLocale);

return current == null ? CurrentVersion : current.Versions.Min();
}

#endregion Domain Methods
Expand All @@ -109,7 +112,11 @@ public DomainExportVersion() {
Types = new List<DomainExportType>();
}

public DomainVersion Version { get; set; }
public List<DomainVersion> Versions { get; set; }

public string Locale { get; set; }

public long Filesize { get; set; }

public List<DomainExportType> Types { get; set; }

Expand Down
6 changes: 5 additions & 1 deletion UpkManager.Entities/ExportVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ namespace UpkManager.Entities {
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public class ExportVersion {

public string Version { get; set; }
public List<string> Versions { get; set; }

public string Locale { get; set; }

public long Filesize { get; set; }

public List<ExportType> Types { get; set; }

Expand Down
2 changes: 0 additions & 2 deletions UpkManager.Entities/UpkFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace UpkManager.Entities {
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public class UpkFile : ModelBase {

public long FileSize { get; set; }

public string ContentsRoot { get; set; }

public string Package { get; set; }
Expand Down
12 changes: 12 additions & 0 deletions UpkManager.Repository/Contracts/IDocumentStoreManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Raven.Client.Documents;


namespace UpkManager.Repository.Contracts {

public interface IDocumentStoreManager {

IDocumentStore GetDocumentStoreFor(string DatabaseName);

}

}
21 changes: 21 additions & 0 deletions UpkManager.Repository/Contracts/IRavenStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Reflection;

using Raven.Client.Documents;
using Raven.Client.Documents.Session;


namespace UpkManager.Repository.Contracts {

public interface IRavenStorage {

IDocumentStore Store { get; }

IAsyncDocumentSession Session { get; }

void Initialize(string DatabaseName, Assembly IndexAssembly = null);

void Shutdown();

}

}
19 changes: 19 additions & 0 deletions UpkManager.Repository/Indexes/UpkFileCommonIndex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Linq;

using Raven.Client.Documents.Indexes;

using UpkManager.Entities;


namespace UpkManager.Repository.Indexes {

public sealed class UpkFileCommonIndex : AbstractIndexCreationTask<UpkFile> {

public UpkFileCommonIndex() {
Map = files => from file in files
select new { file.Package };
}

}

}
11 changes: 9 additions & 2 deletions UpkManager.Repository/Mapping/AutoMapperConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.Composition;
using System.Linq;

using AutoMapper;

Expand Down Expand Up @@ -26,18 +27,24 @@ public void RegisterMappings(IMapperConfigurationExpression config) {
.ForMember(dest => dest.LastAccess, opt => opt.Ignore())
.ForMember(dest => dest.GameFilename, opt => opt.Ignore())
.ForMember(dest => dest.CurrentVersion, opt => opt.Ignore())
.ForMember(dest => dest.CurrentLocale, opt => opt.Ignore())
.ForMember(dest => dest.Filesize, opt => opt.Ignore())
.ReverseMap();

config.CreateMap<ExportVersion, DomainExportVersion>().ForMember(dest => dest.Version, opt => opt.ResolveUsing(src => new DomainVersion(src.Version)))
config.CreateMap<ExportVersion, DomainExportVersion>().ForMember(dest => dest.Versions, opt => opt.ResolveUsing(src => src.Versions.Select(v => new DomainVersion(v))))
.ReverseMap()
.ForMember(dest => dest.Version, opt => opt.MapFrom(src => src.Version.Version));
.ForMember(dest => dest.Versions, opt => opt.ResolveUsing(src => src.Versions.Select(v => v.Version)));

config.CreateMap<ExportType, DomainExportType>().ReverseMap();

config.CreateMap<DomainUpkManagerException, UpkManagerException>().ForMember(dest => dest.Message, opt => opt.MapFrom(src => src.Exception.Message))
.ForMember(dest => dest.StackTrace, opt => opt.MapFrom(src => src.Exception.StackTrace))
.ForMember(dest => dest.Id, opt => opt.Ignore());

config.CreateMap<UpkFile, UpkFile>().ForMember(dest => dest.Id, opt => opt.Ignore());

config.CreateMap<UpkManagerException, UpkManagerException>().ForMember(dest => dest.Id, opt => opt.Ignore());

#endregion DTOs

}
Expand Down
70 changes: 70 additions & 0 deletions UpkManager.Repository/Services/DocumentStoreManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Concurrent;
using System.ComponentModel.Composition;
using System.IO;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;

using Raven.Client.Documents;

using UpkManager.Repository.Contracts;


namespace UpkManager.Repository.Services {

[Export(typeof(IDocumentStoreManager))]
public sealed class DocumentStoreManager : IDocumentStoreManager {

#region Private Fields

private readonly ConcurrentDictionary<string, Lazy<IDocumentStore>> stores = new ConcurrentDictionary<string, Lazy<IDocumentStore>>();

#endregion Private Fields

#region IDocumentStoreManager Implementation

public IDocumentStore GetDocumentStoreFor(string DatabaseName) {
Lazy<IDocumentStore> store = createDocumentStore(DatabaseName);

return stores.GetOrAdd(DatabaseName, store).Value;
}

#endregion IDocumentStoreManager Implementation

#region Private Methods

private static Lazy<IDocumentStore> createDocumentStore(string databaseName) {
X509Certificate2 certificate;

using(Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"UpkManager.Repository.Certificates.{databaseName}Client.pfx")) {
if (stream == null) return null;

byte[] data = new byte[stream.Length];

stream.Read(data, 0, data.Length);

certificate = new X509Certificate2(data);
}

return new Lazy<IDocumentStore>(() => {
IDocumentStore documentStore = new DocumentStore {
Certificate = certificate,
Database = databaseName,
Urls = new [] {
"https://a.stricq.com:8844",
"https://b.stricq.com:8845",
"https://c.stricq.com:8846"
}
};

documentStore.Initialize();

return documentStore;
});
}

#endregion Private Methods

}

}
53 changes: 53 additions & 0 deletions UpkManager.Repository/Services/RavenStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.ComponentModel.Composition;
using System.Reflection;

using Raven.Client.Documents;
using Raven.Client.Documents.Indexes;
using Raven.Client.Documents.Session;

using UpkManager.Repository.Contracts;


namespace UpkManager.Repository.Services {

[Export(typeof(IRavenStorage))]
public sealed class RavenStorage : IRavenStorage {

#region Private Fields

private readonly IDocumentStoreManager manager;

#endregion Private Fields

#region Constructor

[ImportingConstructor]
public RavenStorage(IDocumentStoreManager Manager) {
manager = Manager;
}

#endregion Constructor

#region IRavenStorage Implementation

public IDocumentStore Store { get; private set; }

public IAsyncDocumentSession Session => Store?.OpenAsyncSession();

public void Initialize(string DatabaseName, Assembly IndexAssembly = null) {
Store = manager.GetDocumentStoreFor(DatabaseName);

#if DEBUG
// if (IndexAssembly != null) IndexCreation.CreateIndexes(IndexAssembly, Store);
#endif
}

public void Shutdown() {
Store?.Dispose();
}

#endregion IRavenStorage Implementation

}

}
2 changes: 1 addition & 1 deletion UpkManager.Repository/Services/UpkFileRemoteRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace UpkManager.Repository.Services {

[Export(typeof(IUpkFileRemoteRepository))]
//[Export(typeof(IUpkFileRemoteRepository))]
public sealed class UpkFileRemoteRepository : IUpkFileRemoteRepository {

#region Private Fields
Expand Down
Loading

0 comments on commit 58c66ec

Please sign in to comment.