/// <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}"); }
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); } }
/// <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); } }
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); }
public static void Write(string message) { if (_dumpOn) { Dbg.CheckThat(_dumpStream != null); _dumpStream.WriteLine(message); } else { CommandLogger.Write(message); } }
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); } }
/// <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); }
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); }
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); }
/// <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); }
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); }
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); }
/// <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); }
/// <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); }
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); }
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); }
/// <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); }
/// <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); } }
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); } }