/// <summary> /// Removes neighborhood actions whose target servers do not exist in our database. /// </summary> /// <returns>true if the function succeeds, false otherwise.</returns> private bool DeleteInvalidNeighborhoodActions() { log.Info("()"); bool res = false; using (UnitOfWork unitOfWork = new UnitOfWork()) { DatabaseLock[] lockObjects = new DatabaseLock[] { UnitOfWork.NeighborLock, UnitOfWork.FollowerLock, UnitOfWork.NeighborhoodActionLock }; unitOfWork.AcquireLock(lockObjects); try { List <byte[]> neighborIds = unitOfWork.NeighborRepository.Get().Select(n => n.NeighborId).ToList(); HashSet <byte[]> neighborIdsHashSet = new HashSet <byte[]>(neighborIds, StructuralEqualityComparer <byte[]> .Default); List <byte[]> followerIds = unitOfWork.FollowerRepository.Get().Select(f => f.FollowerId).ToList(); HashSet <byte[]> followerIdsHashSet = new HashSet <byte[]>(followerIds, StructuralEqualityComparer <byte[]> .Default); List <NeighborhoodAction> actions = unitOfWork.NeighborhoodActionRepository.Get().ToList(); bool saveDb = false; foreach (NeighborhoodAction action in actions) { bool actionValid = false; if (action.IsActivityAction()) { // Action's serverId should be our follower. actionValid = followerIdsHashSet.Contains(action.ServerId); } else { // Action's serverId should be our neighbor. actionValid = neighborIdsHashSet.Contains(action.ServerId); } if (!actionValid) { log.Debug("Removing invalid action ID {0}, type {1}, server ID '{2}'.", action.Id, action.Type, action.ServerId.ToHex()); unitOfWork.NeighborhoodActionRepository.Delete(action); saveDb = true; } } if (saveDb) { res = unitOfWork.Save(); } else { log.Debug("No invalid neighborhood actions found."); res = true; } } catch (Exception e) { log.Error("Exception occurred: {0}", e.ToString()); } unitOfWork.ReleaseLock(lockObjects); } log.Info("(-):{0}", res); return(res); }