怎样在 Akka Persistence 中实现分页查询
在 Akka Persistence 中,数据都缓存在服务内存(状态),后端存储的都是一些持久化的事件日志,没法使用类似 SQL 一样的 DSL 来进行分页查询。利用 Akka Streams 和 Actor 我们可以通过编码的方式来实现分页查询的效果,而且这个分页查询还是分步式并行的……
EventSourcedBehavior
Akka Persistence的EventSourcedBehavior里实现了CQRS模型,通过commandHandler与eventHandler解耦了命令处理与事件处理。commandHandler处理传入的命令并返回一个事件,并可选择将这个事件持久化;若事件需要持久化,则事件将被传给eventHandler处理,eventHandler处理完事件后将返回一个“新的”状态(也可以不更新,直接返回原状态)。
def apply[Command, Event, State](
persistenceId: PersistenceId,
emptyState: State,
commandHandler: (State, Command) => Effect[Event, State],
eventHandler: (State, Event) => State): EventSourcedBehavior[Command, Event, State]
建模
以我们习惯的数据库表建模来说,我们会有以下一张表:
create table t_config
(
data_id varchar(64),
namespace varchar(64) not null,
config_type varchar(32) not null,
content text not null,
constraint t_config_pk primary key (namespace, data_id)
);
create index t_config_idx_data_id on t_config (data_id);
ConfigManager actor 可以看作 t_config 表,它的 entityId 就是 namespace, State 里保存了所有记录的主键值(ConfigManagerState),这就相当于 t_config 表的 t_config_idx_data_id 索引。
而 ConfigEntity actor 可看作 t_config 表里存储的记录,每个 actor 实例就是一行记录。它的 entityId 由 namespace + data_id 组成,这就相当于 t_config 表的 t_config_pk 复合主键。 这里我们定义两个 EventSourcedBehavior:
ConfigManager:拥有所有配置ID列表,并作为 State 保存在 EventSourcedBehavior
ConfigEntity: 拥有每个配置数据,并作为 State 保存在 EventSourcedBehavior
实现
这里先贴出 ConfigManager 和 ConfigEntity 的部分代码,接下来再详解怎样实现分页查询。
ConfigManager
object ConfigManager {
sealed trait Command extends CborSerializable
sealed trait Event extends CborSerializable
sealed trait Response extends CborSerializable
final case class Query(dataId: Option[String], configType: Option[String], page: Int, size: Int) extends Command
final case class ReplyCommand(in: AnyRef, replyTo: ActorRef[Response]) extends Command
private final case class InternalResponse(replyTo: ActorRef[Response], response: Response) extends Command
case class ConfigResponse(status: Int, message: String = "", data: Option[AnyRef] = None) extends Response
final case class ConfigManagerState(dataIds: Vector[String] = Vector()) extends CborSerializable
val TypeKey: EntityTypeKey[Command] = EntityTypeKey("ConfigManager")
}
import ConfigManager._
class ConfigManager private (namespace: String, context: ActorContext[Command]) {
private implicit val system =www.shentuylzc.cn context.system
private implicit val timeout: Timeout = 5.seconds
import context.executionContext
private val configEntity = ConfigEntity.init(context.system)
def eventSourcedBehavior(www.tengyao3zc.cn): EventSourcedBehavior[Command, Event, ConfigManagerState] =
EventSourcedBehavior(
PersistenceId.of(TypeKey.name, namespace),
ConfigManagerState(), {
case (state, ReplyCommand(in, replyTo)) =>
replyCommandHandler(state, replyTo, in)
case (_, InternalResponse(replyTo,www.xinyueylzc.cn response)) =>
Effect.reply(replyTo)(response)
eventHandler)
private def processPageQuery(
state: ConfigManagerState,
replyTo: ActorRef[Response],
in: Query): Effect[Event, ConfigManagerState] = {
val offset = if (in.page > 0) (in.page - 1) * in.size else 0
val responseF = if (offset < state.dataIds.size) {
Source(state.dataIds)
.filter(dataId => in.dataId.forall(www.huizhonggjpt.cn=> v.contains(dataId)))
.mapAsync(20) { dataId =>
configEntity.ask[Option[ConfigState]](replyTo =>
ShardingEnvelope(dataId, ConfigEntity.Query(in.configType, replyTo)))
.collect { case Some(value) => value }
.drop(offset)
.take(in.size)
.runWith(Sink.seq)
.map(items => ConfigResponse(IntStatus.OK, data www.lafei6d.cn = Some(items)))
} else {
Future.successful(ConfigResponse(IntStatus.NOT_FOUND))
context.pipeToSelf(responseF) {
case Success(value) => InternalResponse(replyTo, value)
case Failure(e) => InternalResponse(replyTo, ConfigResponse(IntStatus.INTERNAL_ERROR, e.getLocalizedMessage))
ConfigEntity
object ConfigEntity {
case class ConfigState(namespace: String, dataId: String, configType: String, content: String)
sealed trait Command extends CborSerializable
sealed trait Event extends CborSerializable
final case class Query(configType: Option[String], replyTo: ActorRef[Option[ConfigState]]) extends Command
final case class ConfigEntityState(config: Option[ConfigState] = None) extends CborSerializable
val TypeKey: EntityTypeKey[Command] = EntityTypeKey("ConfigEntity"
import ConfigEntity._
class ConfigEntity private (namespace: String, dataId: String, context: ActorContext[Command]) {
def eventSourcedBehavior(): EventSourcedBehavior[Command, Event, ConfigEntityState] =
EventSourcedBehavior(PersistenceId.of(TypeKey.name, dataId), ConfigEntityState(), commandHandler, eventHandler)
def commandHandler(state: ConfigEntityState, command: Command): Effect[Event, ConfigEntityState] = command match {
case Query(configType, replyTo) www.jiuyueguojizc.cn=>
state.config match {
case None =>
Effect.reply(replyTo)(None)
case Some(config) =>
val resp = if (configType.forall(v => config.configType.contains(v))) Some(config) else None
Effect.reply(replyTo)(resp)
ConfigManager#processPageQuery 函数实现了大部分的分页查询逻辑(有部分逻辑需要由 ConfigEntity 处理)。
val offset = if (in.page > 0) (in.page - 1) * in.size else 0
val responseF = if (offset <www.baihuaylezc.cn state.dataIds.size) {
// process paging
} else {
Future.successful(ConfigResponse(IntStatus.OK, data = Some(Nil)))
这里首先获取实际的分页数据偏移量 offset ,再于 ConfigManager 状态里保存的 dataIds 的大小进行判断,若 offset < state.dataIds.size 则我们进行分页逻辑,否则直接返回一个空列表给前端。
Source(state.dataIds)
.filter(dataId => in.dataId.forall(v => v.contains(dataId)))
.mapAsync(20) { dataId =>
configEntity.ask[Option[ConfigState]](replyTo =>
ShardingEnvelope(s"$namespace@$dataId", ConfigEntity.Query(in.configType, replyTo)))
}
.collect { case Some(value) www.jujinyule.com=> value }
.drop(offset)
.take(in.size)
.runWith(Sink.seq)
.map(items => ConfigResponse(IntStatus.OK, www.letianhuanchao.cn data = Some(items)))
这个 Akka Streams 流即是分页处理的主要实现,若是SQL的话,它类似:
select * from t_config where data_id like '%"in.dataId"%' offset "offset" limit "in.size"
.mapAsync 在流执行流程中起了20个并发的异步操作,将委托每个匹配的 ConfigEntity (由s"$namespace@$dataId"生成entityId)执行 config_type 字段的查询。这样,完整的SQL语句类似:
select * from t_config where data_id like '%"in.dataId"%' and change_type = "in.changeType" offset "offset" limit "in.size"
ConfigEntity 对 change_type 部分的查询逻辑实现如下:
case Query(configType, replyTo) =>
state.config match {
case None =>
Effect.reply(replyTo)(None)
case Some(config) www.huizhongdl.cn=>
val resp = if (configType.forall(v => config.configType.contains(v))) Some(config) else None
Effect.reply(replyTo)(resp)
}
若in.configType为空,既不需要判断 change_type 这个字段,直接返回 Some(config) 即可,而这时的SQL语句类似:
select * from t_config where data_id like '%"in.dataId"%' and true offset "offset" limit "in.size"
Tip这里有个小技巧,对于 Option[T] 字段的判断,直接使用了 .forall 方法,它等价于:
option match {
case Some(x) => p(x)
case None => true
怎样在 Akka Persistence 中实现分页查询的更多相关文章
- mongo中的分页查询
/** * @param $uid * @param $app_id * @param $start_time * @param $end_time * @param $start_page * @p ...
- ssh框架中的分页查询
ssh中的分页查询是比较常用的,接下来我用代码来介绍如何实现一个分页查询 首先建立一个Model用来储存查询分页的信息 package com.haiziwang.qrlogin.utils; imp ...
- java使用插件pagehelper在mybatis中实现分页查询
摘要: com.github.pagehelper.PageHelper是一款好用的开源免费的Mybatis第三方物理分页插件 PageHelper是国内牛人的一个开源项目,有兴趣的可以去看源码,都有 ...
- JDBC在Java Web中的应用——分页查询
分页查询 通过JDBC实现分页查询的方法有很多种,而且不同的数据库机制也提供了不同的分页方式,在这里介绍两种非常典型的分页方法. 通过ResultSet的光标实现分页 通过ResultSet的光标实现 ...
- Akka系列(八):Akka persistence设计理念之CQRS
前言........ 这一篇文章主要是讲解Akka persistence的核心设计理念,也是CQRS(Command Query Responsibility Segregation)架构设计的典型 ...
- mysql分页查询详解
我们做的后端项目一般都会有admin管理端,当管理端将要展示数据的时候,就需要用到分页.所以分页的考查在面试中也相当多.在mysql中进行分页查询时,一般会使用limit查询,而且通常查询中都会使用o ...
- 用MySQL实现分页查询
MySQL中实现分页查询语句: //定义分页需要的变量 int pageNow=2;//当前页 int pageSize=3;//指定每页显示3条记录 int pageCount=1;//该值是计算出 ...
- [.NET] SQL数据分页查询
[.NET] SQL数据分页查询 程序下载 范例下载:点此下载 原始码下载:点此下载 NuGet封装:点此下载 数据查询 开发系统时,使用C#执行SQL查询指令,就可以从SQL数据库里查询所需数据. ...
- Oracle分页查询语句的写法(转)
Oracle分页查询语句的写法(转) 分页查询是我们在使用数据库系统时经常要使用到的,下文对Oracle数据库系统中的分页查询语句作了详细的介绍,供您参考. Oracle分页查询语句使我们最常用的 ...
随机推荐
- .net core项目iis10上出现 HTTP 错误 500.19,错误代码:0x8007000d
文件权限更改,配置文件更改,IIS重装重启,各种办法都不管用,下面是解决办法: 看错误信息里:模块:IIS Web Core 打开IIS: 模块列表中是否缺少红框里的两个组件(我这是已经安装好的截图) ...
- ICE使用记录
在使用ice中间件的过程中 如果A继承了 ****Disp_类 在使用A类的时候 ****Disp_类会浅表克隆该类出一个新的对象a 在调用重写的接口的时候是使用a 在主动调用A类的对象的时候使用的才 ...
- css把图片方框变为圆角
border-radius:10px; 多少就设多少像素,个人需求.
- CF1141E Superhero Battle
A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly n minu ...
- laravel自动加载公共文件
1. 创建 functions.php 在 app/Common/(目录自己起名字)下新建一个文件 functions.php,在内部补充如下代码: <?php /** 数据返回 * 返回jso ...
- 解决CGrIdCtrl合并单元格后继续拆分后的问题
DWORD dwMergeCellCount = vMergeCells.size(); ; i < dwMergeCellCount; i++){ m_HFlexGrid.SplitCells ...
- yarn storm spark
单机zookeeper http://coolxing.iteye.com/blog/1871009 storm http://os.51cto.com/art/201309/411003_2.htm ...
- cf 444C.
听说这是线段树的裸题??(看来我也就能搞个求和什么的了2333) #include<bits/stdc++.h> #define INF 0x7fffffff #define LL lon ...
- CSS 弹性盒子 flex的三个属性:grow、shrink、basis
flex-grow 首先介绍flex-grow属性,flex-grow会在容器太大时(图片A.B的宽度和 < 父容器宽度)对元素作出调整. 如果图片A的flex-grow属性的值为 1,图片B的 ...
- Linux 正则表达式与文本处理器 三剑客
Linux 正则表达式与文本处理器 三剑客 一.正则表达式 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 在linu ...