SimpleCollectionStore.java

/***************************************************************************
   Copyright 2014 Emily Estes

   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 net.metanotion.contentstore;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import net.metanotion.json.JsonObject;

import net.metanotion.util.Dictionary;
import net.metanotion.util.Pair;

/** Create an implementation of SimpleCollection backed by a ContentStore collection. */
public final class SimpleCollectionStore implements Dictionary<String,SimpleCollection<JsonObject>> {
	private final ContentStore store;

	/** Create a new SimpleCollectionStore instance backed by the content store provided.
		@param store The content store backing this instance.
	*/
	public SimpleCollectionStore(final ContentStore store) { this.store = store; }

	/** A SimpleCollection instance backed by a collection from a ContentStore. */
	public static final class CollectionStore implements SimpleCollection<JsonObject> {
		private static final String JSON_MIME = "application/json; charset=utf-8";
		private final ContentStore store;
		private final long cid;

		/** Create a CollectionStore instance.
			@param store The content store backing this instance.
			@param cid The object ID of the contentstore collection backing this instance.
		*/
		public CollectionStore(final ContentStore store, final long cid) {
			this.store = store;
			this.cid = cid;
		}

		@Override public Iterable<Map.Entry<String,JsonObject>> list(final int maxPageSize, final int offset) {
			try {
				final ArrayList<Map.Entry<String,JsonObject>> result = new ArrayList<>();
				for(final Entry e: store.getCollection(cid).elements(maxPageSize, offset)) {
					result.add(new Pair<>(Long.toString(e.oid()), JsonObject.read(e.readContent())));
				}
				return result;
			} catch (final IOException ioe) { throw new RuntimeException(ioe); }
		}

		@Override public JsonObject get(final String index) {
			final Entry e = store.getEntry(Long.parseLong(index));
			if(e.getCollection().oid() != cid) { return null; }
			try {
				return JsonObject.read(e.readContent());
			} catch (final IOException ioe) { throw new RuntimeException(ioe); }
		}

		@Override public String create(final JsonObject o) {
			return Long.toString(store.getCollection(cid).append("").update(o.toString(), JSON_MIME).oid());
		}

		@Override public Map.Entry<String,JsonObject> update(final String index, final JsonObject o) {
			final Entry e = store.getEntry(Long.parseLong(index));
			if(e.getCollection().oid() != cid) { return null; }
			try {
				final Pair<String,JsonObject> result = new Pair<>(index, JsonObject.read(e.readContent()));
				e.update(o.toString(), JSON_MIME);
				return result;
			} catch (final IOException ioe) { throw new RuntimeException(ioe); }
		}

		@Override public boolean remove(final String index) {
			final Entry e = store.getEntry(Long.parseLong(index));
			if(e.getCollection().oid() != cid) { return false; }
			try {
				e.delete();
				return true;
			} catch (final Exception ex) {
				return false;
			}
		}
	};

	@Override public SimpleCollection<JsonObject> get(final String collection) {
		return new CollectionStore(store, Long.parseLong(collection));
	}
}