@@ -5,7 +5,8 @@ import java.nio.file.Files
55
66import akka .util .ByteString
77import io .iohk .ethereum .ObjectGenerators
8- import io .iohk .ethereum .db .dataSource .DataSource
8+ import io .iohk .ethereum .db .dataSource .{DataSource , DataSourceUpdate }
9+ import io .iohk .ethereum .db .dataSource .DataSource .{Key , Namespace , Value }
910import org .scalatest .FlatSpec
1011import org .scalatest .prop .PropertyChecks
1112
@@ -34,9 +35,16 @@ trait DataSourceIntegrationTestBehavior
3435 }
3536 }
3637
37- def updateInSeparateCalls (dataSource : DataSource , toUpsert : Seq [(ByteString , ByteString )]): DataSource = {
38- toUpsert.foldLeft(dataSource) { case (recDB, keyValuePair) =>
39- recDB.update(OtherNamespace , Seq (), Seq (keyValuePair))
38+ def prepareUpdate (
39+ namespace : Namespace = OtherNamespace ,
40+ toRemove : Seq [Key ] = Nil ,
41+ toUpsert : Seq [(Key , Value )] = Nil
42+ ): Seq [DataSourceUpdate ] =
43+ Seq (DataSourceUpdate (namespace, toRemove, toUpsert))
44+
45+ def updateInSeparateCalls (dataSource : DataSource , toUpsert : Seq [(ByteString , ByteString )]): Unit = {
46+ toUpsert.foreach { keyValuePair =>
47+ dataSource.update(prepareUpdate(toUpsert = Seq (keyValuePair)))
4048 }
4149 }
4250
@@ -46,8 +54,9 @@ trait DataSourceIntegrationTestBehavior
4654 forAll(seqByteStringOfNItemsGen(KeySizeWithoutPrefix )) { unFilteredKeyList : Seq [ByteString ] =>
4755 withDir { path =>
4856 val keyList = unFilteredKeyList.take(KeyNumberLimit )
49- val db = updateInSeparateCalls(
50- dataSource = createDataSource(path),
57+ val db = createDataSource(path)
58+ updateInSeparateCalls(
59+ dataSource = db,
5160 toUpsert = keyList.zip(keyList)
5261 )
5362 keyList.foreach { key => assert(db.get(OtherNamespace , key).contains(key)) }
@@ -61,7 +70,8 @@ trait DataSourceIntegrationTestBehavior
6170 forAll(seqByteStringOfNItemsGen(KeySizeWithoutPrefix )) { unFilteredKeyList : Seq [ByteString ] =>
6271 withDir { path =>
6372 val keyList = unFilteredKeyList.take(KeyNumberLimit )
64- val db = createDataSource(path).update(OtherNamespace , Seq (), keyList.zip(keyList))
73+ val db = createDataSource(path)
74+ db.update(prepareUpdate(toUpsert = keyList.zip(keyList)))
6575
6676 keyList.foreach { key => assert(db.get(OtherNamespace , key).contains(key)) }
6777
@@ -74,16 +84,18 @@ trait DataSourceIntegrationTestBehavior
7484 forAll(seqByteStringOfNItemsGen(KeySizeWithoutPrefix )) { unFilteredKeyList : Seq [ByteString ] =>
7585 withDir { path =>
7686 val keyList = unFilteredKeyList.take(KeyNumberLimit )
77- val db = createDataSource(path).update(OtherNamespace , Seq (), keyList.zip(keyList))
87+ val db = createDataSource(path)
88+ db.update(prepareUpdate(toUpsert = keyList.zip(keyList)))
7889
7990 val keyListWithExtraByte = keyList.map(1 .toByte +: _)
80- val dbAfterUpdate = updateInSeparateCalls(db, keyList.zip(keyListWithExtraByte))
91+ updateInSeparateCalls(db, keyList.zip(keyListWithExtraByte))
8192
82- keyList.zip(keyListWithExtraByte).foreach { case (key, value) =>
83- assert(dbAfterUpdate.get(OtherNamespace , key).contains(value))
93+ keyList.zip(keyListWithExtraByte).foreach {
94+ case (key, value) =>
95+ assert(db.get(OtherNamespace , key).contains(value))
8496 }
8597
86- dbAfterUpdate .destroy()
98+ db .destroy()
8799 }
88100 }
89101 }
@@ -92,16 +104,18 @@ trait DataSourceIntegrationTestBehavior
92104 forAll(seqByteStringOfNItemsGen(KeySizeWithoutPrefix )) { unFilteredKeyList : Seq [ByteString ] =>
93105 withDir { path =>
94106 val keyList = unFilteredKeyList.take(KeyNumberLimit )
95- val db = createDataSource(path).update(OtherNamespace , Seq (), keyList.zip(keyList))
107+ val db = createDataSource(path)
108+ db.update(prepareUpdate(toUpsert = keyList.zip(keyList)))
96109
97110 val keyListWithExtraByte = keyList.map(1 .toByte +: _)
98- val dbAfterUpdate = db.update(OtherNamespace , Seq (), keyList.zip(keyListWithExtraByte))
111+ db.update(prepareUpdate(toUpsert = keyList.zip(keyListWithExtraByte) ))
99112
100- keyList.zip(keyListWithExtraByte).foreach { case (key, value) =>
101- assert(dbAfterUpdate.get(OtherNamespace , key).contains(value))
113+ keyList.zip(keyListWithExtraByte).foreach {
114+ case (key, value) =>
115+ assert(db.get(OtherNamespace , key).contains(value))
102116 }
103117
104- dbAfterUpdate .destroy()
118+ db .destroy()
105119 }
106120 }
107121 }
@@ -110,8 +124,9 @@ trait DataSourceIntegrationTestBehavior
110124 forAll(seqByteStringOfNItemsGen(KeySizeWithoutPrefix )) { unFilteredKeyList : Seq [ByteString ] =>
111125 withDir { path =>
112126 val keyList = unFilteredKeyList.take(KeyNumberLimit )
113- val db = createDataSource(path).update(namespace = OtherNamespace , toRemove = Seq (), toUpsert = keyList.zip(keyList))
114- .clear
127+ val db = createDataSource(path)
128+ db.update(prepareUpdate(toUpsert = keyList.zip(keyList)))
129+ db.clear()
115130
116131 keyList.foreach { key => assert(db.get(OtherNamespace , key).isEmpty) }
117132
@@ -124,7 +139,8 @@ trait DataSourceIntegrationTestBehavior
124139 forAll(seqByteStringOfNItemsGen(KeySizeWithoutPrefix )) { unFilteredKeyList : Seq [ByteString ] =>
125140 withDir { path =>
126141 val keyList = unFilteredKeyList.take(KeyNumberLimit )
127- val db = createDataSource(path).update(namespace = OtherNamespace , toRemove = Seq (), toUpsert = keyList.zip(keyList))
142+ val db = createDataSource(path)
143+ db.update(prepareUpdate(toUpsert = keyList.zip(keyList)))
128144 db.close()
129145
130146 val dbAfterClose = createDataSource(path)
@@ -139,10 +155,11 @@ trait DataSourceIntegrationTestBehavior
139155 withDir { path =>
140156 forAll(seqByteStringOfNItemsGen(KeySizeWithoutPrefix )) { unFilteredKeyList : Seq [ByteString ] =>
141157 val keyList = unFilteredKeyList.take(KeyNumberLimit )
142- val db = createDataSource(path).update(namespace = OtherNamespace , toRemove = Seq (), toUpsert = keyList.zip(keyList))
158+ val db = createDataSource(path)
159+ db.update(prepareUpdate(toUpsert = keyList.zip(keyList)))
143160 db.destroy()
144161
145- assert(! new File (path ).exists())
162+ assert(! new File (" /tmp/iodbDestroy " ).exists())
146163
147164 val dbAfterDestroy = createDataSource(path)
148165 keyList.foreach { key => assert(dbAfterDestroy.get(OtherNamespace , key).isEmpty) }
@@ -160,15 +177,15 @@ trait DataSourceIntegrationTestBehavior
160177 val db = createDataSource(path)
161178
162179 val valList1 = keyList.map(1 .toByte +: _)
163- db.update(OtherNamespace , Seq (), keyList.zip(valList1))
180+ db.update(prepareUpdate(namespace = OtherNamespace , toUpsert = keyList.zip(valList1) ))
164181
165182 val valList2 = keyList.map(2 .toByte +: _)
166- db.update(OtherNamespace2 , Seq (), keyList.zip(valList2))
183+ db.update(prepareUpdate(namespace = OtherNamespace2 , toUpsert = keyList.zip(valList2) ))
167184
168- keyList.zip(valList1).foreach { case (key, value) =>
169- assert(db.get(OtherNamespace , key).contains(value))
185+ keyList.zip(valList1).foreach {
186+ case (key, value) =>
187+ assert(db.get(OtherNamespace , key).contains(value))
170188 }
171-
172189 keyList.zip(valList2).foreach { case (key, value) =>
173190 assert(db.get(OtherNamespace2 , key).contains(value))
174191 }
@@ -186,25 +203,31 @@ trait DataSourceIntegrationTestBehavior
186203 val db = createDataSource(path)
187204
188205 val valList1 = keyList.map(1 .toByte +: _)
189- db.update(OtherNamespace , Seq (), keyList.zip(valList1))
206+ db.update(prepareUpdate(namespace = OtherNamespace , toUpsert = keyList.zip(valList1) ))
190207
191208 val valList2 = keyList.map(2 .toByte +: _)
192- db.update(OtherNamespace2 , Seq (), keyList.zip(valList2))
209+ db.update(prepareUpdate(namespace = OtherNamespace2 , toUpsert = keyList.zip(valList2) ))
193210
194211 // Removal of keys from the OtherNamespace namespace
195- db.update(OtherNamespace , keyList, Nil )
212+ db.update(prepareUpdate(namespace = OtherNamespace , toRemove = keyList) )
196213
197- keyList.foreach { key => assert(db.get(OtherNamespace , key).isEmpty) }
198- keyList.zip(valList2).foreach { case (key, value) =>
199- assert(db.get(OtherNamespace2 , key).contains(value))
214+ keyList.foreach { key =>
215+ assert(db.get(OtherNamespace , key).isEmpty)
216+ }
217+ keyList.zip(valList2).foreach {
218+ case (key, value) =>
219+ assert(db.get(OtherNamespace2 , key).contains(value))
200220 }
201221
202222 // Removal of keys from the OtherNamespace2 namespace
203- db.update(OtherNamespace2 , keyList, Nil )
204-
205- keyList.foreach { key => assert(db.get(OtherNamespace , key).isEmpty) }
206- keyList.foreach { key => assert(db.get(OtherNamespace2 , key).isEmpty) }
223+ db.update(prepareUpdate(namespace = OtherNamespace2 , toRemove = keyList))
207224
225+ keyList.foreach { key =>
226+ assert(db.get(OtherNamespace , key).isEmpty)
227+ }
228+ keyList.foreach { key =>
229+ assert(db.get(OtherNamespace2 , key).isEmpty)
230+ }
208231 db.destroy()
209232 }
210233 }
0 commit comments