Skip to content

Commit

Permalink
chore: Used Maps in place of Lists for faster Evaluation (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsinha610 authored Aug 6, 2024
1 parent 9c961d1 commit cd67f43
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
20 changes: 10 additions & 10 deletions src/main/kotlin/com/featurevisor/sdk/DatafileReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import com.featurevisor.types.Segment
import com.featurevisor.types.SegmentKey

class DatafileReader constructor(
datafileJson: DatafileContent,
datafileContent: DatafileContent,
) {

private val schemaVersion: String = datafileJson.schemaVersion
private val revision: String = datafileJson.revision
private val attributes: List<Attribute> = datafileJson.attributes
private val segments: List<Segment> = datafileJson.segments
private val features: List<Feature> = datafileJson.features
private val schemaVersion: String = datafileContent.schemaVersion
private val revision: String = datafileContent.revision
private val attributes: Map<AttributeKey, Attribute> = datafileContent.attributes.associateBy { it.key }
private val segments: Map<SegmentKey, Segment> = datafileContent.segments.associateBy { it.key }
private val features: Map<FeatureKey, Feature> = datafileContent.features.associateBy { it.key }

fun getRevision(): String {
return revision
Expand All @@ -27,18 +27,18 @@ class DatafileReader constructor(
}

fun getAllAttributes(): List<Attribute> {
return attributes
return attributes.values.toList()
}

fun getAttribute(attributeKey: AttributeKey): Attribute? {
return attributes.find { attribute -> attribute.key == attributeKey }
return attributes[attributeKey]
}

fun getSegment(segmentKey: SegmentKey): Segment? {
return segments.find { segment -> segment.key == segmentKey }
return segments[segmentKey]
}

fun getFeature(featureKey: FeatureKey): Feature? {
return features.find { feature -> feature.key == featureKey }
return features[featureKey]
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/com/featurevisor/sdk/Instance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
val data = datafileJSON.toByteArray(Charsets.UTF_8)
try {
val datafileContent = Json.decodeFromString<DatafileContent>(String(data))
datafileReader = DatafileReader(datafileJson = datafileContent)
datafileReader = DatafileReader(datafileContent = datafileContent)
} catch (e: Exception) {
logger?.error("could not parse datafile", mapOf("error" to e))
}
}

fun setDatafile(datafileContent: DatafileContent) {
datafileReader = DatafileReader(datafileJson = datafileContent)
datafileReader = DatafileReader(datafileContent = datafileContent)
}

fun setStickyFeatures(stickyFeatures: StickyFeatures?) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/com/featurevisor/sdk/DatafileReaderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test
class DatafileReaderTest {

private val systemUnderTest = DatafileReader(
datafileJson = DatafileContentFactory.get()
datafileContent = DatafileContentFactory.get()
)

@Test
Expand Down

0 comments on commit cd67f43

Please sign in to comment.