Create an ObjectArray for generic array implementation

103 views
Skip to first unread message

Sayali

unread,
Jun 1, 2015, 8:20:52 AM6/1/15
to java-ch...@googlegroups.com
Hi,

I need chronicle map key to be a collection of integers and strings(datatypes defined at runtime)

I referred following for ByteArray implementation in Chronicle Maps:

Similarly, I created an object array 
public interface IObjectArray {

public void setObjectAt(@MaxSize(12) int index, Object obj);
public Object getObjectAt(int index);
}

I created the chronicle map as follows:
ChronicleMapBuilder<IObjectArray, IntValue> builder = ChronicleMapBuilder
.of(IObjectArray.class,IntValue.class).entries(1000000);
ChronicleMap<IObjectArray, IntValue> hashMap = builder.create();
IObjectArray key = DataValueClasses.newDirectInstance(IObjectArray.class);//hashMap.newKeyInstance();
Object obj1 = DataValueClasses.newDirectInstance(IntValue.class);
Object obj2 = DataValueClasses.newDirectInstance(IntValue.class);
IntValue value = hashMap.newValueInstance();

I set the records in map as follows:
for (int i = 0; i < max; i++) {
((IntValue) obj1).setValue(i*10);
key4.setObjectAt(0, obj1);

((IntValue) obj2).setValue(i*20);
key4.setObjectAt(1, obj2);

if (!(hashMap.containsKey(key))) {
value.setValue(i);
hashMap.put(key, value);
} else {
hashMap.acquireUsing(key, value);
value.addAtomicValue(10);
}
}


I get following error:
Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalArgumentException: type must be an interface, was class java.lang.Object
        at net.openhft.lang.model.DataValueClassCache.newDirectReference(DataValueClassCache.java:41)
        at net.openhft.lang.model.DataValueClasses.newDirectReference(DataValueClasses.java:40)
        at net.openhft.lang.model.DataValueClasses.newDirectInstance(DataValueClasses.java:44)
        at tests.AggregationTests.main(AggregationTests.java:116)
Caused by: java.lang.IllegalArgumentException: type must be an interface, was class java.lang.Object
        at net.openhft.lang.model.DataValueModelImpl.<init>(DataValueModelImpl.java:64)
        at net.openhft.lang.model.DataValueModelImpl.<init>(DataValueModelImpl.java:217)
        at net.openhft.lang.model.DataValueModels.acquireModel(DataValueModels.java:44)
        at net.openhft.lang.model.DataValueGenerator.acquireNativeClass(DataValueGenerator.java:783)
        at net.openhft.lang.model.DataValueClassCache.directClassFor(DataValueClassCache.java:50)
        at net.openhft.lang.model.DataValueClassCache.newDirectReference(DataValueClassCache.java:39)
        ... 3 more

Any help would be appreciated.

Thanks,
Sayali

Rob Austin

unread,
Jun 1, 2015, 8:33:10 AM6/1/15
to java-ch...@googlegroups.com
what type is the Object in ?

public Object getObjectAt(int index);


On 1 Jun 2015, at 13:20, Sayali <sayalid...@gmail.com> wrote:

public Object getObjectAt(int index);

Rob Austin

unread,
Jun 1, 2015, 8:36:51 AM6/1/15
to java-ch...@googlegroups.com
this test case maybe some help

net.openhft.chronicle.map.CHMUseCasesTest#bondExample

Peter Lawrey

unread,
Jun 1, 2015, 2:09:36 PM6/1/15
to java-ch...@googlegroups.com
You can use types such as String, CharSequence or an interface you have define the same way.  
It can't be a generic Object as this cannot be serialized.

--
You received this message because you are subscribed to the Google Groups "Chronicle" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-chronicl...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sayali

unread,
Jun 2, 2015, 12:02:10 AM6/2/15
to java-ch...@googlegroups.com
Thanks alot for your quick response.

Actually, Object as chronicle map key works as follows:
So I wanted to go for ObjectArray on similar lines

ChronicleMapBuilder<Object, IntValue> builder = ChronicleMapBuilder
.of(Object.class,IntValue.class).entries(max);
ChronicleMap<Object, IntValue> hashMap = builder.create();

Object key = DataValueClasses.newDirectInstance(IntValue.class);
IntValue value = hashMap.newValueInstance();

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

((IntValue) key).setValue(i*10);

if (!(hashMap.containsKey(key))) {
value.setValue(i);
hashMap.put(key, value);
} else {
hashMap.acquireUsing(key, value);
value.addAtomicValue(10);
}
}

Regards,
Sayali

Peter Lawrey

unread,
Jun 2, 2015, 1:10:14 AM6/2/15
to java-ch...@googlegroups.com

The DataValueGenerator assumes you want random access to fields and elements in an array. Ie you can serialize or deserialize any element without having to touch the entire array. This requires that the library can deterine the length in advance.

Btw Object serialization falls back to using Java serialization which is generic but not designed for speed.

--
Reply all
Reply to author
Forward
0 new messages