itsvikramagr commented on a change in pull request #24922: [SPARK-28120][SS]  
Rocksdb state storage implementation
URL: https://github.com/apache/spark/pull/24922#discussion_r300936123
 
 

 ##########
 File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/RocksDbInstance.scala
 ##########
 @@ -0,0 +1,434 @@
+/*
+ * 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.state
+
+import java.io.File
+import java.util.Locale
+
+import org.apache.commons.io.FileUtils
+import org.rocksdb._
+import org.rocksdb.RocksDB
+import org.rocksdb.util.SizeUnit
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.catalyst.expressions.UnsafeRow
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.util.Utils
+
+class RocksDbInstance(keySchema: StructType, valueSchema: StructType, 
identifier: String)
+    extends Logging {
+
+  import RocksDbInstance._
+  RocksDB.loadLibrary()
+
+  protected var db: RocksDB = null
+  protected var dbPath: String = _
+  protected val readOptions: ReadOptions = new ReadOptions()
+  protected val writeOptions: WriteOptions = new WriteOptions()
+  protected val table_options = new BlockBasedTableConfig
+  protected val options: Options = new Options()
+
+  def isOpen(): Boolean = {
+    db != null
+  }
+
+  def open(path: String, conf: Map[String, String], readOnly: Boolean): Unit = 
{
+    verify(db == null, "Another rocksDb instance is already actve")
+    try {
+      setOptions(conf)
+      db = readOnly match {
+        case true =>
+          options.setCreateIfMissing(false)
+          RocksDB.openReadOnly(options, path)
+        case false =>
+          options.setCreateIfMissing(true)
+          RocksDB.open(options, path)
+      }
+      dbPath = path
+    } catch {
+      case e: Throwable =>
+        throw new IllegalStateException(
+          s"Error while creating rocksDb instance ${e.getMessage}",
+          e)
+    }
+  }
+
+  def get(key: UnsafeRow): UnsafeRow = {
+    verify(isOpen(), "Open rocksDb instance before any operation")
+    Option(db.get(readOptions, key.getBytes)) match {
+      case Some(valueInBytes) =>
+        val value = new UnsafeRow(valueSchema.fields.length)
+        value.pointTo(valueInBytes, valueInBytes.length)
+        value
+      case None => null
+    }
+  }
+
+  def put(key: UnsafeRow, value: UnsafeRow): Unit = {
+    verify(isOpen(), "Open rocksDb instance before any operation")
+    db.put(key.getBytes, value.getBytes)
+  }
+
+  def remove(key: UnsafeRow): Unit = {
+    verify(isOpen(), "Open rocksDb instance before any operation")
+    db.delete(key.getBytes)
+  }
+
+  def commit(backupPath: Option[String] = None): Unit = {
 
 Review comment:
   Only in transactional DB, we need to commit and close a transaction before 
closing the DB. So commit is no-op here but handles the transaction in 
OptimisticTransactionDbInstance. 
   
   Close is used to close the DB. 

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

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

Reply via email to