@@ -34,8 +34,8 @@ internal constructor(
34
34
}
35
35
36
36
private class State (val readMode : Boolean ) {
37
- var count = 0
38
- val sequence = mutableListOf<Pair <String , SnapshotValue >>()
37
+ var currentFrame = 0
38
+ val frames = mutableListOf<Pair <String , SnapshotValue >>()
39
39
}
40
40
private val state: State
41
41
@@ -55,61 +55,66 @@ internal constructor(
55
55
check(num == idx)
56
56
++ idx
57
57
val keyAfterNum = key.substring(nextClose + 1 )
58
- state.sequence .add(keyAfterNum to value)
58
+ state.frames .add(keyAfterNum to value)
59
59
}
60
60
}
61
61
}
62
62
override fun close () {
63
63
if (state.readMode) {
64
- if (state.sequence .size != state.count ) {
64
+ if (state.frames .size != state.currentFrame ) {
65
65
throw Selfie .system.fs.assertFailed(
66
- Selfie .system.mode.msgVcrKeyUnread (state.sequence .size, state.count ))
66
+ Selfie .system.mode.msgVcrUnread (state.frames .size, state.currentFrame ))
67
67
}
68
68
} else {
69
69
var snapshot = Snapshot .of(" " )
70
70
var idx = 1
71
- for ((key, value) in state.sequence ) {
71
+ for ((key, value) in state.frames ) {
72
72
snapshot = snapshot.plusFacet(" $OPEN$idx$CLOSE$key " , value)
73
73
}
74
74
disk.writeDisk(snapshot, sub, call)
75
75
}
76
76
}
77
- private fun nextValue (key : String ): SnapshotValue {
77
+ private fun nextFrameValue (key : String ): SnapshotValue {
78
78
val mode = Selfie .system.mode
79
79
val fs = Selfie .system.fs
80
- if (state.sequence .size <= state.count ) {
81
- throw fs.assertFailed(mode.msgVcrKeyUnderflow (state.sequence .size))
80
+ if (state.frames .size <= state.currentFrame ) {
81
+ throw fs.assertFailed(mode.msgVcrUnderflow (state.frames .size))
82
82
}
83
- val expected = state.sequence [state.count ++ ]
83
+ val expected = state.frames [state.currentFrame ++ ]
84
84
if (expected.first != key) {
85
85
throw fs.assertFailed(
86
- mode.msgVcrKeyMismatch (" $sub [$OPEN${state.count }$CLOSE ]" , expected.first, key),
86
+ mode.msgVcrMismatch (" $sub [$OPEN${state.currentFrame }$CLOSE ]" , expected.first, key),
87
87
expected.first,
88
88
key)
89
89
}
90
90
return expected.second
91
91
}
92
- fun <V > next (key : String , roundtripValue : Roundtrip <V , String >, value : Cacheable <V >): V {
92
+ fun <V > nextFrame (key : String , roundtripValue : Roundtrip <V , String >, value : Cacheable <V >): V {
93
93
if (state.readMode) {
94
- return roundtripValue.parse(nextValue (key).valueString())
94
+ return roundtripValue.parse(nextFrameValue (key).valueString())
95
95
} else {
96
96
val value = value.get()
97
- state.sequence .add(key to SnapshotValue .of(roundtripValue.serialize(value)))
97
+ state.frames .add(key to SnapshotValue .of(roundtripValue.serialize(value)))
98
98
return value
99
99
}
100
100
}
101
- fun next (key : String , value : Cacheable <String >): String = next(key, Roundtrip .identity(), value)
102
- inline fun <reified V > nextJson (key : String , value : Cacheable <V >): V =
103
- next(key, RoundtripJson .of<V >(), value)
104
- fun <V > nextBinary (key : String , roundtripValue : Roundtrip <V , ByteArray >, value : Cacheable <V >): V {
101
+ fun nextFrame (key : String , value : Cacheable <String >): String =
102
+ nextFrame(key, Roundtrip .identity(), value)
103
+ inline fun <reified V > nextFrameJson (key : String , value : Cacheable <V >): V =
104
+ nextFrame(key, RoundtripJson .of<V >(), value)
105
+ fun <V > nextFrameBinary (
106
+ key : String ,
107
+ roundtripValue : Roundtrip <V , ByteArray >,
108
+ value : Cacheable <V >
109
+ ): V {
105
110
if (state.readMode) {
106
- return roundtripValue.parse(nextValue (key).valueBinary())
111
+ return roundtripValue.parse(nextFrameValue (key).valueBinary())
107
112
} else {
108
113
val value = value.get()
109
- state.sequence .add(key to SnapshotValue .of(roundtripValue.serialize(value)))
114
+ state.frames .add(key to SnapshotValue .of(roundtripValue.serialize(value)))
110
115
return value
111
116
}
112
117
}
113
- fun <V > nextBinary (key : String , value : Cacheable <ByteArray >): ByteArray =
114
- nextBinary (key, Roundtrip .identity(), value)
118
+ fun <V > nextFrameBinary (key : String , value : Cacheable <ByteArray >): ByteArray =
119
+ nextFrameBinary (key, Roundtrip .identity(), value)
115
120
}
0 commit comments