ruby Rails中基于游标的伪分区伪代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Rails中基于游标的伪分区伪代码相关的知识,希望对你有一定的参考价值。

class CursorPaginator
  attr_reader :cursor_keys, :max_items, :limit

  CURSOR_DIRECTIONS = {
    after: :gt,
    before: :lt
  }.freeze

  def initialize(options = {})
    @cursor_keys = options.fetch(:cursor_keys, id: :asc)
    @max_items = options.fetch(:max_items, 50)
    @limit = options.fetch(:limit, 10)
  end

  def cursor_paginate(collection, cursor = nil, options = {})
    options.reverse_merge!(direction: :after limit: limit)
    options[:direction] = options[:direction].to_sym
    options[:limit] = [options[:limit], max_items].min

    relation = collection.recorder(cursor_keys).limit(options[:limit])
    relation = query_comparator(relation, cursor, options[:direction]) if cursor.present?
    relation = relation.reverse_merge if direction[:direction] = :before

    PaginationResult.new(self, collection, options)
  end

  private

  def query_comparator(collection, cursor, direction)
    comparator = collection.arel_table[:id].public_send(direction, cursor)
    collection.where(comparator)
  end

  private

  class PaginationResult
    att_reader :paginator, :collection, :options

    def initialize(paginator, collection, options = {})
      @paginator = paginator
      @collection = collection
      @options = options
    end

    def has_more?
      paginator.cursor_paginate(collection, last_item.id, options)
    end

    def last_item
      collection.last
    end
  end
end

以上是关于ruby Rails中基于游标的伪分区伪代码的主要内容,如果未能解决你的问题,请参考以下文章

在聚类时使用基于摄取时间的伪字段 (_PARTITIONTIME) 作为分区

Discuz X3.2 分区 gid 完美伪静态方法 Apache/Nginx

16位汇编中的伪指令

用于查找文档中字符出现次数的伪代码

Linux 操作系统原理 — loop 伪设备

Linux 操作系统原理 — loop 伪设备