-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…58)
- Loading branch information
1 parent
8d860e3
commit cb0358e
Showing
11 changed files
with
337 additions
and
74 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,19 @@ | ||
package com.stundb.api.btree; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
public interface BTree<K extends Comparable<K>, V> { | ||
|
||
void putIfAbsent(K key, V value); | ||
|
||
Optional<Node<K, V>> find(K key); | ||
|
||
Collection<Node<K, V>> findAll(); | ||
|
||
void remove(K key); | ||
|
||
void clear(); | ||
|
||
Integer size(); | ||
} |
176 changes: 176 additions & 0 deletions
176
stundb/api/src/main/java/com/stundb/api/btree/BTreeImpl.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,176 @@ | ||
package com.stundb.api.btree; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class BTreeImpl<K extends Comparable<K>, V> implements BTree<K, V> { | ||
|
||
private final AtomicInteger size = new AtomicInteger(0); | ||
private Node<K, V> root; | ||
|
||
@Override | ||
public void putIfAbsent(K key, V value) { | ||
if (root == null) { | ||
root = new Node<>(key, value); | ||
size.getAndIncrement(); | ||
return; | ||
} | ||
|
||
var node = find(key, root); | ||
if (node.isPresent()) { | ||
return; | ||
} | ||
|
||
var target = root; | ||
|
||
while (target != null) { | ||
if (key.compareTo(target.getKey()) > 0) { | ||
if (target.getRight() == null) { | ||
target.setRight(new Node<>(key, value)); | ||
size.getAndIncrement(); | ||
return; | ||
} | ||
|
||
target = target.getRight(); | ||
continue; | ||
} | ||
|
||
if (target.getLeft() == null) { | ||
target.setLeft(new Node<>(key, value)); | ||
size.getAndIncrement(); | ||
return; | ||
} | ||
|
||
target = target.getLeft(); | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<Node<K, V>> find(K key) { | ||
return find(key, root); | ||
} | ||
|
||
@Override | ||
public Collection<Node<K, V>> findAll() { | ||
return getNodeValue(root); | ||
} | ||
|
||
@Override | ||
public void remove(K key) { | ||
root = remove(key, root); | ||
size.getAndDecrement(); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
root = clear(root); | ||
size.set(0); | ||
} | ||
|
||
@Override | ||
public Integer size() { | ||
return size.get(); | ||
} | ||
|
||
private Collection<Node<K, V>> getNodeValue(Node<K, V> node) { | ||
if (node == null) { | ||
return new ArrayList<>(); | ||
} | ||
|
||
if (node.getLeft() == null && node.getRight() == null) { | ||
return new ArrayList<>(List.of(node)); | ||
} | ||
|
||
if (node.getLeft() == null) { | ||
var arr = getNodeValue(node.getRight()); | ||
arr.add(node); | ||
return arr; | ||
} | ||
|
||
if (node.getRight() == null) { | ||
var arr = getNodeValue(node.getLeft()); | ||
arr.add(node); | ||
return arr; | ||
} | ||
|
||
var result = new ArrayList<>(getNodeValue(node.getLeft())); | ||
result.addAll(getNodeValue(node.getRight())); | ||
result.add(node); | ||
return result; | ||
} | ||
|
||
private Optional<Node<K, V>> find(K key, Node<K, V> target) { | ||
if (target == null) { | ||
return Optional.empty(); | ||
} | ||
|
||
// search finished | ||
if (key.compareTo(target.getKey()) == 0) { | ||
return Optional.of(target); | ||
} | ||
|
||
// key is most probably on the right side of the tree | ||
if (key.compareTo(target.getKey()) > 0) { | ||
return find(key, target.getRight()); | ||
} | ||
|
||
// key is most probably on the left side of the tree | ||
return find(key, target.getLeft()); | ||
} | ||
|
||
private Node<K, V> remove(K key, Node<K, V> target) { | ||
if (target == null) { | ||
return null; | ||
} | ||
|
||
// key < 0, then remove left child | ||
if (key.compareTo(target.getKey()) < 0) { | ||
target.setLeft(remove(key, target.getLeft())); | ||
return target; | ||
} | ||
|
||
// key < 0, then remove right child | ||
if (key.compareTo(target.getKey()) > 0) { | ||
target.setRight(remove(key, target.getRight())); | ||
return target; | ||
} | ||
|
||
// if no children, then just remove the node | ||
if (target.getLeft() == null && target.getRight() == null) { | ||
return null; | ||
} | ||
|
||
// if left child is present, then replace parent with child | ||
if (target.getLeft() == null) { | ||
target = target.getRight(); | ||
return target; | ||
} | ||
|
||
// if right child is present, then replace parent with child | ||
if (target.getRight() == null) { | ||
target = target.getLeft(); | ||
return target; | ||
} | ||
|
||
// in case of both children are present, replace with the minimum key | ||
var min = min(target.getRight()); | ||
target.setKey(min); | ||
target.setRight(remove(min, target.getRight())); | ||
return target; | ||
} | ||
|
||
private K min(Node<K, V> node) { | ||
while (node.getLeft() != null) { | ||
node = node.getLeft(); | ||
} | ||
return node.getKey(); | ||
} | ||
|
||
private Node<K, V> clear(Node<K, V> target) { | ||
if (target != null) { | ||
clear(target.getRight()); | ||
clear(target.getLeft()); | ||
} | ||
return null; | ||
} | ||
} |
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,16 @@ | ||
package com.stundb.api.btree; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Node<K extends Comparable<K>, V> { | ||
private K key; | ||
private V value; | ||
private Node<K, V> left; | ||
private Node<K, V> right; | ||
|
||
public Node(K key, V value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
stundb/application/src/main/java/com/stundb/modules/providers/BTreeProvider.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,19 @@ | ||
package com.stundb.modules.providers; | ||
|
||
import com.stundb.api.btree.BTree; | ||
import com.stundb.api.btree.BTreeImpl; | ||
|
||
import jakarta.inject.Provider; | ||
|
||
import java.util.Optional; | ||
|
||
public class BTreeProvider<T> implements Provider<BTree<String, T>> { | ||
|
||
private BTreeImpl<String, T> instance; | ||
|
||
@Override | ||
public BTree<String, T> get() { | ||
instance = Optional.ofNullable(instance).orElseGet(BTreeImpl::new); | ||
return instance; | ||
} | ||
} |
31 changes: 12 additions & 19 deletions
31
stundb/application/src/main/java/com/stundb/modules/providers/CacheProvider.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 |
---|---|---|
@@ -1,26 +1,19 @@ | ||
package com.stundb.modules.providers; | ||
|
||
import static java.util.Optional.ofNullable; | ||
|
||
import com.stundb.api.models.ApplicationConfig; | ||
import com.stundb.api.models.Capacity; | ||
import com.stundb.core.cache.BTreeCache; | ||
import com.stundb.core.cache.Cache; | ||
import com.stundb.core.cache.FIFOCache; | ||
|
||
import java.util.function.Function; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Provider; | ||
import jakarta.inject.Singleton; | ||
|
||
@Singleton | ||
public class CacheProvider<T> implements Provider<Cache<T>> { | ||
|
||
@Inject private BTreeCache<T> cache; | ||
|
||
interface CacheProvider<T> { | ||
default Cache<T> getInstance( | ||
Cache<T> cache, | ||
ApplicationConfig config, | ||
Function<Capacity, Integer> transformer, | ||
Integer defaultCapacity) { | ||
return ofNullable(cache) | ||
.orElseGet( | ||
() -> | ||
new FIFOCache<>( | ||
ofNullable(config.getCapacity()) | ||
.map(transformer) | ||
.orElse(defaultCapacity))); | ||
@Override | ||
public Cache<T> get() { | ||
return cache; | ||
} | ||
} |
24 changes: 0 additions & 24 deletions
24
stundb/application/src/main/java/com/stundb/modules/providers/InternalCacheProvider.java
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
stundb/application/src/main/java/com/stundb/modules/providers/PublicCacheProvider.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.