fedimser commented on code in PR #53159:
URL: https://github.com/apache/spark/pull/53159#discussion_r2663133914


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/transformwithstate/testing/InMemoryStatefulProcessorHandle.scala:
##########
@@ -0,0 +1,332 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package 
org.apache.spark.sql.execution.streaming.operators.stateful.transformwithstate.testing
+
+import java.time.{Clock, Instant}
+import java.util.UUID
+
+import scala.collection.mutable
+import scala.reflect.ClassTag
+
+import org.apache.spark.sql.Encoder
+import 
org.apache.spark.sql.execution.streaming.operators.stateful.transformwithstate.statefulprocessor.ImplicitGroupingKeyTracker
+import 
org.apache.spark.sql.execution.streaming.operators.stateful.transformwithstate.statefulprocessor.QueryInfoImpl
+import org.apache.spark.sql.streaming.{ListState, MapState, QueryInfo, 
StatefulProcessorHandle, TimeMode, TTLConfig, ValueState}
+
+/** Helper to track expired keys. */
+class TtlTracker(val clock: Clock, ttl: TTLConfig) {
+  require(!ttl.ttlDuration.isNegative())
+  private val keyToLastUpdatedTime = mutable.Map[Any, Instant]()
+
+  def isKeyExpired(): Boolean = {
+    if (ttl.ttlDuration.isZero()) {
+      return false
+    }
+    val key = ImplicitGroupingKeyTracker.getImplicitKeyOption.get
+    if (!keyToLastUpdatedTime.contains(key)) {
+      return false
+    }
+    val expiration: Instant = 
keyToLastUpdatedTime.get(key).get.plus(ttl.ttlDuration)
+    return expiration.isBefore(clock.instant())
+  }
+
+  def onKeyUpdated(): Unit = {
+    val key = ImplicitGroupingKeyTracker.getImplicitKeyOption.get
+    keyToLastUpdatedTime.put(key, clock.instant())
+  }
+}
+
+/** In-memory implementation of ValueState. */
+class InMemoryValueState[T](clock: Clock, ttl: TTLConfig) extends 
ValueState[T] {
+  private val keyToStateValue = mutable.Map[Any, T]()
+  private val ttlTracker = new TtlTracker(clock, ttl)
+
+  private def getValue: Option[T] = {
+    if (ttlTracker.isKeyExpired()) {
+      return None
+    }
+    keyToStateValue.get(ImplicitGroupingKeyTracker.getImplicitKeyOption.get)
+  }
+  override def exists(): Boolean = getValue.isDefined
+
+  override def get(): T = getValue.getOrElse(null.asInstanceOf[T])
+
+  override def update(newState: T): Unit = {
+    ttlTracker.onKeyUpdated()
+    keyToStateValue.put(ImplicitGroupingKeyTracker.getImplicitKeyOption.get, 
newState)
+  }
+
+  override def clear(): Unit =
+    keyToStateValue.remove(ImplicitGroupingKeyTracker.getImplicitKeyOption.get)
+}
+
+/** In-memory implementation of ListState. */

Review Comment:
   I'd like to leave TTL unsupported, as this is what I was proposing in my 
design. In design review we talked about this and decided to implement TTL.
   
   Are you sure it's ok if I just ignore TTL config entirely? This will make 
implementastion much easier. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to