Skip to content

Commit

Permalink
#38 Implemented de-registration and re-registration of HTTP handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Feb 14, 2016
1 parent 80433c0 commit dcd82dc
Show file tree
Hide file tree
Showing 24 changed files with 512 additions and 128 deletions.
Expand Up @@ -35,6 +35,8 @@ public interface BufMap<T> {

T get(Buf buf, Range key);

boolean remove(String key);

void clear();

}
Expand Up @@ -66,7 +66,7 @@ public void put(String key, T value) {
public T get(Buf buf, Range key) {
long hash = hash(buf.bytes(), key);

SimpleList<MapEntry<byte[], T>> candidates = entries.get(hash);
SimpleList<MapEntry<byte[], T>> candidates = entries.bucket(hash);

if (candidates != null) {
for (int i = 0; i < candidates.size(); i++) {
Expand All @@ -81,4 +81,28 @@ public T get(Buf buf, Range key) {
return defaultValue;
}

@Override
public boolean remove(String key) {
assert key.length() >= 1;

long hash = hash(key);

SimpleList<MapEntry<byte[], T>> bucket = this.entries.bucket(hash);

if (bucket == null) {
return false;
}

for (int i = 0; i < bucket.size(); i++) {
MapEntry<byte[], T> route = bucket.get(i);

if (new String(route.key).equals(key)) {
bucket.delete(i);
return true;
}
}

return false;
}

}
Expand Up @@ -56,7 +56,7 @@ public V getDefaultValue() {
}

protected MapEntry<K, V> findEntry(K key) {
SimpleList<MapEntry<K, V>> bucket = entries.get(key.hashCode());
SimpleList<MapEntry<K, V>> bucket = entries.bucket(key.hashCode());
return findEntry(key, bucket);
}

Expand Down
16 changes: 8 additions & 8 deletions rapidoid-commons/src/main/java/org/rapidoid/util/Constants.java
Expand Up @@ -73,20 +73,20 @@ public interface Constants {

String SEPARATOR_LINE = "\n--------------------------------------------------------------\n";

String _GET = "GET";
String GET = "GET";

String _POST = "POST";
String POST = "POST";

String _PUT = "PUT";
String PUT = "PUT";

String _DELETE = "DELETE";
String DELETE = "DELETE";

String _PATCH = "PATCH";
String PATCH = "PATCH";

String _OPTIONS = "OPTIONS";
String OPTIONS = "OPTIONS";

String _HEAD = "HEAD";
String HEAD = "HEAD";

String _TRACE = "TRACE";
String TRACE = "TRACE";

}
Expand Up @@ -48,15 +48,15 @@ protected SimpleList<T> newList(int initialBucketSize) {
}

public void put(long key, T value) {
get(key).add(value);
bucket(key).add(value);
}

public SimpleList<T> get(long key) {
public SimpleList<T> bucket(long key) {
int index = index(key);
return bucket(index);
return getBucket(index);
}

protected SimpleList<T> bucket(int index) {
protected SimpleList<T> getBucket(int index) {
SimpleList<T> list;

// after construction, other threads might need some time to see the new references
Expand All @@ -77,7 +77,7 @@ public void clear() {
}

protected void clearBucket(int index) {
bucket(index).clear();
getBucket(index).clear();
}

}
Expand Up @@ -22,6 +22,7 @@

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.commons.Err;

import java.util.Arrays;

Expand Down Expand Up @@ -78,6 +79,14 @@ public void addRotating(T obj) {
}
}

public void delete(int index) {
Err.bounds(index, 0, size - 1);

System.arraycopy(array, index + 1, array, index, size - index - 1);

size--;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Expand All @@ -91,5 +100,4 @@ public String toString() {

return "[" + sb.toString() + "]";
}

}

0 comments on commit dcd82dc

Please sign in to comment.