Skip to content

Commit

Permalink
Always generate a row type interface on the server rather than trying…
Browse files Browse the repository at this point in the history
… to find the named type as given by the client.
  • Loading branch information
broneill committed Sep 30, 2024
1 parent ea05bd8 commit 135c721
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/cojen/tupl/remote/DerivedTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void dispose() throws RemoteException {
}

Class<Row> rowType() {
return RowTypeCache.find(mDescriptor);
return RowTypeCache.findRow(mDescriptor);
}

public static final class Serializer implements org.cojen.dirmi.Serializer {
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/org/cojen/tupl/remote/RowTypeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,48 @@

import org.cojen.tupl.core.TupleKey;

import org.cojen.tupl.table.MultiCache;
import org.cojen.tupl.table.RowGen;
import org.cojen.tupl.table.RowStore;
import org.cojen.tupl.table.SoftCache;
import org.cojen.tupl.table.Unpersisted;

/**
* Cache of row type interfaces keyed by descriptor.
*
* @author Brian S. O'Neill
*/
final class RowTypeCache extends SoftCache<TupleKey, Class<Row>, byte[]> {
final class RowTypeCache extends MultiCache<TupleKey, Class<?>, byte[], RuntimeException> {
private static final RowTypeCache THE = new RowTypeCache();

/**
* @param descriptor see RowStore#primaryRowInfo
*/
static Class<Row> find(byte[] descriptor) {
return THE.obtain(TupleKey.make.with(descriptor), descriptor);
static Class<?> findPlain(byte[] descriptor) {
return THE.cacheObtain(TYPE_1, TupleKey.make.with(descriptor), descriptor);
}

/**
* @param descriptor see RowStore#primaryRowInfo
*/
@SuppressWarnings("unchecked")
static Class<Row> findRow(byte[] descriptor) {
return (Class<Row>) THE.cacheObtain(TYPE_2, TupleKey.make.with(descriptor), descriptor);
}

private RowTypeCache() {
}

@Override
@SuppressWarnings("unchecked")
protected Class<Row> newValue(TupleKey key, byte[] descriptor) {
protected Class<?> cacheNewValue(Type type, TupleKey key, byte[] descriptor) {
ClassMaker cm = RowGen.beginClassMakerForRowType
(DerivedTable.class.getPackageName(), DerivedTable.class.getSimpleName());

cm.implement(Row.class).addAnnotation(Unpersisted.class, true);
cm.addAnnotation(Unpersisted.class, true);

if (type == TYPE_2) {
cm.implement(Row.class);
}

return (Class<Row>) RowStore.primaryRowInfo(cm.name(), descriptor).makeRowType(cm);
return RowStore.primaryRowInfo(cm.name(), descriptor).makeRowType(cm);
}
}
16 changes: 8 additions & 8 deletions src/main/java/org/cojen/tupl/remote/ServerTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,26 +173,26 @@ public boolean isEmpty() throws IOException {
public RemoteTable derive(String typeName, byte[] descriptor, String query, Object... args)
throws IOException
{
RowInfo info = RowStore.primaryRowInfo(typeName, descriptor);
Class rowType;
// Always generate a row type interface rather than trying to find the type by name.
// There's no reason to assume that the server will have an interface that the client
// has, and it might not match anyhow.
Class<?> rowType = RowTypeCache.findPlain(descriptor);
return new ServerTable((BaseTable) mTable.derive(rowType, query, args));

/* Attempt to find the interface by name.
findRowType: {
try {
rowType = Session.current().resolveClass(typeName);
RowInfo existing = RowInfo.find(rowType);
RowInfo info = RowStore.primaryRowInfo(typeName, descriptor);
if (existing.allColumns.equals(info.allColumns)) {
info = existing;
break findRowType;
}
} catch (ClassNotFoundException | IllegalArgumentException e) {
// Row type class doesn't exist or it's not a RowInfo.
}

// FIXME: Use RowTypeCache. No need to preserve the type name.
throw new IllegalArgumentException("FIXME: Unknown type: " + typeName);
}

return new ServerTable((BaseTable) mTable.derive(rowType, query, args));
*/
}

@Override
Expand Down

0 comments on commit 135c721

Please sign in to comment.