/// <summary> /// Constructs a new instance of <see cref="SignatureV31"/>. /// </summary> /// <param name="dataSet"> /// The data set the signature is contained within. /// </param> /// <param name="index"> /// The index in the data structure to the signature. /// </param> /// <param name="reader"> /// Reader connected to the source data structure and positioned to /// start reading. /// </param> internal SignatureV31( DataSet dataSet, int index, Reader reader) : base(dataSet, index, reader) { _nodeOffsets = ReadOffsets(dataSet, reader, dataSet.SignatureNodesCount); }
internal static void AssertCacheMissesGoodAll(DataSet dataSet) { Assert.IsTrue(dataSet.PercentageSignatureCacheMisses < 0.4, "Signature Cache Misses"); Assert.IsTrue(dataSet.PercentageStringsCacheMisses < 0.6, "Strings Cache Misses"); Assert.IsTrue(dataSet.PercentageNodeCacheMisses < 0.3, "Node Cache Misses"); Assert.IsTrue(dataSet.PercentageValuesCacheMisses < 0.3, "Value Cache Misses"); Assert.IsTrue(dataSet.PercentageProfilesCacheMisses < 0.3, "Profile Cache Misses"); }
/// <summary> /// Constructs a new instance of see <see cref="Map"/> /// </summary> /// <param name="dataSet"> /// The <see cref="DataSet"/> being created /// </param> /// <param name="index"> /// Index of the map within the list /// </param> /// <param name="reader"> /// Reader connected to the source data structure and positioned to start reading /// </param> internal Map( DataSet dataSet, int index, BinaryReader reader) : base(dataSet, index) { _nameOffset = reader.ReadInt32(); }
/// <summary> /// Creates a new <see cref="DataSet"/> from the byte array. /// </summary> /// <param name="array">Array of bytes to build the data set from</param> /// <param name="init">True to indicate that the data set should be fulling initialised</param> /// <returns>A <see cref="DataSet"/> filled with data from the array</returns> public static DataSet Create(byte[] array, bool init) { DataSet dataSet = new DataSet(DateTime.MinValue, DataSet.Modes.Memory); using (var reader = new Reader(new MemoryStream(array))) { Load(dataSet, reader, init); } return dataSet; }
/// <summary> /// Constructs a new instance of <see cref="SignatureV32"/>. /// </summary> /// <param name="dataSet"> /// The data set the signature is contained within. /// </param> /// <param name="index"> /// The index in the data structure to the signature. /// </param> /// <param name="reader"> /// Reader connected to the source data structure and positioned to /// start reading. /// </param> internal SignatureV32( DataSet dataSet, int index, Reader reader) : base(dataSet, index, reader) { NodeCount = reader.ReadByte(); FirstNodeOffsetIndex = reader.ReadInt32(); _rank = reader.ReadInt32(); Flags = reader.ReadByte(); }
/// <summary> /// Constructs a new instance of <see cref="AsciiString"/> /// </summary> /// <param name="dataSet"> /// The data set whose strings list the string is contained within /// </param> /// <param name="offset"> /// The offset to the start of the string within the string data structure /// </param> /// <param name="reader"> /// Binary reader positioned at the start of the AsciiString. /// </param> internal AsciiString(DataSet dataSet, int offset, BinaryReader reader) : base(dataSet, offset) { // Read the length of the array minus 1 to remove the // last null character which isn't used by .NET. Value = reader.ReadBytes(reader.ReadInt16() - 1); // Read and discard the null value to ensure the file // position is correct for the next read. reader.ReadByte(); }
/// <summary> /// Adds the items to the packages template zip file. /// </summary> /// <param name="dataSet">Data set used to create the package file</param> /// <param name="packageFile">Location for the generated package file</param> /// <param name="projectFile">Location of the MS project file to generate the DLL</param> internal static void Generate(DataSet dataSet, FileInfo packageFile, FileInfo projectFile) { using (var packageInputStream = new MemoryStream()) { GetPackageTemplate().CopyTo(packageInputStream); packageInputStream.Position = 0; using (var package = ZipFile.Read(packageInputStream)) { GenerateSitecoreItems(dataSet, package, projectFile); Add51DegreesLiteDataFile(package); SaveTemplatePackage(package, packageFile); } } }
/// <summary> /// Generates class conditions that can be added into the Mobile Device /// Detector project to implement the rule conditions defined in the /// package. Classes are only generated for properties that have been /// identified to have their own rules. /// </summary> /// <param name="dataSet"></param> internal static void Generate(DataSet dataSet) { foreach (var property in dataSet.Properties.Where(i => i.IsObsolete == false && i.Name.StartsWith("Javascript") == false && Constants.ItemNameValidator.IsMatch(i.Name) && Constants.TopProperties.Contains(i.Name))) { Console.WriteLine("Generating class for '{0}'", property.JavaScriptName); var propertyGuidId = Helper.GetGuid(Constants.PropertyRuleGuid, property.JavaScriptName); File.WriteAllText(Path.Combine( Constants.CONDITIONS_CLASSES_PATH, String.Format( "{0}Condition.cs", property.JavaScriptName)), Helper.ReplaceTags( property, Helper.GetTemplate( Constants.LookupProperties.Contains(property.JavaScriptName) ? "lookupcondition.class" : "propertycondition.class"), propertyGuidId)); } }
/// <summary> /// Constructs a new provided using the data set. /// </summary> /// <param name="dataSet">Data set to use for device detection</param> /// <param name="recordDetectionTime">True if the detection time should be recorded</param> /// <param name="cacheSize">Size of the cache used with the provider</param> public Provider(DataSet dataSet, bool recordDetectionTime, int cacheSize) { DataSet = dataSet; MethodCounts = new SortedList<MatchMethods, long>(); MethodCounts.Add(MatchMethods.Closest, 0); MethodCounts.Add(MatchMethods.Nearest, 0); MethodCounts.Add(MatchMethods.Exact, 0); MethodCounts.Add(MatchMethods.Numeric, 0); MethodCounts.Add(MatchMethods.None, 0); RecordDetectionTime = recordDetectionTime; _userAgentCache = cacheSize > 0 ? new Cache<string, MatchResult>(cacheSize) : null; }
/// <summary> /// Constructs a new provider using the data set. /// </summary> /// <param name="dataSet">Data set to use for device detection</param> /// <param name="cacheSize">Size of the cache used with the provider</param> public Provider(DataSet dataSet, int cacheSize) : this(dataSet, false, cacheSize) { }
/// <summary> /// Constructs a new provider using the data set. /// </summary> /// <param name="dataSet">Data set to use for device detection</param> public Provider(DataSet dataSet) : this(dataSet, false, 0) { }
public void CleanUp() { _dataSet.Dispose(); _dataSet = null; }
/// <summary> /// Returns true if a newer data set is now available. /// </summary> /// <param name="dataSet">The data set to be checked for changes to the master source</param> /// <returns>true is a newer data set is available</returns> private static bool CheckDataFileHasRefreshed(DataSet dataSet) { if (Manager.BinaryFilePath != null && File.Exists(Manager.BinaryFilePath) && dataSet.Published != null && dataSet.LastModified > DateTime.MinValue) { if (dataSet.LastModified != null && File.GetLastWriteTimeUtc(Manager.BinaryFilePath) > dataSet.LastModified) { return true; } } return false; }
public static void ReportCache(DataSet dataSet) { Console.WriteLine("Node cache misses '{0:P2}'", dataSet.PercentageNodeCacheMisses); Console.WriteLine("Profiles cache misses '{0:P2}'", dataSet.PercentageProfilesCacheMisses); Console.WriteLine("Signatures cache misses '{0:P2}'", dataSet.PercentageSignatureCacheMisses); Console.WriteLine("Strings cache misses '{0:P2}'", dataSet.PercentageStringsCacheMisses); Console.WriteLine("Values cache misses '{0:P2}'", dataSet.PercentageValuesCacheMisses); }
/// <summary> /// Constructs a new instance of <see cref="Integer"/> /// </summary> /// <param name="dataSet"> /// The data set whose strings list the string is contained within /// </param> /// <param name="index"> /// The index in the data structure to the integer. /// </param> /// <param name="reader"> /// Binary reader positioned at the start of the Integer. /// </param> internal Integer(DataSet dataSet, int index, BinaryReader reader) : base(dataSet, index) { _value = reader.ReadInt32(); }
public static void ReportCache(DataSet dataSet) { Console.WriteLine("Node cache switches '{0}' with '{1:P2} misses", dataSet.NodeCacheSwitches, dataSet.PercentageNodeCacheMisses); Console.WriteLine("Profiles cache switches '{0}' with '{1:P2} misses", dataSet.ProfilesCacheSwitches, dataSet.PercentageProfilesCacheMisses); Console.WriteLine("Ranked Signatures cache switches '{0}' with '{1:P2} misses", dataSet.RankedSignatureCacheSwitches, dataSet.PercentageRankedSignatureCacheMisses); Console.WriteLine("Signatures cache switches '{0}' with '{1:P2} misses", dataSet.SignatureCacheSwitches, dataSet.PercentageSignatureCacheMisses); Console.WriteLine("Strings cache switches '{0}' with '{1:P2} misses", dataSet.StringsCacheSwitches, dataSet.PercentageStringsCacheMisses); Console.WriteLine("Values cache switches '{0}' with '{1:P2} misses", dataSet.ValuesCacheSwitches, dataSet.PercentageValuesCacheMisses); }
internal static void AssertCacheMissesGood(DataSet dataSet) { Assert.IsTrue(dataSet.PercentageSignatureCacheMisses < 0.4, "Signature Cache Misses"); Assert.IsTrue(dataSet.PercentageStringsCacheMisses < 0.5, "Strings Cache Misses"); Assert.IsTrue(dataSet.PercentageNodeCacheMisses < 0.3, "Node Cache Misses"); }
/// <summary> /// Creates a new <see cref="DataSet"/> from the binary reader provided. /// </summary> /// <param name="dataSet">The data set to be loaded with data from the reader</param> /// <param name="reader"> /// Reader connected to the source data structure and positioned to start reading /// </param> /// <param name="init"> /// True to indicate that the data set should be fulling initialised /// </param> /// <para> /// A <see cref="DataSet"/> is constructed using the reader to retrieve /// the header information. This is then passed to the Read methods to create the /// lists before reading the data into memory. Finally it initialise is required /// references between entities are worked out and stored. /// </para> internal static void Load(DataSet dataSet, Reader reader, bool init) { CommonFactory.LoadHeader(dataSet, reader); var strings = new MemoryVariableList<AsciiString<DataSet>, DataSet>(dataSet, reader, new MemoryAsciiStringFactory()); MemoryFixedList<Component, DataSet> components = null; switch(dataSet.VersionEnum) { case BinaryConstants.FormatVersions.PatternV31: components = new MemoryFixedList<Component, DataSet>(dataSet, reader, new ComponentFactoryV31()); break; case BinaryConstants.FormatVersions.PatternV32: components = new MemoryFixedList<Component, DataSet>(dataSet, reader, new ComponentFactoryV32()); break; } var maps = new MemoryFixedList<Map, DataSet>(dataSet, reader, new MapFactory()); var properties = new PropertiesList(dataSet, reader, new PropertyFactory()); var values = new MemoryFixedList<Value, DataSet>(dataSet, reader, new ValueFactory<DataSet>()); var profiles = new MemoryVariableList<Entities.Profile, DataSet>(dataSet, reader, new ProfileMemoryFactory()); MemoryFixedList<Signature, DataSet> signatures = null; MemoryIntegerList signatureNodeOffsets = null; MemoryIntegerList nodeRankedSignatureIndexes = null; switch(dataSet.VersionEnum) { case BinaryConstants.FormatVersions.PatternV31: signatures = new MemoryFixedList<Signature, DataSet>(dataSet, reader, new SignatureFactoryV31<DataSet>(dataSet)); break; case BinaryConstants.FormatVersions.PatternV32: signatures = new MemoryFixedList<Signature, DataSet>(dataSet, reader, new SignatureFactoryV32<DataSet>(dataSet)); signatureNodeOffsets = new MemoryIntegerList(reader); nodeRankedSignatureIndexes = new MemoryIntegerList(reader); break; } var rankedSignatureIndexes = new MemoryIntegerList(reader); MemoryVariableList<Entities.Node, DataSet> nodes = null; switch (dataSet.VersionEnum) { case BinaryConstants.FormatVersions.PatternV31: nodes = new MemoryVariableList<Entities.Node, DataSet>(dataSet, reader, new NodeMemoryFactoryV31()); break; case BinaryConstants.FormatVersions.PatternV32: nodes = new MemoryVariableList<Entities.Node, DataSet>(dataSet, reader, new NodeMemoryFactoryV32()); break; } var rootNodes = new MemoryFixedList<Entities.Node, DataSet>(dataSet, reader, new RootNodeFactory()); var profileOffsets = new MemoryFixedList<ProfileOffset, DataSet>(dataSet, reader, new ProfileOffsetFactory()); dataSet.Strings = strings; dataSet._components = components; dataSet._maps = maps; dataSet._properties = properties; dataSet._values = values; dataSet.Profiles = profiles; dataSet._signatures = signatures; dataSet._rankedSignatureIndexes = rankedSignatureIndexes; switch (dataSet.VersionEnum) { case BinaryConstants.FormatVersions.PatternV32: dataSet._signatureNodeOffsets = signatureNodeOffsets; dataSet._nodeRankedSignatureIndexes = nodeRankedSignatureIndexes; break; } dataSet.Nodes = nodes; dataSet.RootNodes = rootNodes; dataSet._profileOffsets = profileOffsets; strings.Read(reader); components.Read(reader); maps.Read(reader); properties.Read(reader); values.Read(reader); profiles.Read(reader); signatures.Read(reader); switch (dataSet.VersionEnum) { case BinaryConstants.FormatVersions.PatternV32: signatureNodeOffsets.Read(reader); nodeRankedSignatureIndexes.Read(reader); break; } rankedSignatureIndexes.Read(reader); nodes.Read(reader); rootNodes.Read(reader); profileOffsets.Read(reader); if (init) { // Set references between objects. dataSet.Init(); // The following lists will not be needed anymore // so they can be freed. dataSet._signatureNodeOffsets = null; dataSet._nodeRankedSignatureIndexes = null; // Force garbage collection as a lot of memory has been freed. GC.Collect(); } }
/// <summary> /// Creates a new <see cref="DataSet"/> from the file provided. /// </summary> /// <param name="filePath"> /// Uncompressed file containing the data for the data set /// </param> /// <param name="init"> /// True to indicate that the data set should be fully initialised /// </param> /// <param name="lastModified">Date and time the source data was last /// modified.</param> /// <returns>A <see cref="DataSet"/> filled with data from the array /// </returns> public static DataSet Create(string filePath, bool init, DateTime lastModified) { DataSet dataSet = new DataSet(lastModified, DataSet.Modes.Memory); using (var reader = new Reader( File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))) { Load(dataSet, reader, init); } return dataSet; }
/// <summary> /// Loads the data set headers information. /// </summary> /// <param name="dataSet">The data set to be loaded</param> /// <param name="reader">Reader positioned at the beginning of the /// data source</param> internal static void LoadHeader(DataSet dataSet, BinaryReader reader) { // Check for an exception which would indicate the file is the // wrong type for the API. try { dataSet.Version = new Version( reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32()); } catch (ArgumentOutOfRangeException ex) { throw new MobileException(String.Format( "Data file is invalid. Check that the data file is " + "decompressed and is version '{0}' format.", String.Join(",", BinaryConstants.SupportedPatternFormatVersions.Select(i => i.Value.ToString()))), ex); } // Throw exception if the data file does not have the correct // version in formation. if (BinaryConstants.SupportedPatternFormatVersions.Any(i => i.Value.Equals(dataSet.Version)) == false) { throw new MobileException(String.Format( "Version mismatch. Data is version '{0}' for '{1}' reader", dataSet.Version, String.Join(",", BinaryConstants.SupportedPatternFormatVersions.Select(i => i.Value.ToString())))); } // Set the enum format version value for easier if logic. dataSet.VersionEnum = BinaryConstants.SupportedPatternFormatVersions.FirstOrDefault(i => i.Value.Equals(dataSet.Version)).Key; // Read the common header fields. dataSet.Tag = new Guid(reader.ReadBytes(16)); switch(dataSet.VersionEnum) { case BinaryConstants.FormatVersions.PatternV32: dataSet.Export = new Guid(reader.ReadBytes(16)); break; } dataSet.CopyrightOffset = reader.ReadInt32(); dataSet.AgeAtPublication = new TimeSpan(reader.ReadInt16() * TimeSpan.TicksPerDay * 30); dataSet.MinUserAgentCount = reader.ReadInt32(); dataSet.NameOffset = reader.ReadInt32(); dataSet.FormatOffset = reader.ReadInt32(); dataSet.Published = ReadDate(reader); dataSet.NextUpdate = ReadDate(reader); dataSet.DeviceCombinations = reader.ReadInt32(); dataSet.MaxUserAgentLength = reader.ReadInt16(); dataSet.MinUserAgentLength = reader.ReadInt16(); dataSet.LowestCharacter = reader.ReadByte(); dataSet.HighestCharacter = reader.ReadByte(); dataSet.MaxSignatures = reader.ReadInt32(); dataSet.SignatureProfilesCount = reader.ReadInt32(); dataSet.SignatureNodesCount = reader.ReadInt32(); dataSet.MaxValues = reader.ReadInt16(); dataSet.CsvBufferLength = reader.ReadInt32(); dataSet.JsonBufferLength = reader.ReadInt32(); dataSet.XmlBufferLength = reader.ReadInt32(); dataSet.MaxSignaturesClosest = reader.ReadInt32(); // Read the V32 headers specifically. if (dataSet.VersionEnum == BinaryConstants.FormatVersions.PatternV32) { dataSet._maximumRank = reader.ReadInt32(); } }
public void CreateDataSet() { Utils.CheckFileExists(DataFile); _dataSet = StreamFactory.Create(DataFile, false); _provider = new Provider(_dataSet); }
/// <summary> /// Constructs a new instance of <see cref="ProfileOffset"/> /// </summary> /// <param name="dataSet"> /// The data set whose strings list the string is contained within /// </param> /// <param name="offset"> /// The offset to the start of the profile within the profiles data structure /// </param> /// <param name="reader"> /// Binary reader positioned at the start of the ProfileOffset. /// </param> internal ProfileOffset(DataSet dataSet, int offset, BinaryReader reader) : base(dataSet, offset) { _profileId = reader.ReadInt32(); _offset = reader.ReadInt32(); }
/// <summary> /// Creates a stream data set for the file provided and /// returns the published data of the file. Throws an /// exception if there's a problem accessing the file. /// </summary> /// <param name="fileName"></param> /// <returns>The date time from the file, or null if the file is not a valid data file</returns> internal static DateTime? GetDataFileDate(string fileName) { try { using (var reader = new BinaryReader(File.OpenRead(fileName))) { using (var dataSet = new DataSet(File.GetLastWriteTimeUtc(fileName), DataSet.Modes.File)) { CommonFactory.LoadHeader(dataSet, reader); return dataSet.Published; } } } catch(Exception ex) { EventLog.Info(String.Format( "Exception getting data file date from file '{0}'", fileName)); EventLog.Debug(ex); return null; } }
/// <summary> /// Creates the bespoke custom Sitecore items in the package template /// which is part of the package builder. /// </summary> /// <remarks> /// Ignores properties that are prefixed Javascript as these relate to /// embedded javascript snippets which aren't relevent to Sitecore rules. /// Property names that don't comply with Sitecore item names, or that are /// marked as obsolete in 51Degrees aren't processed. /// </remarks> /// <param name="dataSet"></param> /// <param name="package"></param> /// <param name="projectFile"></param> private static void GenerateSitecoreItems(DataSet dataSet, ZipFile package, FileInfo projectFile) { package.CompressionLevel = CompressionLevel.None; foreach (var property in dataSet.Properties.Where(i => i.IsObsolete == false && i.Name.StartsWith("Javascript") == false && Constants.ItemNameValidator.IsMatch(i.Name))) { GeneratePropertyLookupItem(package, property); GeneratePropertyCondition(package, property); } BuildDlls(package, projectFile); }
/// <summary> /// Constructs a new instance of the provider with the data set /// supplied. /// </summary> /// <param name="dataSet"></param> private WebProvider(DataSet dataSet) : base(dataSet, true, Constants.UserAgentCacheSize) { }
public void CreateDataSet() { Utils.CheckFileExists(DataFile); _dataSet = StreamFactory.Create(File.ReadAllBytes(DataFile)); _provider = new Provider(_dataSet); Console.WriteLine("Dataset: {0}", _dataSet.Name); Console.WriteLine("Format: {0}", _dataSet.Format); }