add Option for getVal setVal

This commit is contained in:
c4w 2019-04-01 16:46:20 +08:00
parent fb2d091f18
commit 6db089a124

View File

@ -49,7 +49,7 @@ object Shim {
* @param oldValue 旧键值 * @param oldValue 旧键值
* @param newValue 新写入的键值 * @param newValue 新写入的键值
*/ */
case class Oper(key: Key, oldValue: Array[Byte], newValue: Array[Byte]) case class Oper(key: Key, oldValue: Option[Array[Byte]], newValue: Option[Array[Byte]])
import rep.storage.IdxPrefix._ import rep.storage.IdxPrefix._
val ERR_CERT_EXIST = "证书已存在" val ERR_CERT_EXIST = "证书已存在"
@ -91,21 +91,21 @@ class Shim(system: ActorSystem, cName: String) {
//从交易传入, 内存中的worldState快照 //从交易传入, 内存中的worldState快照
var sr:ImpDataPreload = null; var sr:ImpDataPreload = null;
//记录初始state //记录初始state
var mb = scala.collection.mutable.Map[Key, Array[Byte]]() var mb = scala.collection.mutable.Map[Key, Option[Array[Byte]]]()
//记录状态修改日志 //记录状态修改日志
var ol = scala.collection.mutable.ListBuffer.empty[Oper] var ol = scala.collection.mutable.ListBuffer.empty[Oper]
def setVal(key: Key, value: Any):Unit ={ def setVal(key: Key, value: Option[Any]):Unit ={
setState(key,serialise(value)) setState(key, Some(serialise(value.get)))
} }
def getVal(key: Key):Any ={ def getVal(key: Key):Option[Any] ={
deserialise(getState(key)) Some(deserialise(getState(key).get))
} }
def setState(key: Key, value: Array[Byte]): Unit = { def setState(key: Key, value: Option[Array[Byte]]): Unit = {
val pkey = pre_key + key val pkey = pre_key + key
val oldValue = get(pkey) val oldValue = get(pkey)
sr.Put(pkey, value) sr.Put(pkey, value.get)
//记录初始值 //记录初始值
if (!mb.contains(pkey)) { if (!mb.contains(pkey)) {
mb.put(pkey, oldValue) mb.put(pkey, oldValue)
@ -114,15 +114,15 @@ class Shim(system: ActorSystem, cName: String) {
ol += new Oper(key, oldValue, value) ol += new Oper(key, oldValue, value)
} }
private def get(key: Key): Array[Byte] = { private def get(key: Key): Option[Array[Byte]] = {
sr.Get(key) Some(sr.Get(key))
} }
def getState(key: Key): Array[Byte] = { def getState(key: Key): Option[Array[Byte]] = {
get(pre_key + key) get(pre_key + key)
} }
def getStateEx(cName:String, key: Key): Array[Byte] = { def getStateEx(cName:String, key: Key): Option[Array[Byte]] = {
get(WorldStateKeyPreFix + cName + PRE_SPLIT + key) get(WorldStateKeyPreFix + cName + PRE_SPLIT + key)
} }
@ -131,13 +131,6 @@ class Shim(system: ActorSystem, cName: String) {
mb.clear() mb.clear()
ol.clear() ol.clear()
} }
//回滚到初始值
def rollback() = {
for ((k, v) <- mb) sr.Put(k, v)
//回滚仍然保留操作日志,否则api拿不到操作日志
// ol.clear()
}
//判断账号是否节点账号 TODO //判断账号是否节点账号 TODO
def bNodeCreditCode(credit_code: String) : Boolean ={ def bNodeCreditCode(credit_code: String) : Boolean ={