Beispiel #1
0
        public Server(NodeConfig config, ILog log = null)
        {
            _serviceContainer = new Services(log, config);
            _dataContainer    = new DataContainer(_serviceContainer);

            _config = config;
        }
Beispiel #2
0
        public DataContainer(Profiler profiler, NodeConfig config)
        {
            _config  = config;
            Profiler = profiler;

            _dataStores = new Dictionary <string, DataStore>();
            _knownTypes = new Dictionary <string, TypeDescription>();

            _jsonSerializer = JsonSerializer.Create(_schemaSerializerSettings);
            _jsonSerializer.Converters.Add(new StringEnumConverter());
        }
Beispiel #3
0
        /// <summary>
        ///     Initialize an empty datastore from a type description
        /// </summary>
        /// <param name="typeDescription"></param>
        /// <param name="evictionPolicy"></param>
        /// <param name="config"></param>
        public DataStore(TypeDescription typeDescription, EvictionPolicy evictionPolicy, NodeConfig config)
        {
            TypeDescription = typeDescription ?? throw new ArgumentNullException(nameof(typeDescription));

            EvictionPolicy = evictionPolicy ?? throw new ArgumentNullException(nameof(evictionPolicy));

            //initialize the primary key dictionary
            _dataByPrimaryKey = new Dictionary <KeyValue, CachedObject>();


            //initialize the unique keys dictionaries (une by unique key)
            _dataByUniqueKey = new Dictionary <string, Dictionary <KeyValue, CachedObject> >();

            foreach (var keyInfo in typeDescription.UniqueKeyFields)
            {
                _dataByUniqueKey.Add(keyInfo.Name, new Dictionary <KeyValue, CachedObject>());
            }

            //initialize the indexes (one by index key)
            _dataByIndexKey = new Dictionary <string, IndexBase>();

            // scalar indexed fields
            foreach (var indexField in typeDescription.IndexFields)
            {
                var index = IndexFactory.CreateIndex(indexField);
                _dataByIndexKey.Add(indexField.Name, index);
            }


            // list indexed fields
            foreach (var indexField in typeDescription.ListFields)
            {
                var index = IndexFactory.CreateIndex(indexField);
                _dataByIndexKey.Add(indexField.Name, index);
            }


            // create the full-text index if required
            if (typeDescription.FullText.Count > 0)
            {
                _fullTextIndex = new FullTextIndex(config.FullTextConfig)
                {
                    // a function that allows the full text engine to find the original line of text
                    LineProvider = pointer => _dataByPrimaryKey[pointer.PrimaryKey].FullText[pointer.Line]
                };
            }
        }
Beispiel #4
0
        public Server(NodeConfig config)
        {
            _dataContainer = new DataContainer(_serverProfiler, config);

            _config = config;
        }
Beispiel #5
0
        public bool Start(string instance)
        {
            try
            {
                var configFile = Constants.NodeConfigFileName;

                if (instance != null)
                {
                    var baseName = configFile.Split('.').FirstOrDefault();
                    configFile = $"{baseName}_{instance}.json";
                }

                var nodeConfig = new NodeConfig {TcpPort = Constants.DefaultPort, IsPersistent = true};

                if (File.Exists(configFile))
                {
                    try
                    {
                        var configFromFile = SerializationHelper.FormattedSerializer.Deserialize<NodeConfig>(
                            new JsonTextReader(new StringReader(File.ReadAllText(configFile))));

                        nodeConfig = configFromFile;

                        HostServices.HostServices.Start(configFromFile.DataPath);

                        Log.LogInfo("----------------------------------------------------------");
                        Log.LogInfo($"Reading configuration file {configFile} ");
                    }
                    catch (Exception e)
                    {
                        Log.LogError($"Error reading configuration file {configFile} : {e.Message}");
                    }
                }
                else
                {
                    HostServices.HostServices.Start(nodeConfig.DataPath);
                    Log.LogWarning($"Configuration file {configFile} not found. Using defaults");
                }

                _cacheServer = new global::Server.Server(nodeConfig);

                _listener = new TcpServerChannel();
                _cacheServer.Channel = _listener;
                _listener.Init(nodeConfig.TcpPort);
                _listener.Start();

                var fullDataPath = Path.GetFullPath(nodeConfig.DataPath ?? Constants.DataPath);

                var persistentDescription = nodeConfig.IsPersistent ? fullDataPath : " NO";

                try
                {
                    Console.Title = $"Cachalot Core on port {nodeConfig.TcpPort} persistent = {persistentDescription}";
                }
                catch (Exception )
                {
                    //ignore this may throw an exception when run in service mode
                }

                Log.LogInfo(
                    $"Starting hosted service on port {nodeConfig.TcpPort} persistent = {persistentDescription}");

                _cacheServer.StopRequired += (sender, args) =>
                {
                    HostServices.HostServices.Stop();
                    Stop();

                    _stopEvent.Set();
                };
                _cacheServer.Start();
            }
            catch (Exception e)
            {
                Log.LogError($"Failed to start host: {e}");

                return false;
            }

            Log.LogInfo("Host started successfully");

            return true;
        }