Skip to content

Commit

Permalink
refactored fix for mulivalue manual index. issue #2741
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Jan 15, 2015
1 parent 5c11aac commit d354e28
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 151 deletions.
Expand Up @@ -35,7 +35,6 @@ public class ODefaultCollateFactory implements OCollateFactory {

static {
register(new ODefaultCollate());
register(new OMultivalueCollate());
register(new OCaseInsensitiveCollate());
}

Expand Down

This file was deleted.

@@ -0,0 +1,90 @@
package com.orientechnologies.orient.core.index;

import java.util.ArrayList;
import java.util.List;

import com.orientechnologies.orient.core.collate.OCollate;

public class OCompositeCollate implements OCollate {
/**
*
*/
private final OAbstractIndexDefinition oCompositeIndexDefinition;

/**
* @param oCompositeIndexDefinition
*/
public OCompositeCollate(OAbstractIndexDefinition oCompositeIndexDefinition) {
this.oCompositeIndexDefinition = oCompositeIndexDefinition;
}

private final List<OCollate> collates = new ArrayList<OCollate>();

public void addCollate(OCollate collate) {
collates.add(collate);
}

@Override
public String getName() {
throw new UnsupportedOperationException("getName");
}

@SuppressWarnings("unchecked")
@Override
public Object transform(Object obj) {
List<Object> keys = null;
if (obj instanceof OCompositeKey) {
final OCompositeKey compositeKey = (OCompositeKey) obj;
keys = compositeKey.getKeys();
} else if (obj instanceof List) {
keys = (List<Object>) obj;
} else {
throw new OIndexException("Impossible add as key of a CompositeIndex a value of type " + obj.getClass());
}

OCompositeKey transformedKey = new OCompositeKey();

final int size = Math.min(keys.size(), collates.size());
for (int i = 0; i < size; i++) {
final Object key = keys.get(i);

final OCollate collate = collates.get(i);
transformedKey.addKey(collate.transform(key));
}

for (int i = size; i < keys.size(); i++)
transformedKey.addKey(keys.get(i));

return transformedKey;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

OCompositeCollate that = (OCompositeCollate) o;

if (!collates.equals(that.collates))
return false;

return true;
}

@Override
public int hashCode() {
return collates.hashCode();
}

public List<OCollate> getCollates() {
return collates;
}

@Override
public String toString() {
return "OCompositeCollate{" + "collates=" + collates + ", null values ignored = "
+ this.oCompositeIndexDefinition.isNullValuesIgnored() + '}';
}
}
@@ -1,22 +1,22 @@
/*
*
* * Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
* *
* * Licensed 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.
* *
* * For more information: http://www.orientechnologies.com
*
*/
*
* * Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
* *
* * Licensed 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.
* *
* * For more information: http://www.orientechnologies.com
*
*/
package com.orientechnologies.orient.core.index;

import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -44,7 +44,7 @@ public class OCompositeIndexDefinition extends OAbstractIndexDefinition {
private final List<OIndexDefinition> indexDefinitions;
private String className;
private int multiValueDefinitionIndex = -1;
private OCompositeCollate collate = new OCompositeCollate();
private OCompositeCollate collate = new OCompositeCollate(this);

public OCompositeIndexDefinition() {
indexDefinitions = new ArrayList<OIndexDefinition>(5);
Expand Down Expand Up @@ -420,7 +420,7 @@ protected void fromStream() {

indexDefinitions.clear();

collate = new OCompositeCollate();
collate = new OCompositeCollate(this);

for (int i = 0; i < indClasses.size(); i++) {
final Class<?> clazz = Class.forName(indClasses.get(i));
Expand Down Expand Up @@ -545,71 +545,4 @@ private OCompositeKey convertToCompositeKey(Object key) {
public boolean isAutomatic() {
return indexDefinitions.get(0).isAutomatic();
}

private final class OCompositeCollate implements OCollate {
private final List<OCollate> collates = new ArrayList<OCollate>();

public void addCollate(OCollate collate) {
collates.add(collate);
}

@Override
public String getName() {
throw new UnsupportedOperationException("getName");
}

@SuppressWarnings("unchecked")
@Override
public Object transform(Object obj) {
List<Object> keys = null;
if (obj instanceof OCompositeKey) {
final OCompositeKey compositeKey = (OCompositeKey) obj;
keys = compositeKey.getKeys();
} else if (obj instanceof List) {
keys = (List<Object>) obj;
} else {
throw new OIndexException("Impossible add as key of a CompositeIndex a value of type " + obj.getClass());
}

OCompositeKey transformedKey = new OCompositeKey();

final int size = Math.min(keys.size(), collates.size());
for (int i = 0; i < size; i++) {
final Object key = keys.get(i);

final OCollate collate = collates.get(i);
transformedKey.addKey(collate.transform(key));
}

for (int i = size; i < keys.size(); i++)
transformedKey.addKey(keys.get(i));

return transformedKey;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

OCompositeCollate that = (OCompositeCollate) o;

if (!collates.equals(that.collates))
return false;

return true;
}

@Override
public int hashCode() {
return collates.hashCode();
}

@Override
public String toString() {
return "OCompositeCollate{" + "collates=" + collates + ", null values ignored = " + isNullValuesIgnored() + '}';
}
}
}
Expand Up @@ -25,7 +25,8 @@
import java.util.Collections;
import java.util.List;

import com.orientechnologies.orient.core.collate.OMultivalueCollate;
import com.orientechnologies.orient.core.collate.OCollate;
import com.orientechnologies.orient.core.collate.ODefaultCollate;
import com.orientechnologies.orient.core.db.record.ORecordElement;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
Expand All @@ -36,14 +37,29 @@ public class OSimpleKeyIndexDefinition extends OAbstractIndexDefinition {

public OSimpleKeyIndexDefinition(final OType... keyTypes) {
this.keyTypes = keyTypes;
if (keyTypes.length > 1)
collate = OSQLEngine.getCollate(OMultivalueCollate.NAME);

}

public OSimpleKeyIndexDefinition() {
}

public OSimpleKeyIndexDefinition(OType[] keyTypes2, List<OCollate> collatesList) {
this.keyTypes = keyTypes2;
if (keyTypes.length > 1) {
OCompositeCollate collate = new OCompositeCollate(this);
if (collatesList != null) {
for (OCollate oCollate : collatesList) {
collate.addCollate(oCollate);
}
} else {
for (OType type : keyTypes) {
collate.addCollate(OSQLEngine.getCollate(ODefaultCollate.NAME));
}
}
this.collate = collate;
}

}

public List<String> getFields() {
return Collections.emptyList();
}
Expand Down Expand Up @@ -99,8 +115,15 @@ public ODocument toStream() {
keyTypeNames.add(keyType.toString());

document.field("keyTypes", keyTypeNames, OType.EMBEDDEDLIST);
document.field("collate", collate.getName());
if (collate instanceof OCompositeCollate) {
List<String> collatesNames = new ArrayList<String>();
for (OCollate collate : ((OCompositeCollate) collate).getCollates())
collatesNames.add(collate.getName());
document.field("collates", collatesNames, OType.EMBEDDEDLIST);
} else
document.field("collate", collate.getName());
document.field("nullValuesIgnored", isNullValuesIgnored());

return document;
} finally {
document.setInternalStatus(ORecordElement.STATUS.LOADED);
Expand All @@ -117,8 +140,17 @@ protected void fromStream() {
keyTypes[i] = OType.valueOf(keyTypeName);
i++;
}
String collate = document.field("collate");
if (collate != null) {
setCollate(collate);
} else {
List<String> collatesNames = document.field("collates");
OCompositeCollate collates = new OCompositeCollate(this);
for (String collateName : collatesNames)
collates.addCollate(OSQLEngine.getCollate(collateName));
this.collate = collates;
}

setCollate((String) document.field("collate"));
setNullValuesIgnored(!Boolean.FALSE.equals(document.<Boolean> field("nullValuesIgnored")));
}

Expand Down

0 comments on commit d354e28

Please sign in to comment.