8000 Object item performance by ghislainfourny · Pull Request #704 · RumbleDB/rumble · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Object item performance #704

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions src/main/java/org/rumbledb/api/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.io.ObjectOutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
* An instance of this class is an item in the JSONiq data model.
Expand Down Expand Up @@ -219,6 +220,15 @@ public List<Item> getValues() {
throw new OurBadException(" Item '" + this.serialize() + "' is not an object.");
}

/**
* Returns the map represented by the item, if it is an object item.
*
* @return the map.
*/
public Map<String, Item> getAsMap() {
throw new OurBadException(" Item '" + this.serialize() + "' is not an object.");
}

/**
* Returns the value associated with a specific key, if it is an object item.
*
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/rumbledb/cli/JsoniqQueryExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ public List<Item> runQuery() throws IOException {
URI logUri = null;
if (logPath != null) {
logUri = FileSystemUtil.resolveURIAgainstWorkingDirectory(logPath, ExceptionMetadata.EMPTY_METADATA);
FileSystemUtil.delete(logUri, ExceptionMetadata.EMPTY_METADATA);
if (FileSystemUtil.exists(logUri, ExceptionMetadata.EMPTY_METADATA)) {
FileSystemUtil.delete(logUri, ExceptionMetadata.EMPTY_METADATA);
}
}

List<Item> outputList = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public DynamicContext visit(Node node, DynamicContext argument) {
@Override
public DynamicContext visitFunctionDeclaration(FunctionDeclaration declaration, DynamicContext argument) {
InlineFunctionExpression expression = (InlineFunctionExpression) declaration.getExpression();
Map<Name, SequenceType> paramNameToSequenceTypes = new LinkedHashMap<>();
Map<Name, SequenceType> paramNameToSequenceTypes = new LinkedHashMap<>(expression.getParams().size(), 1);
for (Map.Entry<Name, SequenceType> paramEntry : expression.getParams().entrySet()) {
paramNameToSequenceTypes.put(paramEntry.getKey(), paramEntry.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public RuntimeIterator visitContextExpr(ContextItemExpression expression, Runtim

@Override
public RuntimeIterator visitInlineFunctionExpr(InlineFunctionExpression expression, RuntimeIterator argument) {
Map<Name, SequenceType> paramNameToSequenceTypes = new LinkedHashMap<>();
Map<Name, SequenceType> paramNameToSequenceTypes = new LinkedHashMap<>(expression.getParams().size(), 1);
for (Map.Entry<Name, SequenceType> paramEntry : expression.getParams().entrySet()) {
paramNameToSequenceTypes.put(paramEntry.getKey(), paramEntry.getValue());
}
Expand Down Expand Up @@ -896,7 +896,7 @@ public RuntimeIterator visitConditionalExpression(ConditionalExpression expressi

@Override
public RuntimeIterator visitSwitchExpression(SwitchExpression expression, RuntimeIterator argument) {
Map<RuntimeIterator, RuntimeIterator> cases = new LinkedHashMap<>();
Map<RuntimeIterator, RuntimeIterator> cases = new LinkedHashMap<>(expression.getCases().size(), 1);
for (SwitchCase caseExpression : expression.getCases()) {
RuntimeIterator caseExpr = this.visit(caseExpression.getReturnExpression(), argument);
for (Expression conditionExpr : caseExpression.getConditionExpressions()) {
Expand Down Expand Up @@ -948,7 +948,7 @@ public RuntimeIterator visitTypeSwitchExpression(TypeSwitchExpression expression
@Override
public RuntimeIterator visitTryCatchExpression(TryCatchExpression expression, RuntimeIterator argument) {
System.out.println("Visiting!");
Map<String, RuntimeIterator> cases = new LinkedHashMap<>();
Map<String, RuntimeIterator> cases = new LinkedHashMap<>(expression.getErrorsCaught().size(), 1);
for (String code : expression.getErrorsCaught()) {
cases.put(
code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,12 @@ public Name parseName(JsoniqParser.QnameContext ctx, boolean isFunction) {
@Override
public Node visitFunctionDecl(JsoniqParser.FunctionDeclContext ctx) {
Name name = parseName(ctx.qname(), true);
Map<Name, SequenceType> fnParams = new LinkedHashMap<>();
SequenceType fnReturnType = MOST_GENERAL_SEQUENCE_TYPE;
Name paramName;
SequenceType paramType;
Map<Name, SequenceType> fnParams = null;
if (ctx.paramList() != null) {
fnParams = new LinkedHashMap<>(ctx.paramList().param().size(), 1);
for (JsoniqParser.ParamContext param : ctx.paramList().param()) {
paramName = parseName(param.qname(), false);
paramType = MOST_GENERAL_SEQUENCE_TYPE;
Expand All @@ -291,6 +292,8 @@ public Node visitFunctionDecl(JsoniqParser.FunctionDeclContext ctx) {
}
fnParams.put(paramName, paramType);
}
} else {
fnParams = new LinkedHashMap<>(1, 1);
}

if (ctx.return_type != null) {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/rumbledb/items/ItemFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.rumbledb.api.Item;
import org.rumbledb.exceptions.ExceptionMetadata;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -115,8 +114,8 @@ public Item createArrayItem(List<Item> items) {
return new ArrayItem(items);
}

public Item createObjectItem(List<String> keys, List<Item> values, ExceptionMetadata itemMetadata) {
return new ObjectItem(keys, values, itemMetadata);
public Item createObjectItem(LinkedHashMap<String, Item> content) {
return new ObjectItem(content);
}

public Item createObjectItem(Map<String, List<Item>> keyValuePairs) {
Expand Down
75 changes: 26 additions & 49 deletions src/main/java/org/rumbledb/items/ObjectItem.java
B4F0
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,26 @@
import com.esotericsoftware.kryo.io.Output;
import org.apache.commons.text.StringEscapeUtils;
import org.rumbledb.api.Item;
import org.rumbledb.exceptions.DuplicateObjectKeyException;
import org.rumbledb.exceptions.ExceptionMetadata;
import org.rumbledb.types.ItemType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ObjectItem extends JsonItem {


private static final long serialVersionUID = 1L;
private List<Item> values;
private List<String> keys;
private Map<String, Item> content;

public ObjectItem() {
super();
this.keys = new ArrayList<>();
this.values = new ArrayList<>();
this.content = new LinkedHashMap<>();
}

public ObjectItem(List<String> keys, List<Item> values, ExceptionMetadata itemMetadata) {
public ObjectItem(LinkedHashMap<String, Item> content) {
super();
checkForDuplicateKeys(keys, itemMetadata);
this.keys = keys;
this.values = values;
this.content = content;
}

/**
Expand All @@ -64,63 +58,50 @@ public ObjectItem(List<String> keys, List<Item> values, ExceptionMetadata itemMe
public ObjectItem(Map<String, List<Item>> keyValuePairs) {
super();

List<String> keyList = new ArrayList<>();
List<Item> valueList = new ArrayList<>();
this.content = new LinkedHashMap<>(keyValuePairs.size(), 1);
for (String key : keyValuePairs.keySet()) {
// add all keys to the keyList
keyList.add(key);
List<Item> values = keyValuePairs.get(key);
// for each key, convert the lists of values into arrayItems
if (values.size() > 1) {
Item valuesArray = ItemFactory.getInstance().createArrayItem(values);
valueList.add(valuesArray);
this.content.put(key, valuesArray);
} else if (values.size() == 1) {
Item value = values.get(0);
valueList.add(value);
this.content.put(key, value);
} else {
throw new RuntimeException("Unexpected list size found.");
}
}

this.keys = keyList;
this.values = valueList;
}

@Override
public List<String> getKeys() {
return this.keys;
return new ArrayList<String>(this.content.keySet());
}

@Override
public List<Item> getValues() {
return this.values;
return new ArrayList<Item>(this.content.values());
}

private void checkForDuplicateKeys(List<String> keys, ExceptionMetadata metadata) {
HashMap<String, Integer> frequencies = new HashMap<>();
for (String key : keys) {
if (frequencies.containsKey(key)) {
throw new DuplicateObjectKeyException(key, metadata);
} else {
frequencies.put(key, 1);
}
}
@Override
public Map<String, Item> getAsMap() {
return this.content;
}

@Override
public Item getItemByKey(String s) {
if (this.keys.contains(s)) {
return this.values.get(this.keys.indexOf(s));
if (this.content.containsKey(s)) {
return this.content.get(s);
} else {
return null;
}
}

@Override
public void putItemByKey(String s, Item value) {
this.keys.add(s);
this.values.add(value);
checkForDuplicateKeys(this.keys, ExceptionMetadata.EMPTY_METADATA);
this.content.put(s, value);
}

@Override
Expand All @@ -136,10 +117,13 @@ public boolean isTypeOf(ItemType type) {
@Override
public String serialize() {
StringBuilder sb = new StringBuilder();
sb.append("{ ");
for (int i = 0; i < this.keys.size(); ++i) {
String key = this.keys.get(i);
Item value = this.values.get(i);
sb.append("{");
String comma = " ";
for (String key : this.content.keySet()) {
sb.append(comma);
comma = ", ";

Item value = this.content.get(key);
boolean isStringValue = value.isString();
sb.append("\"").append(StringEscapeUtils.escapeJson(key)).append("\"").append(" : ");
if (isStringValue) {
Expand All @@ -150,27 +134,20 @@ public String serialize() {
sb.append(value.serialize());
}

if (i < this.keys.size() - 1) {
sb.append(", ");
} else {
sb.append(" ");
}
}
sb.append("}");
sb.append(" }");
return sb.toString();
}

@Override
public void write(Kryo kryo, Output output) {
kryo.writeObject(output, this.keys);
kryo.writeObject(output, this.values);
kryo.writeObject(output, this.content);
}

@SuppressWarnings("unchecked")
@Override
public void read(Kryo kryo, Input input) {
this.keys = kryo.readObject(input, ArrayList.class);
this.values = kryo.readObject(input, ArrayList.class);
this.content = kryo.readObject(input, LinkedHashMap.class);
}

public boolean equals(Object otherItem) {
Expand Down
Loading
0