Skip to content
This repository
Browse code

Issue 1179 - Invalidate cached object in MongoRefField upon setting t…

…he value
  • Loading branch information...
commit 1ae400ac6b3cd3ff14bfd849a486910b2a6caa51 1 parent 43ee344
Tim Nelson authored February 24, 2012
18  persistence/mongodb-record/src/main/scala/net/liftweb/mongodb/record/field/MongoRefField.scala
@@ -42,13 +42,18 @@ trait MongoRefField[RefType <: MongoRecord[RefType], MyType] extends TypedField[
42 42
   /** The MongoMetaRecord of the referenced object **/
43 43
   def refMeta: MongoMetaRecord[RefType]
44 44
 
45  
-  /*
46  
-  * get the referenced object
47  
-  */
  45
+  /**
  46
+    * Find the referenced object
  47
+    */
  48
+  def find = valueBox.flatMap(v => refMeta.findAny(v))
  49
+
  50
+  /**
  51
+    * Get the cacheable referenced object
  52
+    */
48 53
   def obj = synchronized {
49 54
     if (!_calcedObj) {
50 55
       _calcedObj = true
51  
-      this._obj = valueBox.flatMap(v => refMeta.findAny(v))
  56
+      this._obj = find
52 57
     }
53 58
     _obj
54 59
   }
@@ -63,6 +68,11 @@ trait MongoRefField[RefType <: MongoRecord[RefType], MyType] extends TypedField[
63 68
   private var _obj: Box[RefType] = Empty
64 69
   private var _calcedObj = false
65 70
 
  71
+  override def setBox(in: Box[MyType]): Box[MyType] = synchronized {
  72
+    _calcedObj = false // invalidate the cache
  73
+    super.setBox(in)
  74
+  }
  75
+
66 76
   /** Options for select list **/
67 77
   def options: List[(Box[MyType], String)] = Nil
68 78
 
12  persistence/mongodb-record/src/main/scala/net/liftweb/mongodb/record/field/MongoRefListField.scala
@@ -41,13 +41,18 @@ abstract class MongoRefListField[OwnerType <: BsonRecord[OwnerType], RefType <:
41 41
   /** The MongoMetaRecord of the referenced object **/
42 42
   def refMeta: MongoMetaRecord[RefType]
43 43
 
  44
+  /**
  45
+    * Find the referenced objects
  46
+    */
  47
+  def findAll = refMeta.findAllByList(this.value)
  48
+
44 49
   /*
45 50
    * get the referenced objects
46 51
    */
47 52
   def objs = synchronized {
48 53
     if (!_calcedObjs) {
49 54
       _calcedObjs = true
50  
-      this._objs = refMeta.findAllByList(this.value)
  55
+      this._objs = findAll
51 56
     }
52 57
     _objs
53 58
   }
@@ -61,6 +66,11 @@ abstract class MongoRefListField[OwnerType <: BsonRecord[OwnerType], RefType <:
61 66
 
62 67
   private var _objs: List[RefType] = Nil
63 68
   private var _calcedObjs = false
  69
+
  70
+  override def setBox(in: Box[MyType]): Box[MyType] = synchronized {
  71
+    _calcedObjs = false // invalidate the cache
  72
+    super.setBox(in)
  73
+  }
64 74
 }
65 75
 
66 76
 class ObjectIdRefListField[OwnerType <: BsonRecord[OwnerType], RefType <: MongoRecord[RefType]](
1  persistence/mongodb-record/src/test/scala/net/liftweb/mongodb/record/MongoFieldSpec.scala
@@ -21,7 +21,6 @@ package record
21 21
 import java.util.{Date, UUID}
22 22
 import java.util.regex.Pattern
23 23
 
24  
-import com.mongodb.DBRef
25 24
 import org.bson.types.ObjectId
26 25
 import org.specs.Specification
27 26
 
21  persistence/mongodb-record/src/test/scala/net/liftweb/mongodb/record/MongoRecordSpec.scala
@@ -548,17 +548,38 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
548 548
         .mandatoryIntRefListField(List(ntr.id.is))
549 549
         .mandatoryLongRefListField(List(btr.id.is))
550 550
 
  551
+      // single objects
551 552
       rftr.mandatoryObjectIdRefField.obj mustEqual Full(fttr)
552 553
       rftr.mandatoryUUIDRefField.obj mustEqual Full(ltr)
553 554
       rftr.mandatoryStringRefField.obj mustEqual Full(mtr)
554 555
       rftr.mandatoryIntRefField.obj mustEqual Full(ntr)
555 556
       rftr.mandatoryLongRefField.obj mustEqual Full(btr)
556 557
 
  558
+      val fttr2 = FieldTypeTestRecord.createRecord.save
  559
+
  560
+      rftr.mandatoryObjectIdRefField.cached_? mustEqual true
  561
+      rftr.mandatoryObjectIdRefField(fttr2.id.is)
  562
+      rftr.mandatoryObjectIdRefField.cached_? mustEqual false
  563
+      rftr.mandatoryObjectIdRefField.find mustEqual Full(fttr2)
  564
+      rftr.mandatoryObjectIdRefField.obj mustEqual Full(fttr2)
  565
+      rftr.mandatoryObjectIdRefField.cached_? mustEqual true
  566
+
  567
+      // lists
557 568
       rftr.mandatoryObjectIdRefListField.objs mustEqual List(fttr)
558 569
       rftr.mandatoryUUIDRefListField.objs mustEqual List(ltr)
559 570
       rftr.mandatoryStringRefListField.objs mustEqual List(mtr)
560 571
       rftr.mandatoryIntRefListField.objs mustEqual List(ntr)
561 572
       rftr.mandatoryLongRefListField.objs mustEqual List(btr)
  573
+
  574
+      val fttr3 = FieldTypeTestRecord.createRecord.save
  575
+      val objList = List(fttr2, fttr3)
  576
+
  577
+      rftr.mandatoryObjectIdRefListField.cached_? mustEqual true
  578
+      rftr.mandatoryObjectIdRefListField(objList.map(_.id.is))
  579
+      rftr.mandatoryObjectIdRefListField.cached_? mustEqual false
  580
+      rftr.mandatoryObjectIdRefListField.findAll mustEqual objList
  581
+      rftr.mandatoryObjectIdRefListField.objs mustEqual objList
  582
+      rftr.mandatoryObjectIdRefListField.cached_? mustEqual true
562 583
     }
563 584
 
564 585
     "use defaultValue when field is not present in the database" in {

0 notes on commit 1ae400a

Please sign in to comment.
Something went wrong with that request. Please try again.