提交 c7f23504 authored 作者: Thomas Mueller's avatar Thomas Mueller

A persistent multi-version map: move to the main source tree

上级 983b9fe9
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.dev.store.type;
import java.nio.ByteBuffer;
/**
* A data type.
*/
public interface DataType {
/**
* Compare two keys.
*
* @param a the first key
* @param b the second key
* @return -1 if the first key is smaller, 1 if larger, and 0 if equal
*/
int compare(Object a, Object b);
/**
* Get the maximum length in bytes used to store an object. In many cases,
* this method can be faster than calculating the exact length.
*
* @param obj the object
* @return the maximum length
*/
int getMaxLength(Object obj);
/**
* Estimate the used memory in bytes.
*
* @param obj the object
* @return the used memory
*/
int getMemory(Object obj);
/**
* Write the object.
*
* @param buff the target buffer
* @param obj the value
*/
void write(ByteBuffer buff, Object obj);
/**
* Read an object.
*
* @param buff the source buffer
* @return the object
*/
Object read(ByteBuffer buff);
/**
* Get the stable string representation that is used to build this data
* type.
* <p>
* To avoid conflict with the default factory, the returned string should
* start with the package name of the type factory.
*
* @return the string representation
*/
String asString();
}
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.dev.store.type;
/**
* A factory for maps and data types.
*/
public interface DataTypeFactory {
/**
* Set the parent factory.
*
* @param parent the parent factory
*/
void setParent(DataTypeFactory parent);
/**
* Parse the data type.
*
* @param dataType the string and type specific meta data
* @return the type, or null if unknown
*/
DataType buildDataType(String dataType);
/**
* Get the data type identifier for the given class.
* <p>
* To avoid conflict with the default factory, the returned string should
* start with the package name of the type factory.
*
* @param objectClass the class
* @return the data type identifier, or null if not supported
*/
String getDataType(Class<?> objectClass);
}
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, Version
* 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group
*/
package org.h2.dev.store.type;
import org.h2.dev.store.rtree.SpatialType;
/**
* A data type factory.
*/
public class ObjectTypeFactory implements DataTypeFactory {
@Override
public void setParent(DataTypeFactory parent) {
// never called for this factory
}
@Override
public DataType buildDataType(String s) {
if ("s".equals(s)) {
return SpatialType.fromString(s);
} else if ("o".equals(s)) {
return new ObjectType();
}
return null;
}
@Override
public String getDataType(Class<?> objectClass) {
if (objectClass == SpatialType.class) {
return "s";
}
return "o";
}
}
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.dev.store.type;
import java.nio.ByteBuffer;
import org.h2.dev.store.btree.DataUtils;
/**
* A string type.
*/
public class StringType implements DataType {
public static final StringType INSTANCE = new StringType();
public int compare(Object a, Object b) {
return a.toString().compareTo(b.toString());
}
public int getMaxLength(Object obj) {
return DataUtils.MAX_VAR_INT_LEN + 3 * obj.toString().length();
}
public int getMemory(Object obj) {
return 24 + 2 * obj.toString().length();
}
public String read(ByteBuffer buff) {
int len = DataUtils.readVarInt(buff);
return DataUtils.readString(buff, len);
}
public void write(ByteBuffer buff, Object obj) {
String s = obj.toString();
int len = s.length();
DataUtils.writeVarInt(buff, len);
DataUtils.writeStringData(buff, s, len);
}
public String asString() {
return "";
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, Version 1.0,
and under the Eclipse Public License, Version 1.0
(http://h2database.com/html/license.html).
Initial Developer: H2 Group
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title>
Javadoc package documentation
</title></head><body style="font: 9pt/130% Tahoma, Arial, Helvetica, sans-serif; font-weight: normal;"><p>
Data types and serialization / deserialization
</p></body></html>
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论