private void EnsureUserExists(string userName)
 {
     if (!UsersByName.ContainsKey(userName))
     {
         var user = new User(userName);
         UsersByName.Add(userName, user);
     }
 }
 public Tweet[] GetUserTweets(string userName, int skip = 0, int take = 20)
 {
     if (UsersByName.TryGetValue(userName, out var user))
     {
         return(user.Tweets.Skip(skip).Take(take).ToArray());
     }
     throw new ArgumentException("No such user: " + userName);
 }
 public void Unfollow(string followerName, string followeeName)
 {
     if (UsersByName.TryGetValue(followerName, out var follower))
     {
         if (UsersByName.TryGetValue(followeeName, out var followee))
         {
             follower.Followees.Remove(followee);
             followee.Followers.Remove(follower);
             return;
         }
         throw new ArgumentException("No such user: "******"No such user: " + followerName);
 }
        public async Task DontPurgeTombstonesWhenNodeIsDown()
        {
            var clusterSize = 3;
            var leader      = await CreateRaftClusterAndGetLeader(clusterSize, leaderIndex : 0);

            using (var store = GetDocumentStore(new Options
            {
                CreateDatabase = true,
                ReplicationFactor = clusterSize,
                Server = leader
            }))
            {
                var index = new UsersByName();
                await index.ExecuteAsync(store);

                using (var session = store.OpenAsyncSession())
                {
                    session.Advanced.WaitForReplicationAfterSaveChanges(TimeSpan.FromSeconds(30), replicas: 2);
                    await session.StoreAsync(new User
                    {
                        Name = "Karmel"
                    }, "users/1");

                    await session.SaveChangesAsync();
                }
                DisposeServerAndWaitForFinishOfDisposal(Servers[1]);
                using (var session = store.OpenAsyncSession())
                {
                    session.Advanced.WaitForReplicationAfterSaveChanges(TimeSpan.FromSeconds(30), replicas: 1);
                    session.Delete("users/1");
                    await session.SaveChangesAsync();
                }

                WaitForIndexing(store);

                var database = await leader.ServerStore.DatabasesLandlord.TryGetOrCreateResourceStore(store.Database);

                await database.TombstoneCleaner.ExecuteCleanup();

                using (database.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext ctx))
                    using (ctx.OpenReadTransaction())
                    {
                        Assert.Equal(2, database.DocumentsStorage.GetLastTombstoneEtag(ctx, "Users"));
                    }
            }
        }
Beispiel #5
0
        // USERS
        public virtual User GetOrCreateUser(string id, string name)
        {
            User u;

            if (!UsersByID.TryGetValue(id, out u))
            {
                u = new User()
                {
                    Name = name,
                    ID   = id
                };
                UsersByID[id] = u;
                if (!UsersByName.ContainsKey(name.ToLower()))
                {
                    UsersByName[name.ToLower()] = u;
                }
            }
            u.Name = name;
            return(u);
        }
Beispiel #6
0
    public void WillRejectJsIndexDeploymentAsValidUserIfItIsRestrictedToAdminsOnly()
    {
        var certificates = Certificates.SetupServerAuthentication(customSettings: new Dictionary <string, string>
        {
            { RavenConfiguration.GetKey(x => x.Indexing.RequireAdminToDeployJavaScriptIndexes), "true" }
        });

        var dbName    = GetDatabaseName();
        var adminCert = Certificates.RegisterClientCertificate(certificates.ServerCertificate.Value, certificates.ClientCertificate1.Value, new Dictionary <string, DatabaseAccess>(), SecurityClearance.ClusterAdmin);
        var userCert  = Certificates.RegisterClientCertificate(certificates.ServerCertificate.Value, certificates.ClientCertificate2.Value, new Dictionary <string, DatabaseAccess>
        {
            [dbName] = DatabaseAccess.ReadWrite
        }, SecurityClearance.ValidUser);

        using (var store = GetDocumentStore(new Options
        {
            AdminCertificate = adminCert,
            ClientCertificate = userCert,
            ModifyDatabaseName = s => dbName,
        }))
        {
            var index = new UsersByName();

            Assert.Throws <AuthorizationException>(() => store.ExecuteIndex(index));

            using (var storeWithAdminCert = new DocumentStore
            {
                Urls = store.Urls,
                Certificate = adminCert,
                Database = store.Database
            }.Initialize())
            {
                storeWithAdminCert.ExecuteIndex(index);
            }
        }
    }
Beispiel #7
0
        public virtual User GetUserOrDefaultByName(string id)
        {
            User u;

            return(UsersByName.TryGetValue(id, out u) ? u : null);
        }
        public void ShouldReturnCorrectResults()
        {
            using (var store = NewDocumentStore())
            {
                var index = new UsersByName();
                index.Execute(store);

                using (var session = store.OpenSession())
                {
                    session.Store(new User
                    {
                        Id   = "Users/1",
                        Name = "Adam"
                    });

                    session.Store(new User
                    {
                        Id   = "Users/2",
                        Name = "Adam"
                    });

                    session.Store(new User
                    {
                        Id   = "Users/3",
                        Name = "John"
                    });

                    session.SaveChanges();
                }

                var storage = store.SystemDatabase.TransactionalStorage;

                using (var session = store.OpenSession())
                {
                    session.Query <UsersByName.Result, UsersByName>().Customize(x => x.WaitForNonStaleResults()).ToList();
                }

                var indexId = store.SystemDatabase.Indexes.GetIndexDefinition(index.IndexName).IndexId;
                storage.Batch(accessor =>
                {
                    var stats = accessor.MapReduce.GetKeysStats(indexId, 0, 10).ToList();
                    Assert.Equal(2, stats.Count);
                    Assert.Equal(2, stats.First(x => x.Key == "Adam").Count);
                    Assert.Equal(1, stats.First(x => x.Key == "John").Count);
                });

                store.SystemDatabase.Documents.Delete("Users/1", null, null); // delete "Adam" reduce key

                using (var session = store.OpenSession())
                {
                    session.Query <UsersByName.Result, UsersByName>().Customize(x => x.WaitForNonStaleResults()).ToList();
                }

                storage.Batch(accessor =>
                {
                    var stats = accessor.MapReduce.GetKeysStats(indexId, 0, 10).ToList();
                    Assert.Equal(2, stats.Count);
                    Assert.Equal(1, stats.First(x => x.Key == "Adam").Count);
                    Assert.Equal(1, stats.First(x => x.Key == "John").Count);
                });
            }
        }