Skip to content

Commit

Permalink
refactor(core): improve code by flow idea suggests
Browse files Browse the repository at this point in the history
  • Loading branch information
jadepeng committed Nov 11, 2022
1 parent 19ef0ac commit b681523
Show file tree
Hide file tree
Showing 22 changed files with 122 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public BackendException(Throwable cause) {
this("Exception in backend", cause);
}

public static final void check(boolean expression,
String message, Object... args)
public static final void check(boolean expression, String message, Object... args)
throws BackendException {
if (!expression) {
throw new BackendException(message, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class CacheManager {

private static final Logger LOG = Log.logger(CacheManager.class);

private static CacheManager INSTANCE = new CacheManager();
private static final CacheManager INSTANCE = new CacheManager();

// Check the cache expiration every 30s by default
private static final long TIMER_TICK_PERIOD = 30;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ protected Iterator<HugeEdge> queryEdgesFromBackend(Query query) {
* try fetch a few of the head results and determine whether to cache.
*/
final int tryMax = 1 + MAX_CACHE_EDGES_PER_QUERY;
assert tryMax > MAX_CACHE_EDGES_PER_QUERY;
edges = new ArrayList<>(tryMax);
for (int i = 0; rs.hasNext() && i < tryMax; i++) {
edges.add(rs.next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,23 +386,8 @@ public void set(HugeType type, Id id, V value) {
if (key >= this.size) {
return;
}
switch (type) {
case PROPERTY_KEY:
this.pks.set(key, value);
break;
case VERTEX_LABEL:
this.vls.set(key, value);
break;
case EDGE_LABEL:
this.els.set(key, value);
break;
case INDEX_LABEL:
this.ils.set(key, value);
break;
default:
// pass
break;
}

this.setValue(type, key, value);
}

public void remove(HugeType type, Id id) {
Expand All @@ -412,10 +397,27 @@ public void remove(HugeType type, Id id) {
return;
}
int key = (int) longId;
V value = null;
if (key >= this.size) {
return;
}

this.setValue(type, key, null);
}

public void clear() {
this.pks.clear();
this.vls.clear();
this.els.clear();
this.ils.clear();

this.cachedTypes.clear();
}

public CachedTypes cachedTypes() {
return this.cachedTypes;
}

private void setValue(HugeType type, int key, V value) {
switch (type) {
case PROPERTY_KEY:
this.pks.set(key, value);
Expand All @@ -434,19 +436,6 @@ public void remove(HugeType type, Id id) {
break;
}
}

public void clear() {
this.pks.clear();
this.vls.clear();
this.els.clear();
this.ils.clear();

this.cachedTypes.clear();
}

public CachedTypes cachedTypes() {
return this.cachedTypes;
}
}

private static class CachedTypes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ private enum ValueType {
DATE(DataType.DATE),
UUID(DataType.UUID);

private DataType dataType;
private final DataType dataType;

ValueType() {
this(DataType.UNKNOWN);
Expand All @@ -390,7 +390,7 @@ public static ValueType valueOf(int index) {

public static ValueType valueOf(Object object) {
E.checkNotNull(object, "object");
Class<? extends Object> clazz = object.getClass();
Class<?> clazz = object.getClass();
if (Collection.class.isAssignableFrom(clazz)) {
return ValueType.LIST;
} else if (HugeVertex.class.isAssignableFrom(clazz)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,38 +117,7 @@ protected final boolean write(Id id, Object value, long timeOffset) {
final Lock lock = this.keyLock.lock(id);
try {
// The cache is full
while (this.map.size() >= capacity) {
/*
* Remove the oldest from the queue
* NOTE: it maybe return null if someone else (that's other
* threads) are doing dequeue() and the queue may be empty.
*/
LinkNode<Id, Object> removed = this.queue.dequeue();
if (removed == null) {
/*
* If at this time someone add some new items, these will
* be cleared in the map, but still stay in the queue, so
* the queue will have some more nodes than the map.
*/
this.map.clear();
break;
}
/*
* Remove the oldest from the map
* NOTE: it maybe return null if other threads are doing remove
*/
this.map.remove(removed.key());
if (LOG.isDebugEnabled()) {
LOG.debug("RamCache replaced '{}' with '{}' (capacity={})",
removed.key(), id, capacity);
}
/*
* Release the object
* NOTE: we can't reuse the removed node due to someone else
* may access the node (will do remove() -> enqueue())
*/
removed = null;
}
this.removeOldestIfCacheFull(id, capacity);

// Remove the old node if exists
LinkNode<Id, Object> node = this.map.get(id);
Expand All @@ -170,7 +139,6 @@ protected final void remove(Id id) {
if (id == null) {
return;
}
assert id != null;

final Lock lock = this.keyLock.lock(id);
try {
Expand Down Expand Up @@ -229,6 +197,41 @@ public String toString() {
return this.map.toString();
}

private void removeOldestIfCacheFull(Id id, long capacity) {
while (this.map.size() >= capacity) {
/*
* Remove the oldest from the queue
* NOTE: it maybe return null if someone else (that's other
* threads) are doing dequeue() and the queue may be empty.
*/
LinkNode<Id, Object> removed = this.queue.dequeue();
if (removed == null) {
/*
* If at this time someone add some new items, these will
* be cleared in the map, but still stay in the queue, so
* the queue will have some more nodes than the map.
*/
this.map.clear();
break;
}
/*
* Remove the oldest from the map
* NOTE: it maybe return null if other threads are doing remove
*/
this.map.remove(removed.key());
if (LOG.isDebugEnabled()) {
LOG.debug("RamCache replaced '{}' with '{}' (capacity={})",
removed.key(), id, capacity);
}
/*
* Release the object
* NOTE: we can't reuse the removed node due to someone else
* may access the node (will do remove() -> enqueue())
*/
removed = null;
}
}

private static final class LinkNode<K, V> extends CacheNode<K, V> {

private LinkNode<K, V> prev;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ public abstract class IdGenerator {

public abstract Id generate(HugeVertex vertex);

public static final Id of(String id) {
public static Id of(String id) {
return new StringId(id);
}

public static final Id of(UUID id) {
public static Id of(UUID id) {
return new UuidId(id);
}

public static final Id of(String id, boolean uuid) {
public static Id of(String id, boolean uuid) {
return uuid ? new UuidId(id) : new StringId(id);
}

public static final Id of(long id) {
public static Id of(long id) {
return new LongId(id);
}

Expand All @@ -65,7 +65,7 @@ public static Id of(Object id) {
return new ObjectId(id);
}

public static final Id of(byte[] bytes, IdType type) {
public static Id of(byte[] bytes, IdType type) {
switch (type) {
case LONG:
return new LongId(bytes);
Expand All @@ -78,7 +78,7 @@ public static final Id of(byte[] bytes, IdType type) {
}
}

public static final Id ofStoredString(String id, IdType type) {
public static Id ofStoredString(String id, IdType type) {
switch (type) {
case LONG:
return of(LongEncoding.decodeSignedB64(id));
Expand All @@ -92,7 +92,7 @@ public static final Id ofStoredString(String id, IdType type) {
}
}

public static final String asStoredString(Id id) {
public static String asStoredString(Id id) {
switch (id.type()) {
case LONG:
return LongEncoding.encodeSignedB64(id.asLong());
Expand All @@ -105,7 +105,7 @@ public static final String asStoredString(Id id) {
}
}

public static final IdType idType(Id id) {
public static IdType idType(Id id) {
if (id instanceof LongId) {
return IdType.LONG;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ public static Id readBinString(Object id) {

public static String writeString(Id id) {
String idString = id.asString();
StringBuilder sb = new StringBuilder(1 + idString.length());
sb.append(id.type().prefix()).append(idString);
return sb.toString();
return id.type().prefix() + idString;
}

public static Id readString(String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public Id generate(HugeVertex vertex) {
*/
private static class IdWorker {

private long workerId;
private long datacenterId;
private final long workerId;
private final long datacenterId;
private long sequence = 0L; // AtomicLong
private long lastTimestamp = -1L;

Expand Down Expand Up @@ -159,7 +159,6 @@ public synchronized long nextId() {
timestamp = TimeUtil.tillNextMillis(this.lastTimestamp);
}
} else {
assert timestamp < this.lastTimestamp;
LOG.error("Clock is moving backwards, " +
"rejecting requests until {}.",
this.lastTimestamp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected Query parent() {
return this.parent;
}

protected QueryResults.Fetcher<R> fetcher() {
private QueryResults.Fetcher<R> fetcher() {
return this.fetcher;
}

Expand Down Expand Up @@ -97,7 +97,7 @@ public QueryResults<R> fetch(int pageSize) {
}

// Fetch all results once
return QueryResults.flatMap(this.queries.iterator(), q -> q.iterator());
return QueryResults.flatMap(this.queries.iterator(), FlattenQuery::iterator);
}

protected PageResults<R> fetchNext(PageInfo pageInfo, long pageSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ public String string() {
* @param second is value in query condition
* @return true if equal, otherwise false
*/
protected static boolean equals(final Object first,
final Object second) {
private static boolean equals(final Object first,
final Object second) {
assert second != null;
if (first instanceof Id) {
if (second instanceof String) {
Expand All @@ -196,7 +196,7 @@ protected static boolean equals(final Object first,
* second; and a value greater than 0 if first is
* numerically greater than second.
*/
protected static int compare(final Object first, final Object second) {
private static int compare(final Object first, final Object second) {
assert second != null;
if (second instanceof Number) {
return NumericUtil.compareNumber(first == null ? 0 : first,
Expand All @@ -211,7 +211,7 @@ protected static int compare(final Object first, final Object second) {
second, second.getClass().getSimpleName()));
}

protected static int compareDate(Object first, Date second) {
private static int compareDate(Object first, Date second) {
if (first == null) {
first = DateUtil.DATE_ZERO;
}
Expand Down Expand Up @@ -646,7 +646,7 @@ public int hashCode() {

public static class SyspropRelation extends Relation {

private HugeKeys key;
private final HugeKeys key;

public SyspropRelation(HugeKeys key, Object value) {
this(key, RelationType.EQ, value);
Expand Down Expand Up @@ -701,7 +701,7 @@ public boolean isFlattened() {
public static class UserpropRelation extends Relation {

// Id of property key
private Id key;
private final Id key;

public UserpropRelation(Id key, Object value) {
this(key, RelationType.EQ, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,7 @@ public <T> T condition(Object key) {
}

public void unsetCondition(Object key) {
for (Iterator<Condition> iter = this.conditions.iterator();
iter.hasNext();) {
Condition c = iter.next();
if (c.isRelation() && ((Condition.Relation) c).key().equals(key)) {
iter.remove();
}
}
this.conditions.removeIf(c -> c.isRelation() && ((Relation) c).key().equals(key));
}

public boolean containsCondition(HugeKeys key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private static Condition convTextContainsAny2Or(Relation relation) {
assert relation.relation() == Condition.RelationType.TEXT_CONTAINS_ANY;
@SuppressWarnings("unchecked")
Collection<String> words = (Collection<String>) relation.value();
Condition cond = null;
Condition cond;
Condition conds = null;
for (String word : words) {
assert relation.key() instanceof Id;
Expand Down
Loading

0 comments on commit b681523

Please sign in to comment.