コード例 #1
0
ファイル: KeyInfo.cs プロジェクト: mufeng1122/Cachalot
        /// <summary>
        ///     Helper method. Convert a value to a key value
        /// </summary>
        /// <param name="value"> </param>
        /// <param name="info"> </param>
        /// <returns> </returns>
        public static KeyValue ValueToKeyValue(object value, KeyInfo info)
        {
            //check if directly assignable to int
            if (info.KeyDataType == KeyDataType.IntKey)
            {
                // default behavior for nullable values (works fine for dates)
                if (value == null)
                {
                    return(new KeyValue(0, info));
                }

                var propertyType = value.GetType();

                //integer types
                if (propertyType == typeof(int) || propertyType == typeof(short) || propertyType == typeof(long) ||
                    propertyType == typeof(byte) || propertyType == typeof(char) || propertyType == typeof(bool) ||
                    propertyType == typeof(IntPtr))
                {
                    var longVal = Convert.ToInt64(value);
                    return(new KeyValue(longVal, info));
                }

                if (propertyType.IsEnum)
                {
                    var longVal = Convert.ToInt64(value);
                    return(new KeyValue(longVal, info));
                }


                //other types. Can be used as keys if a key converter is provided
                var converter = KeyConverters.GetIfAvailable(propertyType);
                if (converter != null)
                {
                    //prefer conversion to long if available
                    if (converter.CanConvertToLong)
                    {
                        return(new KeyValue(converter.GetAsLong(value), info));
                    }

                    if (converter.CanConvertToString)
                    {
                        return(new KeyValue(converter.GetAsString(value), info));
                    }

                    Dbg.CheckThat(false, "trying to use an invalid key converter");
                }
            }
            else
            {
                if (value != null)
                {
                    return(new KeyValue(value.ToString(), info));
                }
                return(new KeyValue(null, info));
            }


            throw new InvalidOperationException($"Can not compute key value for object {value}");
        }
コード例 #2
0
        internal override ICacheClient TryExecute(ICacheClient client)
        {
            if (!CanExecute)
            {
                return(client);
            }

            Dbg.CheckThat(Params.Count == 2);

            IList <CachedObject> listResult = null;

            try
            {
                Dbg.CheckThat(Query != null);

                Profiler.IsActive = true;
                Profiler.Start("SEARCH");

                listResult = client.GetObjectDescriptions(Query);


                Logger.Write("[");
                for (var i = 0; i < listResult.Count; i++)
                {
                    Logger.Write(listResult[i].AsJson());
                    if (i < listResult.Count - 1)
                    {
                        Logger.Write(",");
                    }
                }

                Logger.Write("]");

                return(client);
            }
            catch (CacheException ex)
            {
                Logger.WriteEror("Can not execute SEARCH : {0} {1}", ex.Message, ex.ServerMessage);
                return(client);
            }
            catch (Exception ex)
            {
                Logger.WriteEror("Can not execute SEARCH : {0}", ex.Message);
                return(client);
            }
            finally
            {
                var profilerResult = Profiler.End();

                var count = 0;
                if (listResult != null)
                {
                    count = listResult.Count;
                }

                Logger.Write("Found {0} items. The call took {1} milliseconds", count,
                             profilerResult.TotalTimeMiliseconds);
            }
        }
コード例 #3
0
 /// <summary>
 ///     Write an error message using format string
 /// </summary>
 /// <param name="format"></param>
 /// <param name="parameters"></param>
 public static void WriteEror(string format, params object[] parameters)
 {
     CommandLogger.WriteError(format, parameters);
     if (_dumpOn)
     {
         Dbg.CheckThat(_dumpStream != null);
         _dumpStream.WriteLine(format, parameters);
     }
 }
コード例 #4
0
        public static bool ReadAck(Stream stream)
        {
            //BufferedStream bufferedStream = new BufferedStream(stream);
            var reader = new BinaryReader(stream);
            var ack    = reader.ReadInt64();

            Dbg.CheckThat(ack == Ack);

            return(ack == Ack);
        }
コード例 #5
0
 public static void Write(string message)
 {
     if (_dumpOn)
     {
         Dbg.CheckThat(_dumpStream != null);
         _dumpStream.WriteLine(message);
     }
     else
     {
         CommandLogger.Write(message);
     }
 }
コード例 #6
0
        private void InternalProcessRegisterType(RegisterTypeRequest request)
        {
            var typeDescription = request.TypeDescription;

            if (ShardCount == 0) // not initialized
            {
                ShardIndex = request.ShardIndex;
                ShardCount = request.ShardsInCluster;
            }
            else // check it didn't change
            {
                if (ShardIndex != request.ShardIndex || ShardCount != request.ShardsInCluster)
                {
                    throw new NotSupportedException(
                              $"Cluster configuration changed. This node was shard {ShardIndex} / {ShardCount} and now is {request.ShardIndex} / {request.ShardsInCluster}");
                }
            }

            if (_knownTypes.ContainsKey(typeDescription.FullTypeName)) //type already registered
            {
                //check that the type description is the same
                if (!typeDescription.Equals(_knownTypes[typeDescription.FullTypeName]))
                {
                    var message =
                        $"The type {typeDescription.FullTypeName} is already registered but the type description is different";
                    throw new NotSupportedException(message);
                }
            }
            else // new type, store it in the type dictionary and initialize its DataStore
            {
                _knownTypes.Add(typeDescription.FullTypeName, typeDescription);

                PersistenceEngine?.UpdateSchema(GenerateSchema());


                var cfg = Config.ConfigByType.ContainsKey(typeDescription.FullTypeName)
                    ? Config.ConfigByType[typeDescription.FullTypeName]
                    : new ServerDatastoreConfig();

                var evictionPolicy = EvictionPolicyFactory.CreateEvictionPolicy(cfg.Eviction);

                var newDataStore =
                    new DataStore(typeDescription, evictionPolicy);


                Dbg.CheckThat(Profiler != null);

                newDataStore.Profiler = Profiler;
                DataStores.Add(typeDescription.FullTypeName, newDataStore);
            }
        }
コード例 #7
0
        /// <summary>
        ///     Return useful information about the server process and the "data stores"
        ///     A "data store" is a data container on the server containing all the objects of a given type
        /// </summary>
        /// <returns></returns>
        public ServerDescriptionResponse GetServerDescription()
        {
            var request = new GetKnownTypesRequest();

            var response = Channel.SendRequest(request);

            if (response is ExceptionResponse exResponse)
            {
                throw new CacheException("Error while getting server information", exResponse.Message,
                                         exResponse.CallStack);
            }

            var concreteResponse = response as ServerDescriptionResponse;

            Dbg.CheckThat(concreteResponse != null);
            return(concreteResponse);
        }
コード例 #8
0
        public override bool TryExecute()
        {
            if (!CanExecute)
            {
                return(false);
            }


            Dbg.CheckThat(Params.Count == 1);


            try
            {
                Profiler.IsActive = true;
                Profiler.Start("LAST");

                var lines = int.Parse(Params[0]);

                var response = _client.GetServerLog(lines);

                var profilerResult = Profiler.End();

                if (response.Entries.Count > 0)
                {
                    Logger.Write("");
                    foreach (var entry in response.Entries)
                    {
                        Logger.Write("{0}", entry.ToString());
                    }

                    Logger.Write("");
                    Logger.Write("Maximum access time was:");
                    Logger.Write(response.MaxLockEntry.ToString());
                }

                Logger.Write("The call took {0} miliseconds", profilerResult.TotalTimeMiliseconds);
            }
            catch (Exception ex)
            {
                Logger.WriteEror("Can not execute LAST : {0}", ex.Message);
            }


            return(true);
        }
コード例 #9
0
ファイル: CommandLast.cs プロジェクト: usinesoft/Cachalot
        internal override IDataClient TryExecute(IDataClient client)
        {
            if (!CanExecute)
            {
                return(client);
            }


            Dbg.CheckThat(Params.Count == 1);


            try
            {
                Profiler.IsActive = true;
                Profiler.Start("LAST");

                var lines = int.Parse(Params[0]);

                var response = client.GetLog(lines);

                Profiler.End();

                if (response.Entries.Count > 0)
                {
                    Logger.Write("");
                    foreach (var entry in response.Entries)
                    {
                        Logger.Write(entry);
                    }

                    Logger.Write("");
                    Logger.Write("Maximum access time was:");
                }

                Logger.Write("The call took {0} milliseconds", Profiler.TotalTimeMilliseconds);
            }
            catch (Exception ex)
            {
                Logger.WriteEror("Can not execute LAST : {0}", ex.Message);
            }


            return(client);
        }
コード例 #10
0
ファイル: CommandDelete.cs プロジェクト: mufeng1122/Cachalot
        /// <summary>
        /// </summary>
        /// <returns></returns>
        internal override ICacheClient TryExecute(ICacheClient client)
        {
            if (!CanExecute)
            {
                return(null);
            }

            Dbg.CheckThat(Params.Count >= 1);

            var deletedItems = 0;

            try
            {
                Dbg.CheckThat(Query != null);

                Profiler.IsActive = true;
                Profiler.Start("DELETE");

                deletedItems = client.RemoveMany(Query);
            }
            catch (CacheException ex)
            {
                Logger.WriteEror("Can not execute GET : {0} {1}", ex.Message, ex.ServerMessage);
                return(null);
            }
            catch (Exception ex)
            {
                Logger.WriteEror("Can not execute GET : {0}", ex.Message);
                return(null);
            }
            finally
            {
                var profilerResult = Profiler.End();

                Logger.Write("Deleted {0} items. The call took {1} miliseconds", deletedItems,
                             profilerResult.TotalTimeMiliseconds);
            }


            return(client);
        }
コード例 #11
0
        public override bool TryExecute()
        {
            if (!CanExecute)
            {
                return(false);
            }

            Dbg.CheckThat(Params.Count == 2);

            var result = new KeyValuePair <bool, int>();

            try
            {
                Dbg.CheckThat(Query != null);

                Profiler.IsActive = true;
                Profiler.Start("COUNT");

                result = _client.EvalQuery(Query);
            }
            catch (CacheException ex)
            {
                Logger.WriteEror("Can not execute COUNT : {0} {1}", ex.Message, ex.ServerMessage);
            }
            catch (Exception ex)
            {
                Logger.WriteEror("Can not execute COUNT : {0}", ex.Message);
                return(false);
            }
            finally
            {
                var profilerResult = Profiler.End();


                Logger.Write("Found {0} items. The call took {1} miliseconds, data is complete = {2}", result.Value,
                             profilerResult.TotalTimeMiliseconds, result.Key);
            }


            return(true);
        }
コード例 #12
0
ファイル: CommandCount.cs プロジェクト: mufeng1122/Cachalot
        internal override ICacheClient TryExecute(ICacheClient client)
        {
            if (!CanExecute)
            {
                return(client);
            }

            Dbg.CheckThat(Params.Count == 2 || Params.Count == 1);

            var result = new KeyValuePair <bool, int>();

            try
            {
                Dbg.CheckThat(Query != null);

                Profiler.IsActive = true;
                Profiler.Start("COUNT");

                result = client.EvalQuery(Query);
            }
            catch (CacheException ex)
            {
                Logger.WriteEror("Can not execute COUNT : {0} {1}", ex.Message, ex.ServerMessage);
            }
            catch (Exception ex)
            {
                Logger.WriteEror("Can not execute COUNT : {0}", ex.Message);
                return(client);
            }
            finally
            {
                var profilerResult = Profiler.End();


                Logger.Write("Found {0} items. The call took {1:F4} miliseconds", result.Value,
                             profilerResult.TotalTimeMiliseconds);
            }


            return(client);
        }
コード例 #13
0
        /// <summary>
        ///     Match a regular expression against a string. If successfull return the captured strings
        /// </summary>
        /// <param name="toParse">string to parse</param>
        /// <param name="regex">regular expression as text</param>
        /// <param name="captures">list to be filled with captured strings</param>
        /// <returns>true if successfull match</returns>
        private static bool Parse(string toParse, string regex, ICollection <string> captures)
        {
            var expression = new Regex(regex, RegexOptions.IgnoreCase);
            var match      = expression.Match(toParse);

            if (!match.Success || match.Captures.Count != 1)
            {
                return(false);
            }

            //group 0 is the full matched expression
            //captures start at group 1
            Dbg.CheckThat(match.Groups.Count >= 1);
            for (var i = 1; i < match.Groups.Count; i++)
            {
                var value = match.Groups[i].Value;
                if (!string.IsNullOrEmpty(value))
                {
                    captures.Add(value);
                }
            }

            return(true);
        }
コード例 #14
0
        /// <summary>
        /// </summary>
        /// <returns></returns>
        internal override IDataClient TryExecute(IDataClient client)
        {
            if (!CanExecute)
            {
                return(client);
            }

            Dbg.CheckThat(Params.Count >= 2);

            IList <JObject> listResult = null;

            try
            {
                Dbg.CheckThat(Query != null);

                Profiler.IsActive = true;
                Profiler.Start("SELECT");

                listResult = client.GetMany(Query).Select(r => r.Item).ToList();

                var dumpOk = true;

                // if an into clause was specified the file name is in the single optional parameter
                if (Params.Count == 1)
                {
                    dumpOk = Logger.DumpFile(Params[0]);
                }

                if (dumpOk)
                {
                    Logger.Write("[");
                    for (var i = 0; i < listResult.Count; i++)
                    {
                        Logger.Write(listResult[i].ToString());
                        if (i < listResult.Count - 1)
                        {
                            Logger.Write(",");
                        }
                    }

                    Logger.Write("]");

                    if (Params.Count == 1)
                    {
                        Logger.EndDump();
                    }
                }
            }
            catch (CacheException ex)
            {
                Logger.WriteEror("Can not execute SELECT : {0} {1}", ex.Message, ex.ServerMessage);
                return(client);
            }
            catch (Exception ex)
            {
                Logger.WriteEror("Can not execute SELECT : {0}", ex.Message);
                return(client);
            }
            finally
            {
                Profiler.End();

                var count = 0;
                if (listResult != null)
                {
                    count = listResult.Count;
                }

                Logger.Write("Found {0} items. The call took {1} milliseconds", count,
                             Profiler.TotalTimeMilliseconds);
            }


            return(client);
        }
コード例 #15
0
        public override bool TryExecute()
        {
            if (!CanExecute)
            {
                return(false);
            }


            Dbg.CheckThat(Params.Count <= 1);


            try
            {
                Profiler.IsActive = true;
                Profiler.Start("DESC");

                var serverInfo = _client.GetServerDescription();


                var profilerResult = Profiler.End();


                if (Params.Count == 1)
                {
                    var tableName = Params[0];
                    foreach (var keyValuePair in serverInfo.KnownTypesByFullName)
                    {
                        if (tableName.ToUpper() == keyValuePair.Value.TypeName.ToUpper())
                        {
                            LogTypeInfo(keyValuePair.Value, false);
                            break;
                        }
                    }
                }
                else
                {
                    Logger.Write("");
                    Logger.Write("Server process");
                    Logger.Write("----------------------------------------------------------------------------");

                    //Logger.Write("    server name  = {0}", serverInfo.ServerProcessInfo.Host);
                    Logger.Write("      image type = {0} bits", serverInfo.ServerProcessInfo.Bits);
                    Logger.Write("      started at = {0}", serverInfo.ServerProcessInfo.StartTime);
                    Logger.Write("  active clients = {0}", serverInfo.ServerProcessInfo.ConnectedClients);
                    Logger.Write("         threads = {0}", serverInfo.ServerProcessInfo.Threads);
                    Logger.Write(" physical memory = {0} MB", serverInfo.ServerProcessInfo.WorkingSet / 1000000);
                    Logger.Write("  virtual memory = {0} MB", serverInfo.ServerProcessInfo.VirtualMemory / 1000000);
                    Logger.Write("software version = {0} ", serverInfo.ServerProcessInfo.SoftwareVersion);
                    Logger.Write("");
                    Logger.Write("Tables");
                    Logger.Write("-----------------------------------------------------------------------------");


                    var header = string.Format("| {0,15} | {1,9} | {2,5} | {3,6} | {4, 16} | {5, 7} |", "Name",
                                               "Count",
                                               "Zip", "Hits", "Eviction", "Serial.");

                    Logger.Write(header);
                    Logger.Write("-----------------------------------------------------------------------------");

                    foreach (var keyValuePair in serverInfo.DataStoreInfoByFullName)
                    {
                        var compression = keyValuePair.Value.DataCompression.ToString();
                        var hitCount    = keyValuePair.Value.ReadCount == 0
                            ? 0
                            : keyValuePair.Value.HitCount * 100 / keyValuePair.Value.ReadCount;

                        var serialization = ".Net";

                        Logger.Write("| {0,15} | {1,9} | {2,5} | {3,6}%| {4, 16} | {5, 7} |",
                                     keyValuePair.Value.TableName,
                                     keyValuePair.Value.Count,
                                     compression,
                                     hitCount,
                                     keyValuePair.Value.EvictionPolicyDescription,
                                     serialization);
                    }

                    Logger.Write("-----------------------------------------------------------------------------");
                }


                Logger.Write("The call took {0} milliseconds", profilerResult.TotalTimeMiliseconds);
            }
            catch (Exception ex)
            {
                Logger.WriteEror("Can not execute DESC : {0}", ex.Message);
                return(false);
            }


            return(true);
        }
コード例 #16
0
        internal override ICacheClient TryExecute(ICacheClient client)
        {
            if (!CanExecute)
            {
                return(null);
            }


            Dbg.CheckThat(Params.Count <= 1);


            try
            {
                Profiler.IsActive = true;
                Profiler.Start("DESC");

                var serverInfo = client.GetClusterInformation();


                ProfilingData profilerResult = Profiler.End();


                if (Params.Count == 1)
                {
                    string tableName = Params[0];
                    foreach (var typeDescription in serverInfo.Schema)
                    {
                        if (tableName.ToUpper() == typeDescription.TypeName.ToUpper())
                        {
                            LogTypeInfo(typeDescription);
                            break;
                        }
                    }
                }
                else
                {
                    foreach (var info in serverInfo.ServersStatus)
                    {
                        Logger.Write("");
                        Logger.Write("Server process");
                        Logger.Write("----------------------------------------------------------------------------");

                        //Logger.Write("    server name  = {0}", serverInfo.ServerProcessInfo.Host);
                        Logger.Write("      image type = {0} bits", info.Bits);
                        Logger.Write("      started at = {0}", info.StartTime);
                        Logger.Write("  active clients = {0}", info.ConnectedClients);
                        Logger.Write("         threads = {0}", info.Threads);
                        Logger.Write(" physical memory = {0} MB", info.WorkingSet / 1000000);
                        Logger.Write("  virtual memory = {0} MB", info.VirtualMemory / 1000000);
                        Logger.Write("software version = {0} ", info.SoftwareVersion);
                        Logger.Write("");
                    }

                    Logger.Write("Tables");
                    Logger.Write("-----------------------------------------------------------------------------");

                    string header =
                        $"| {"Name",15} | {"Zip",5} |";

                    Logger.Write(header);
                    Logger.Write("-----------------------------------------------------------------------------");

                    foreach (var typeDescription in serverInfo.Schema)
                    {
                        string compression = typeDescription.UseCompression.ToString();



                        Logger.Write("| {0,15} | {1,5} |",
                                     typeDescription.TypeName,
                                     compression
                                     );
                    }

                    Logger.Write("-----------------------------------------------------------------------------");
                }


                Logger.Write("The call took {0} milliseconds", profilerResult.TotalTimeMiliseconds);
            }
            catch (Exception ex)
            {
                Profiler.End();
                Logger.WriteEror("Can not execute DESC : {0}", ex.Message);

                return(null);
            }


            return(client);
        }
コード例 #17
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        internal override ICacheClient TryExecute(ICacheClient client)
        {
            if (!CanExecute)
            {
                return(client);
            }

            Dbg.CheckThat(Params.Count >= 2);

            IList <CachedObject> listResult = null;

            try
            {
                Dbg.CheckThat(Query != null);

                Profiler.IsActive = true;
                Profiler.Start("SELECT");

                listResult = client.GetObjectDescriptions(Query);

                bool dumpOk = true;

                // the third parameter(optional) is the output file name
                if (Params.Count == 3)
                {
                    dumpOk = Logger.DumpFile(Params[2]);
                }

                if (dumpOk)
                {
                    Logger.Write("[");
                    for (int i = 0; i < listResult.Count; i++)
                    {
                        Logger.Write(listResult[i].AsJson());
                        if (i < listResult.Count - 1)
                        {
                            Logger.Write(",");
                        }
                    }

                    Logger.Write("]");

                    if (Params.Count == 3)
                    {
                        Logger.EndDump();
                    }
                }
            }
            catch (CacheException ex)
            {
                Logger.WriteEror("Can not execute GET : {0} {1}", ex.Message, ex.ServerMessage);
                return(client);
            }
            catch (Exception ex)
            {
                Logger.WriteEror("Can not execute GET : {0}", ex.Message);
                return(client);
            }
            finally
            {
                ProfilingData profilerResult = Profiler.End();

                int count = 0;
                if (listResult != null)
                {
                    count = listResult.Count;
                }

                Logger.Write("Found {0} items. The call took {1} miliseconds", count,
                             profilerResult.TotalTimeMiliseconds);
            }


            return(client);
        }
コード例 #18
0
        /// <summary>
        ///     Check if this atomic query is a subset of another atomic query
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public bool IsSubsetOf(AtomicQuery query)
        {
            var rightOperator = query.Operator;

            if (PropertyName != query.PropertyName)
            {
                return(false);
            }

            Dbg.CheckThat(Value.Type == query.Value.Type);

            if (!AreOperatorsCompatibleWithSubset(Operator, rightOperator))
            {
                return(false);
            }


            switch (Operator)
            {
            case QueryOperator.Eq:
                if (rightOperator == QueryOperator.Eq)
                {
                    return(Value == query.Value);
                }
                if (rightOperator == QueryOperator.Le)
                {
                    return(Value <= query.Value);
                }
                if (rightOperator == QueryOperator.Lt)
                {
                    return(Value < query.Value);
                }
                if (rightOperator == QueryOperator.Ge)
                {
                    return(Value >= query.Value);
                }
                if (rightOperator == QueryOperator.Gt)
                {
                    return(Value > query.Value);
                }
                return(false);

            case QueryOperator.Le:
                return(Value <= query.Value);

            case QueryOperator.Lt:
                return(Value <= query.Value);

            case QueryOperator.Gt:
                return(Value >= query.Value);

            case QueryOperator.Ge:
                return(Value >= query.Value);

            case QueryOperator.GeLe:
            case QueryOperator.GeLt:
            case QueryOperator.GtLe:
            case QueryOperator.GtLt:
                return(Value >= query.Value && Value2 <= query.Value2);



            case QueryOperator.In:
                return(_inValues.IsSubsetOf(query._inValues));

            default:
                return(false);
            }
        }
コード例 #19
0
ファイル: CommandSearch.cs プロジェクト: usinesoft/Cachalot
        internal override IDataClient TryExecute(IDataClient client)
        {
            if (!CanExecute)
            {
                return(client);
            }

            Dbg.CheckThat(Params.Count == 2);

            IList <JObject> listResult = null;

            try
            {
                Dbg.CheckThat(Query != null);

                Profiler.IsActive = true;
                Profiler.Start("SEARCH");

                listResult = client.GetMany(Query).Select(r => r.Item).Cast <JObject>().ToList();



                Logger.Write("[");
                for (var i = 0; i < listResult.Count; i++)
                {
                    Logger.Write(listResult[i].ToString());
                    if (i < listResult.Count - 1)
                    {
                        Logger.Write(",");
                    }
                }

                Logger.Write("]");

                Logger.DumpFile("ftresult.json");

                Logger.Write("[");
                for (var i = 0; i < listResult.Count; i++)
                {
                    Logger.Write(listResult[i].ToString());
                    if (i < listResult.Count - 1)
                    {
                        Logger.Write(",");
                    }
                }

                Logger.Write("]");
                Logger.EndDump();

                return(client);
            }
            catch (CacheException ex)
            {
                Logger.WriteEror("Can not execute SEARCH : {0} {1}", ex.Message, ex.ServerMessage);
                return(client);
            }
            catch (Exception ex)
            {
                Logger.WriteEror("Can not execute SEARCH : {0}", ex.Message);
                return(client);
            }
            finally
            {
                Profiler.End();

                var count = 0;
                if (listResult != null)
                {
                    count = listResult.Count;
                }

                Logger.Write("Found {0} items. The call took {1} milliseconds", count,
                             Profiler.TotalTimeMilliseconds);
            }
        }