use of org.h2.value.CompareMode in project ignite by apache.
the class GridH2ValueCacheObject method compareSecure.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
protected int compareSecure(Value v, CompareMode mode) {
Object o1 = getObject();
Object o2 = v.getObject();
boolean o1Comparable = o1 instanceof Comparable;
boolean o2Comparable = o2 instanceof Comparable;
if (o1Comparable && o2Comparable && Utils.haveCommonComparableSuperclass(o1.getClass(), o2.getClass())) {
Comparable<Object> c1 = (Comparable<Object>) o1;
return c1.compareTo(o2);
}
// Group by types.
if (o1.getClass() != o2.getClass()) {
if (o1Comparable != o2Comparable)
return o1Comparable ? -1 : 1;
return o1.getClass().getName().compareTo(o2.getClass().getName());
}
// Compare hash codes.
int h1 = hashCode();
int h2 = v.hashCode();
if (h1 == h2) {
if (o1.equals(o2))
return 0;
return Utils.compareNotNullSigned(getBytesNoCopy(), v.getBytesNoCopy());
}
return h1 > h2 ? 1 : -1;
}
use of org.h2.value.CompareMode in project h2database by h2database.
the class Set method update.
@Override
public int update() {
Database database = session.getDatabase();
String name = SetTypes.getTypeName(type);
switch(type) {
case SetTypes.ALLOW_LITERALS:
{
session.getUser().checkAdmin();
int value = getIntValue();
if (value < 0 || value > 2) {
throw DbException.getInvalidValueException("ALLOW_LITERALS", getIntValue());
}
database.setAllowLiterals(value);
addOrUpdateSetting(name, null, value);
break;
}
case SetTypes.CACHE_SIZE:
if (getIntValue() < 0) {
throw DbException.getInvalidValueException("CACHE_SIZE", getIntValue());
}
session.getUser().checkAdmin();
database.setCacheSize(getIntValue());
addOrUpdateSetting(name, null, getIntValue());
break;
case SetTypes.CLUSTER:
{
if (Constants.CLUSTERING_ENABLED.equals(stringValue)) {
// ignore, as the cluster setting is checked later
break;
}
String value = StringUtils.quoteStringSQL(stringValue);
if (!value.equals(database.getCluster())) {
if (!value.equals(Constants.CLUSTERING_DISABLED)) {
// anybody can disable the cluster
// (if he can't access a cluster node)
session.getUser().checkAdmin();
}
database.setCluster(value);
// use the system session so that the current transaction
// (if any) is not committed
Session sysSession = database.getSystemSession();
synchronized (sysSession) {
synchronized (database) {
addOrUpdateSetting(sysSession, name, value, 0);
sysSession.commit(true);
}
}
}
break;
}
case SetTypes.COLLATION:
{
session.getUser().checkAdmin();
final boolean binaryUnsigned = database.getCompareMode().isBinaryUnsigned();
CompareMode compareMode;
StringBuilder buff = new StringBuilder(stringValue);
if (stringValue.equals(CompareMode.OFF)) {
compareMode = CompareMode.getInstance(null, 0, binaryUnsigned);
} else {
int strength = getIntValue();
buff.append(" STRENGTH ");
if (strength == Collator.IDENTICAL) {
buff.append("IDENTICAL");
} else if (strength == Collator.PRIMARY) {
buff.append("PRIMARY");
} else if (strength == Collator.SECONDARY) {
buff.append("SECONDARY");
} else if (strength == Collator.TERTIARY) {
buff.append("TERTIARY");
}
compareMode = CompareMode.getInstance(stringValue, strength, binaryUnsigned);
}
CompareMode old = database.getCompareMode();
if (old.equals(compareMode)) {
break;
}
Table table = database.getFirstUserTable();
if (table != null) {
throw DbException.get(ErrorCode.COLLATION_CHANGE_WITH_DATA_TABLE_1, table.getSQL());
}
addOrUpdateSetting(name, buff.toString(), 0);
database.setCompareMode(compareMode);
break;
}
case SetTypes.BINARY_COLLATION:
{
session.getUser().checkAdmin();
Table table = database.getFirstUserTable();
if (table != null) {
throw DbException.get(ErrorCode.COLLATION_CHANGE_WITH_DATA_TABLE_1, table.getSQL());
}
CompareMode currentMode = database.getCompareMode();
CompareMode newMode;
if (stringValue.equals(CompareMode.SIGNED)) {
newMode = CompareMode.getInstance(currentMode.getName(), currentMode.getStrength(), false);
} else if (stringValue.equals(CompareMode.UNSIGNED)) {
newMode = CompareMode.getInstance(currentMode.getName(), currentMode.getStrength(), true);
} else {
throw DbException.getInvalidValueException("BINARY_COLLATION", stringValue);
}
addOrUpdateSetting(name, stringValue, 0);
database.setCompareMode(newMode);
break;
}
case SetTypes.COMPRESS_LOB:
{
session.getUser().checkAdmin();
int algo = CompressTool.getCompressAlgorithm(stringValue);
database.setLobCompressionAlgorithm(algo == Compressor.NO ? null : stringValue);
addOrUpdateSetting(name, stringValue, 0);
break;
}
case SetTypes.CREATE_BUILD:
{
session.getUser().checkAdmin();
if (database.isStarting()) {
// just ignore the command if not starting
// this avoids problems when running recovery scripts
int value = getIntValue();
addOrUpdateSetting(name, null, value);
}
break;
}
case SetTypes.DATABASE_EVENT_LISTENER:
{
session.getUser().checkAdmin();
database.setEventListenerClass(stringValue);
break;
}
case SetTypes.DB_CLOSE_DELAY:
{
int x = getIntValue();
if (x == -1) {
// -1 is a special value for in-memory databases,
// which means "keep the DB alive and use the same
// DB for all connections"
} else if (x < 0) {
throw DbException.getInvalidValueException("DB_CLOSE_DELAY", x);
}
session.getUser().checkAdmin();
database.setCloseDelay(getIntValue());
addOrUpdateSetting(name, null, getIntValue());
break;
}
case SetTypes.DEFAULT_LOCK_TIMEOUT:
if (getIntValue() < 0) {
throw DbException.getInvalidValueException("DEFAULT_LOCK_TIMEOUT", getIntValue());
}
session.getUser().checkAdmin();
addOrUpdateSetting(name, null, getIntValue());
break;
case SetTypes.DEFAULT_TABLE_TYPE:
session.getUser().checkAdmin();
database.setDefaultTableType(getIntValue());
addOrUpdateSetting(name, null, getIntValue());
break;
case SetTypes.EXCLUSIVE:
{
session.getUser().checkAdmin();
int value = getIntValue();
switch(value) {
case 0:
database.setExclusiveSession(null, false);
break;
case 1:
database.setExclusiveSession(session, false);
break;
case 2:
database.setExclusiveSession(session, true);
break;
default:
throw DbException.getInvalidValueException("EXCLUSIVE", value);
}
break;
}
case SetTypes.JAVA_OBJECT_SERIALIZER:
{
session.getUser().checkAdmin();
Table table = database.getFirstUserTable();
if (table != null) {
throw DbException.get(ErrorCode.JAVA_OBJECT_SERIALIZER_CHANGE_WITH_DATA_TABLE, table.getSQL());
}
database.setJavaObjectSerializerName(stringValue);
addOrUpdateSetting(name, stringValue, 0);
break;
}
case SetTypes.IGNORECASE:
session.getUser().checkAdmin();
database.setIgnoreCase(getIntValue() == 1);
addOrUpdateSetting(name, null, getIntValue());
break;
case SetTypes.LOCK_MODE:
session.getUser().checkAdmin();
database.setLockMode(getIntValue());
addOrUpdateSetting(name, null, getIntValue());
break;
case SetTypes.LOCK_TIMEOUT:
if (getIntValue() < 0) {
throw DbException.getInvalidValueException("LOCK_TIMEOUT", getIntValue());
}
session.setLockTimeout(getIntValue());
break;
case SetTypes.LOG:
{
int value = getIntValue();
if (database.isPersistent() && value != database.getLogMode()) {
session.getUser().checkAdmin();
database.setLogMode(value);
}
break;
}
case SetTypes.MAX_LENGTH_INPLACE_LOB:
{
if (getIntValue() < 0) {
throw DbException.getInvalidValueException("MAX_LENGTH_INPLACE_LOB", getIntValue());
}
session.getUser().checkAdmin();
database.setMaxLengthInplaceLob(getIntValue());
addOrUpdateSetting(name, null, getIntValue());
break;
}
case SetTypes.MAX_LOG_SIZE:
if (getIntValue() < 0) {
throw DbException.getInvalidValueException("MAX_LOG_SIZE", getIntValue());
}
session.getUser().checkAdmin();
database.setMaxLogSize((long) getIntValue() * 1024 * 1024);
addOrUpdateSetting(name, null, getIntValue());
break;
case SetTypes.MAX_MEMORY_ROWS:
{
if (getIntValue() < 0) {
throw DbException.getInvalidValueException("MAX_MEMORY_ROWS", getIntValue());
}
session.getUser().checkAdmin();
database.setMaxMemoryRows(getIntValue());
addOrUpdateSetting(name, null, getIntValue());
break;
}
case SetTypes.MAX_MEMORY_UNDO:
{
if (getIntValue() < 0) {
throw DbException.getInvalidValueException("MAX_MEMORY_UNDO", getIntValue());
}
session.getUser().checkAdmin();
database.setMaxMemoryUndo(getIntValue());
addOrUpdateSetting(name, null, getIntValue());
break;
}
case SetTypes.MAX_OPERATION_MEMORY:
{
if (getIntValue() < 0) {
throw DbException.getInvalidValueException("MAX_OPERATION_MEMORY", getIntValue());
}
session.getUser().checkAdmin();
int value = getIntValue();
database.setMaxOperationMemory(value);
break;
}
case SetTypes.MODE:
Mode mode = Mode.getInstance(stringValue);
if (mode == null) {
throw DbException.get(ErrorCode.UNKNOWN_MODE_1, stringValue);
}
if (database.getMode() != mode) {
session.getUser().checkAdmin();
database.setMode(mode);
session.getColumnNamerConfiguration().configure(mode.getEnum());
}
break;
case SetTypes.MULTI_THREADED:
{
session.getUser().checkAdmin();
database.setMultiThreaded(getIntValue() == 1);
break;
}
case SetTypes.MVCC:
{
if (database.isMultiVersion() != (getIntValue() == 1)) {
throw DbException.get(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, "MVCC");
}
break;
}
case SetTypes.OPTIMIZE_REUSE_RESULTS:
{
session.getUser().checkAdmin();
database.setOptimizeReuseResults(getIntValue() != 0);
break;
}
case SetTypes.QUERY_TIMEOUT:
{
if (getIntValue() < 0) {
throw DbException.getInvalidValueException("QUERY_TIMEOUT", getIntValue());
}
int value = getIntValue();
session.setQueryTimeout(value);
break;
}
case SetTypes.REDO_LOG_BINARY:
{
int value = getIntValue();
session.setRedoLogBinary(value == 1);
break;
}
case SetTypes.REFERENTIAL_INTEGRITY:
{
session.getUser().checkAdmin();
int value = getIntValue();
if (value < 0 || value > 1) {
throw DbException.getInvalidValueException("REFERENTIAL_INTEGRITY", getIntValue());
}
database.setReferentialIntegrity(value == 1);
break;
}
case SetTypes.QUERY_STATISTICS:
{
session.getUser().checkAdmin();
int value = getIntValue();
if (value < 0 || value > 1) {
throw DbException.getInvalidValueException("QUERY_STATISTICS", getIntValue());
}
database.setQueryStatistics(value == 1);
break;
}
case SetTypes.QUERY_STATISTICS_MAX_ENTRIES:
{
session.getUser().checkAdmin();
int value = getIntValue();
if (value < 1) {
throw DbException.getInvalidValueException("QUERY_STATISTICS_MAX_ENTRIES", getIntValue());
}
database.setQueryStatisticsMaxEntries(value);
break;
}
case SetTypes.SCHEMA:
{
Schema schema = database.getSchema(stringValue);
session.setCurrentSchema(schema);
break;
}
case SetTypes.SCHEMA_SEARCH_PATH:
{
session.setSchemaSearchPath(stringValueList);
break;
}
case SetTypes.TRACE_LEVEL_FILE:
session.getUser().checkAdmin();
if (getCurrentObjectId() == 0) {
// don't set the property when opening the database
// this is for compatibility with older versions, because
// this setting was persistent
database.getTraceSystem().setLevelFile(getIntValue());
}
break;
case SetTypes.TRACE_LEVEL_SYSTEM_OUT:
session.getUser().checkAdmin();
if (getCurrentObjectId() == 0) {
// don't set the property when opening the database
// this is for compatibility with older versions, because
// this setting was persistent
database.getTraceSystem().setLevelSystemOut(getIntValue());
}
break;
case SetTypes.TRACE_MAX_FILE_SIZE:
{
if (getIntValue() < 0) {
throw DbException.getInvalidValueException("TRACE_MAX_FILE_SIZE", getIntValue());
}
session.getUser().checkAdmin();
int size = getIntValue() * 1024 * 1024;
database.getTraceSystem().setMaxFileSize(size);
addOrUpdateSetting(name, null, getIntValue());
break;
}
case SetTypes.THROTTLE:
{
if (getIntValue() < 0) {
throw DbException.getInvalidValueException("THROTTLE", getIntValue());
}
session.setThrottle(getIntValue());
break;
}
case SetTypes.UNDO_LOG:
{
int value = getIntValue();
if (value < 0 || value > 1) {
throw DbException.getInvalidValueException("UNDO_LOG", getIntValue());
}
session.setUndoLogEnabled(value == 1);
break;
}
case SetTypes.VARIABLE:
{
Expression expr = expression.optimize(session);
session.setVariable(stringValue, expr.getValue(session));
break;
}
case SetTypes.WRITE_DELAY:
{
if (getIntValue() < 0) {
throw DbException.getInvalidValueException("WRITE_DELAY", getIntValue());
}
session.getUser().checkAdmin();
database.setWriteDelay(getIntValue());
addOrUpdateSetting(name, null, getIntValue());
break;
}
case SetTypes.RETENTION_TIME:
{
if (getIntValue() < 0) {
throw DbException.getInvalidValueException("RETENTION_TIME", getIntValue());
}
session.getUser().checkAdmin();
database.setRetentionTime(getIntValue());
addOrUpdateSetting(name, null, getIntValue());
break;
}
case SetTypes.ROW_FACTORY:
{
session.getUser().checkAdmin();
String rowFactoryName = expression.getColumnName();
Class<RowFactory> rowFactoryClass = JdbcUtils.loadUserClass(rowFactoryName);
RowFactory rowFactory;
try {
rowFactory = rowFactoryClass.newInstance();
} catch (Exception e) {
throw DbException.convert(e);
}
database.setRowFactory(rowFactory);
break;
}
case SetTypes.BATCH_JOINS:
{
int value = getIntValue();
if (value != 0 && value != 1) {
throw DbException.getInvalidValueException("BATCH_JOINS", getIntValue());
}
session.setJoinBatchEnabled(value == 1);
break;
}
case SetTypes.FORCE_JOIN_ORDER:
{
int value = getIntValue();
if (value != 0 && value != 1) {
throw DbException.getInvalidValueException("FORCE_JOIN_ORDER", value);
}
session.setForceJoinOrder(value == 1);
break;
}
case SetTypes.LAZY_QUERY_EXECUTION:
{
int value = getIntValue();
if (value != 0 && value != 1) {
throw DbException.getInvalidValueException("LAZY_QUERY_EXECUTION", value);
}
session.setLazyQueryExecution(value == 1);
break;
}
case SetTypes.BUILTIN_ALIAS_OVERRIDE:
{
session.getUser().checkAdmin();
int value = getIntValue();
if (value != 0 && value != 1) {
throw DbException.getInvalidValueException("BUILTIN_ALIAS_OVERRIDE", value);
}
database.setAllowBuiltinAliasOverride(value == 1);
break;
}
case SetTypes.COLUMN_NAME_RULES:
{
session.getUser().checkAdmin();
session.getColumnNamerConfiguration().configure(expression.getColumnName());
break;
}
default:
DbException.throwInternalError("type=" + type);
}
// the meta data information has changed
database.getNextModificationDataId();
// query caches might be affected as well, for example
// when changing the compatibility mode
database.getNextModificationMetaId();
return 0;
}
use of org.h2.value.CompareMode in project h2database by h2database.
the class AggregateDataMedian method getValue.
@Override
Value getValue(Database database, int dataType, boolean distinct) {
Value[] a = getArray();
if (a == null) {
return ValueNull.INSTANCE;
}
final CompareMode mode = database.getCompareMode();
Arrays.sort(a, new Comparator<Value>() {
@Override
public int compare(Value o1, Value o2) {
return o1.compareTo(o2, mode);
}
});
int len = a.length;
int idx = len / 2;
Value v1 = a[idx];
if ((len & 1) == 1) {
return v1.convertTo(dataType);
}
return getMedian(a[idx - 1], v1, dataType, mode);
}
use of org.h2.value.CompareMode in project h2database by h2database.
the class AggregateDataHistogram method getValue.
@Override
Value getValue(Database database, int dataType, boolean distinct) {
if (distinct) {
count = 0;
groupDistinct(database, dataType);
}
ValueArray[] values = new ValueArray[distinctValues.size()];
int i = 0;
for (Value dv : distinctValues.keys()) {
AggregateDataHistogram d = distinctValues.get(dv);
values[i] = ValueArray.get(new Value[] { dv, ValueLong.get(d.count) });
i++;
}
final CompareMode compareMode = database.getCompareMode();
Arrays.sort(values, new Comparator<ValueArray>() {
@Override
public int compare(ValueArray v1, ValueArray v2) {
Value a1 = v1.getList()[0];
Value a2 = v2.getList()[0];
return a1.compareTo(a2, compareMode);
}
});
Value v = ValueArray.get(values);
return v.convertTo(dataType);
}
use of org.h2.value.CompareMode in project h2database by h2database.
the class PageStore method addMeta.
private void addMeta(Row row, Session session, boolean redo) {
int id = row.getValue(0).getInt();
int type = row.getValue(1).getInt();
int parent = row.getValue(2).getInt();
int rootPageId = row.getValue(3).getInt();
String[] options = StringUtils.arraySplit(row.getValue(4).getString(), ',', false);
String columnList = row.getValue(5).getString();
String[] columns = StringUtils.arraySplit(columnList, ',', false);
Index meta;
if (trace.isDebugEnabled()) {
trace.debug("addMeta id=" + id + " type=" + type + " root=" + rootPageId + " parent=" + parent + " columns=" + columnList);
}
if (redo && rootPageId != 0) {
// ensure the page is empty, but not used by regular data
writePage(rootPageId, createData());
allocatePage(rootPageId);
}
metaRootPageId.put(id, rootPageId);
if (type == META_TYPE_DATA_INDEX) {
CreateTableData data = new CreateTableData();
if (SysProperties.CHECK) {
if (columns == null) {
throw DbException.throwInternalError(row.toString());
}
}
for (int i = 0, len = columns.length; i < len; i++) {
Column col = new Column("C" + i, Value.INT);
data.columns.add(col);
}
data.schema = metaSchema;
data.tableName = "T" + id;
data.id = id;
data.temporary = options[2].equals("temp");
data.persistData = true;
data.persistIndexes = true;
data.create = false;
data.session = session;
RegularTable table = new RegularTable(data);
boolean binaryUnsigned = SysProperties.SORT_BINARY_UNSIGNED;
if (options.length > 3) {
binaryUnsigned = Boolean.parseBoolean(options[3]);
}
CompareMode mode = CompareMode.getInstance(options[0], Integer.parseInt(options[1]), binaryUnsigned);
table.setCompareMode(mode);
meta = table.getScanIndex(session);
} else {
Index p = metaObjects.get(parent);
if (p == null) {
throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "Table not found:" + parent + " for " + row + " meta:" + metaObjects);
}
RegularTable table = (RegularTable) p.getTable();
Column[] tableCols = table.getColumns();
int len = columns.length;
IndexColumn[] cols = new IndexColumn[len];
for (int i = 0; i < len; i++) {
String c = columns[i];
IndexColumn ic = new IndexColumn();
int idx = c.indexOf('/');
if (idx >= 0) {
String s = c.substring(idx + 1);
ic.sortType = Integer.parseInt(s);
c = c.substring(0, idx);
}
ic.column = tableCols[Integer.parseInt(c)];
cols[i] = ic;
}
IndexType indexType;
if (options[3].equals("d")) {
indexType = IndexType.createPrimaryKey(true, false);
Column[] tableColumns = table.getColumns();
for (IndexColumn indexColumn : cols) {
tableColumns[indexColumn.column.getColumnId()].setNullable(false);
}
} else {
indexType = IndexType.createNonUnique(true);
}
meta = table.addIndex(session, "I" + id, id, cols, indexType, false, null);
}
PageIndex index;
if (meta instanceof MultiVersionIndex) {
index = (PageIndex) ((MultiVersionIndex) meta).getBaseIndex();
} else {
index = (PageIndex) meta;
}
metaObjects.put(id, index);
}
Aggregations