Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
feat: data tree and node
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Apr 2, 2023
1 parent 730d8e2 commit d4417af
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2023 Korandoru Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

root = true

[*]
end_of_line = lf
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- lib dependencies -->
<assertj.version>3.24.2</assertj.version>
<junit.version>5.9.2</junit.version>
<lombok.version>1.18.26</lombok.version>
<ratis.version>2.4.1</ratis.version>
<slf4j.version>1.7.36</slf4j.version>

<!-- bundled maven plugins -->
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
</properties>

<modules>
Expand Down
5 changes: 5 additions & 0 deletions zeronos-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
47 changes: 47 additions & 0 deletions zeronos-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,51 @@
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.ratis</groupId>
<artifactId>ratis-server-api</artifactId>
</dependency>

<!-- tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.korandoru.zeronos.server;

import java.util.Deque;
import java.util.LinkedList;

public class DataNode {

private final Deque<DataNodeEntry> revisions = new LinkedList<>();

public DataNode(DataNodeEntry entry) {
this.revisions.addLast(entry);
}

public DataNodeEntry readNodeLatest() {
// TODO - check if removed
return this.revisions.getLast();
}

public void createNode(DataNodeEntry entry) {
// TODO - check if exist
this.revisions.addLast(entry);
}

public void updateNode(DataNodeEntry entry) {
// TODO - check if exist
this.revisions.addLast(entry);
}

public void deleteNode(DataNodeEntry entry) {
// TODO - check if exist
this.revisions.addLast(entry);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.korandoru.zeronos.server;

import lombok.Data;

@Data
public class DataNodeEntry {
private final long revision;
private final byte[] data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.korandoru.zeronos.server;

import java.util.SortedMap;
import java.util.TreeMap;

public class DataTree {

private final SortedMap<String, DataNode> nodes = new TreeMap<>();

public DataTree() {
this.nodes.put("", new DataNode(new DataNodeEntry(0, new byte[0])));
}

public void createNode(String path, byte[] data, long revision) {
final int slash = path.lastIndexOf('/');
final String parentPath = path.substring(0, slash);

final DataNode parent = nodes.get(parentPath);
if (parent == null) {
throw new RuntimeException("NoNode");
}

final DataNode child = nodes.get(path);
if (child == null) {
nodes.put(path, new DataNode(new DataNodeEntry(revision, data)));
} else {
child.createNode(new DataNodeEntry(revision, data));
}
}

public DataNodeEntry readNode(String path) {
final DataNode node = nodes.get(path);
if (node == null) {
throw new RuntimeException("NoNode");
}
return node.readNodeLatest();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.korandoru.zeronos.server;

import static org.assertj.core.api.Assertions.assertThat;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.jupiter.api.Test;

class DataTreeTest {

@Test
void testCreateNode() {
final DataTree tree = new DataTree();
final AtomicLong revision = new AtomicLong();
final byte[][] datas = new byte[][]{
"zero".getBytes(StandardCharsets.UTF_8),
"one".getBytes(StandardCharsets.UTF_8),
"two".getBytes(StandardCharsets.UTF_8),
};
tree.createNode("/zeronos", datas[0], revision.incrementAndGet());
tree.createNode("/zeronos/altair", datas[1], revision.incrementAndGet());
tree.createNode("/zeronos/vega", datas[2], revision.incrementAndGet());
assertThat(tree.readNode("/zeronos")).isEqualTo(new DataNodeEntry(1, datas[0]));
assertThat(tree.readNode("/zeronos/altair")).isEqualTo(new DataNodeEntry(2, datas[1]));
assertThat(tree.readNode("/zeronos/vega")).isEqualTo(new DataNodeEntry(3, datas[2]));
}

}

0 comments on commit d4417af

Please sign in to comment.