Scylla gocqlx如何实现类似游标的分页功能。

huangapple go评论83阅读模式
英文:

Scylla gocqlx how to implement pagination similar to a cursor

问题

我正在使用Scylla来保存用户创建的聚会。下面的方法返回一个用户创建的聚会列表。目前我返回所有的聚会,没有进行分页,但是我正在尝试为下面的方法实现分页,但我还不太理解Scylla如何处理分页。

我猜测可以将一个游标传递给查询。根据这个示例,看起来PageState可以用来传递类似游标的东西。

我希望能简要解释一下PageState是什么,以及我是否应该使用它来实现基于令牌的分页。如果能提供一个示例,展示如何返回一个新的PageState给客户端,并在第二次请求时使用它来获取新的页面,那就太好了。

谢谢你的帮助,我很感激你的时间。

编辑:
对于任何感兴趣的人,这是我如何修改我的函数以允许分页:

func (pq *partyQuery) GetByUser(ctx context.Context, uId string, page []byte) (result []datastruct.Party, nextPage []byte, err error) {
	stmt, names := qb.
		Select(TABLE_NAME).
		Where(qb.Eq("user_id")).
		ToCql()

	q := pq.sess.
		Query(stmt, names).
		BindMap((qb.M{"user_id": uId}))
	defer q.Release()

	q.PageState(page)
	q.PageSize(10)

	iter := q.Iter()
	err = iter.Select(&result)
	if err != nil {
		log.Println(err)
		return []datastruct.Party{}, nil, errors.New("no parties found")
	}

	return result, iter.PageState(), nil
}
英文:

I'm using Scylla to save parties created by a users. The method below returns a list of parties created by a user. I currently return all parties without allowing pagination, but I'm trying to implement Pagination for the method below but I still don't quite understand how pagination is handled with Scylla.
My guess would be that a cursor can be passed to a query. Based on this example it looks like the PageState can be used to pass something similar to a cursor.
I would appreciate a short explanation what PageState is and if I should use it to accomplish token based pagination. It would also be great if an example could be provided that shows how a new PageState can be returned to the client and used to fetch a new page on a second request.

func (pq *partyQuery) GetByUser(ctx context.Context, uId string) ([]datastruct.Party, error) {
	var result []datastruct.Party
	stmt, names := qb.
		Select(TABLE_NAME).
		Where(qb.Eq("user_id")).
		ToCql()

	err := pq.sess.
		Query(stmt, names).
		BindMap((qb.M{"user_id": uId})).
		PageSize(10).
		Iter().
		Select(&result)
	if err != nil {
		log.Println(err)
		return []datastruct.Party{}, errors.New("no parties found")
	}

	return result, nil
}

Thanks in advance and I appreciate your time.

Edit

For anybody interested, this is how I transformed my function to allow paging:

func (pq *partyQuery) GetByUser(ctx context.Context, uId string, page []byte) (result []datastruct.Party, nextPage []byte, err error) {
	stmt, names := qb.
		Select(TABLE_NAME).
		Where(qb.Eq("user_id")).
		ToCql()

	q := pq.sess.
		Query(stmt, names).
		BindMap((qb.M{"user_id": uId}))
	defer q.Release()

	q.PageState(page)
	q.PageSize(10)

	iter := q.Iter()
	err = iter.Select(&result)
	if err != nil {
		log.Println(err)
		return []datastruct.Party{}, nil, errors.New("no parties found")
	}

	return result, iter.PageState(), nil
}

答案1

得分: 2

嗨,gocqlx的作者在这里。

请查看这个示例:https://github.com/scylladb/gocqlx/blob/25d81de30ebcdfa02d3d849b518fc57b839e4399/example_test.go#L482

getUserVideos := func(userID int, page []byte) (userVideos []Video, nextPage []byte, err error) {
    q := videoTable.SelectQuery(session).Bind(userID)
    defer q.Release()
    q.PageState(page)
    q.PageSize(itemsPerPage)

    iter := q.Iter()
    return userVideos, iter.PageState(), iter.Select(&userVideos)
}

你需要将页面状态发送给调用者。

英文:

Hi gocqlx author here.

Please take a look at this example https://github.com/scylladb/gocqlx/blob/25d81de30ebcdfa02d3d849b518fc57b839e4399/example_test.go#L482

    getUserVideos := func(userID int, page []byte) (userVideos []Video, nextPage []byte, err error) {
    	q := videoTable.SelectQuery(session).Bind(userID)
    	defer q.Release()
    	q.PageState(page)
    	q.PageSize(itemsPerPage)
    
    	iter := q.Iter()
    	return userVideos, iter.PageState(), iter.Select(&userVideos)
    }

You need to send page state to caller.

huangapple
  • 本文由 发表于 2022年3月6日 05:21:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/71366029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定