示例#1
0
 public RethinkClient()
 {
     r      = RethinkDB.R;
     config = new RethinkClientConfig();
     connect();
     populateComponentTypes();
 }
 public MainWindow()
 {
     InitializeComponent();
     db   = new RethinkDb.Driver.RethinkDB();
     conn = db.Connection()
            .Hostname("localhost")
            .Db("devtalks")
            .Port(RethinkDBConstants.DefaultPort)
            .Timeout(60)
            .Connect();
 }
示例#3
0
        public async Task Reset()
        {
            var r = new RethinkDB();

            // Create a new connection in order to drop the Db.
            var connection = await r.Connection()
                .Hostname("localhost")
                .ConnectAsync();

            // Retrieve a list of databases and delete the test one if needed.
            var dbList = await r.DbList().RunResultAsync<List<string>>(connection);
            if (dbList.Contains(this.dbName))
            {
                var dbDropResult = await r.DbDrop(this.dbName).RunResultAsync(connection);
                dbDropResult.AssertDatabasesDropped(1);
            }

            // Recreate the database and tables.
            await this.db.InitializeAsync();
        } 
示例#4
0
        public async Task<ReqlExpr> AsTableQueryAsync(RethinkDB rdb, Table table, string requesterId, string feedOwnerId, CancellationToken cancellationToken = default(CancellationToken))
        {
            // Build the base query.
            var query = await this.AsCountTableQueryAsync(rdb, table, requesterId, feedOwnerId, cancellationToken);

            // Apply the skip.
            if (this.skip.HasValue)
                query = query.Skip(this.skip.Value);

            // Apply the limit.
            if (this.limit.HasValue)
                query = query.Limit(this.limit.Value);

            return query;
        }
示例#5
0
        public async Task<ReqlExpr> AsCountTableQueryAsync(RethinkDB rdb, Table table, string requesterId, string feedOwnerId, CancellationToken cancellationToken = default(CancellationToken))
        {
            // Make sure we have all the data we need.
            await this.resolveDependenciesRunner.RunOnce(cancellationToken);

            // Find the name of the index to use.
            var index = this.TableIndex();

            // Find the date boundary values.
            var lowerDateBound = this.boundaries?.TryGetValue(TentFeedRequestBoundaryType.Since)?.Date
                ?? this.boundaries?.TryGetValue(TentFeedRequestBoundaryType.Until)?.Date;
            var upperDateBound = this.boundaries?.TryGetValue(TentFeedRequestBoundaryType.Before)?.Date;

            // Filter by owner and date.
            var query = (ReqlExpr)table.Between(
                new object[] { feedOwnerId, lowerDateBound != null ? rdb.Expr(lowerDateBound) : rdb.Minval() },
                new object[] { feedOwnerId, upperDateBound != null ? rdb.Expr(upperDateBound) : rdb.Maxval() })[new
                {
                    index,
                    left_bound = "open",
                    right_bound = "open"
                }];

            // Set the order-by depending on the boundary type.
            query = query.OrderBy()[new { index = this.boundaries != null && this.boundaries.ContainsKey(TentFeedRequestBoundaryType.Since) ? (object)rdb.Asc(index) : (object)rdb.Desc(index) }];

            var filters = new List<Func<ReqlExpr, ReqlExpr>>();

            // Entities.
            if (this.specialEntities == TentFeedRequestSpecialEntities.Followings)
                filters.Add(r => r.G("is_from_following").Eq(true));
            else if (this.users != null && this.users.Any())
                filters.Add(r => rdb.BetterOr(this.users.Select(u => r.G("user").Eq(u.Id)).Cast<object>().ToArray()));

            // Post type filter.
            if (this.types != null && this.types.Any())
            {
                // Condition on a single type.
                var typeCondition = new Func<ReqlExpr, ITentPostType, ReqlExpr>((r, type) => type.WildCard
                    ? (ReqlExpr)r.G("type").Match($"^{Regex.Escape(type.Type)}#")
                    : r.G("type").Eq(type.ToString()));

                // Combine the type conditions as part of an OR expression.
                filters.Add(r => rdb.BetterOr(this.types.Select(type => typeCondition(r, type)).Cast<object>().ToArray()));
            }

            // Condition on a single mention.
            var mentionCondition = new Func<ReqlExpr, ITentRequestPost, ReqlExpr>((r, mention) => r.G("mentions")
                // Map each mention for the current row to a boolean.
                .Map(m => mention.Post == null
                    ? m.G("user").Eq(mention.User.Id)
                    : rdb.BetterAnd(m.G("user").Eq(mention.User.Id), m.G("post").Eq(mention.Post.Id)))
                // Reduce the resulting booleans to just one (any).
                .Reduce((b1, b2) => rdb.BetterOr(b1, b2))
                .Default_(false));

            // Mentions.
            if (this.mentions != null && this.mentions.Any())
            {
                // Combine the mention conditions, first by AND, then by OR.
                filters.Add(r => rdb.BetterAnd(this.mentions
                    .Select(andMentions =>
                        rdb.BetterOr(andMentions.Select(mention =>
                            mentionCondition(r, mention)).Cast<object>().ToArray()))
                    .Cast<object>().ToArray()));
            }

            // Not mentions.
            if (this.notMentions != null && this.notMentions.Any())
            {
                // Combine the not mention conditions, first by AND, then by OR.
                filters.Add(r => rdb.BetterAnd(this.notMentions
                    .Select(andNotMentions =>
                        rdb.BetterOr(andNotMentions.Select(notMention =>
                            mentionCondition(r, notMention).Not()).Cast<object>().ToArray()))
                    .Cast<object>().ToArray()));
            }

            // Permissions.
            if (requesterId != feedOwnerId)
            {
                filters.Add(r => rdb.BetterAnd(
                    r.G("user").Eq(feedOwnerId),
                    rdb.BetterOr(
                        r.G("permissions").G("public").Eq(true),
                        r.G("permissions").G("users").Contains(requesterId))));
            }

            // Apply all the filters as part of an AND expression.
            if (filters.Any())
                query = query.Filter(r => rdb.BetterAnd(filters.Select(f => f(r)).Cast<object>().ToArray()));

            return query;
        }