Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BackendEntry.columns() var-arg to single-arg avoid array-construct #1787

Merged
merged 3 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ public void columns(Collection<BackendColumn> bytesColumns) {
}

@Override
public void columns(BackendColumn... bytesColumns) {
this.columns.addAll(Arrays.asList(bytesColumns));
public void columns(BackendColumn bytesColumn) {
this.columns.add(bytesColumn);
long maxSize = BackendEntryIterator.INLINE_BATCH_SIZE;
E.checkState(this.columns.size() <= maxSize,
"Too many columns in one entry: %s", maxSize);
if (this.columns.size() > maxSize) {
E.checkState(false, "Too many columns in one entry: %s", maxSize);
}
}

public BackendColumn removeColumn(int index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public void olap(boolean olap) {
this.olap = olap;
}

@Override
public boolean olap() {
return this.olap;
}
Expand Down Expand Up @@ -263,7 +264,7 @@ public void columns(Collection<BackendEntry.BackendColumn> bytesColumns) {
}

@Override
public void columns(BackendEntry.BackendColumn... bytesColumns) {
public void columns(BackendEntry.BackendColumn bytesColumn) {
throw new NotImplementedException("Not supported by table backend");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,9 @@ public void columns(Collection<BackendColumn> bytesColumns) {
}

@Override
public void columns(BackendColumn... bytesColumns) {
for (BackendColumn column : bytesColumns) {
this.columns.put(StringEncoding.decode(column.name),
StringEncoding.decode(column.value));
}
public void columns(BackendColumn bytesColumn) {
this.columns.put(StringEncoding.decode(bytesColumn.name),
StringEncoding.decode(bytesColumn.value));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package com.baidu.hugegraph.backend.store;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -87,7 +86,7 @@ public boolean equals(Object obj) {
public Collection<BackendColumn> columns();

public void columns(Collection<BackendColumn> columns);
public void columns(BackendColumn... columns);
public void columns(BackendColumn column);

public void merge(BackendEntry other);
public boolean mergable(BackendEntry other);
Expand All @@ -104,6 +103,7 @@ public default boolean olap() {

public interface BackendIterator<T> extends Iterator<T>, AutoCloseable {

@Override
public void close();

public byte[] position();
Expand All @@ -116,7 +116,18 @@ public static BackendColumnIterator empty() {
return EMPTY;
}

public final BackendColumnIterator EMPTY = new BackendColumnIterator() {
public static BackendColumnIterator iterator(BackendColumn element) {
return new OneColumnIterator(element);
}

public static BackendColumnIterator wrap(Iterator<BackendColumn> iter) {
return new BackendColumnIteratorWrapper(iter);
}

public static final BackendColumnIterator EMPTY = new EmptyIterator();

public static final class EmptyIterator
implements BackendColumnIterator {

@Override
public boolean hasNext() {
Expand All @@ -137,41 +148,74 @@ public void close() {
public byte[] position() {
return null;
}
};
}
}

public static class BackendColumnIteratorWrapper
implements BackendColumnIterator {
public static final class OneColumnIterator
implements BackendColumnIterator {

private final Iterator<BackendColumn> iter;
private BackendColumn element;

public BackendColumnIteratorWrapper(BackendColumn... cols) {
this.iter = Arrays.asList(cols).iterator();
}
public OneColumnIterator(BackendColumn element) {
assert element != null;
this.element = element;
}

public BackendColumnIteratorWrapper(Iterator<BackendColumn> cols) {
E.checkNotNull(cols, "cols");
this.iter = cols;
}
@Override
public boolean hasNext() {
return this.element != null;
}

@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public BackendColumn next() {
if (this.element == null) {
throw new NoSuchElementException();
}
BackendColumn result = this.element;
this.element = null;
return result;
}

@Override
public BackendColumn next() {
return iter.next();
}
@Override
public void close() {
// pass
}

@Override
public void close() {
WrappedIterator.close(this.iter);
@Override
public byte[] position() {
return null;
}
}

@Override
public byte[] position() {
return null;
public static final class BackendColumnIteratorWrapper
implements BackendColumnIterator {

private final Iterator<BackendColumn> iter;

public BackendColumnIteratorWrapper(Iterator<BackendColumn> iter) {
E.checkNotNull(iter, "iter");
this.iter = iter;
}

@Override
public boolean hasNext() {
return this.iter.hasNext();
}

@Override
public BackendColumn next() {
return this.iter.next();
}

@Override
public void close() {
WrappedIterator.close(this.iter);
}

@Override
public byte[] position() {
// No paging position for discrete iterators like queryByIds()
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import com.baidu.hugegraph.backend.store.BackendEntry;
import com.baidu.hugegraph.backend.store.BackendEntry.BackendColumn;
import com.baidu.hugegraph.backend.store.BackendEntry.BackendColumnIterator;
import com.baidu.hugegraph.backend.store.BackendEntry.BackendColumnIteratorWrapper;
import com.baidu.hugegraph.backend.store.BackendEntryIterator;
import com.baidu.hugegraph.backend.store.BackendTable;
import com.baidu.hugegraph.backend.store.Shard;
Expand Down Expand Up @@ -199,7 +198,7 @@ protected BackendColumnIterator queryByIds(Session session,
}

// NOTE: this will lead to lazy create rocksdb iterator
return new BackendColumnIteratorWrapper(new FlatMapperIterator<>(
return BackendColumnIterator.wrap(new FlatMapperIterator<>(
ids.iterator(), id -> this.queryById(session, id)
));
}
Expand All @@ -210,7 +209,7 @@ protected BackendColumnIterator getById(Session session, Id id) {
return BackendColumnIterator.empty();
}
BackendColumn col = BackendColumn.of(id.asBytes(), value);
return new BackendEntry.BackendColumnIteratorWrapper(col);
return BackendColumnIterator.iterator(col);
}

protected BackendColumnIterator getByIds(Session session, Set<Id> ids) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,54 @@ public class RocksDBPerfTest extends BaseRocksDBUnitTest {

private static final int TIMES = 10000 * 1000;

@Test
public void testSeekExistKey() throws RocksDBException {
put("exist", "value");

Session session = this.rocks.session();
for (int i = 0; i < TIMES; i++) {
Iterator<BackendColumn> iter = session.scan(TABLE, b("exist"));
while (iter.hasNext()) {
iter.next();
}
}
}

@Test
public void testSeekNonExistKey() throws RocksDBException {
put("exist", "value");

Session session = this.rocks.session();
for (int i = 0; i < TIMES; i++) {
Iterator<BackendColumn> iter = session.scan(TABLE, b("non-exist"));
while (iter.hasNext()) {
iter.next();
}
}
}

@Test
public void testGetExistKey() throws RocksDBException {
put("exist", "value");

Session session = this.rocks.session();
for (int i = 0; i < TIMES; i++) {
byte[] value = session.get(TABLE, b("exist"));
assert value.length == "value".length();
}
}

@Test
public void testGetNonExistKey() throws RocksDBException {
put("exist", "value");

Session session = this.rocks.session();
for (int i = 0; i < TIMES; i++) {
byte[] value = session.get(TABLE, b("non-exist"));
assert value == null;
}
}

@Test
public void testPut() throws RocksDBException {
for (int i = 0; i < TIMES; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ private static BackendEntry parse(BackendEntry originEntry) {
byte[] bytes = originEntry.id().asBytes();
BackendEntry parsedEntry = new BinaryBackendEntry(originEntry.type(),
bytes);
for (BackendEntry.BackendColumn col : originEntry.columns()) {
parsedEntry.columns(col);
}
parsedEntry.columns(originEntry.columns());
return parsedEntry;
}
}