package Ġdev . cap y bar al abs . d 4 j . store . redis . repository Ċ Ċ import Ġdev . cap y bar al abs . d 4 j . store . common . Common Gateway Data Updater Ċ import Ġdev . cap y bar al abs . d 4 j . store . common . collect Set Ċ import Ġdev . cap y bar al abs . d 4 j . store . common . repository . Emoji Repository Ċ import Ġdev . cap y bar al abs . d 4 j . store . redis . Redis Factory Ċ import Ġdiscord 4 j . discord json . json . Emoji Data Ċ import Ġorg . slf 4 j . LoggerFactory Ċ import Ġreactor . core . publisher . Flux Ċ import Ġreactor . core . publisher . Mono Ċ Ċ internal Ġclass ĠRedis Emoji Repository ( prefix : ĠString , Ġfactory : ĠRedis Factory ) Ġ: ĠRedis Repository ( prefix ), ĠEmoji Repository Ġ{ ĊĊ ĉ comp anion Ġobject Ġ{ Ċĉ ĉ private Ġval Ġlog Ġ= ĠLoggerFactory . getLogger ( Common Gateway Data Updater :: class . java ) Ċ ĉ } ĊĊ ĉ private Ġval Ġemoji Key Ġ= Ġkey (" emoji ") Ċ ĉ private Ġval Ġemoji Ops Ġ= ĠRedis Hash Ops ( emoji Key , Ġfactory , ĠLong :: class . java , ĠEmoji Data :: class . java ) ĊĊ ĉ private Ġval Ġshard Index Ġ= Ġtwo Way Index ("$ emoji Key : shard - index ", Ġfactory ) Ċ ĉ private Ġval Ġguild Index Ġ= Ġone Way Index ("$ emoji Key : guild - index ", Ġfactory ) Ċ ĉ private Ġval Ġg Shard Index Ġ= Ġtwo Way Index ("$ emoji Key : guild - shard - index ", Ġfactory ) ĊĊ ĉ override Ġfun Ġsave ( guild Id : ĠLong , Ġemoji : ĠEmoji Data , Ġshard Id : ĠInt ): ĠMono < Void > Ġ{ Ċĉ ĉ return Ġsave All ( map Of ( Pair ( guild Id , ĠlistOf ( emoji ))), Ġshard Id ) Ċ ĉ } ĊĊ ĉ override Ġfun Ġsave All ( emo jis By Guild : ĠMap < Long , ĠList < Emoji Data >>, Ġshard Id : ĠInt ): ĠMono < Void > Ġ{ Ċĉ ĉ val Ġfiltered Ġ= Ġemo jis By Guild . entries Ċĉĉ ĉ . map Ġ{ ĠPair ( it . key , Ġit . value . filter Ġ{ Ġemoji Data Ġ-> Ġemoji Data . id (). isPresent Ġ}) Ġ} Ċĉĉ ĉ . filter Ġ{ Ġit . second . isNotEmpty () Ġ} Ċĉ ĉ if Ġ( filtered . isEmpty ()) Ġ{ Ċĉĉ ĉ return ĠMono . empty () Ċĉ ĉ } ĊĊĉ ĉ val Ġids Ġ= Ġfiltered . flatMap Ġ{ Ġit . second Ġ}. map Ġ{ Ġit . id (). orElse Throw (). as Long () Ġ} Ċĉ ĉ val Ġgu il Ids Ġ= Ġfiltered . map Ġ{ Ġit . first Ġ} Ċĉ ĉ val Ġemoji Map Ġ= Ġfiltered . flatMap Ġ{ Ġit . second Ġ}. associate By Ġ{ Ġit . id (). orElse Throw (). as Long () Ġ} ĊĊĉ ĉ return ĠMono . defer Ġ{ Ċĉĉ ĉ val ĠaddTo Shard Index Ġ= Ġshard Index . add Elements ( shard Id , Ġids ) Ċĉĉ ĉ val ĠaddTo Guild Index Ġ= ĠFlux . from Iterable ( Ċĉĉĉ ĉ filtered Ċĉĉĉĉ ĉ . map Ġ{ Ġguild Index . add Elements ( it . first , Ġ* it . second . map Ġ{ Ġemoji Ġ-> Ġemoji . id (). orElse Throw (). as Long () Ġ}. to Typed Array ()) Ġ} Ċĉĉ ĉ ). flatMap Ġ{ Ġit Ġ} Ċĉĉ ĉ val ĠaddTo Guild Shard Index Ġ= Ġg Shard Index . add Elements ( shard Id , Ġgu il Ids ) ĊĊĉĉ ĉ val Ġsave Ġ= Ġemoji Ops . put All ( emoji Map ) ĊĊĉĉ ĉ Mono .` when `( addTo Shard Index , ĠaddTo Guild Index , ĠaddTo Guild Shard Index , Ġsave ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun Ġdelete ById s ( emoji Ids : ĠList < Long >, Ġguild Id : ĠLong ): ĠMono < Long > Ġ{ Ċĉ ĉ return ĠMono . defer Ġ{ Ċĉĉ ĉ val Ġremove From Shard Index Ġ= Ġshard Index . remove Elements (* emoji Ids . to Typed Array ()) Ċĉĉ ĉ val Ġdelete Guild Index Entry Ġ= Ġguild Index . remove Elements ( guild Id , Ġ* emoji Ids . to Typed Array ()) ĊĊĉĉ ĉ val Ġremove Ġ= Ġemoji Ops . remove (* emoji Ids . to Typed Array ()) ĊĊĉĉ ĉ Mono .` when `( remove From Shard Index , Ġdelete Guild Index Entry ) Ċĉĉĉ ĉ . then ( remove ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun Ġdelete By Guild Id ( guild Id : ĠLong ): ĠMono < Long > Ġ{ Ċĉ ĉ // ĠTODO Ġconsider ĠLUA Ġscript Ġfor Ġatomic ity Ċĉ ĉ return ĠMono . defer Ġ{ Ċĉĉ ĉ guild Index . getElement s In Group ( guild Id ). collect List () Ċĉĉĉ ĉ . flatMap Ġ{ Ġemoji Ids In Guild Ġ-> Ċĉĉĉĉ ĉ val Ġremove From Shard Index Ġ= Ġshard Index . remove Elements (* emoji Ids In Guild . to Typed Array ()) Ċĉĉĉĉ ĉ val Ġdelete Guild Index Entry Ġ= Ġguild Index . delete Group ( guild Id ) Ċĉĉĉĉ ĉ val Ġremove Guild From Shard Index Ġ= Ġg Shard Index . remove Elements ( guild Id ) ĊĊĉĉĉĉ ĉ val Ġremove Ġ= Ġemoji Ops . remove (* emoji Ids In Guild . to Typed Array ()) ĊĊĉĉĉĉ ĉ Mono .` when `( remove From Shard Index , Ġdelete Guild Index Entry , Ġremove Guild From Shard Index ) Ċĉĉĉĉĉ ĉ . then ( remove ) Ċĉĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun Ġdelete By Shard Id ( shard Id : ĠInt ): ĠMono < Long > Ġ{ Ċĉ ĉ // ĠTODO Ġconsider ĠLUA Ġscript Ġfor Ġatomic ity Ċĉ ĉ return ĠMono . defer Ġ{ Ċĉĉ ĉ val Ġget Ids : ĠMono < Set < Long >> Ġ= Ġshard Index . getElementsBy Group ( shard Id ). collect Set () Ċĉĉĉ ĉ . flatMap Ġ{ Ġshard Index . delete By GroupId ( shard Id ). then ( Mono . just ( it )) Ġ} Ċĉĉ ĉ val Ġget Guild Ids : ĠMono < Set < Long >> Ġ= Ġg Shard Index . getElementsBy Group ( shard Id ). collect Set () Ċĉĉĉ ĉ . flatMap Ġ{ Ġg Shard Index . delete By GroupId ( shard Id ). then ( Mono . just ( it )) Ġ} ĊĊĉĉ ĉ val ĠgetAll Ids Ġ= Ġget Guild Ids . flatMap Ġ{ Ġguild Ids Ġ-> Ċĉĉĉ ĉ // ĠTech n ically Ġwe Ġdon 't Ġneed Ġto Ġfetch Ġthe Ġids Ġfrom Ġthe Ġgroups Ġhere , Ġwe Ġcan Ġrely Ġon Ġthe Ġshard Ġindex Ġonly . Ċĉĉĉ ĉ guild Index . getElement s In Groups ( guild Ids ). collect Set () Ċĉĉĉĉ ĉ . flatMap Ġ{ Ġguild Index . delete Groups ( guild Ids ). then ( Mono . just ( it )) Ġ} Ċĉĉĉĉ ĉ . flatMap Ġ{ Ġids In Guild s Ġ-> Ġget Ids . map Ġ{ Ġids Ġ-> Ġids In Guild s Ġ+ Ġids Ġ} Ġ} Ċĉĉ ĉ } ĊĊĉĉ ĉ getAll Ids . flatMap Ġ{ Ġemoji Ops . remove (* it . to Typed Array ()) Ġ} Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun Ġcount E moj is (): ĠMono < Long > Ġ{ Ċĉ ĉ return ĠMono . defer Ġ{ Ċĉĉ ĉ emoji Ops . size () Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun Ġcount E moj isIn Guild ( guild Id : ĠLong ): ĠMono < Long > Ġ{ Ċĉ ĉ return ĠMono . defer Ġ{ Ċĉĉ ĉ guild Index . count Elements In Group ( guild Id ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun Ġget E moj is (): ĠFlux < Emoji Data > Ġ{ Ċĉ ĉ return ĠFlux . defer Ġ{ Ċĉĉ ĉ emoji Ops . values () Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun Ġget E moj isIn Guild ( guild Id : ĠLong ): ĠFlux < Emoji Data > Ġ{ Ċĉ ĉ return ĠFlux . defer Ġ{ Ċĉĉ ĉ guild Index . getElement s In Group ( guild Id ). collect List () Ċĉĉĉ ĉ . flatMap Ġ{ Ġemoji Ops . multi Get ( it ) Ġ} Ċĉĉĉ ĉ . flatMap Many Ġ{ ĠFlux . from Iterable ( it ) Ġ} Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun Ġget Emoji ById ( guild Id : ĠLong , Ġemoji Id : ĠLong ): ĠMono < Emoji Data > Ġ{ Ċĉ ĉ return ĠMono . defer Ġ{ Ċĉĉ ĉ emoji Ops . get ( emoji Id ) Ċĉ ĉ } Ċ ĉ } Ċ } Ċ