com.bennavetta.appsite.util.DatastoreConfig.java Source code

Java tutorial

Introduction

Here is the source code for com.bennavetta.appsite.util.DatastoreConfig.java

Source

/**
 * Copyright 2013 Ben Navetta <ben@bennavetta.com>
 *
 * 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.bennavetta.appsite.util;

import static com.google.appengine.api.datastore.FetchOptions.Builder.withDefaults;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.configuration.AbstractConfiguration;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

public class DatastoreConfig extends AbstractConfiguration {
    public static final String ENTITY = "ConfigProperty";
    public static final String VALUE_PROPERTY = "value";

    private DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    private LoadingCache<String, Object> cache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES)
            .build(new CacheLoader<String, Object>() {
                @Override
                public Object load(String key) throws Exception {
                    return getPropertyDirect(key);
                }
            });

    @Override
    public boolean containsKey(String key) {
        if (cache.getIfPresent(key) != null)
            return true; //save a trip to the datastore if possible
        return !datastore.get(Collections.singleton(KeyFactory.createKey(ENTITY, key))).isEmpty();
    }

    @Override
    public Iterator<String> getKeys() {
        PreparedQuery pq = datastore.prepare(new Query(ENTITY));
        List<Entity> entities = pq.asList(withDefaults());
        List<String> keys = new ArrayList<>(entities.size());
        for (Entity prop : entities) {
            keys.add(prop.getKey().getName());
        }
        return keys.iterator();
    }

    @Override
    public Object getProperty(String key) {
        return cache.getUnchecked(key);
    }

    @Override
    public boolean isEmpty() {
        if (cache.size() > 0)
            return false; //save a trip to the datastore if possible
        return datastore.prepare(new Query(ENTITY)).countEntities(withDefaults()) == 0;
    }

    @Override
    protected void addPropertyDirect(String key, Object value) {
        Entity prop = new Entity(KeyFactory.createKey(ENTITY, key));
        prop.setUnindexedProperty(VALUE_PROPERTY, value);
        datastore.put(prop);
    }

    protected Object getPropertyDirect(String key) {
        Key k = KeyFactory.createKey(ENTITY, key);
        return datastore.get(Collections.singleton(k)).get(k).getProperty(VALUE_PROPERTY);
    }

    public Object get(String key) {
        return getProperty(key);
    }

    public void set(String key, Object value) {
        addPropertyDirect(key, value);
    }

    @Override
    protected void clearPropertyDirect(String key) {
        datastore.delete(KeyFactory.createKey(ENTITY, key));
    }
}