Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
oxsean committed Dec 20, 2023
1 parent 6118aa5 commit 1834fc2
Show file tree
Hide file tree
Showing 57 changed files with 1,453 additions and 2,040 deletions.
39 changes: 36 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,45 @@ root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
tab_width = 4
max_line_length = 360
insert_final_newline = true
trim_trailing_whitespace = true

# 4 space indentation
[*.{java,xml}]
indent_style = space
indent_size = 4
[*.java]
ij_java_continuation_indent_size = 8
ij_java_align_multiline_deconstruction_list_components = false
ij_java_align_multiline_parameters = false
ij_java_call_parameters_new_line_after_left_paren = true
ij_java_call_parameters_wrap = normal
ij_java_keep_control_statement_in_one_line = false
ij_java_keep_first_column_comment = false
ij_java_keep_line_breaks = false
ij_java_method_call_chain_wrap = normal
ij_java_method_parameters_new_line_after_left_paren = true
ij_java_rparen_on_new_line_in_deconstruction_pattern = false
ij_java_wrap_first_method_in_call_chain = true
ij_java_wrap_long_lines = true
ij_java_class_count_to_use_import_on_demand = 999
ij_java_names_count_to_use_import_on_demand = 999
ij_java_insert_inner_class_imports = true
ij_java_for_brace_force = always
ij_java_if_brace_force = always
ij_java_blank_lines_around_class = 1
ij_java_blank_lines_before_class_end = 1

[*.json]
tab_width = 2

[*.{yml,yaml}]
indent_size = 2

[*.xml]
ij_xml_attribute_wrap = off
ij_xml_keep_blank_lines = 1

[pom.xml]
indent_size = 2
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@
*/
package org.apache.dubbo.common.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* Stream utils.
*/
public class StreamUtils {
private StreamUtils() {}
private StreamUtils() {
}

public static InputStream limitedInputStream(final InputStream is, final int limit) throws IOException {
return new InputStream() {
Expand Down Expand Up @@ -229,4 +235,51 @@ public static void skipUnusedStream(InputStream is) throws IOException {
is.skip(is.available());
}
}

public static void copy(InputStream in, OutputStream out) throws IOException {
if (in.getClass() == ByteArrayInputStream.class) {
copy((ByteArrayInputStream) in, out);
return;
}
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}

public static void copy(ByteArrayInputStream in, OutputStream out) throws IOException {
int len = in.available();
byte[] buffer = new byte[len];
in.read(buffer, 0, len);
out.write(buffer, 0, len);
}

public static byte[] readBytes(InputStream in) throws IOException {
if (in.getClass() == ByteArrayInputStream.class) {
return readBytes((ByteArrayInputStream) in);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
return out.toByteArray();
}

public static byte[] readBytes(ByteArrayInputStream in) throws IOException {
int len = in.available();
byte[] bytes = new byte[len];
in.read(bytes, 0, len);
return bytes;
}

public static String toString(InputStream in) throws IOException {
return new String(readBytes(in), StandardCharsets.UTF_8);
}

public static String toString(InputStream in, Charset charset) throws IOException {
return new String(readBytes(in), charset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import static java.util.Collections.emptySet;
Expand Down Expand Up @@ -404,13 +406,19 @@ public static <T> T first(Collection<T> values) {
return null;
}
if (values instanceof List) {
List<T> list = (List<T>) values;
return list.get(0);
return ((List<T>) values).get(0);
} else {
return values.iterator().next();
}
}

public static <T> T first(List<T> values) {
if (isEmpty(values)) {
return null;
}
return values.get(0);
}

public static <T> Set<T> toTreeSet(Set<T> set) {
if (isEmpty(set)) {
return set;
Expand All @@ -420,4 +428,41 @@ public static <T> Set<T> toTreeSet(Set<T> set) {
}
return set;
}

public static <T> Set<T> newHashSet(int expectedSize) {
return new HashSet<>(capacity(expectedSize));
}

public static <T> Set<T> newLinkedHashSet(int expectedSize) {
return new LinkedHashSet<>(capacity(expectedSize));
}

public static <T> Set<T> newConcurrentHashSet(int expectedSize) {
return Collections.newSetFromMap(newConcurrentHashMap(expectedSize));
}

public static <T, K> Map<K, T> newHashMap(int expectedSize) {
return new HashMap<>(capacity(expectedSize));
}

public static <T, K> Map<K, T> newLinkedHashMap(int expectedSize) {
return new LinkedHashMap<>(capacity(expectedSize));
}

public static <T, K> Map<K, T> newConcurrentHashMap(int expectedSize) {
return new ConcurrentHashMap<>(capacity(expectedSize));
}

public static int capacity(int expectedSize) {
if (expectedSize < 3) {
if (expectedSize < 0) {
throw new IllegalArgumentException("expectedSize cannot be negative but was: " + expectedSize);
}
return expectedSize + 1;
}
if (expectedSize < 1 << (Integer.SIZE - 2)) {
return (int) (expectedSize / 0.75F + 1.0F);
}
return Integer.MAX_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@
package org.apache.dubbo.common.utils;

import java.util.LinkedHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Function;

/**
* LRU-2
* </p>
* <p>
* When the data accessed for the first time, add it to history list. If the size of history list reaches max capacity, eliminate the earliest data (first in first out).
* When the data already exists in the history list, and be accessed for the second time, then it will be put into cache.
*
* </p>
* TODO, consider replacing with ConcurrentHashMap to improve performance under concurrency
*/
public class LRU2Cache<K, V> extends LinkedHashMap<K, V> {

private static final long serialVersionUID = -5167631809472116969L;

private static final float DEFAULT_LOAD_FACTOR = 0.75f;

private static final int DEFAULT_MAX_CAPACITY = 1000;
private final Lock lock = new ReentrantLock();
private volatile int maxCapacity;

private final ReadWriteLock lock = new ReentrantReadWriteLock();
private volatile int maxCapacity;
// as history list
private final PreCache<K, Boolean> preCache;

Expand All @@ -58,27 +58,27 @@ protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) {

@Override
public boolean containsKey(Object key) {
lock.lock();
lock.readLock().lock();
try {
return super.containsKey(key);
} finally {
lock.unlock();
lock.readLock().unlock();
}
}

@Override
public V get(Object key) {
lock.lock();
lock.readLock().lock();
try {
return super.get(key);
} finally {
lock.unlock();
lock.readLock().unlock();
}
}

@Override
public V put(K key, V value) {
lock.lock();
lock.writeLock().lock();
try {
if (preCache.containsKey(key)) {
// add it to cache
Expand All @@ -90,39 +90,49 @@ public V put(K key, V value) {
return value;
}
} finally {
lock.unlock();
lock.writeLock().unlock();
}
}

@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> fn) {
V value = get(key);
if (value == null) {
value = fn.apply(key);
put(key, value);
}
return value;
}

@Override
public V remove(Object key) {
lock.lock();
lock.writeLock().lock();
try {
preCache.remove(key);
return super.remove(key);
} finally {
lock.unlock();
lock.writeLock().unlock();
}
}

@Override
public int size() {
lock.lock();
lock.readLock().lock();
try {
return super.size();
} finally {
lock.unlock();
lock.readLock().unlock();
}
}

@Override
public void clear() {
lock.lock();
lock.writeLock().lock();
try {
preCache.clear();
super.clear();
} finally {
lock.unlock();
lock.writeLock().unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,32 @@ private static synchronized void startTicker() {
isTickerAlive = true;
}
}

public static Long parseTimeoutToMills(String timeoutVal) {
if (StringUtils.isEmpty(timeoutVal) || StringUtils.isContains(timeoutVal,
"null")) {
return null;
}
long value = Long.parseLong(timeoutVal.substring(0,
timeoutVal.length() - 1));
char unit = timeoutVal.charAt(timeoutVal.length() - 1);
switch (unit) {
case 'n':
return TimeUnit.NANOSECONDS.toMillis(value);
case 'u':
return TimeUnit.MICROSECONDS.toMillis(value);
case 'm':
return value;
case 'S':
return TimeUnit.SECONDS.toMillis(value);
case 'M':
return TimeUnit.MINUTES.toMillis(value);
case 'H':
return TimeUnit.HOURS.toMillis(value);
default:
// invalid timeout config
return null;
}
}

}
Loading

0 comments on commit 1834fc2

Please sign in to comment.