Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.swm.gwt.storage;
  
  import  com.google.code.gwt.database.client.GenericRow;
  import  com.google.code.gwt.database.client.service.DataServiceException;
  import  com.google.code.gwt.database.client.service.ListCallback;
  import  com.google.code.gwt.database.client.service.VoidCallback;
 
 import java.util.*;
This class implements all operation specified in http://www.w3.org/TR/webstorage/. Under the hood webdatabase is used which allows store bigger amounts of data. In IOs 6 applications are allowed to use up to 70 Mb limit. Because of the asynchronous nature of http://www.w3.org/TR/webdatabase/ all date are loaded into memory and changes are persisted asynchronously.

Author(s):
wiese.daniel
copyright (C) 2012, SWM Services GmbH
See also:
http://www.w3.org/TR/webstorage/ but avoiding the 5 mb limit.
 
 public class BigLocalStorage implements IStorage {
 
 	private static final Logger LOGGER = Logger.getLogger(BigLocalStorage.class.getName());
 
 
 	public static final String NOT_SUPPORTED = "Not supported";
 	private final SQLStorageLayer sqlLayer = GWT.create(SQLStorageLayer.class);
 	private Map<StringStringinMemoryStorage = new HashMap<StringString>();
 
 	private boolean isSupported = false;
 
 	public BigLocalStorage() {
 
 	}
 
 
 		throw new IllegalArgumentException("Usupported Operation Use: TransactionStorage");
 	}

Hanlder for uncought exceptions.

Parameters:
handler
 
 		this. = handler;
 	}

Will initialize the local storage implementation.

Parameters:
afterInitHanlder
 
 	public void initialize(final AsyncCallback<VoidafterInitHanlder) {
 		.info("initializing SQL-Lite-Datatbase ...");
 		.initTable(new VoidCallback() {
 			public void onSuccess() {
 				.getAllValues(new ListCallback<GenericRow>() {
 					public void onSuccess(List<GenericRow> result) {
 						for (GenericRow genericRow : result) {
 							.put(genericRow.getString("id"), genericRow.getString("value"));
 						}
 						.info("SQL-Lite-Datatbase initialized");
 						afterInitHanlder.onSuccess(null);
 					}
 
 					public void onFailure(DataServiceException error) {
 						.info("Can't read storage");
 						afterInitHanlder.onFailure(error);
 					}
 				});
 				 = true;
 			}
 
 			public void onFailure(DataServiceException e) {
 				.info("SQL Lite datatbase not supported");
 				afterInitHanlder.onFailure(e);
 			}
 		});
 	}

Registriert einen Handler für die StorageEvents.

Parameters:
handler .
Returns:
.
		throw new IllegalArgumentException("Not supported");
	}

Liefert den local Storage zurück.

Returns:
.
		return this;
	}

Prüft ob local storage vorhanden ist.

Returns:
true - wenn ja,false - wenn nein
	public boolean isLocalStorageSupported() {
		return ;
	}

Löscht einen bestimmten Handler, der auf StorageEvents horcht.

Parameters:
handler .
	}

Löscht den Inhalt der Storage.
	public void clear() {
		this..deleteAll(new VoidCallback() {
			public void onSuccess() {
				.info("All rows deleted");
			}
			public void onFailure(DataServiceException e) {
				.info("Can't delete rowns");
				if ( != null) {
				}
			}
		});
	}

Holt die Daten für einen bestimmten Schlüssel.

Parameters:
key .
Returns:
.
	public String getItem(String key) {
		//will be always returnd from memory
		return .get(key);
	}

Gibt die Anzahl der gespeicherten Elemente zurück.

Returns:
.
	public int getLength() {
		//will be always returnd from memory
	}

Liefert den Schlüssel an einer bestimmten Position zurück.

Parameters:
index index position
Returns:
.
	public String key(int index) {
	}

Löscht den Wert für einen bestimmten Schlüssel.

Parameters:
key .
	public void removeItem(String key) {
		final VoidCallback callback = new VoidCallback() {
			public void onSuccess() {
				//nothing to do
			}
			public void onFailure(DataServiceException e) {
				if ( != null) {
				}
			}
		};
		this..deleteKeyAndValue(keycallback);
	}

Löscht den Wert für einen bestimmten Schlüssel.

Parameters:
key .
	private void removeItemInternal(String key, VoidCallback callback) {
		this..deleteKeyAndValue(keycallback);
	}


Speichert einen String für einen bestimmten Schlüssel (überschreibt falls vorhanden).

Parameters:
key the key
data the value
	public void setItem(final String keyfinal String data) {
		final VoidCallback callback = new VoidCallback() {
			public void onSuccess() {
				//nothing to do
			}
			public void onFailure(DataServiceException e) {
				if ( != null) {
				}
			}
		};
		setItemInternal(keydatacallback);
	}
	public void setItemInternal(final String keyfinal String datafinal VoidCallback callback) {
			.put(keydata);
			//update value
			.updateValue(keydatacallback);
else {
			.put(keydata);
			//insert value
			.insertValue(keydatacallback);
		}
	}

Will delete the underlying storage asynchronously.

Parameters:
callback callback when completed
	public void clearAsync(final IStorageOperationCompleted callback) {
		this..deleteAll(new VoidCallback() {
			public void onSuccess() {
				.info("All rows deleted");
				callback.isCompleted();
			}
			public void onFailure(DataServiceException e) {
				.info("Can't delete rows");
				if ( != null) {
else {
					callback.isCompleted();
				}
			}
		});
	}

Parameters:
keys the keys to delete.
callback callback when completed
	public void removeItemsAsync(Set<Stringkeysfinal IStorageOperationCompleted callback) {
		final Queue<StringkeysToInsert = new LinkedList<String>(keys);
		final VoidCallback afterOneRowIsDeletedCallback = new VoidCallback() {
			public void onSuccess() {
				final String nextKey = keysToInsert.poll();
				if (nextKey != null) {
					removeItemInternal(nextKeythis);
else {
					callback.isCompleted();
				}
			}
			public void onFailure(DataServiceException e) {
				if ( != null) {
else {
					final String nextKey = keysToInsert.poll();
					if (nextKey != null) {
						removeItemInternal(nextKeythis);
else {
						callback.isCompleted();
					}
				}
			}
		};
		//Chain starten
		final String nextKey = keysToInsert.poll();
		if (nextKey != null) {
			removeItemInternal(nextKeyafterOneRowIsDeletedCallback);
else {
			callback.isCompleted();
		}
	}

Parameters:
values the values to store.
callback callback when completed
	public void setItemsAsync(final Map<StringStringvaluesfinal IStorageOperationCompleted callback) {
		final Queue<StringkeysToInsert = new LinkedList<String>(values.keySet());
		final VoidCallback afterOneRowIsInsertedCallback = new VoidCallback() {
			public void onSuccess() {
				final String nextKey = keysToInsert.poll();
				if (nextKey != null) {
					setItemInternal(nextKeyvalues.get(nextKey), this);
else {
					callback.isCompleted();
				}
			}
			public void onFailure(DataServiceException e) {
				if ( != null) {
else {
					final String nextKey = keysToInsert.poll();
					if (nextKey != null) {
						setItemInternal(nextKeyvalues.get(nextKey), this);
else {
						callback.isCompleted();
					}
				}
			}
		};
		//Chain starten
		final String nextKey = keysToInsert.poll();
		if (nextKey != null) {
			setItemInternal(nextKeyvalues.get(nextKey), afterOneRowIsInsertedCallback);
else {
			callback.isCompleted();
		}
	}
	public Set<StringgetKeys() {
		return this..keySet();
	}
New to GrepCode? Check out our FAQ X