Search in sources :

Example 6 with PdxInstanceImpl

use of org.apache.geode.pdx.internal.PdxInstanceImpl in project geode by apache.

the class PdxRenameDUnitTest method testPdxRenameVersioning.

@Test
public void testPdxRenameVersioning() throws Exception {
    final String DS_NAME = "PdxRenameDUnitTestDiskStore";
    final String DS_NAME2 = "PdxRenameDUnitTestDiskStore2";
    final int[] locatorPorts = AvailablePortHelper.getRandomAvailableTCPPorts(2);
    final File f = new File(DS_NAME);
    f.mkdir();
    final File f2 = new File(DS_NAME2);
    f2.mkdir();
    this.filesToBeDeleted.add(DS_NAME);
    this.filesToBeDeleted.add(DS_NAME2);
    final Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "localhost[" + locatorPorts[0] + "],localhost[" + locatorPorts[1] + "]");
    props.setProperty(ENABLE_CLUSTER_CONFIGURATION, "false");
    Host host = Host.getHost(0);
    VM vm1 = host.getVM(0);
    VM vm2 = host.getVM(1);
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            disconnectFromDS();
            props.setProperty(START_LOCATOR, "localhost[" + locatorPorts[0] + "]");
            final Cache cache = (new CacheFactory(props)).setPdxPersistent(true).setPdxDiskStore(DS_NAME).create();
            DiskStoreFactory dsf = cache.createDiskStoreFactory();
            dsf.setDiskDirs(new File[] { f });
            dsf.create(DS_NAME);
            RegionFactory<String, PdxValue> rf1 = cache.createRegionFactory(RegionShortcut.REPLICATE_PERSISTENT);
            rf1.setDiskStoreName(DS_NAME);
            Region<String, PdxValue> region1 = rf1.create("region1");
            region1.put("key1", new PdxValue(1));
            return null;
        }
    });
    vm2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            disconnectFromDS();
            props.setProperty(START_LOCATOR, "localhost[" + locatorPorts[1] + "]");
            final Cache cache = (new CacheFactory(props)).setPdxReadSerialized(true).setPdxPersistent(true).setPdxDiskStore(DS_NAME2).create();
            DiskStoreFactory dsf = cache.createDiskStoreFactory();
            dsf.setDiskDirs(new File[] { f2 });
            dsf.create(DS_NAME2);
            RegionFactory rf1 = cache.createRegionFactory(RegionShortcut.REPLICATE_PERSISTENT);
            rf1.setDiskStoreName(DS_NAME2);
            Region region1 = rf1.create("region1");
            Object v = region1.get("key1");
            assertNotNull(v);
            cache.close();
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Cache cache = CacheFactory.getAnyInstance();
            if (cache != null && !cache.isClosed()) {
                cache.close();
            }
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Collection<Object> renameResults = DiskStoreImpl.pdxRename(DS_NAME, new File[] { f }, "apache", "pivotal");
            assertEquals(2, renameResults.size());
            for (Object o : renameResults) {
                if (o instanceof PdxType) {
                    PdxType t = (PdxType) o;
                    assertEquals("org.pivotal.geode.internal.PdxRenameDUnitTest$PdxValue", t.getClassName());
                } else {
                    EnumInfo ei = (EnumInfo) o;
                    assertEquals("org.pivotal.geode.internal.PdxRenameDUnitTest$Day", ei.getClassName());
                }
            }
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            props.setProperty(START_LOCATOR, "localhost[" + locatorPorts[0] + "]");
            final Cache cache = (new CacheFactory(props)).setPdxPersistent(true).setPdxDiskStore(DS_NAME).create();
            DiskStoreFactory dsf = cache.createDiskStoreFactory();
            dsf.setDiskDirs(new File[] { f });
            dsf.create(DS_NAME);
            RegionFactory<String, PdxValue> rf1 = cache.createRegionFactory(RegionShortcut.REPLICATE_PERSISTENT);
            rf1.setDiskStoreName(DS_NAME);
            Region<String, PdxValue> region1 = rf1.create("region1");
            return null;
        }
    });
    vm2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            disconnectFromDS();
            props.setProperty(START_LOCATOR, "localhost[" + locatorPorts[1] + "]");
            final Cache cache = (new CacheFactory(props)).setPdxReadSerialized(true).setPdxPersistent(true).setPdxDiskStore(DS_NAME2).create();
            DiskStoreFactory dsf = cache.createDiskStoreFactory();
            dsf.setDiskDirs(new File[] { f2 });
            dsf.create(DS_NAME2);
            RegionFactory rf1 = cache.createRegionFactory(RegionShortcut.REPLICATE_PERSISTENT);
            rf1.setDiskStoreName(DS_NAME2);
            Region region1 = rf1.create("region1");
            PdxInstance v = (PdxInstance) region1.get("key1");
            assertNotNull(v);
            assertEquals("org.pivotal.geode.internal.PdxRenameDUnitTest$PdxValue", ((PdxInstanceImpl) v).getClassName());
            cache.close();
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Cache cache = CacheFactory.getAnyInstance();
            if (cache != null && !cache.isClosed()) {
                cache.close();
            }
            return null;
        }
    });
}
Also used : PdxType(org.apache.geode.pdx.internal.PdxType) EnumInfo(org.apache.geode.pdx.internal.EnumInfo) Host(org.apache.geode.test.dunit.Host) Properties(java.util.Properties) IOException(java.io.IOException) DiskStoreFactory(org.apache.geode.cache.DiskStoreFactory) RegionFactory(org.apache.geode.cache.RegionFactory) PdxInstance(org.apache.geode.pdx.PdxInstance) PdxInstanceImpl(org.apache.geode.pdx.internal.PdxInstanceImpl) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) CacheFactory(org.apache.geode.cache.CacheFactory) File(java.io.File) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 7 with PdxInstanceImpl

use of org.apache.geode.pdx.internal.PdxInstanceImpl in project geode by apache.

the class PdxStringJUnitTest method testHashCodeEquals.

@Test
public void testHashCodeEquals() throws Exception {
    PdxInstanceFactory pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
    pf.writeString("secId", "abc");
    PdxInstanceImpl pi = (PdxInstanceImpl) pf.create();
    PdxString pdx1 = (PdxString) pi.getRawField("secId");
    Map<PdxString, String> map = new HashMap<PdxString, String>();
    map.put(pdx1, "abc");
    PdxString pdx2 = new PdxString("abc");
    assertEquals(map.get(pdx2), "abc");
    map = new Object2ObjectOpenHashMap();
    map.put(pdx1, "abc");
    assertEquals(map.get(pdx2), "abc");
}
Also used : Object2ObjectOpenHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap) PdxInstanceImpl(org.apache.geode.pdx.internal.PdxInstanceImpl) HashMap(java.util.HashMap) Object2ObjectOpenHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap) PdxString(org.apache.geode.pdx.internal.PdxString) PdxString(org.apache.geode.pdx.internal.PdxString) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 8 with PdxInstanceImpl

use of org.apache.geode.pdx.internal.PdxInstanceImpl in project geode by apache.

the class PdxStringJUnitTest method testEquals.

@Test
public void testEquals() throws Exception {
    PdxInstanceFactory pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
    pf.writeString("secId", "abc");
    PdxInstanceImpl pi = (PdxInstanceImpl) pf.create();
    PdxString pdx1 = (PdxString) pi.getRawField("secId");
    assertEquals(false, pdx1.equals(null));
    assertEquals(false, pdx1.equals(new Date(37)));
    PdxString pdx2 = new PdxString("abc");
    assertEquals(pdx1, pdx2);
    pdx2 = new PdxString("ABC");
    assertEquals(false, pdx1.equals(pdx2));
}
Also used : PdxInstanceImpl(org.apache.geode.pdx.internal.PdxInstanceImpl) PdxString(org.apache.geode.pdx.internal.PdxString) Date(java.util.Date) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 9 with PdxInstanceImpl

use of org.apache.geode.pdx.internal.PdxInstanceImpl in project geode by apache.

the class CompiledOperation method eval0.

@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RV_RETURN_VALUE_OF_PUTIFABSENT_IGNORED", justification = "Does not matter if the methodDispatch that isn't stored in the map is used")
private Object eval0(Object receiver, Class resolutionType, ExecutionContext context) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    if (receiver == null || receiver == QueryService.UNDEFINED)
        return QueryService.UNDEFINED;
    List args = new ArrayList();
    List argTypes = new ArrayList();
    Iterator i = this.args.iterator();
    while (i.hasNext()) {
        CompiledValue arg = (CompiledValue) i.next();
        Object o = arg.evaluate(context);
        // undefined arg produces undefines method result
        if (o == QueryService.UNDEFINED)
            return QueryService.UNDEFINED;
        args.add(o);
        // pass in null for the type if the runtime value is null
        if (o == null)
            argTypes.add(null);
        else
            // commented out because we currently always use the runtime type for args
            // else if (arg.getType() == Identifier)
            // {
            // CompiledValue resolved = context.resolve(((CompiledID)arg).getId());
            // if (resolved != null && resolved.getType() == ITERATOR)
            // argTypes.add(((RuntimeIterator)resolved).getBaseCollection().getConstraint());
            // else
            // argTypes.add(o.getClass());
            // }
            // otherwise use the runtime type
            argTypes.add(o.getClass());
    }
    // see if in cache
    MethodDispatch methodDispatch;
    List key = Arrays.asList(new Object[] { resolutionType, this.methodName, argTypes });
    methodDispatch = (MethodDispatch) CompiledOperation.cache.get(key);
    if (methodDispatch == null) {
        try {
            methodDispatch = new MethodDispatch(resolutionType, this.methodName, argTypes);
        } catch (NameResolutionException nre) {
            if (!org.apache.geode.cache.query.Struct.class.isAssignableFrom(resolutionType) && (DefaultQueryService.QUERY_HETEROGENEOUS_OBJECTS || DefaultQueryService.TEST_QUERY_HETEROGENEOUS_OBJECTS)) {
                return QueryService.UNDEFINED;
            } else {
                throw nre;
            }
        }
        // cache
        CompiledOperation.cache.putIfAbsent(key, methodDispatch);
    }
    if (receiver instanceof PdxInstance) {
        try {
            if (receiver instanceof PdxInstanceImpl) {
                receiver = ((PdxInstanceImpl) receiver).getCachedObject();
            } else {
                receiver = ((PdxInstance) receiver).getObject();
            }
        } catch (PdxSerializationException ex) {
            throw new QueryInvocationTargetException(ex);
        }
    } else if (receiver instanceof PdxString) {
        receiver = ((PdxString) receiver).toString();
    }
    return methodDispatch.invoke(receiver, args);
}
Also used : ArrayList(java.util.ArrayList) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) PdxString(org.apache.geode.pdx.internal.PdxString) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) PdxInstance(org.apache.geode.pdx.PdxInstance) PdxInstanceImpl(org.apache.geode.pdx.internal.PdxInstanceImpl) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with PdxInstanceImpl

use of org.apache.geode.pdx.internal.PdxInstanceImpl in project geode by apache.

the class AutoSerializableJUnitTest method testPrimitiveObjects.

@Test
public void testPrimitiveObjects() throws Exception {
    setupSerializer(new PrimitiveObjectsAutoSerializer(true, "org.apache.geode.pdx.AutoSerializableJUnitTest.PrimitiveObjectHolder"), true);
    PrimitiveObjectHolder nullHolder = new PrimitiveObjectHolder();
    PrimitiveObjectHolder defaultHolder = new PrimitiveObjectHolder();
    defaultHolder.bool = false;
    defaultHolder.b = 0;
    defaultHolder.c = 0;
    defaultHolder.s = 0;
    defaultHolder.i = 0;
    defaultHolder.l = 0L;
    defaultHolder.f = 0.0f;
    defaultHolder.d = 0.0;
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeObject(nullHolder, out);
    PdxInstance pi = DataSerializer.readObject(new DataInputStream(new ByteArrayInputStream(out.toByteArray())));
    PdxField pf = ((PdxInstanceImpl) pi).getPdxField("f");
    assertEquals(FieldType.FLOAT, pf.getFieldType());
    Object dObj = pi.getObject();
    assertFalse(nullHolder.equals(dObj));
    assertEquals(defaultHolder, dObj);
    out = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeObject(defaultHolder, out);
    pi = DataSerializer.readObject(new DataInputStream(new ByteArrayInputStream(out.toByteArray())));
    dObj = pi.getObject();
    assertEquals(defaultHolder, dObj);
}
Also used : PdxInstanceImpl(org.apache.geode.pdx.internal.PdxInstanceImpl) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) PdxField(org.apache.geode.pdx.internal.PdxField) PdxSerializerObject(org.apache.geode.internal.PdxSerializerObject) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

PdxInstanceImpl (org.apache.geode.pdx.internal.PdxInstanceImpl)14 Test (org.junit.Test)9 PdxString (org.apache.geode.pdx.internal.PdxString)8 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)8 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)7 PdxInstance (org.apache.geode.pdx.PdxInstance)4 PdxType (org.apache.geode.pdx.internal.PdxType)4 IOException (java.io.IOException)3 Properties (java.util.Properties)2 Cache (org.apache.geode.cache.Cache)2 CacheFactory (org.apache.geode.cache.CacheFactory)2 PdxSerializationException (org.apache.geode.pdx.PdxSerializationException)2 EnumInfo (org.apache.geode.pdx.internal.EnumInfo)2 PdxField (org.apache.geode.pdx.internal.PdxField)2 TypeRegistry (org.apache.geode.pdx.internal.TypeRegistry)2 Object2ObjectOpenHashMap (it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1