Ejemplo n.º 1
0
Archivo: Lookup.cs Proyecto: jpree/nit
        /// <summary>
        /// Gets dictionary of all blob identities to number of keyword hits.
        /// </summary>
        /// <param name="tags">Tags to look for.</param>
        /// <returns>Dictionary of blob hash identities to kit count.</returns>
        public static Dictionary <byte[], uint> GetKeywordDictionary(string[] tags)
        {
            Guard.ThrowIfEmpty(tags, nameof(tags));

            var result = new Dictionary <byte[], uint>(new ByteArrayCompare());
            var buffer = new byte[Hash.Length];

            foreach (var tag in tags)
            {
                var tagHash = Hash.HashString(tag.ToUpper());
                var tagPath = NitPath.GetFullTagPath(tagHash);

                if (File.Exists(tagPath))
                {
                    HashFileReader.Read(tagPath, (input) =>
                    {
                        // if not there, add it with cnt 1
                        if (!result.TryAdd(input, 1))
                        {
                            // it already exists, so increment
                            result[input]++;
                        }

                        return(true);
                    });
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
Archivo: Tag.cs Proyecto: jpree/nit
        /// <summary>
        /// Create tags for a give hash code.
        /// </summary>
        /// <param name="hash">A hash to associate tags with.</param>
        /// <param name="tags">The tags to associate.</param>
        public static void CreateTags(Span <byte> hash, string[] tags)
        {
            foreach (var tag in tags)
            {
                var tagHash       = Hash.HashString(tag.ToUpper());
                var fullPath      = NitPath.GetFullTagPath(tagHash);
                var directoryPath = NitPath.GetTagDirectoryPath(tagHash);

                if (!File.Exists(fullPath))
                {
                    Directory.CreateDirectory(directoryPath);
                    using (var s = File.CreateText(fullPath))
                    {
                        var l = hash.GetHexString();
                        s.WriteLine(l);
                    }

                    continue;
                }

                // need a value type for lambda
                byte[] temp = hash.ToArray();

                // scan hashes in the file and stop when a match is found
                HashFileReader.Read(fullPath, (input) => !((Span <byte>)temp).SequenceEqual((Span <byte>)input));

                // Add the entry to the end
                var line = hash.GetHexString();
                File.AppendAllLines(fullPath, new List <string>()
                {
                    line
                });
            }
        }