Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds methods to list all keys and clear all items except a list of keys #178

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions benchmark/src/main/java/com/orhanobut/benchmark/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.orhanobut.hawk.Hawk;
import com.orhanobut.hawk.LogInterceptor;

import java.util.Arrays;

public class MainActivity extends Activity {

@Override
Expand All @@ -19,7 +21,9 @@ protected void onCreate(Bundle savedInstanceState) {
timeHawkGet();
timeHawkContains();
timeHawkCount();
timeHawkGetKeys();
timeHawkDelete();
timeHawkSelectiveDelete();
}

private void timeHawkInit() {
Expand Down Expand Up @@ -79,4 +83,22 @@ private void timeHawkDelete() {
long endTime = System.currentTimeMillis();
System.out.println("Hawk.count: " + (endTime - startTime) + "ms");
}

private void timeHawkGetKeys() {
long startTime = System.currentTimeMillis();

Hawk.getAllKeys();

long endTime = System.currentTimeMillis();
System.out.println("Hawk.getAllKeys: " + (endTime - startTime) + "ms");
}

private void timeHawkSelectiveDelete() {
long startTime = System.currentTimeMillis();

Hawk.selectiveDelete(Arrays.asList("key", "key2", "key3"));

long endTime = System.currentTimeMillis();
System.out.println("Hawk.getAllKeys: " + (endTime - startTime) + "ms");
}
}
11 changes: 11 additions & 0 deletions hawk/src/main/java/com/orhanobut/hawk/DefaultHawkFacade.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.orhanobut.hawk;

import java.util.List;

public class DefaultHawkFacade implements HawkFacade {

private final Storage storage;
Expand Down Expand Up @@ -145,6 +147,15 @@ public DefaultHawkFacade(HawkBuilder builder) {
@Override public void destroy() {
}

@Override public List<String> getAllKeys() {
return storage.getAllKeys();
}

@Override
public long selectiveDelete(List<String> keys) {
return storage.selectiveDelete(keys);
}

private void log(String message) {
logInterceptor.onLog(message);
}
Expand Down
18 changes: 18 additions & 0 deletions hawk/src/main/java/com/orhanobut/hawk/Hawk.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;

import java.util.List;

public final class Hawk {

Expand Down Expand Up @@ -118,4 +119,21 @@ public static void destroy() {
hawkFacade.destroy();
}

/**
* Use this method to get all the keys saved
*
* @return the list of all keys saved in the DB
*/
public static List<String> getAllKeys() {
return hawkFacade.getAllKeys();
}

/**
* Use this method to clear all, except a list of keys
*
* @return the number of objects deleted
*/
public static long selectiveDelete(List<String> keys) {
return hawkFacade.selectiveDelete(keys);
}
}
16 changes: 16 additions & 0 deletions hawk/src/main/java/com/orhanobut/hawk/HawkFacade.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.orhanobut.hawk;

import java.util.List;

public interface HawkFacade {

<T> boolean put(String key, T value);
Expand All @@ -20,6 +22,10 @@ public interface HawkFacade {

void destroy();

List<String> getAllKeys();

long selectiveDelete(List<String> keys);

class EmptyHawkFacade implements HawkFacade {

@Override public <T> boolean put(String key, T value) {
Expand Down Expand Up @@ -65,6 +71,16 @@ class EmptyHawkFacade implements HawkFacade {
throwValidation();
}

@Override public List<String> getAllKeys() {
throwValidation();
return null;
}

@Override public long selectiveDelete(List<String> keys) {
throwValidation();
return 0;
}

private void throwValidation() {
throw new IllegalStateException("Hawk is not built. " +
"Please call build() and wait the initialisation finishes.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

final class SharedPreferencesStorage implements Storage {

private final SharedPreferences preferences;
Expand Down Expand Up @@ -45,4 +49,26 @@ private SharedPreferences.Editor getEditor() {
return preferences.edit();
}

@Override
public List<String> getAllKeys() {
List<String> result = new ArrayList<>();
Map<String, ?> allEntries = preferences.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
result.add(entry.getKey());
}
return result;
}

@Override
public long selectiveDelete(List<String> keys) {
long result = 0;
Map<String, ?> allEntries = preferences.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
if (!keys.contains(entry.getKey())) {
delete(entry.getKey());
result++;
}
}
return result;
}
}
15 changes: 15 additions & 0 deletions hawk/src/main/java/com/orhanobut/hawk/Storage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.orhanobut.hawk;

import java.util.List;

@SuppressWarnings("WeakerAccess")
public interface Storage {

Expand Down Expand Up @@ -56,4 +58,17 @@ public interface Storage {
*/
boolean contains(String key);

/**
* Retrieve a list with all the keys in the storage
*
* @return list of keys
*/
List<String> getAllKeys();

/**
* Clear all the data stored except the keys specified.
* @param keys to don't delete
* @return the number of items deleted
*/
long selectiveDelete(List<String> keys);
}
11 changes: 11 additions & 0 deletions hawk/src/test/java/com/orhanobut/hawk/HawkBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.robolectric.annotation.Config;

import java.lang.reflect.Type;
import java.util.List;

import static com.google.common.truth.Truth.assertThat;
import static junit.framework.Assert.fail;
Expand Down Expand Up @@ -62,6 +63,16 @@ class MyStorage implements Storage {
@Override public boolean contains(String key) {
return false;
}

@Override
public List<String> getAllKeys() {
return null;
}

@Override
public long selectiveDelete(List<String> keys) {
return 0;
}
}
builder.setStorage(new MyStorage()).build();
assertThat(builder.getStorage()).isInstanceOf(MyStorage.class);
Expand Down