ln.skyscanner/services/EntityService.cs

143 lines
4.1 KiB
C#
Raw Normal View History

2019-08-03 12:57:32 +02:00
using System;
using ln.application;
using ln.application.service;
using System.Threading;
using System.IO;
2019-08-29 13:14:52 +02:00
using ln.types.odb.ng;
2019-08-03 12:57:32 +02:00
using ln.skyscanner.entities;
using ln.logging;
2019-08-29 13:14:52 +02:00
using System.Collections.Generic;
using ln.types.odb.ng.storage;
2019-08-30 07:16:54 +02:00
using System.Linq;
using ln.types.net;
using ln.skyscanner.import.skytron;
2019-08-03 12:57:32 +02:00
namespace ln.skyscanner.services
{
public class EntityService : ApplicationServiceBase
{
public string BasePath { get; private set; }
2019-08-29 13:14:52 +02:00
public SkyScanner.Service CoreService { get; private set; }
public RPC RPCInstance { get; private set; }
2019-08-03 12:57:32 +02:00
2019-08-29 13:14:52 +02:00
public IStorageContainer StorageContainer { get; private set; }
public IStorageContainer StorageSession { get; private set; }
public Mapper SessionMapper { get; private set; }
2019-08-03 12:57:32 +02:00
2019-08-29 13:14:52 +02:00
Queue<object> upsertQueue = new Queue<object>();
2019-08-03 12:57:32 +02:00
public EntityService()
:base("Entity Service")
{
2019-08-29 13:14:52 +02:00
DependOnService<SkyScanner.Service>();
RPCInstance = new RPC(this);
2019-08-03 12:57:32 +02:00
}
public override void ServiceMain(IApplicationInterface applicationInterface)
{
2019-08-29 13:14:52 +02:00
CoreService = Dependency<SkyScanner.Service>();
2019-08-03 12:57:32 +02:00
BasePath = Path.Combine(
CurrentApplicationInterface.Arguments["base-path"].Value,
"entities"
);
Logging.Log(LogLevel.INFO, "Entity Service: Initializing");
2019-08-29 13:14:52 +02:00
StorageContainer = new FSStorageContainer(System.IO.Path.Combine(BasePath, "storage"));
StorageSession = new Session(StorageContainer);
SessionMapper = new Mapper(StorageSession);
2019-08-03 12:57:32 +02:00
2019-08-29 13:14:52 +02:00
StorageContainer.Open();
StorageSession.Open();
2019-08-03 12:57:32 +02:00
2019-08-29 13:14:52 +02:00
CoreService.RPCContainer.Add("entities",RPCInstance);
2019-08-03 12:57:32 +02:00
2019-08-29 13:14:52 +02:00
Ready();
2019-08-03 12:57:32 +02:00
2019-08-29 13:14:52 +02:00
while (!StopRequested)
{
//object o;
//while (
// (o = DequeueUpsert()) != null
// )
//{
// ODB.GetCollection(o.GetType()).Upsert(o);
//}
lock (upsertQueue)
{
if (upsertQueue.Count == 0)
Monitor.Wait(upsertQueue,2500);
}
}
2019-08-03 12:57:32 +02:00
2019-08-29 13:14:52 +02:00
CoreService.RPCContainer.Remove(RPCInstance);
CoreService = null;
2019-08-03 12:57:32 +02:00
2019-08-29 13:14:52 +02:00
SessionMapper = null;
StorageSession.Dispose();
StorageContainer.Dispose();
2019-08-03 12:57:32 +02:00
2019-08-29 13:14:52 +02:00
BasePath = null;
}
2019-08-03 12:57:32 +02:00
2019-08-29 13:14:52 +02:00
public void EnqueueUpsert<T>(T o) where T: class
{
//ObjectCollection<T> col = ODBMapper.GetCollection<T>();
//lock (upsertQueue)
//{
// upsertQueue.Enqueue(o);
// Monitor.Pulse(upsertQueue);
//}
}
2019-08-03 12:57:32 +02:00
2019-08-29 13:14:52 +02:00
public object DequeueUpsert()
{
lock (upsertQueue)
{
if (upsertQueue.Count == 0)
return null;
return upsertQueue.Dequeue();
}
}
public class RPC
{
EntityService EntityService { get; }
2019-08-06 00:26:53 +02:00
2019-08-29 13:14:52 +02:00
public RPC(EntityService entityService)
2019-08-03 12:57:32 +02:00
{
2019-08-29 13:14:52 +02:00
EntityService = entityService;
2019-08-03 12:57:32 +02:00
}
2019-08-29 13:14:52 +02:00
public Node GetNode(Guid nodeID)
{
2019-08-30 07:16:54 +02:00
return EntityService.SessionMapper.Load<Node>(Query.Equals<Node>("ID", nodeID)).FirstOrDefault();
2019-08-29 13:14:52 +02:00
}
2019-08-30 07:16:54 +02:00
public Node[] GetNodes()
{
return EntityService.SessionMapper.Load<Node>().ToArray();
}
2019-08-29 13:14:52 +02:00
2019-08-30 07:16:54 +02:00
public Node CreateNode()
{
Node node = new Node(IPv4.ANY);
EntityService.SessionMapper.Save<Node>(node);
return node;
}
public void SyncSkytron()
{
SkytronImport skytronImport = new SkytronImport(EntityService.CurrentApplicationInterface.Arguments["import-skytron"].Value);
ThreadPool.QueueUserWorkItem((state) => skytronImport.Import(EntityService.SessionMapper));
}
2019-08-03 12:57:32 +02:00
}
2019-08-29 13:14:52 +02:00
2019-08-03 12:57:32 +02:00
}
}