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

BREAKING-CHANGES: insertLong(), insertString() etc, moved into public inner class + ColumnType enum renamed #97

Merged
merged 11 commits into from
Sep 3, 2013
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.tightdb.examples.quickbenchmark;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we move this to /experiment folder, since all under examples are distributed to customers.
It would also be good here to test the difference between add() and the local insert() methods.


import java.util.Date;

import com.tightdb.ColumnType;
import com.tightdb.Table;

public class InsertPerformance {

public static void main(String[] args) {

Table t = new Table();


t.addColumn(ColumnType.STRING, "String");
t.addColumn(ColumnType.BOOLEAN, "Bool");
t.addColumn(ColumnType.LONG, "Long");
t.addColumn(ColumnType.DATE, "Date");

Long timer = System.currentTimeMillis();

System.out.println("Performance test for inserting values in table:");

for (int i=0;i<50000000;i++){

t.add("String", false, 4000L, new Date());

if (i % 1000000 == 0 && i > 0){
System.out.println(i + " split time: " + (System.currentTimeMillis() - timer));
}
}

System.out.println("Total time in miliseconds: " + (System.currentTimeMillis() - timer));
}
}
18 changes: 9 additions & 9 deletions tightdb-java-core/src/main/java/com/tightdb/ColumnType.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
// FIXME: Add a unit test that verifies the correct correspondance.

public enum ColumnType {
ColumnTypeBool(1),
ColumnTypeInt(0),
ColumnTypeFloat(9),
ColumnTypeDouble(10),
ColumnTypeString(2),
ColumnTypeBinary(4),
ColumnTypeDate(7),
ColumnTypeTable(5),
ColumnTypeMixed(6);
BOOLEAN(1),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should consider to make the Enums with only first letter Uppercase, and the rest lowercase. That way, it's the same as you write in the typed interface.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is, Enums are constants in java. The convention is to use upper case. See:
http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

It's correct that it differs from the typed interface differs anyway. But the typed interface is a different animal and also supports simple types e.g. int and long instead of Integer and Long.

LONG(0),
FLOAT(9),
DOUBLE(10),
STRING(2),
BINARY(4),
DATE(7),
TABLE(5),
MIXED(6);
// When adding above, remember to update size of largest number below

private final int nativeValue;
Expand Down
18 changes: 9 additions & 9 deletions tightdb-java-core/src/main/java/com/tightdb/Mixed.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Mixed(double value) {

public Mixed(ColumnType columnType) {
// It's actually ok to call with any columnType - it will however be assumed to be a ColumnTypeTable.
assert (columnType == null || columnType == ColumnType.ColumnTypeTable);
assert (columnType == null || columnType == ColumnType.TABLE);
this.value = null;
}

Expand Down Expand Up @@ -99,22 +99,22 @@ public boolean equals(Object second) {

public ColumnType getType() {
if (value == null) {
return ColumnType.ColumnTypeTable;
return ColumnType.TABLE;
}
if (value instanceof String)
return ColumnType.ColumnTypeString;
return ColumnType.STRING;
else if (value instanceof Long)
return ColumnType.ColumnTypeInt;
return ColumnType.LONG;
else if (value instanceof Float)
return ColumnType.ColumnTypeFloat;
return ColumnType.FLOAT;
else if (value instanceof Double)
return ColumnType.ColumnTypeDouble;
return ColumnType.DOUBLE;
else if (value instanceof Date)
return ColumnType.ColumnTypeDate;
return ColumnType.DATE;
else if (value instanceof Boolean)
return ColumnType.ColumnTypeBool;
return ColumnType.BOOLEAN;
else if (value instanceof ByteBuffer || (value instanceof byte[])) {
return ColumnType.ColumnTypeBinary;
return ColumnType.BINARY;
}

throw new IllegalStateException("Unknown column type!");
Expand Down
165 changes: 100 additions & 65 deletions tightdb-java-core/src/main/java/com/tightdb/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,42 +360,44 @@ public void insert(long rowIndex, Object... values) {
for (long columnIndex = 0; columnIndex < columns; columnIndex++) {
Object value = values[(int)columnIndex];
switch (colTypes[(int)columnIndex]) {
case ColumnTypeBool:
case BOOLEAN:
nativeInsertBoolean(nativePtr, columnIndex, rowIndex, (Boolean)value);
break;
case ColumnTypeInt:
case LONG:
nativeInsertLong(nativePtr, columnIndex, rowIndex, ((Number)value).longValue());
break;
case ColumnTypeFloat:
case FLOAT:
nativeInsertFloat(nativePtr, columnIndex, rowIndex, ((Float)value).floatValue());
break;
case ColumnTypeDouble:
case DOUBLE:
nativeInsertDouble(nativePtr, columnIndex, rowIndex, ((Double)value).doubleValue());
break;
case ColumnTypeString:
case STRING:
nativeInsertString(nativePtr, columnIndex, rowIndex, (String)value);
break;
case ColumnTypeDate:
case DATE:
nativeInsertDate(nativePtr, columnIndex, rowIndex, ((Date)value).getTime()/1000);
break;
case ColumnTypeMixed:
case MIXED:
nativeInsertMixed(nativePtr, columnIndex, rowIndex, Mixed.mixedValue(value));
break;
case ColumnTypeBinary:
case BINARY:
if (value instanceof byte[])
nativeInsertByteArray(nativePtr, columnIndex, rowIndex, (byte[])value);
else if (value instanceof ByteBuffer)
nativeInsertByteBuffer(nativePtr, columnIndex, rowIndex, (ByteBuffer)value);
break;
case ColumnTypeTable:
case TABLE:
nativeInsertSubTable(nativePtr, columnIndex, rowIndex);
insertSubtableValues(rowIndex, columnIndex, value);
break;
default:
throw new RuntimeException("Unexpected columnType: " + String.valueOf(colTypes[(int)columnIndex]));
}
}
insertDone();
//Insert done. Use native, no need to check for immutable again here
nativeInsertDone(nativePtr);

}

private void insertSubtableValues(long rowIndex, long columnIndex, Object value) {
Expand Down Expand Up @@ -445,88 +447,121 @@ public void set(long rowIndex, Object... values) {
remove(rowIndex);
insert(rowIndex, values);
}

//Instance of the inner class InternalMethods.
private InternalMethods internal = new InternalMethods();

//Returns InternalMethods instance with public internal methods. Should only be called by AbstractTable
public InternalMethods getInternalMethods(){
return this.internal;
}


//Holds methods that must be publicly available for AbstractClass.
//Should not be called when using the dynamic interface. The methods can be accessed by calling getInternalMethods() in Table class
public class InternalMethods{

public void insertLong(long columnIndex, long rowIndex, long value) {
if (immutable) throwImmutable();
nativeInsertLong(nativePtr, columnIndex, rowIndex, value);
}

public void insertDouble(long columnIndex, long rowIndex, double value) {
if (immutable) throwImmutable();
nativeInsertDouble(nativePtr, columnIndex, rowIndex, value);
}

public void insertFloat(long columnIndex, long rowIndex, float value) {
if (immutable) throwImmutable();
nativeInsertFloat(nativePtr, columnIndex, rowIndex, value);
}

public void insertBoolean(long columnIndex, long rowIndex, boolean value) {
if (immutable) throwImmutable();
nativeInsertBoolean(nativePtr, columnIndex, rowIndex, value);
}

public void insertDate(long columnIndex, long rowIndex, Date date) {
if (immutable) throwImmutable();
nativeInsertDate(nativePtr, columnIndex, rowIndex, date.getTime()/1000);
}

public void insertString(long columnIndex, long rowIndex, String value) {
if (immutable) throwImmutable();
nativeInsertString(nativePtr, columnIndex, rowIndex, value);
}

public void insertMixed(long columnIndex, long rowIndex, Mixed data) {
if (immutable) throwImmutable();
nativeInsertMixed(nativePtr, columnIndex, rowIndex, data);
}

public void insertBinary(long columnIndex, long rowIndex, ByteBuffer data) {
if (immutable) throwImmutable();
//System.err.printf("\ninsertBinary(col %d, row %d, ByteBuffer)\n", columnIndex, rowIndex);
//System.err.println("-- HasArray: " + (data.hasArray() ? "yes":"no") + " len= " + data.array().length);
if (data.isDirect())
nativeInsertByteBuffer(nativePtr, columnIndex, rowIndex, data);
else
throw new RuntimeException("Currently ByteBuffer must be allocateDirect()."); // FIXME: support other than allocateDirect
}

public void insertBinary(long columnIndex, long rowIndex, byte[] data) {
if (immutable) throwImmutable();
nativeInsertByteArray(nativePtr, columnIndex, rowIndex, data);
}

public void insertSubTable(long columnIndex, long rowIndex, Object[][] values) {
if (immutable) throwImmutable();
nativeInsertSubTable(nativePtr, columnIndex, rowIndex);
insertSubtableValues(rowIndex, columnIndex, values);
}

public void insertDone() {
if (immutable) throwImmutable();
nativeInsertDone(nativePtr);
}
}


public void insertLong(long columnIndex, long rowIndex, long value) {
if (immutable) throwImmutable();
nativeInsertLong(nativePtr, columnIndex, rowIndex, value);
}


protected native void nativeInsertFloat(long nativeTablePtr, long columnIndex, long rowIndex, float value);

public void insertFloat(long columnIndex, long rowIndex, float value) {
if (immutable) throwImmutable();
nativeInsertFloat(nativePtr, columnIndex, rowIndex, value);
}


protected native void nativeInsertDouble(long nativeTablePtr, long columnIndex, long rowIndex, double value);

public void insertDouble(long columnIndex, long rowIndex, double value) {
if (immutable) throwImmutable();
nativeInsertDouble(nativePtr, columnIndex, rowIndex, value);
}


protected native void nativeInsertLong(long nativeTablePtr, long columnIndex, long rowIndex, long value);

public void insertBoolean(long columnIndex, long rowIndex, boolean value) {
if (immutable) throwImmutable();
nativeInsertBoolean(nativePtr, columnIndex, rowIndex, value);
}


protected native void nativeInsertBoolean(long nativeTablePtr, long columnIndex, long rowIndex, boolean value);

public void insertDate(long columnIndex, long rowIndex, Date date) {
if (immutable) throwImmutable();
nativeInsertDate(nativePtr, columnIndex, rowIndex, date.getTime()/1000);
}


protected native void nativeInsertDate(long nativePtr, long columnIndex, long rowIndex, long dateTimeValue);

public void insertString(long columnIndex, long rowIndex, String value) {
if (immutable) throwImmutable();
nativeInsertString(nativePtr, columnIndex, rowIndex, value);
}


protected native void nativeInsertString(long nativeTablePtr, long columnIndex, long rowIndex, String value);

public void insertMixed(long columnIndex, long rowIndex, Mixed data) {
if (immutable) throwImmutable();
nativeInsertMixed(nativePtr, columnIndex, rowIndex, data);
}


protected native void nativeInsertMixed(long nativeTablePtr, long columnIndex, long rowIndex, Mixed mixed);

public void insertBinary(long columnIndex, long rowIndex, ByteBuffer data) {
if (immutable) throwImmutable();
//System.err.printf("\ninsertBinary(col %d, row %d, ByteBuffer)\n", columnIndex, rowIndex);
//System.err.println("-- HasArray: " + (data.hasArray() ? "yes":"no") + " len= " + data.array().length);
if (data.isDirect())
nativeInsertByteBuffer(nativePtr, columnIndex, rowIndex, data);
else
throw new RuntimeException("Currently ByteBuffer must be allocateDirect()."); // FIXME: support other than allocateDirect
}


protected native void nativeInsertByteBuffer(long nativeTablePtr, long columnIndex, long rowIndex, ByteBuffer data);

public void insertBinary(long columnIndex, long rowIndex, byte[] data) {
if (immutable) throwImmutable();
nativeInsertByteArray(nativePtr, columnIndex, rowIndex, data);
}


protected native void nativeInsertByteArray(long nativePtr, long columnIndex, long rowIndex, byte[] data);

public void insertSubTable(long columnIndex, long rowIndex, Object[][] values) {
if (immutable) throwImmutable();
nativeInsertSubTable(nativePtr, columnIndex, rowIndex);
insertSubtableValues(rowIndex, columnIndex, values);
}


protected native void nativeInsertSubTable(long nativeTablePtr, long columnIndex, long rowIndex);

public void insertDone() {
if (immutable) throwImmutable();
nativeInsertDone(nativePtr);
}


protected native void nativeInsertDone(long nativeTablePtr);

Expand Down Expand Up @@ -762,7 +797,7 @@ public void addLong(long columnIndex, long value) {

public void setIndex(long columnIndex) {
if (immutable) throwImmutable();
if (getColumnType(columnIndex) != ColumnType.ColumnTypeString)
if (getColumnType(columnIndex) != ColumnType.STRING)
throw new IllegalArgumentException("Index is only supported on string columns.");
nativeSetIndex(nativePtr, columnIndex);
}
Expand Down Expand Up @@ -969,7 +1004,7 @@ public TableView findAllString(long columnIndex, String value) {

// Requires that the first column is a string column with index
public long lookup(String value) {
if (!this.hasIndex(0) || this.getColumnType(0) != ColumnType.ColumnTypeString)
if (!this.hasIndex(0) || this.getColumnType(0) != ColumnType.STRING)
throw new RuntimeException("lookup() requires index on column 0 which must be a String column.");
return nativeLookup(nativePtr, value);
}
Expand Down
4 changes: 2 additions & 2 deletions tightdb-java-core/src/main/java/com/tightdb/TableSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class ColumnInfo {
public ColumnInfo(ColumnType type, String name) {
this.name = name;
this.type = type;
this.tableSpec = (type == ColumnType.ColumnTypeTable) ? new TableSpec() : null;
this.tableSpec = (type == ColumnType.TABLE) ? new TableSpec() : null;
}

@Override
Expand Down Expand Up @@ -73,7 +73,7 @@ public TableSpec addSubtableColumn(String name) {
if (name.length() > 63) {
throw new IllegalArgumentException("Column names are currently limited to max 63 characters.");
}
ColumnInfo columnInfo = new ColumnInfo(ColumnType.ColumnTypeTable, name);
ColumnInfo columnInfo = new ColumnInfo(ColumnType.TABLE, name);
columnInfos.add(columnInfo);
return columnInfo.tableSpec;
}
Expand Down
Loading