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

Fix line retainAll doesn't modify names and values array #110

Merged
merged 3 commits into from
Nov 26, 2019
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 @@ -104,7 +104,7 @@ public boolean hasNext() {
@Override
public Record<GE> next() {
Line line = this.reader.next();
Map<String, Object> keyValues = line.toMap();
Map<String, Object> keyValues = line.keyValues();
try {
keyValues = this.filterFields(keyValues);
GE element = this.build(keyValues);
Expand Down
67 changes: 34 additions & 33 deletions src/main/java/com/baidu/hugegraph/loader/reader/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,72 +22,73 @@
import java.util.Arrays;
import java.util.Map;

import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.InsertionOrderUtil;

public final class Line {

private final String rawLine;
private String[] names;
private Object[] values;
private Map<String, Object> keyValues;
private final Map<String, Object> keyValues;

public Line(String rawLine, Map<String, Object> keyValues) {
E.checkArgumentNotNull(rawLine, "The rawLine can't be null");
E.checkArgumentNotNull(keyValues, "The keyValues can't be null");
this.rawLine = rawLine;
Linary marked this conversation as resolved.
Show resolved Hide resolved
this.names = null;
this.values = null;
this.keyValues = keyValues;
this.names = getNames(keyValues);
this.values = getValues(keyValues, this.names);
}

public Line(String rawLine, String[] names, Object[] values) {
assert names.length == values.length;
E.checkArgumentNotNull(rawLine, "The rawLine can't be null");
E.checkArgumentNotNull(names, "The names can't be null");
E.checkArgumentNotNull(values, "The values can't be null");
E.checkArgument(names.length == values.length,
"The length of names %s should be same as values %s");
this.rawLine = rawLine;
this.names = names;
this.values = values;
this.keyValues = null;
this.keyValues = InsertionOrderUtil.newMap();
for (int i = 0; i < this.names.length; i++) {
this.keyValues.put(this.names[i], this.values[i]);
}
}

public String rawLine() {
return this.rawLine;
}

public final String[] names() {
if (this.names != null) {
return this.names;
} else {
assert this.keyValues != null;
return this.keyValues.keySet().toArray(new String[]{});
}
return this.names;
}

public final Object[] values() {
if (this.values != null) {
return this.values;
} else {
assert this.keyValues != null;
String[] names = this.names();
Object[] values = new Object[names.length];
for (int i = 0; i < names.length; i++) {
values[i] = this.keyValues.get(names[i]);
}
return values;
}
return this.values;
}

public Map<String, Object> toMap() {
if (this.keyValues != null) {
return this.keyValues;
}
String[] names = this.names();
Object[] values = this.values();
this.keyValues = InsertionOrderUtil.newMap();
for (int i = 0, n = names.length; i < n; i++) {
this.keyValues.put(names[i], values[i]);
}
public Map<String, Object> keyValues() {
return this.keyValues;
}

public void retainAll(String[] names) {
this.toMap().keySet().retainAll(Arrays.asList(names));
this.keyValues.keySet().retainAll(Arrays.asList(names));
this.names = getNames(keyValues);
this.values = getValues(keyValues, this.names);
}

private static String[] getNames(Map<String, Object> keyValues) {
return keyValues.keySet().toArray(new String[]{});
}

private static Object[] getValues(Map<String, Object> keyValues,
String[] names) {
Object[] values = new Object[names.length];
for (int i = 0; i < names.length; i++) {
values[i] = keyValues.get(names[i]);
}
return values;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public List<Line> nextBatch() throws SQLException {

String select = this.source.vendor().buildSelectSql(this.source,
this.nextStartRow);
LOG.debug("The sql for select is: {}", select);

List<Line> batch = new ArrayList<>(this.source.batchSize() + 1);
try (Statement stmt = this.conn.createStatement();
ResultSet result = stmt.executeQuery(select)) {
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/com/baidu/hugegraph/loader/test/unit/LineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.loader.test.unit;

import org.junit.Test;

import com.baidu.hugegraph.loader.reader.Line;
import com.baidu.hugegraph.testutil.Assert;
import com.google.common.collect.ImmutableMap;

public class LineTest {

@Test
public void testInvalidParam() {
Assert.assertThrows(IllegalArgumentException.class, () -> {
new Line(null, ImmutableMap.of("id", 1, "name", "marko"));
});
Assert.assertThrows(IllegalArgumentException.class, () -> {
new Line("1,marko", null);
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
new Line(null, new String[]{"id", "name"},
new Object[]{1, "marko"});
});
Assert.assertThrows(IllegalArgumentException.class, () -> {
new Line("1,marko", null, new Object[]{1, "marko"});
});
Assert.assertThrows(IllegalArgumentException.class, () -> {
new Line("1,marko", new String[]{"id", "name"}, null);
});
}

@Test
public void testKeyValues() {
Line line = new Line("1,marko,27",
new String[]{"id", "name", "age"},
new Object[]{1, "marko", 27});
Assert.assertEquals(ImmutableMap.of("id", 1, "name", "marko", "age", 27),
line.keyValues());
}

@Test
public void testRetainAll() {
Line line = new Line("1,marko,27",
new String[]{"id", "name", "age"},
new Object[]{1, "marko", 27});
line.retainAll(new String[]{"id"});
Assert.assertArrayEquals(new String[]{"id"}, line.names());
Assert.assertArrayEquals(new Object[]{1}, line.values());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

@RunWith(Suite.class)
@Suite.SuiteClasses({
LineTest.class,
LoadProgressTest.class
})
public class UnitTestSuite {
Expand Down