@@ -8,6 +8,7 @@ import org.sunbird.common.exception.{ClientException, ErrorCodes, ResponseCode}
88import org .sunbird .graph .OntologyEngineContext
99import org .sunbird .graph .external .store .ExternalStoreFactory
1010import org .sunbird .telemetry .logger .TelemetryManager
11+ import org .sunbird .utils .content .HierarchyRelationCache
1112
1213import java .util
1314import scala .concurrent .{ExecutionContext , Future }
@@ -28,6 +29,8 @@ object RelationManager {
2829 private val primaryKey : java.util.List [String ] = java.util.Arrays .asList(" relationship_key" )
2930 private val propsMapping : Map [String , String ] = Map (" node_ids" -> " " )
3031
32+ private val isRedisEnabled : Boolean = Platform .getBoolean(" redis.enable" , false )
33+
3134
3235 def updateHierarchyRelationships (request : Request )(implicit oec : OntologyEngineContext , ec : ExecutionContext ): Future [Response ] = {
3336 val rootId = request.getOrDefault(" rootId" , " " ).asInstanceOf [String ]
@@ -173,26 +176,33 @@ object RelationManager {
173176 if (CollectionUtils .isEmpty(children)) new java.util.ArrayList [util.Map [String , AnyRef ]]() else children
174177 }
175178
179+ private def relationshipKey (rootId : String , identifier : String , relationshipType : String ): String =
180+ if (StringUtils .isNotBlank(rootId)) s " $rootId: $identifier: $relationshipType"
181+ else s " $identifier: $relationshipType"
182+
176183 private def storeRelationshipData (
177184 rootId : String ,
178185 relationshipType : String ,
179186 dataMap : Map [String , List [String ]]
180187 )(implicit ec : ExecutionContext ): Future [List [Response ]] = {
181- val store = ExternalStoreFactory .getExternalStore(
182- s " $relationCacheKeyspace. $relationCacheTable" , primaryKey)
183-
184- val futures = dataMap.map { case (identifier, nodeIds) =>
185- val relationshipKey =
186- if (StringUtils .isNotBlank(rootId)) s " $rootId: $identifier: $relationshipType"
187- else s " $identifier: $relationshipType"
188-
189- store.update(
190- relationshipKey,
191- List (" node_ids" ),
192- List (nodeIds.asJava.asInstanceOf [AnyRef ]),
193- propsMapping
194- )
188+ if (isRedisEnabled) {
189+ dataMap.foreach { case (identifier, nodeIds) =>
190+ HierarchyRelationCache .replaceSet(relationshipKey(rootId, identifier, relationshipType), nodeIds)
191+ }
192+ Future .successful(List (ResponseHandler .OK ))
193+ } else {
194+ val store = ExternalStoreFactory .getExternalStore(
195+ s " $relationCacheKeyspace. $relationCacheTable" , primaryKey)
196+
197+ val futures = dataMap.map { case (identifier, nodeIds) =>
198+ store.update(
199+ relationshipKey(rootId, identifier, relationshipType),
200+ List (" node_ids" ),
201+ List (nodeIds.asJava.asInstanceOf [AnyRef ]),
202+ propsMapping
203+ )
204+ }
205+ Future .sequence(futures.toList)
195206 }
196- Future .sequence(futures.toList)
197207 }
198- }
208+ }
0 commit comments