Example #1
0
        private static MusicNote CheckCache(string note, int a4Reference)
        {
            MusicNote musicNote = null;

            if (MusicNoteCache == null)
            {
                MusicNoteCache = new BiMap <string, MusicNote>();
            }
            if (MusicNoteCache.Forward.ContainsKey(note))
            {
                musicNote = MusicNoteCache.Forward[note];
            }
            if (musicNote == null)
            {
                musicNote = new MusicNote()
                {
                    Key       = note,
                    Reference = a4Reference
                };
                musicNote.Error += MusicNote_Error;
                musicNote.InitializeNote();
                if (musicNote.IsValid)
                {
                    FactoryCreateNote?.Invoke(musicNote, new EventArgs());
                    MusicNoteCache.Add(note, musicNote);
                }
                else
                {
                    return(null);
                }
            }
            return(musicNote);
        }
 private FabricRegistry(int version, BiMap <string, int> blockMap, BiMap <string, int> blockEntityTypeMap, BiMap <string, int> itemMap)
 {
     Version            = version;
     BlockMap           = blockMap;
     BlockEntityTypeMap = blockEntityTypeMap;
     ItemMap            = itemMap;
 }
Example #3
0
        public virtual void TestDuplicates()
        {
            Assume.AssumeTrue(!Shell.Windows);
            string GetAllUsersCmd = "echo \"root:x:0:0:root:/root:/bin/bash\n" + "hdfs:x:11501:10787:Grid Distributed File System:/home/hdfs:/bin/bash\n"
                                    + "hdfs:x:11502:10788:Grid Distributed File System:/home/hdfs:/bin/bash\n" + "hdfs1:x:11501:10787:Grid Distributed File System:/home/hdfs:/bin/bash\n"
                                    + "hdfs2:x:11502:10787:Grid Distributed File System:/home/hdfs:/bin/bash\n" + "bin:x:2:2:bin:/bin:/bin/sh\n"
                                    + "bin:x:1:1:bin:/bin:/sbin/nologin\n" + "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n"
                                    + "daemon:x:2:2:daemon:/sbin:/sbin/nologin\"" + " | cut -d: -f1,3";
            string GetAllGroupsCmd = "echo \"hdfs:*:11501:hrt_hdfs\n" + "mapred:x:497\n" + "mapred2:x:497\n"
                                     + "mapred:x:498\n" + "mapred3:x:498\"" + " | cut -d: -f1,3";
            // Maps for id to name map
            BiMap <int, string> uMap = HashBiMap.Create();
            BiMap <int, string> gMap = HashBiMap.Create();

            ShellBasedIdMapping.UpdateMapInternal(uMap, "user", GetAllUsersCmd, ":", EmptyPassThroughMap
                                                  );
            Assert.Equal(5, uMap.Count);
            Assert.Equal("root", uMap[0]);
            Assert.Equal("hdfs", uMap[11501]);
            Assert.Equal("hdfs2", uMap[11502]);
            Assert.Equal("bin", uMap[2]);
            Assert.Equal("daemon", uMap[1]);
            ShellBasedIdMapping.UpdateMapInternal(gMap, "group", GetAllGroupsCmd, ":", EmptyPassThroughMap
                                                  );
            Assert.True(gMap.Count == 3);
            Assert.Equal("hdfs", gMap[11501]);
            Assert.Equal("mapred", gMap[497]);
            Assert.Equal("mapred3", gMap[498]);
        }
Example #4
0
        public virtual void TestStaticMapping()
        {
            Assume.AssumeTrue(!Shell.Windows);
            IDictionary <int, int> uidStaticMap = new ShellBasedIdMapping.PassThroughMap <int>(
                );
            IDictionary <int, int> gidStaticMap = new ShellBasedIdMapping.PassThroughMap <int>(
                );

            uidStaticMap[11501] = 10;
            gidStaticMap[497]   = 200;
            // Maps for id to name map
            BiMap <int, string> uMap = HashBiMap.Create();
            BiMap <int, string> gMap = HashBiMap.Create();
            string GetAllUsersCmd    = "echo \"atm:x:1000:1000:Aaron T. Myers,,,:/home/atm:/bin/bash\n"
                                       + "hdfs:x:11501:10787:Grid Distributed File System:/home/hdfs:/bin/bash\"" + " | cut -d: -f1,3";
            string GetAllGroupsCmd = "echo \"hdfs:*:11501:hrt_hdfs\n" + "mapred:x:497\n" + "mapred2:x:498\""
                                     + " | cut -d: -f1,3";

            ShellBasedIdMapping.UpdateMapInternal(uMap, "user", GetAllUsersCmd, ":", uidStaticMap
                                                  );
            ShellBasedIdMapping.UpdateMapInternal(gMap, "group", GetAllGroupsCmd, ":", gidStaticMap
                                                  );
            Assert.Equal("hdfs", uMap[10]);
            Assert.Equal(10, (int)uMap.Inverse()["hdfs"]);
            Assert.Equal("atm", uMap[1000]);
            Assert.Equal(1000, (int)uMap.Inverse()["atm"]);
            Assert.Equal("hdfs", gMap[11501]);
            Assert.Equal(11501, (int)gMap.Inverse()["hdfs"]);
            Assert.Equal("mapred", gMap[200]);
            Assert.Equal(200, (int)gMap.Inverse()["mapred"]);
            Assert.Equal("mapred2", gMap[498]);
            Assert.Equal(498, (int)gMap.Inverse()["mapred2"]);
        }
Example #5
0
 /// <summary>
 /// One guitar string on a guitar
 /// </summary>
 /// <param name="number"></param>
 /// <param name="tuning"></param>
 /// <param name="frets"></param>
 public GuitarString(int number, string tuning, int frets)
 {
     Number  = number;
     Tuning  = tuning;
     MaxFret = frets;
     NoteMap = new BiMap <string, int>();
     InitializeFretBoard();
 }
    public BiMap <T> Flopped()
    {
        BiMap <T> f = new BiMap <T>();

        f.listA = listB;
        f.listB = listA;
        return(f);
    }
        private static BiMap <string, int> CreateMap(TagNode node)
        {
            var map = new BiMap <string, int>();

            foreach (var(key, value) in node.ToTagCompound())
            {
                map.Add(key, value.ToTagInt().Data);
            }
            return(map);
        }
 public EntitySelectionCache(TrackerContext context, string model)
 {
     this.ModelName = model;
     this.Model = DonationModels.GetModel(this.ModelName);
     this.Context = context;
     this.AccessControl = new Mutex();
     this.LastRefreshTime = DateTime.MinValue;
     this.PublicCache = new BiMap<int,string>(null, StringComparer.OrdinalIgnoreCase);
     this.BlockRequests = false;
 }
 /// <exception cref="System.IO.IOException"/>
 private void LoadFullGroupMap()
 {
     lock (this)
     {
         BiMap <int, string> gMap = HashBiMap.Create();
         if (Os.StartsWith("Mac"))
         {
             UpdateMapInternal(gMap, "group", MacGetAllGroupsCmd, "\\s+", staticMapping.gidMapping
                               );
         }
         else
         {
             UpdateMapInternal(gMap, "group", GetAllGroupsCmd, ":", staticMapping.gidMapping);
         }
         gidNameMap     = gMap;
         lastUpdateTime = Time.MonotonicNow();
     }
 }
 /// <exception cref="System.IO.IOException"/>
 private void LoadFullUserMap()
 {
     lock (this)
     {
         BiMap <int, string> uMap = HashBiMap.Create();
         if (Os.StartsWith("Mac"))
         {
             UpdateMapInternal(uMap, "user", MacGetAllUsersCmd, "\\s+", staticMapping.uidMapping
                               );
         }
         else
         {
             UpdateMapInternal(uMap, "user", GetAllUsersCmd, ":", staticMapping.uidMapping);
         }
         uidNameMap     = uMap;
         lastUpdateTime = Time.MonotonicNow();
     }
 }
Example #11
0
        public virtual void TestIdOutOfIntegerRange()
        {
            Assume.AssumeTrue(!Shell.Windows);
            string GetAllUsersCmd = "echo \"" + "nfsnobody:x:4294967294:4294967294:Anonymous NFS User:/var/lib/nfs:/sbin/nologin\n"
                                    + "nfsnobody1:x:4294967295:4294967295:Anonymous NFS User:/var/lib/nfs1:/sbin/nologin\n"
                                    + "maxint:x:2147483647:2147483647:Grid Distributed File System:/home/maxint:/bin/bash\n"
                                    + "minint:x:2147483648:2147483648:Grid Distributed File System:/home/minint:/bin/bash\n"
                                    + "archivebackup:*:1031:4294967294:Archive Backup:/home/users/archivebackup:/bin/sh\n"
                                    + "hdfs:x:11501:10787:Grid Distributed File System:/home/hdfs:/bin/bash\n" + "daemon:x:2:2:daemon:/sbin:/sbin/nologin\""
                                    + " | cut -d: -f1,3";
            string GetAllGroupsCmd = "echo \"" + "hdfs:*:11501:hrt_hdfs\n" + "rpcuser:*:29:\n"
                                     + "nfsnobody:*:4294967294:\n" + "nfsnobody1:*:4294967295:\n" + "maxint:*:2147483647:\n"
                                     + "minint:*:2147483648:\n" + "mapred3:x:498\"" + " | cut -d: -f1,3";
            // Maps for id to name map
            BiMap <int, string> uMap = HashBiMap.Create();
            BiMap <int, string> gMap = HashBiMap.Create();

            ShellBasedIdMapping.UpdateMapInternal(uMap, "user", GetAllUsersCmd, ":", EmptyPassThroughMap
                                                  );
            Assert.True(uMap.Count == 7);
            Assert.Equal("nfsnobody", uMap[-2]);
            Assert.Equal("nfsnobody1", uMap[-1]);
            Assert.Equal("maxint", uMap[2147483647]);
            Assert.Equal("minint", uMap[-2147483648]);
            Assert.Equal("archivebackup", uMap[1031]);
            Assert.Equal("hdfs", uMap[11501]);
            Assert.Equal("daemon", uMap[2]);
            ShellBasedIdMapping.UpdateMapInternal(gMap, "group", GetAllGroupsCmd, ":", EmptyPassThroughMap
                                                  );
            Assert.True(gMap.Count == 7);
            Assert.Equal("hdfs", gMap[11501]);
            Assert.Equal("rpcuser", gMap[29]);
            Assert.Equal("nfsnobody", gMap[-2]);
            Assert.Equal("nfsnobody1", gMap[-1]);
            Assert.Equal("maxint", gMap[2147483647]);
            Assert.Equal("minint", gMap[-2147483648]);
            Assert.Equal("mapred3", gMap[498]);
        }
Example #12
0
        public static void Main()
        {
            //RollableLogWriter writer = new RollableLogWriter("Test.txt");
            //writer.WriteLine("Sync");
            //writer.WriteLineAsync("Async").Wait();

            //var cm = new ColourMatrix(1, 2, 3, 4, 5, 6, 7, 8, 9);
            //var v = new ColourVector(1, 2, 3);

            //var srgb = new SRGBColour(12, 34, 56, 78);
            //var hsv = srgb.ToHSV();

            //var gg = new SRGBColour(0x0C, 0x66, 0xEE).ToHSL();

            var biMap = new BiMap <int, int>()
            {
                { 1, (short)1 },
                { 2, (short)2 },
                { 3, (short)3 },
            };

            biMap[3] = 1;
        }
    public static void LoadCSV(this BiMap <string> d, string csv, char delim)
    {
        d.Clear();

        csv = csv.ConvertNewlines();
        string[] lines = csv.Split('\n');

        for (int i = 0; i < lines.Length; i++)
        {
            if (lines[i].Length < 3)
            {
                continue;
            }
            if (lines[i][0] == '#')
            {
                continue;
            }
            string[] content = lines[i].Split(delim);
            for (int j = 0; j < content.Length; j += 2)
            {
                d.Add(content[j], content[j + 1]);
            }
        }
    }
        public static bool UpdateMapInternal(BiMap <int, string> map, string mapName, string
                                             command, string regex, IDictionary <int, int> staticMapping)
        {
            bool           updated = false;
            BufferedReader br      = null;

            try
            {
                SystemProcess process = Runtime.GetRuntime().Exec(new string[] { "bash", "-c", command });
                br = new BufferedReader(new InputStreamReader(process.GetInputStream(), Encoding.
                                                              Default));
                string line = null;
                while ((line = br.ReadLine()) != null)
                {
                    string[] nameId = line.Split(regex);
                    if ((nameId == null) || (nameId.Length != 2))
                    {
                        throw new IOException("Can't parse " + mapName + " list entry:" + line);
                    }
                    Log.Debug("add to " + mapName + "map:" + nameId[0] + " id:" + nameId[1]);
                    // HDFS can't differentiate duplicate names with simple authentication
                    int    key   = staticMapping[ParseId(nameId[1])];
                    string value = nameId[0];
                    if (map.Contains(key))
                    {
                        string prevValue = map[key];
                        if (value.Equals(prevValue))
                        {
                            // silently ignore equivalent entries
                            continue;
                        }
                        ReportDuplicateEntry("Got multiple names associated with the same id: ", key, value
                                             , key, prevValue);
                        continue;
                    }
                    if (map.ContainsValue(value))
                    {
                        int prevKey = map.Inverse()[value];
                        ReportDuplicateEntry("Got multiple ids associated with the same name: ", key, value
                                             , prevKey, value);
                        continue;
                    }
                    map[key] = value;
                    updated  = true;
                }
                Log.Debug("Updated " + mapName + " map size: " + map.Count);
            }
            catch (IOException e)
            {
                Log.Error("Can't update " + mapName + " map");
                throw;
            }
            finally
            {
                if (br != null)
                {
                    try
                    {
                        br.Close();
                    }
                    catch (IOException e1)
                    {
                        Log.Error("Can't close BufferedReader of command result", e1);
                    }
                }
            }
            return(updated);
        }
        private void RefreshCache(JArray results)
        {
            try
            {
                this.AccessControl.WaitOne();
                this.BlockRequests = true;
                this.LastRefreshTime = DateTime.Now;

                BiMap<int, string> newCache = new BiMap<int, string>(null, StringComparer.OrdinalIgnoreCase);

                foreach (JObject obj in results.Values<JObject>())
                {
                    int id = obj.GetId() ?? 0;
                    string label = this.Model.DisplayConverter(obj);

                    newCache.AddPair(id, label);
                }

                // I'm going to pray that assignment is atomic
                this.PublicCache = newCache;

                if (DataRefreshed != null)
                    DataRefreshed(this.PublicCache);
            }
            finally
            {
                this.BlockRequests = false;
                this.AccessControl.ReleaseMutex();
            }
        }
        private static NotifyBatchCollectionChangedEventArgs Combine(NotifyBatchCollectionChangedEventArgs x, NotifyBatchCollectionChangedEventArgs y)
        {
            if (x == null)
            {
                return(y);
            }

            if (y == null)
            {
                return(x);
            }

            var replacements = default(BiMap);
            var result       = default(NotifyBatchCollectionChangedEventArgs);

            if (x.Action == y.Action)
            {
                switch (x.Action)
                {
                case NotifyCollectionChangedAction.Replace:
                {
                    replacements = NotifyBatchCollectionChangedEventArgs.Merge(
                        NotifyBatchCollectionChangedEventArgs.Map(x.OldItems, x.NewItems),
                        NotifyBatchCollectionChangedEventArgs.Map(y.OldItems, y.NewItems));
                    if (replacements == null)
                    {
                        return(null);
                    }

                    result = new NotifyBatchCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
                }
                break;

                case NotifyCollectionChangedAction.Add:
                case NotifyCollectionChangedAction.Remove:
                case NotifyCollectionChangedAction.Move:
                    result = new NotifyBatchCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
                    break;

                case NotifyCollectionChangedAction.Reset:
                    result       = new NotifyBatchCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
                    replacements = NotifyBatchCollectionChangedEventArgs.Merge(x.m_fromTo, y.m_fromTo);
                    break;

                default:
                    throw new NotSupportedException();
                }
            }
            else
            {
                if (x.Action == NotifyCollectionChangedAction.Replace)
                {
                    replacements = NotifyBatchCollectionChangedEventArgs.Merge(NotifyBatchCollectionChangedEventArgs.Map(x.OldItems, x.NewItems), null);
                }
                else if ((x.Action == NotifyCollectionChangedAction.Reset) && (x.m_fromTo != null))
                {
                    replacements = new BiMap(x.m_fromTo, x.m_toFrom);
                }

                switch (y.Action)
                {
                case NotifyCollectionChangedAction.Add:
                {
                    if (replacements != null)
                    {
                        var fromTo = replacements.FromTo;
                        var toFrom = replacements.ToFrom;

                        foreach (var item in y.NewItems)
                        {
                            object to;

                            if (fromTo.TryGetValue(item, out to))
                            {
                                fromTo.Remove(item);
                                toFrom.Remove(to);
                            }
                        }
                    }
                }
                break;

                case NotifyCollectionChangedAction.Remove:
                {
                    if (replacements != null)
                    {
                        var fromTo = replacements.FromTo;
                        var toFrom = replacements.ToFrom;

                        foreach (var item in y.OldItems)
                        {
                            object from;

                            if (toFrom.TryGetValue(item, out from))
                            {
                                fromTo.Remove(from);
                                toFrom.Remove(item);
                            }
                        }
                    }
                }
                break;

                case NotifyCollectionChangedAction.Replace:
                {
                    var mapping = NotifyBatchCollectionChangedEventArgs.Map(y.OldItems, y.NewItems);

                    if (replacements != null)
                    {
                        replacements = NotifyBatchCollectionChangedEventArgs.Merge(replacements.FromTo, mapping);
                    }
                    else
                    {
                        replacements = NotifyBatchCollectionChangedEventArgs.Merge(mapping, null);
                    }
                }
                break;

                case NotifyCollectionChangedAction.Move:
                    break;

                case NotifyCollectionChangedAction.Reset:
                {
                    if (replacements != null)
                    {
                        replacements = NotifyBatchCollectionChangedEventArgs.Merge(replacements.FromTo, y.m_fromTo);
                    }
                    else
                    {
                        replacements = NotifyBatchCollectionChangedEventArgs.Merge(y.m_fromTo, null);
                    }
                }
                break;

                default:
                    throw new NotSupportedException();
                }

                result = new NotifyBatchCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
            }

            if ((result != null) && (replacements != null))
            {
                result.m_fromTo = replacements.FromTo;
                result.m_toFrom = replacements.ToFrom;
            }

            return(result);
        }
 public static void LoadCSV(this BiMap <string> d, string csv)
 {
     d.LoadCSV(csv, ',');
 }
Example #18
0
        public virtual void TestStaticMapUpdate()
        {
            Assume.AssumeTrue(!Shell.Windows);
            FilePath tempStaticMapFile = FilePath.CreateTempFile("nfs-", ".map");

            tempStaticMapFile.Delete();
            Configuration conf = new Configuration();

            conf.SetLong(IdMappingConstant.UsergroupidUpdateMillisKey, 1000);
            conf.Set(IdMappingConstant.StaticIdMappingFileKey, tempStaticMapFile.GetPath());
            ShellBasedIdMapping refIdMapping  = new ShellBasedIdMapping(conf, true);
            ShellBasedIdMapping incrIdMapping = new ShellBasedIdMapping(conf);
            BiMap <int, string> uidNameMap    = refIdMapping.GetUidNameMap();
            BiMap <int, string> gidNameMap    = refIdMapping.GetGidNameMap();

            // Force empty map, to see effect of incremental map update of calling
            // getUid()
            incrIdMapping.ClearNameMaps();
            uidNameMap = refIdMapping.GetUidNameMap();
            {
                KeyValuePair <int, string> me = uidNameMap.GetEnumerator().Next();
                int    id   = me.Key;
                string name = me.Value;
                // The static map is empty, so the id found for "name" would be
                // the same as "id"
                int nid = incrIdMapping.GetUid(name);
                Assert.Equal(id, nid);
                // Clear map and update staticMap file
                incrIdMapping.ClearNameMaps();
                int    rid     = id + 10000;
                string smapStr = "uid " + rid + " " + id;
                CreateStaticMapFile(tempStaticMapFile, smapStr);
                // Now the id found for "name" should be the id specified by
                // the staticMap
                nid = incrIdMapping.GetUid(name);
                Assert.Equal(rid, nid);
            }
            // Force empty map, to see effect of incremental map update of calling
            // getGid()
            incrIdMapping.ClearNameMaps();
            gidNameMap = refIdMapping.GetGidNameMap();
            {
                KeyValuePair <int, string> me = gidNameMap.GetEnumerator().Next();
                int    id   = me.Key;
                string name = me.Value;
                // The static map is empty, so the id found for "name" would be
                // the same as "id"
                int nid = incrIdMapping.GetGid(name);
                Assert.Equal(id, nid);
                // Clear map and update staticMap file
                incrIdMapping.ClearNameMaps();
                int    rid     = id + 10000;
                string smapStr = "gid " + rid + " " + id;
                // Sleep a bit to avoid that two changes have the same modification time
                try
                {
                    Thread.Sleep(1000);
                }
                catch (Exception)
                {
                }
                CreateStaticMapFile(tempStaticMapFile, smapStr);
                // Now the id found for "name" should be the id specified by
                // the staticMap
                nid = incrIdMapping.GetGid(name);
                Assert.Equal(rid, nid);
            }
        }
Example #19
0
        public virtual void TestUpdateMapIncr()
        {
            Configuration conf = new Configuration();

            conf.SetLong(IdMappingConstant.UsergroupidUpdateMillisKey, 600000);
            ShellBasedIdMapping refIdMapping  = new ShellBasedIdMapping(conf, true);
            ShellBasedIdMapping incrIdMapping = new ShellBasedIdMapping(conf);
            // Command such as "getent passwd <userName>" will return empty string if
            // <username> is numerical, remove them from the map for testing purpose.
            BiMap <int, string> uidNameMap = refIdMapping.GetUidNameMap();
            BiMap <int, string> gidNameMap = refIdMapping.GetGidNameMap();

            // Force empty map, to see effect of incremental map update of calling
            // getUserName()
            incrIdMapping.ClearNameMaps();
            uidNameMap = refIdMapping.GetUidNameMap();
            foreach (KeyValuePair <int, string> me in uidNameMap)
            {
                int    id    = me.Key;
                string name  = me.Value;
                string tname = incrIdMapping.GetUserName(id, null);
                Assert.Equal(name, tname);
            }
            Assert.Equal(uidNameMap.Count, incrIdMapping.GetUidNameMap().Count
                         );
            // Force empty map, to see effect of incremental map update of calling
            // getUid()
            incrIdMapping.ClearNameMaps();
            foreach (KeyValuePair <int, string> me_1 in uidNameMap)
            {
                int    id   = me_1.Key;
                string name = me_1.Value;
                int    tid  = incrIdMapping.GetUid(name);
                Assert.Equal(id, tid);
            }
            Assert.Equal(uidNameMap.Count, incrIdMapping.GetUidNameMap().Count
                         );
            // Force empty map, to see effect of incremental map update of calling
            // getGroupName()
            incrIdMapping.ClearNameMaps();
            gidNameMap = refIdMapping.GetGidNameMap();
            foreach (KeyValuePair <int, string> me_2 in gidNameMap)
            {
                int    id    = me_2.Key;
                string name  = me_2.Value;
                string tname = incrIdMapping.GetGroupName(id, null);
                Assert.Equal(name, tname);
            }
            Assert.Equal(gidNameMap.Count, incrIdMapping.GetGidNameMap().Count
                         );
            // Force empty map, to see effect of incremental map update of calling
            // getGid()
            incrIdMapping.ClearNameMaps();
            gidNameMap = refIdMapping.GetGidNameMap();
            foreach (KeyValuePair <int, string> me_3 in gidNameMap)
            {
                int    id   = me_3.Key;
                string name = me_3.Value;
                int    tid  = incrIdMapping.GetGid(name);
                Assert.Equal(id, tid);
            }
            Assert.Equal(gidNameMap.Count, incrIdMapping.GetGidNameMap().Count
                         );
        }
Example #20
0
 public ModelTypeMapper()
 {
     _biMap = new BiMap <string, Type>();
 }