public List <DTOs.App> GetApps()
        {
            var        bytesAppIds = KeyValueStore.Get(KeyValueStore.DefaultType, KeyGenerator.AppsKey, null);
            List <int> appIds      = null;

            if (bytesAppIds == null)
            {
                return(new List <DTOs.App>());
            }

            appIds = (List <int>)BinarySerializerHelper.DeserializeObject(bytesAppIds);

            var res = new List <DTOs.App>();

            foreach (var appId in appIds)
            {
                var bytesApp = KeyValueStore.Get(KeyValueStore.DefaultType, KeyGenerator.GenerateAppKey(appId), null);
                if (bytesApp == null)
                {
                    continue;
                }

                var app = ProtoBufSerializerHelper.Deserialize <App>(bytesApp);

                res.Add(new DTOs.App
                {
                    Id          = app.Id,
                    Name        = app.Name,
                    Description = app.Description
                });
            }

            return(res);
        }
        private ForkRawData GetFork(int id)
        {
            if (id == 0)
            {
                throw new ArgumentNullException(nameof(id));
            }

            var forkData = KeyValueStore.Get(KeyValueStore.DefaultType, KeyGenerator.GenerateForkKey(AppId, id), null);

            if (forkData == null)
            {
                throw new ArgumentException($"Fork id:{id} doesn't reference actual fork");
            }

            return(ProtoBufSerializerHelper.Deserialize <ForkRawData>(forkData));
        }
        private void InitForksDict()
        {
            var rawForks = new Dictionary <int, ForkRawData>();
            var forkIds  = (List <int>)BinarySerializerHelper.DeserializeObject(Store.Get(Store.DefaultType, KeyGenerator.GenerateForksKey(AppId), null));

            foreach (var forkId in forkIds)
            {
                rawForks[forkId]         = ProtoBufSerializerHelper.Deserialize <ForkRawData>(Store.Get(Store.DefaultType, KeyGenerator.GenerateForkKey(AppId, forkId), null));
                _forksTimeStamps[forkId] = (DateTime)BinarySerializerHelper.DeserializeObject(Store.Get(Store.DefaultType, KeyGenerator.GenerateForkTimeStampKey(AppId, forkId), null));
            }

            var masterForks = rawForks.Where(x => x.Value.ParentId == 0).Select(x => x.Key).ToArray();

            foreach (var masterFork in masterForks)
            {
                CreateFork(rawForks, masterFork);
            }
        }
        private void UpdateForks()
        {
            var changedForkIds = new HashSet <int>();

            var updatedForkIds = (List <int>)BinarySerializerHelper.DeserializeObject(Store.Get(Store.DefaultType, KeyGenerator.GenerateForksKey(AppId), null));
            var newForkIds     = updatedForkIds.Except(_forks.Keys);

            foreach (var newForkId in newForkIds)
            {
                var bytesNewRawFork = Store.Get(Store.DefaultType, KeyGenerator.GenerateForkKey(AppId, newForkId), null);
                if (bytesNewRawFork == null)
                {
                    continue;
                }

                var newRawFork = ProtoBufSerializerHelper.Deserialize <ForkRawData>(bytesNewRawFork);

                var bytesNewForkTimeStamp = Store.Get(Store.DefaultType, KeyGenerator.GenerateForkTimeStampKey(AppId, newForkId), null);
                if (bytesNewForkTimeStamp == null)
                {
                    continue;
                }

                var newForkTimeStamp = (DateTime)BinarySerializerHelper.DeserializeObject(bytesNewForkTimeStamp);

                var parentFork = newRawFork.ParentId == 0 ? null : _forks[newRawFork.ParentId];
                var newFork    = new Fork
                {
                    Id              = newForkId,
                    Name            = newRawFork.Name,
                    Description     = newRawFork.Description,
                    IsInGracePeriod = newRawFork.IsInGracePeriod,
                    Parent          = parentFork
                };

                if (parentFork != null)
                {
                    parentFork.Children.Add(newFork);
                    changedForkIds.Add(parentFork.Id);
                }

                _forks[newForkId]           = newFork;
                _forksTimeStamps[newForkId] = newForkTimeStamp;
            }
            var toDel = _forks.Keys.Except(updatedForkIds).ToList();

            foreach (var toDelFork in toDel)
            {
                _forks.Remove(toDelFork);
                _forksTimeStamps.Remove(toDelFork);
            }

            foreach (var forkId in _forks.Keys.ToList())
            {
                var newTimeStamp          = (DateTime)BinarySerializerHelper.DeserializeObject(Store.Get(Store.DefaultType, KeyGenerator.GenerateForkTimeStampKey(AppId, forkId), null));
                var _currentForkTimeStamp = _forksTimeStamps[forkId];
                if (newTimeStamp > _currentForkTimeStamp)
                {
                    var rawFork = ProtoBufSerializerHelper.Deserialize <ForkRawData>(Store.Get(Store.DefaultType, KeyGenerator.GenerateForkKey(AppId, forkId), null));
                    _forksTimeStamps[forkId] = newTimeStamp;

                    UpdateFork(_forks[forkId], rawFork);
                    changedForkIds.Add(forkId);
                }
            }

            RaiseForkChanged(changedForkIds.ToList());
        }