Skip to content

Commit 934d808

Browse files
committed
feat: add comments to code to repository part
1 parent 1c3d41a commit 934d808

File tree

14 files changed

+128
-27
lines changed

14 files changed

+128
-27
lines changed

lib/src/main/kotlin/repository/JsonRepository.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ import trees.nodes.AbstractNode
1818
import java.io.File
1919
import java.io.FileNotFoundException
2020

21-
21+
//creates a new JsonRepository object with the given strategy and dirPath. strategy
2222
class JsonRepository<T : Comparable<T>,
2323
NodeType : AbstractNode<T, NodeType>,
2424
TreeType : AbstractTree<T, NodeType>>
2525
(
26+
//serialization strategy for working with trees and nodes.
2627
strategy: Serialization<T, NodeType, TreeType, *>,
27-
dirPath: String,
28-
private val filename: String,
28+
//path to the directory where tree files will be stored in JSON format.
29+
dirPath: String
2930
) : Repository<T, NodeType, TreeType>(strategy) {
3031

3132
private val dirPath = "$dirPath/${strategy.typeOfTree.name.lowercase()}"
3233

34+
//function to convert JsonNode to SerializableNode.
3335
private fun JsonNode.toSerializableNode(): SerializableNode {
3436
return SerializableNode(
3537
data,
@@ -39,6 +41,7 @@ class JsonRepository<T : Comparable<T>,
3941
)
4042
}
4143

44+
//a function to deserialize a JsonNode into a tree node
4245
private fun JsonNode.deserialize(parent: NodeType? = null): NodeType? {
4346
val node = strategy.createNode(this.toSerializableNode())
4447
node?.parent = parent
@@ -47,6 +50,7 @@ class JsonRepository<T : Comparable<T>,
4750
return node
4851
}
4952

53+
//function to convert SerializableNode to JsonNode.
5054
private fun SerializableNode.toJsonNode(): JsonNode {
5155
return JsonNode(
5256
data,
@@ -56,6 +60,7 @@ class JsonRepository<T : Comparable<T>,
5660
)
5761
}
5862

63+
//function to convert SerializableTree to JsonTree
5964
private fun SerializableTree.toJsonTree(): JsonTree {
6065
return JsonTree(
6166
name,
@@ -64,14 +69,16 @@ class JsonRepository<T : Comparable<T>,
6469
)
6570
}
6671

72+
//method for getting a list of tree names.
6773
override fun getNames(): List<String> =
6874
File(dirPath).listFiles()?.map {
6975
Json.decodeFromString<JsonTree>(it.readText()).name
7076
} ?: listOf()
7177

78+
//method to load a tree by name
7279
override fun loadByName(name: String): TreeType? {
7380
val json = try {
74-
File(dirPath, "${name.hashCode()}.json").readText()
81+
File(dirPath, "${name}.json").readText()
7582
} catch (_: FileNotFoundException) {
7683
return null
7784
}
@@ -82,17 +89,19 @@ class JsonRepository<T : Comparable<T>,
8289
}
8390
}
8491

92+
//method to save tree by name
8593
override fun save(name: String, tree: TreeType) {
8694
val jsonTree = tree.toSerializableTree(name).toJsonTree()
8795

8896
File(dirPath).mkdirs()
89-
File(dirPath, filename).run {
97+
File(dirPath, "${name}.json").run {
9098
createNewFile()
9199
writeText(Json.encodeToString(jsonTree))
92100
}
93101
}
94102

103+
//a method for deleting a tree by name
95104
override fun deleteByName(name: String) {
96-
File(dirPath, "${name.hashCode()}.json").delete()
105+
File(dirPath, "${name}.json").delete()
97106
}
98107
}

lib/src/main/kotlin/repository/Neo4jRepository.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Neo4jRepo<T : Comparable<T>,
2727
private val sessionFactory = SessionFactory(configuration, "repository")
2828
private val session = sessionFactory.openSession()
2929

30+
//converts Neo4jNodeEntity to SerializableNode.
3031
private fun Neo4jNodeEntity.toSerializableNode(): SerializableNode {
3132
return SerializableNode(
3233
data,
@@ -36,6 +37,7 @@ class Neo4jRepo<T : Comparable<T>,
3637
)
3738
}
3839

40+
//converts Neo4jNodeEntity to a node
3941
private fun Neo4jNodeEntity.deserialize(parent: NodeType? = null): NodeType? {
4042
val node = strategy.createNode(this.toSerializableNode())
4143
node?.parent = parent
@@ -44,6 +46,7 @@ class Neo4jRepo<T : Comparable<T>,
4446
return node
4547
}
4648

49+
//converts SerializableTree to Neo4jTreeEntity.
4750
private fun SerializableNode.toEntity(): Neo4jNodeEntity {
4851
return Neo4jNodeEntity(
4952
data,
@@ -53,6 +56,7 @@ class Neo4jRepo<T : Comparable<T>,
5356
)
5457
}
5558

59+
//converts Neo4jTreeEntity to SerializableTree.
5660
private fun Neo4jTreeEntity.toTree(): SerializableTree {
5761
return SerializableTree(
5862
name,
@@ -69,12 +73,14 @@ class Neo4jRepo<T : Comparable<T>,
6973
)
7074
}
7175

76+
//saves the tree with the specified name to the database.
7277
override fun save(name: String, tree: TreeType) {
7378
deleteByName(name)
7479
val entityTree = tree.toSerializableTree(name).toEntity()
7580
session.save(entityTree)
7681
}
7782

83+
//is used to get a list of trees from the database that match the specified filtering options.
7884
private fun findByVerboseName(name: String) = session.loadAll(
7985
Neo4jTreeEntity::class.java,
8086
Filters().and(
@@ -85,6 +91,7 @@ class Neo4jRepo<T : Comparable<T>,
8591
-1
8692
)
8793

94+
//loads the tree with the specified name from the database.
8895
override fun loadByName(name: String): TreeType {
8996
val tree = findByVerboseName(name).singleOrNull()
9097
val result = strategy.createTree().apply {
@@ -93,6 +100,7 @@ class Neo4jRepo<T : Comparable<T>,
93100
return result
94101
}
95102

103+
//removes the tree with the specified name from the database.
96104
override fun deleteByName(name: String) {
97105
session.query(
98106
"MATCH toDelete=(" +
@@ -102,6 +110,7 @@ class Neo4jRepo<T : Comparable<T>,
102110
)
103111
}
104112

113+
//returns a list of the names of all saved trees in the database.
105114
override fun getNames(): List<String> = session.loadAll(
106115
Neo4jTreeEntity::class.java,
107116
Filter("typeOfTree", ComparisonOperator.EQUALS, strategy.typeOfTree),

lib/src/main/kotlin/repository/Repository.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ abstract class Repository<T : Comparable<T>,
1616
TreeType : AbstractTree<T, NodeType>>(
1717
protected val strategy: Serialization<T, NodeType, TreeType, *>
1818
) {
19+
//An extension function that converts an instance of a NodeType to a serializable
20+
//representation of a SerializableNode using the serialization strategy
1921
protected fun NodeType.toSerializableNode(): SerializableNode {
2022
return SerializableNode(
2123
strategy.serializeValue(this.data),
@@ -25,7 +27,9 @@ abstract class Repository<T : Comparable<T>,
2527
)
2628
}
2729

28-
30+
//An extension function that converts a TreeType instance to a serializable
31+
// representation of a SerializableTree using the serialization strategy.
32+
// The name of the tree is given by the name parameter
2933
protected fun TreeType.toSerializableTree(name: String): SerializableTree {
3034
return SerializableTree(
3135
name = name,
@@ -34,8 +38,17 @@ abstract class Repository<T : Comparable<T>,
3438
)
3539
}
3640

41+
//an abstract method that saves an instance of tree in the repository under the given name
3742
abstract fun save(name: String, tree: TreeType)
43+
44+
//abstract method that loads a tree instance from the repository given name.
45+
// If no tree with the specified name is found, the method should return null.
3846
abstract fun loadByName(name: String): TreeType?
47+
48+
//an abstract method that removes a tree instance from the repository at the given name
3949
abstract fun deleteByName(name: String)
50+
51+
//abstract method that returns a list of the names of all trees stored in the repository.
52+
// If there are no trees in the repository, the method should return an empty list
4053
abstract fun getNames(): List<String>
4154
}

lib/src/main/kotlin/repository/SQLRepository.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
package repository
77

8+
import NodesTable
9+
import SQLNodeEntity
10+
import SQLTreeEntity
11+
import TreesTable
812
import org.jetbrains.exposed.sql.Database
913
import org.jetbrains.exposed.sql.SchemaUtils
1014
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
@@ -16,10 +20,6 @@ import repository.serialization.SerializableNode
1620
import repository.serialization.SerializableTree
1721
import repository.serialization.SerializableValue
1822
import repository.serialization.strategies.Serialization
19-
import repository.sqliteEntities.NodesTable
20-
import repository.sqliteEntities.SQLNodeEntity
21-
import repository.sqliteEntities.SQLTreeEntity
22-
import repository.sqliteEntities.TreesTable
2323
import trees.AbstractTree
2424
import trees.nodes.AbstractNode
2525

@@ -32,13 +32,15 @@ class SQLRepository<T : Comparable<T>,
3232

3333
private val typeOfTree = strategy.typeOfTree.toString()
3434

35+
//Initializes the database tables for storing trees and nodes.
3536
init {
3637
transaction(db) {
3738
SchemaUtils.create(TreesTable)
3839
SchemaUtils.create(NodesTable)
3940
}
4041
}
4142

43+
//Converts an instance of SQLNodeEntity to a SerializableNode object using the serialization strategy.
4244
private fun SQLNodeEntity.toSerializableNode(): SerializableNode {
4345
return SerializableNode(
4446
SerializableValue(data),
@@ -48,6 +50,7 @@ class SQLRepository<T : Comparable<T>,
4850
)
4951
}
5052

53+
//Deserializes an instance of SQLNodeEntity to a NodeType object using the serialization strategy.
5154
private fun SQLNodeEntity.deserialize(parent: NodeType? = null): NodeType? {
5255
val node = strategy.createNode(this.toSerializableNode())
5356
node?.parent = parent
@@ -56,6 +59,7 @@ class SQLRepository<T : Comparable<T>,
5659
return node
5760
}
5861

62+
//Converts a SerializableNode object to an instance of SQLNodeEntity using the serialization strategy.
5963
private fun SerializableNode.toEntity(tree: SQLTreeEntity): SQLNodeEntity = SQLNodeEntity.new {
6064
this@new.data = this@toEntity.data.value
6165
this@new.metadata = this@toEntity.metadata.value
@@ -64,20 +68,22 @@ class SQLRepository<T : Comparable<T>,
6468
this.tree = tree
6569
}
6670

71+
//Converts a SerializableTree object to an instance of SQLTreeEntity.
6772
private fun SerializableTree.toEntity(): SQLTreeEntity {
6873
return SQLTreeEntity.new {
6974
this.name = this@toEntity.name
7075
this.typeOfTree = this@SQLRepository.typeOfTree
7176
}
7277
}
7378

79+
//Saves a TreeType object to the database with a given name by deleting any existing tree with the same name and creating a new one.
7480
override fun save(name: String, tree: TreeType): Unit = transaction(db) {
7581
deleteByName(name)
7682
val entityTree = tree.toSerializableTree(name).toEntity()
7783
entityTree.root = tree.root?.toSerializableNode()?.toEntity(entityTree)
7884
}
7985

80-
86+
//Loads a TreeType object from the database by name, if it exists.
8187
override fun loadByName(name: String): TreeType? = transaction(db) {
8288
SQLTreeEntity.find(
8389
TreesTable.typeOfTree eq typeOfTree and (TreesTable.name eq name)
@@ -86,6 +92,7 @@ class SQLRepository<T : Comparable<T>,
8692
}
8793
}
8894

95+
//Deletes a tree from the database by name.
8996
override fun deleteByName(name: String): Unit = transaction(db) {
9097
val treeId = SQLTreeEntity.find(
9198
TreesTable.typeOfTree eq strategy.typeOfTree.toString() and (TreesTable.name eq name)
@@ -98,6 +105,7 @@ class SQLRepository<T : Comparable<T>,
98105
}
99106
}
100107

108+
//Returns a list of names of all the trees in the database.
101109
override fun getNames(): List<String> = transaction(db) {
102110
SQLTreeEntity.find(TreesTable.typeOfTree eq typeOfTree).map(SQLTreeEntity::name)
103111
}

lib/src/main/kotlin/repository/jsonEntities/JsonNode.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ import kotlinx.serialization.Serializable
99
import repository.serialization.Metadata
1010
import repository.serialization.SerializableValue
1111

12-
12+
//A class that represents a tree node in JSON format. Contains
1313
@Serializable
1414
data class JsonNode(
15+
//Node value in serializable format;
1516
val data: SerializableValue,
17+
//Node metadata, including node height, balancing factor,
18+
//and other information needed to work with the tree;
1619
val metadata: Metadata,
20+
//Link to the left child node.
1721
val left: JsonNode?,
22+
//Link to the right child node.
1823
val right: JsonNode?
1924
)

lib/src/main/kotlin/repository/jsonEntities/JsonTree.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55

66
package repository.jsonEntities
77

8-
98
import kotlinx.serialization.Serializable
109
import repository.serialization.TypeOfTree
1110

11+
//Describes the "repository.jsonEntities.JsonTree" object - this is the tree
12+
// that will be serialized and stored in JSON format
1213
@Serializable
1314
data class JsonTree(
15+
//a string that contains the name of the tree.
1416
val name: String,
17+
//contains a tree type, which can be one of the "TypeOfTree" enums.
1518
val typeOfTree: TypeOfTree,
19+
//This is a reference to the root node of the tree, which can either be a "repository.jsonEntities.JsonNode" object
1620
val root: JsonNode?
1721
)

lib/src/main/kotlin/repository/neo4jEntities/Neo4jNodeEntity.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@ import repository.serialization.SerializableValue
1111

1212
@NodeEntity("Node")
1313
class Neo4jNodeEntity(
14+
15+
//serializable node value stored in the database as a string.
1416
@Property("data")
1517
var data: SerializableValue,
16-
18+
//node metadata stored in the database as a string.
1719
@Property("metadata")
1820
var metadata: Metadata,
19-
21+
//reference to the left child node in the tree structure.
2022
@Relationship(type = "LEFT", direction = Relationship.Direction.OUTGOING)
2123
var left: Neo4jNodeEntity? = null,
22-
24+
//reference to the right child node in the tree structure.
2325
@Relationship(type = "RIGHT", direction = Relationship.Direction.OUTGOING)
2426
var right: Neo4jNodeEntity? = null,
2527
) {
28+
//the node ID generated by the database on save.
2629
@Id
2730
@GeneratedValue
2831
var id: Long? = null

lib/src/main/kotlin/repository/neo4jEntities/Neo4jTreeEntity.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ import repository.serialization.TypeOfTree
1010

1111
@NodeEntity("Tree")
1212
class Neo4jTreeEntity(
13+
//a class property that stores the name of the tree.
1314
@Property("name")
1415
var name: String,
15-
16+
//a class property that stores the tree type.
1617
@Property("typeOfTree")
1718
var typeOfTree: TypeOfTree,
18-
19+
//a class property that stores a reference to the root node of the tree.
1920
@Relationship(type = "ROOT", direction = Relationship.Direction.OUTGOING)
2021
var root: Neo4jNodeEntity?,
2122
) {
22-
23+
//a class property that stores the tree ID in the Neo4j database.
2324
@Id
2425
@GeneratedValue
2526
var id: Long? = null

lib/src/main/kotlin/repository/serialization/strategies/AVLStrategy.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class AVLStrategy<T : Comparable<T>>(
1818
) : Serialization<T, AVLNode<T>, AVLTree<T>, Int>(serializeData, deserializeData) {
1919
override val typeOfTree: TypeOfTree = TypeOfTree.AVL_TREE
2020

21+
//method that creates an AVL tree node from the passed SerializableNode, which was obtained as a result of serialization.
2122
override fun createNode(node: SerializableNode?): AVLNode<T>? = node?.let {
2223
AVLNode(
2324
data = deserializeValue(node.data),
@@ -27,9 +28,12 @@ class AVLStrategy<T : Comparable<T>>(
2728
)
2829
}
2930

31+
//method that deserializes the node's metadata (in this case, the node's height). Returns the deserialized metadata.
3032
override fun deserializeMetadata(metadata: Metadata) = metadata.value.toInt()
3133

34+
//method that serializes the node's metadata (in this case, the node's height). Returns the serialized metadata.
3235
override fun serializeMetadata(node: AVLNode<T>) = Metadata(node.height.toString())
3336

37+
//method that creates an AVL tree. Returns a new AVL tree of type AVLTree<T>.
3438
override fun createTree() = AVLTree<T>()
3539
}

0 commit comments

Comments
 (0)