Skip to content

Commit

Permalink
Merge pull request #97 from mekjaer/master
Browse files Browse the repository at this point in the history
BREAKING-CHANGES: insertLong(), insertString() etc, moved into public inner class + ColumnType enum renamed
  • Loading branch information
mekjaer committed Sep 3, 2013
2 parents 02ff4fb + ad1466d commit 532c057
Show file tree
Hide file tree
Showing 33 changed files with 569 additions and 309 deletions.
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),
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

This file was deleted.

Loading

0 comments on commit 532c057

Please sign in to comment.