Skip to content
This repository was archived by the owner on Aug 18, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<appengine.target.version>1.9.26</appengine.target.version>
<objectify.version>[5.1.0,5.2.0)</objectify.version>
<objectify.version>6.0.3</objectify.version>

<joda-time.version>2.8.2</joda-time.version>
<lombok.version>1.16.6</lombok.version>
Expand Down Expand Up @@ -281,6 +281,9 @@
<goals>
<goal>jar</goal>
</goals>
<configuration>
<failOnError>false</failOnError>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public Key<CounterData> getTypedKey()
* Assembles the Key for this entity. If an Entity has a Parent Key, that key will be included in the returned Key
* heirarchy.
*/
public com.google.appengine.api.datastore.Key getKey()
public com.google.cloud.datastore.Key getKey()
{
return this.getTypedKey().getRaw();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public Key<CounterShardData> getTypedKey()
* Assembles the Key for this entity. If an Entity has a Parent Key, that key will be included in the returned Key
* heirarchy.
*/
public com.google.appengine.api.datastore.Key getKey()
public com.google.cloud.datastore.Key getKey()
{
return this.getTypedKey().getRaw();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Key<CounterShardOperationData> getTypedKey()
* Assembles the Key for this entity. If an Entity has a Parent Key, that key will be included in the returned Key
* heirarchy.
*/
public com.google.appengine.api.datastore.Key getKey()
public com.google.cloud.datastore.Key getKey()
{
return this.getTypedKey().getRaw();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* Copyright (C) 2019 Instacount Inc. (developers@instacount.io)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package com.google.appengine.api.memcache;

import com.googlecode.objectify.cache.IdentifiableValue;
import com.googlecode.objectify.cache.MemcacheService;
import com.googlecode.objectify.cache.spymemcached.SpyIdentifiableValue;

import net.spy.memcached.CASValue;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* Wrapper to use google's MemecacheService with Objectify
*
* This class is in google's appengine package to use a package private class
*
* @author Mohammad Sarhan
*/
public class LocalMemcacheService implements MemcacheService {

private com.google.appengine.api.memcache.MemcacheService memcacheService;

public LocalMemcacheService(com.google.appengine.api.memcache.MemcacheService memcacheService)
{
this.memcacheService = memcacheService;
}

@Override
public Object get(String s)
{
return memcacheService.get(s);
}

@Override
public Map<String, IdentifiableValue> getIdentifiables(Collection<String> collection)
{

Map<String, com.google.appengine.api.memcache.MemcacheService.IdentifiableValue> values =
memcacheService.getIdentifiables(collection);

Map<String, IdentifiableValue> transformed = new HashMap<>(values.size());

for (Map.Entry<String, com.google.appengine.api.memcache.MemcacheService.IdentifiableValue> entry : values.entrySet())
{
long cas = ((AsyncMemcacheServiceImpl.IdentifiableValueImpl) entry.getValue()).getCasId();
Object value = entry.getValue().getValue();

transformed.put(entry.getKey(),
new SpyIdentifiableValue(
new CASValue<>(cas, value)

));
}

return transformed;
}

@Override
public Map<String, Object> getAll(Collection<String> collection)
{
return memcacheService.getAll(collection);
}

@Override
public void put(String s, Object o)
{
memcacheService.put(s, o);
}

@Override
public void putAll(Map<String, Object> map)
{
memcacheService.putAll(map);
}

@Override
public Set<String> putIfUntouched(Map<String, CasPut> map)
{

Map<String, com.google.appengine.api.memcache.MemcacheService.CasValues> transformed =
new HashMap<>(map.size());

for (Map.Entry<String, CasPut> entry : map.entrySet())
{
CasPut put = entry.getValue();
long cas = ((SpyIdentifiableValue) put.getIv()).getCasValue().getCas();
Object oldVal = put.getIv().getValue();
Object newVal = put.getNextToStore();

transformed.put(entry.getKey(),
new com.google.appengine.api.memcache.MemcacheService.CasValues(
new AsyncMemcacheServiceImpl.IdentifiableValueImpl(oldVal, cas), newVal));
}

return memcacheService.putIfUntouched(transformed);
}

@Override
public void deleteAll(Collection<String> collection)
{
memcacheService.deleteAll(collection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,11 @@
*/
package io.instacount.appengine.counter.service;

import static org.junit.Assert.*;

import java.math.BigInteger;

import io.instacount.appengine.counter.data.CounterShardOperationData;
import org.junit.After;
import org.junit.Before;

import com.google.appengine.api.capabilities.CapabilitiesService;
import com.google.appengine.api.capabilities.CapabilitiesServiceFactory;
import com.google.appengine.api.capabilities.Capability;
import com.google.appengine.api.capabilities.CapabilityStatus;
import com.google.appengine.api.memcache.LocalMemcacheService;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest;
Expand All @@ -32,12 +25,28 @@
import com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.testing.LocalDatastoreHelper;

import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.impl.translate.opt.joda.JodaTimeTranslators;
import com.googlecode.objectify.util.Closeable;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

import java.math.BigInteger;

import io.instacount.appengine.counter.Counter;
import io.instacount.appengine.counter.data.CounterData;
import io.instacount.appengine.counter.data.CounterShardData;
import io.instacount.appengine.counter.data.CounterShardOperationData;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* An abstract base class for testing {@link ShardedCounterServiceImpl}
Expand All @@ -64,6 +73,8 @@ public abstract class AbstractShardedCounterServiceTest

protected MemcacheService memcache;

protected static LocalDatastoreHelper localDatastoreHelper;

protected CapabilitiesService capabilitiesService;

public static class DeleteShardedCounterDeferredCallback extends LocalTaskQueueTestConfig.DeferredTaskCallback
Expand All @@ -86,6 +97,13 @@ protected int executeNonDeferredRequest(URLFetchRequest req)
}
}

@BeforeClass
public static void startLocalDatastore() throws Exception
{
localDatastoreHelper = LocalDatastoreHelper.create();
localDatastoreHelper.start();
}

@Before
public void setUp() throws Exception
{
Expand All @@ -111,6 +129,11 @@ public void setUp() throws Exception
memcache = MemcacheServiceFactory.getMemcacheService();
capabilitiesService = CapabilitiesServiceFactory.getCapabilitiesService();

// Objectify 6.x can only be tested with LocalDatastoreHelper and its own memcache interface.
final com.googlecode.objectify.cache.MemcacheService localMemcacheService = new LocalMemcacheService(memcache);
Datastore ds = localDatastoreHelper.getOptions().getService();
ObjectifyService.init(new ObjectifyFactory(ds,localMemcacheService));

// New Objectify 5.1 Way. See https://groups.google.com/forum/#!topic/objectify-appengine/O4FHC_i7EGk
this.session = ObjectifyService.begin();

Expand All @@ -135,6 +158,21 @@ public void tearDown()
this.session.close();

this.helper.tearDown();

try
{
//Reset after each test for test isolation.
localDatastoreHelper.reset();
} catch (Exception e) {
//do nothing
}
}

@AfterClass
public static void shutdownDatastore() throws Exception
{
//Only shutdown the datatstore at the end of all the tests.
localDatastoreHelper.stop();
}

// ///////////////////////
Expand Down