forked from hugegraph/hugegraph-sync
refactor(core): improve code by flow idea suggests (#2000)
Co-authored-by: jadepeng <jqpeng@iflytek.com>
This commit is contained in:
parent
b1b12098fe
commit
020b7dc00a
|
|
@ -45,8 +45,7 @@ public class BackendException extends HugeException {
|
|||
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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -345,7 +345,6 @@ public final class CachedGraphTransaction extends GraphTransaction {
|
|||
* 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());
|
||||
|
|
|
|||
|
|
@ -386,23 +386,8 @@ public final class CachedSchemaTransaction extends SchemaTransaction {
|
|||
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) {
|
||||
|
|
@ -412,27 +397,11 @@ public final class CachedSchemaTransaction extends SchemaTransaction {
|
|||
return;
|
||||
}
|
||||
int key = (int) longId;
|
||||
V value = null;
|
||||
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, null);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
|
|
@ -447,6 +416,26 @@ public final class CachedSchemaTransaction extends SchemaTransaction {
|
|||
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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class CachedTypes
|
||||
|
|
|
|||
|
|
@ -363,7 +363,7 @@ public class OffheapCache extends AbstractCache<Id, Object> {
|
|||
DATE(DataType.DATE),
|
||||
UUID(DataType.UUID);
|
||||
|
||||
private DataType dataType;
|
||||
private final DataType dataType;
|
||||
|
||||
ValueType() {
|
||||
this(DataType.UNKNOWN);
|
||||
|
|
@ -390,7 +390,7 @@ public class OffheapCache extends AbstractCache<Id, Object> {
|
|||
|
||||
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)) {
|
||||
|
|
|
|||
|
|
@ -117,38 +117,7 @@ public class RamCache extends AbstractCache<Id, Object> {
|
|||
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);
|
||||
|
|
@ -170,7 +139,6 @@ public class RamCache extends AbstractCache<Id, Object> {
|
|||
if (id == null) {
|
||||
return;
|
||||
}
|
||||
assert id != null;
|
||||
|
||||
final Lock lock = this.keyLock.lock(id);
|
||||
try {
|
||||
|
|
@ -229,6 +197,41 @@ public class RamCache extends AbstractCache<Id, Object> {
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
@ -65,7 +65,7 @@ public abstract class IdGenerator {
|
|||
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);
|
||||
|
|
@ -78,7 +78,7 @@ public abstract class IdGenerator {
|
|||
}
|
||||
}
|
||||
|
||||
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));
|
||||
|
|
@ -92,7 +92,7 @@ public abstract class IdGenerator {
|
|||
}
|
||||
}
|
||||
|
||||
public static final String asStoredString(Id id) {
|
||||
public static String asStoredString(Id id) {
|
||||
switch (id.type()) {
|
||||
case LONG:
|
||||
return LongEncoding.encodeSignedB64(id.asLong());
|
||||
|
|
@ -105,7 +105,7 @@ public abstract class IdGenerator {
|
|||
}
|
||||
}
|
||||
|
||||
public static final IdType idType(Id id) {
|
||||
public static IdType idType(Id id) {
|
||||
if (id instanceof LongId) {
|
||||
return IdType.LONG;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,9 +74,7 @@ public final class IdUtil {
|
|||
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -110,8 +110,8 @@ public class SnowflakeIdGenerator extends IdGenerator {
|
|||
*/
|
||||
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;
|
||||
|
||||
|
|
@ -159,7 +159,6 @@ public class SnowflakeIdGenerator extends IdGenerator {
|
|||
timestamp = TimeUtil.tillNextMillis(this.lastTimestamp);
|
||||
}
|
||||
} else {
|
||||
assert timestamp < this.lastTimestamp;
|
||||
LOG.error("Clock is moving backwards, " +
|
||||
"rejecting requests until {}.",
|
||||
this.lastTimestamp);
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ public final class QueryList<R> {
|
|||
return this.parent;
|
||||
}
|
||||
|
||||
protected QueryResults.Fetcher<R> fetcher() {
|
||||
private QueryResults.Fetcher<R> fetcher() {
|
||||
return this.fetcher;
|
||||
}
|
||||
|
||||
|
|
@ -97,7 +97,7 @@ public final class QueryList<R> {
|
|||
}
|
||||
|
||||
// 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) {
|
||||
|
|
|
|||
|
|
@ -168,8 +168,8 @@ public abstract class Condition {
|
|||
* @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) {
|
||||
|
|
@ -196,7 +196,7 @@ public abstract class Condition {
|
|||
* 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,
|
||||
|
|
@ -211,7 +211,7 @@ public abstract class Condition {
|
|||
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;
|
||||
}
|
||||
|
|
@ -646,7 +646,7 @@ public abstract class Condition {
|
|||
|
||||
public static class SyspropRelation extends Relation {
|
||||
|
||||
private HugeKeys key;
|
||||
private final HugeKeys key;
|
||||
|
||||
public SyspropRelation(HugeKeys key, Object value) {
|
||||
this(key, RelationType.EQ, value);
|
||||
|
|
@ -701,7 +701,7 @@ public abstract class Condition {
|
|||
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);
|
||||
|
|
|
|||
|
|
@ -286,13 +286,7 @@ public class ConditionQuery extends IdQuery {
|
|||
}
|
||||
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -186,7 +186,7 @@ public final class ConditionQueryFlatten {
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -522,7 +522,7 @@ public class Query implements Cloneable {
|
|||
StringBuilder sb = new StringBuilder(128);
|
||||
sb.append("`Query ");
|
||||
if (this.aggregate != null) {
|
||||
sb.append(this.aggregate.toString());
|
||||
sb.append(this.aggregate);
|
||||
} else {
|
||||
sb.append('*');
|
||||
}
|
||||
|
|
@ -568,8 +568,7 @@ public class Query implements Cloneable {
|
|||
return capacity != null ? capacity : DEFAULT_CAPACITY;
|
||||
}
|
||||
|
||||
public static final void checkForceCapacity(long count)
|
||||
throws LimitExceedException {
|
||||
public static void checkForceCapacity(long count) throws LimitExceedException {
|
||||
if (count > Query.DEFAULT_CAPACITY) {
|
||||
throw new LimitExceedException(
|
||||
"Too many records(must <= %s) for one query",
|
||||
|
|
@ -579,6 +578,6 @@ public class Query implements Cloneable {
|
|||
|
||||
public enum Order {
|
||||
ASC,
|
||||
DESC;
|
||||
DESC
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,12 +31,14 @@ import com.baidu.hugegraph.type.HugeType;
|
|||
public abstract class AbstractSerializer
|
||||
implements GraphSerializer, SchemaSerializer {
|
||||
|
||||
protected HugeConfig config;
|
||||
|
||||
public AbstractSerializer() {
|
||||
// TODO: default constructor
|
||||
}
|
||||
|
||||
public AbstractSerializer(HugeConfig config) {
|
||||
// TODO: use the config
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
protected BackendEntry convertEntry(BackendEntry entry) {
|
||||
|
|
|
|||
|
|
@ -129,7 +129,7 @@ public class BinaryEntryIterator<Elem> extends BackendEntryIterator {
|
|||
((BinaryBackendEntry) this.current).removeColumn(lastOne);
|
||||
}
|
||||
|
||||
public static final long sizeOfEntry(BackendEntry entry) {
|
||||
public static long sizeOfEntry(BackendEntry entry) {
|
||||
/*
|
||||
* 3 cases:
|
||||
* 1) one vertex per entry
|
||||
|
|
|
|||
|
|
@ -123,7 +123,7 @@ public final class BytesBuffer extends OutputStream {
|
|||
}
|
||||
|
||||
public BytesBuffer forReadWritten() {
|
||||
((Buffer) this.buffer).flip();
|
||||
this.buffer.flip();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -263,7 +263,7 @@ public final class BytesBuffer extends OutputStream {
|
|||
}
|
||||
|
||||
public boolean readBoolean() {
|
||||
return this.buffer.get() == 0 ? false : true;
|
||||
return this.buffer.get() != 0;
|
||||
}
|
||||
|
||||
public char readChar() {
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ import com.baidu.hugegraph.config.HugeConfig;
|
|||
|
||||
public class SerializerFactory {
|
||||
|
||||
private static Map<String, Class<? extends AbstractSerializer>> serializers;
|
||||
private static final Map<String, Class<? extends AbstractSerializer>> serializers;
|
||||
|
||||
static {
|
||||
serializers = new ConcurrentHashMap<>();
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ public class TableBackendEntry implements BackendEntry {
|
|||
private HugeType type;
|
||||
private Id id;
|
||||
private Id subId;
|
||||
private Map<HugeKeys, Object> columns;
|
||||
private final Map<HugeKeys, Object> columns;
|
||||
private long ttl;
|
||||
|
||||
public Row(HugeType type) {
|
||||
|
|
|
|||
|
|
@ -28,18 +28,32 @@ import com.baidu.hugegraph.exception.NotSupportException;
|
|||
|
||||
public final class IntObjectMap<V> implements RamMap {
|
||||
|
||||
private final Object[] array;
|
||||
private static final float DEFAULT_INITIAL_FACTOR = 0.25f;
|
||||
|
||||
private final int maxSize;
|
||||
private int currentSize;
|
||||
private Object[] array;
|
||||
|
||||
public IntObjectMap(int size) {
|
||||
this.array = new Object[size];
|
||||
this.maxSize = size;
|
||||
this.currentSize = (int) (size * DEFAULT_INITIAL_FACTOR);
|
||||
this.array = new Object[this.currentSize];
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public V get(int key) {
|
||||
if (key >= this.currentSize) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (V) this.array[key];
|
||||
}
|
||||
|
||||
public void set(int key, V value) {
|
||||
if (key >= this.currentSize) {
|
||||
this.expandCapacity();
|
||||
}
|
||||
|
||||
this.array[key] = value;
|
||||
}
|
||||
|
||||
|
|
@ -50,7 +64,7 @@ public final class IntObjectMap<V> implements RamMap {
|
|||
|
||||
@Override
|
||||
public long size() {
|
||||
return this.array.length;
|
||||
return this.maxSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -62,4 +76,15 @@ public final class IntObjectMap<V> implements RamMap {
|
|||
public void readFrom(DataInputStream buffer) throws IOException {
|
||||
throw new NotSupportException("IntObjectMap.readFrom");
|
||||
}
|
||||
|
||||
private synchronized void expandCapacity() {
|
||||
if (this.currentSize == this.maxSize) {
|
||||
return;
|
||||
}
|
||||
this.currentSize = Math.min(this.currentSize * 2, this.maxSize);
|
||||
Object[] newArray = new Object[this.currentSize];
|
||||
System.arraycopy(this.array, 0, newArray, 0, this.array.length);
|
||||
this.clear();
|
||||
this.array = newArray;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ public class SchemaIndexTransaction extends AbstractTransaction {
|
|||
return super.query(query);
|
||||
}
|
||||
IndexLabel il = IndexLabel.label(query.resultType());
|
||||
String name = (String) query.condition(HugeKeys.NAME);
|
||||
String name = query.condition(HugeKeys.NAME);
|
||||
E.checkState(name != null, "The name in condition can't be null " +
|
||||
"when querying schema by name");
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue