This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: tison <[email protected]>
- Loading branch information
Showing
8 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
zeronos-server/src/main/java/io/korandoru/zeronos/server/DataNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
zeronos-server/src/main/java/io/korandoru/zeronos/server/DataNodeEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
38 changes: 38 additions & 0 deletions
38
zeronos-server/src/main/java/io/korandoru/zeronos/server/DataTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
zeronos-server/src/test/java/io/korandoru/zeronos/server/DataTreeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} | ||
|
||
} |