Minor improvements

* generalise davdroid.ArrayUtils.partition; tests
* lower multi-get limit to 35
pull/2/head
rfc2822 11 years ago
parent 4505a5958d
commit 558075f1bb

@ -1,19 +1,20 @@
package at.bitfire.davdroid;
import at.bitfire.davdroid.resource.Resource;
import java.lang.reflect.Array;
public class ArrayUtils {
public static <T> Resource[][] partition(Resource[] bigArray, int max) {
@SuppressWarnings("unchecked")
public static <T> T[][] partition(T[] bigArray, int max) {
int nItems = bigArray.length;
int nPartArrays = (nItems + max-1)/max;
Resource[][] partArrays = new Resource[nPartArrays][];
T[][] partArrays = (T[][])Array.newInstance(bigArray.getClass().getComponentType(), nPartArrays, 0);
// nItems: number of remaining items
// nItems is now the number of remaining items
for (int i = 0; nItems > 0; i++) {
int n = (nItems < max) ? nItems : max;
partArrays[i] = new Resource[n];
partArrays[i] = (T[])Array.newInstance(bigArray.getClass().getComponentType(), n);
System.arraycopy(bigArray, i*max, partArrays[i], 0, n);
nItems -= n;

@ -29,7 +29,7 @@ import at.bitfire.davdroid.webdav.PreconditionFailedException;
public class SyncManager {
private static final String TAG = "davdroid.SyncManager";
private static final int MAX_MULTIGET_RESOURCES = 50;
private static final int MAX_MULTIGET_RESOURCES = 35;
public void synchronize(LocalCollection<? extends Resource> local, RemoteCollection<? extends Resource> remote, boolean manualSync, SyncResult syncResult) throws LocalStorageException, IOException, HttpException {
@ -74,6 +74,7 @@ public class SyncManager {
// PHASE 3: pull remote changes from server
syncResult.stats.numInserts = pullNew(local, remote, remotelyAdded.toArray(new Resource[0]));
syncResult.stats.numUpdates = pullChanged(local, remote, remotelyUpdated.toArray(new Resource[0]));
syncResult.stats.numEntries += syncResult.stats.numInserts + syncResult.stats.numUpdates;
Log.i(TAG, "Removing non-dirty resources that are not present remotely anymore");
local.deleteAllExceptRemoteNames(remoteResources);

@ -0,0 +1,32 @@
package at.bitfire.davdroid.test;
import java.util.Arrays;
import junit.framework.TestCase;
import at.bitfire.davdroid.ArrayUtils;
public class ArrayUtilsTest extends TestCase {
public void testPartition() {
// n == 0
assertTrue(Arrays.deepEquals(
new Long[0][0],
ArrayUtils.partition(new Long[] { }, 5)));
// n < max
assertTrue(Arrays.deepEquals(
new Long[][] { { 1l, 2l } },
ArrayUtils.partition(new Long[] { 1l, 2l }, 5)));
// n == max
assertTrue(Arrays.deepEquals(
new Long[][] { { 1l, 2l }, { 3l, 4l } },
ArrayUtils.partition(new Long[] { 1l, 2l, 3l, 4l }, 2)));
// n > max
assertTrue(Arrays.deepEquals(
new Long[][] { { 1l, 2l, 3l, 4l, 5l }, { 6l, 7l, 8l, 9l, 10l }, { 11l } },
ArrayUtils.partition(new Long[] { 1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l, 9l, 10l, 11l }, 5)));
}
}

@ -1,23 +0,0 @@
package at.bitfire.davdroid.test;
import junit.framework.TestCase;
import lombok.NonNull;
/* Lombok has some strange issues under Android:
* @NonNull always crashes on Android http://code.google.com/p/projectlombok/issues/detail?id=612
*/
public class LombokTest extends TestCase {
private String appendSlash(@NonNull String s) {
return s + "/";
}
public void testNonNull() {
try {
appendSlash(null);
fail();
} catch(NullPointerException e) {
}
assertEquals("1/", appendSlash("1"));
}
}

@ -4,6 +4,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import lombok.Cleanup;
@ -146,7 +147,7 @@ public class WebDavResourceTest extends InstrumentationTestCase {
simpleFile.get();
@Cleanup InputStream is = assetMgr.open("test.random", AssetManager.ACCESS_STREAMING);
byte[] expected = IOUtils.toByteArray(is);
assertEquals(expected, simpleFile.getContent());
assertTrue(Arrays.equals(expected, simpleFile.getContent()));
}
public void testMultiGet() throws DavException, IOException, HttpException {

Loading…
Cancel
Save