From b681523045fe010358dc7d2f8a1422c08b34d460 Mon Sep 17 00:00:00 2001 From: jadepeng Date: Wed, 2 Nov 2022 16:50:22 +0800 Subject: [PATCH] refactor(core): improve code by flow idea suggests --- .../hugegraph/backend/BackendException.java | 3 +- .../hugegraph/backend/cache/CacheManager.java | 2 +- .../backend/cache/CachedGraphTransaction.java | 1 - .../cache/CachedSchemaTransaction.java | 51 ++++++-------- .../hugegraph/backend/cache/OffheapCache.java | 4 +- .../hugegraph/backend/cache/RamCache.java | 69 ++++++++++--------- .../hugegraph/backend/id/IdGenerator.java | 16 ++--- .../baidu/hugegraph/backend/id/IdUtil.java | 4 +- .../backend/id/SnowflakeIdGenerator.java | 5 +- .../hugegraph/backend/page/QueryList.java | 4 +- .../hugegraph/backend/query/Condition.java | 12 ++-- .../backend/query/ConditionQuery.java | 8 +-- .../backend/query/ConditionQueryFlatten.java | 2 +- .../baidu/hugegraph/backend/query/Query.java | 7 +- .../serializer/AbstractSerializer.java | 4 +- .../serializer/BinaryEntryIterator.java | 2 +- .../backend/serializer/BinarySerializer.java | 2 +- .../backend/serializer/BytesBuffer.java | 4 +- .../backend/serializer/SerializerFactory.java | 2 +- .../backend/serializer/TableBackendEntry.java | 2 +- .../backend/store/ram/IntObjectMap.java | 31 ++++++++- .../backend/tx/SchemaIndexTransaction.java | 2 +- 22 files changed, 122 insertions(+), 115 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/BackendException.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/BackendException.java index 18c55f9750..ce60d12cd2 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/BackendException.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/BackendException.java @@ -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); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CacheManager.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CacheManager.java index 8090815d54..1963a268f4 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CacheManager.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CacheManager.java @@ -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; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java index 6ba16dca96..7a2a852cce 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java @@ -345,7 +345,6 @@ protected Iterator 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()); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedSchemaTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedSchemaTransaction.java index a145b53fad..07eac8a8b6 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedSchemaTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedSchemaTransaction.java @@ -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) { @@ -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); @@ -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 diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/OffheapCache.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/OffheapCache.java index cc3ccf05c9..8354487952 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/OffheapCache.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/OffheapCache.java @@ -363,7 +363,7 @@ private enum ValueType { DATE(DataType.DATE), UUID(DataType.UUID); - private DataType dataType; + private final DataType dataType; ValueType() { this(DataType.UNKNOWN); @@ -390,7 +390,7 @@ public static ValueType valueOf(int index) { public static ValueType valueOf(Object object) { E.checkNotNull(object, "object"); - Class clazz = object.getClass(); + Class clazz = object.getClass(); if (Collection.class.isAssignableFrom(clazz)) { return ValueType.LIST; } else if (HugeVertex.class.isAssignableFrom(clazz)) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/RamCache.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/RamCache.java index d4c55d4d26..459de43e80 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/RamCache.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/RamCache.java @@ -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 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 node = this.map.get(id); @@ -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 { @@ -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 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 extends CacheNode { private LinkNode prev; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/IdGenerator.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/IdGenerator.java index a6b0b829b1..4c93e8a21d 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/IdGenerator.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/IdGenerator.java @@ -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 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); @@ -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)); @@ -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()); @@ -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; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/IdUtil.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/IdUtil.java index 60111f57c2..befc7485b9 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/IdUtil.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/IdUtil.java @@ -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) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/SnowflakeIdGenerator.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/SnowflakeIdGenerator.java index b597e59e0e..615e0e4ee4 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/SnowflakeIdGenerator.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/SnowflakeIdGenerator.java @@ -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; @@ -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); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/QueryList.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/QueryList.java index a72ef072d5..1f5dd533da 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/QueryList.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/QueryList.java @@ -52,7 +52,7 @@ protected Query parent() { return this.parent; } - protected QueryResults.Fetcher fetcher() { + private QueryResults.Fetcher fetcher() { return this.fetcher; } @@ -97,7 +97,7 @@ public QueryResults 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 fetchNext(PageInfo pageInfo, long pageSize) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Condition.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Condition.java index fbeddaa02a..1e66324401 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Condition.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Condition.java @@ -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) { @@ -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, @@ -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; } @@ -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); @@ -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); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java index 6ccfbc025d..0def61cf28 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java @@ -286,13 +286,7 @@ public T condition(Object key) { } public void unsetCondition(Object key) { - for (Iterator 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) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQueryFlatten.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQueryFlatten.java index d9639b36ea..c0bd718df0 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQueryFlatten.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQueryFlatten.java @@ -186,7 +186,7 @@ private static Condition convTextContainsAny2Or(Relation relation) { assert relation.relation() == Condition.RelationType.TEXT_CONTAINS_ANY; @SuppressWarnings("unchecked") Collection words = (Collection) relation.value(); - Condition cond = null; + Condition cond; Condition conds = null; for (String word : words) { assert relation.key() instanceof Id; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java index 397b313b52..317699ebfc 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java @@ -522,7 +522,7 @@ public String toString() { 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 static long defaultCapacity() { 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 static final void checkForceCapacity(long count) public enum Order { ASC, - DESC; + DESC } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/AbstractSerializer.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/AbstractSerializer.java index 56e75fc7ac..47245c22c7 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/AbstractSerializer.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/AbstractSerializer.java @@ -31,12 +31,14 @@ 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) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinaryEntryIterator.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinaryEntryIterator.java index c4f87516bf..e75ad6d056 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinaryEntryIterator.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinaryEntryIterator.java @@ -129,7 +129,7 @@ private void removeLastRecord() { ((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 diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java index 4702d3a978..aa4093c10f 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java @@ -924,7 +924,7 @@ protected static boolean indexFieldValuesUnmatched(byte[] value, return false; } - public static final byte[] increaseOne(byte[] bytes) { + public static byte[] increaseOne(byte[] bytes) { final byte BYTE_MAX_VALUE = (byte) 0xff; assert bytes.length > 0; byte last = bytes[bytes.length - 1]; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BytesBuffer.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BytesBuffer.java index 7caba21716..4cda2cb33a 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BytesBuffer.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BytesBuffer.java @@ -123,7 +123,7 @@ public ByteBuffer asByteBuffer() { } public BytesBuffer forReadWritten() { - ((Buffer) this.buffer).flip(); + this.buffer.flip(); return this; } @@ -263,7 +263,7 @@ public byte[] read(int length) { } public boolean readBoolean() { - return this.buffer.get() == 0 ? false : true; + return this.buffer.get() != 0; } public char readChar() { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/SerializerFactory.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/SerializerFactory.java index 56a6956bb1..995c5b4dac 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/SerializerFactory.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/SerializerFactory.java @@ -27,7 +27,7 @@ public class SerializerFactory { - private static Map> serializers; + private static final Map> serializers; static { serializers = new ConcurrentHashMap<>(); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableBackendEntry.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableBackendEntry.java index 6d1ecd9843..cfa9b169e8 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableBackendEntry.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableBackendEntry.java @@ -43,7 +43,7 @@ public static class Row { private HugeType type; private Id id; private Id subId; - private Map columns; + private final Map columns; private long ttl; public Row(HugeType type) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/ram/IntObjectMap.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/ram/IntObjectMap.java index 22d4ebc7dc..72b379c16e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/ram/IntObjectMap.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/ram/IntObjectMap.java @@ -28,18 +28,32 @@ public final class IntObjectMap 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 void clear() { @Override public long size() { - return this.array.length; + return this.maxSize; } @Override @@ -62,4 +76,15 @@ public void writeTo(DataOutputStream buffer) throws IOException { 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; + } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaIndexTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaIndexTransaction.java index e65c20be05..153d730e34 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaIndexTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaIndexTransaction.java @@ -86,7 +86,7 @@ private QueryResults queryByName(ConditionQuery query) { 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");