All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.jackrabbit.mongomk.MemoryDocumentStore Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.jackrabbit.mongomk;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import org.apache.jackrabbit.mk.api.MicroKernelException;
import org.apache.jackrabbit.mongomk.UpdateOp.Operation;
import org.apache.jackrabbit.mongomk.util.Utils;

/**
 * Emulates a MongoDB store (possibly consisting of multiple shards and
 * replicas).
 */
public class MemoryDocumentStore implements DocumentStore {

    /**
     * The 'nodes' collection.
     */
    private ConcurrentSkipListMap> nodes =
            new ConcurrentSkipListMap>();
    
    /**
     * The 'clusterNodes' collection.
     */
    private ConcurrentSkipListMap> clusterNodes =
            new ConcurrentSkipListMap>();

    public Map find(Collection collection, String key, int maxCacheAge) {
        return find(collection, key);
    }
    
    public Map find(Collection collection, String key) {
        ConcurrentSkipListMap> map = getMap(collection);
        Map n = map.get(key);
        if (n == null) {
            return null;
        }
        Map copy = Utils.newMap();
        synchronized (n) {
            Utils.deepCopyMap(n, copy);
        }
        return copy;
    }
    
    @Nonnull
    public List> query(Collection collection, String fromKey, String toKey, int limit) {
        ConcurrentSkipListMap> map = getMap(collection);
        ConcurrentNavigableMap> sub = map.subMap(fromKey, toKey);
        ArrayList> list = new ArrayList>();
        for (Map n : sub.values()) {
            Map copy = Utils.newMap();
            synchronized (n) {
                Utils.deepCopyMap(n, copy);
            }
            list.add(copy);
            if (list.size() > limit) {
                break;
            }
        }
        return list;
    }

    public void remove(Collection collection, String path) {
        getMap(collection).remove(path);
    }

    /**
     * Get the in-memory map for this collection.
     *
     * @param collection the collection
     * @return the map
     */
    private ConcurrentSkipListMap> getMap(Collection collection) {
        switch (collection) {
        case NODES:
            return nodes;
        case CLUSTER_NODES:
            return clusterNodes;
        default:
            throw new IllegalArgumentException(collection.name());
        }
    }

    @CheckForNull
    private Map internalCreateOrUpdate(Collection collection,
                                                       UpdateOp update,
                                                       boolean checkConditions) {
        ConcurrentSkipListMap> map = getMap(collection);
        Map n;
        Map oldNode;

        // get the node if it's there
        oldNode = n = map.get(update.key);

        if (n == null) {
            if (!update.isNew) {
                throw new MicroKernelException("Document does not exist: " + update.key);
            }
            // for a new node, add it (without synchronization)
            n = Utils.newMap();
            oldNode = map.putIfAbsent(update.key, n);
            if (oldNode != null) {
                // somebody else added it at the same time
                n = oldNode;
            }
        }
        synchronized (n) {
            if (checkConditions && !checkConditions(n, update)) {
                return null;
            }
            if (oldNode != null) {
                // clone the old node
                // (document level operations are synchronized)
                Map oldNode2 = Utils.newMap();
                Utils.deepCopyMap(oldNode, oldNode2);
                oldNode = oldNode2;
            }
            // to return the new document:
            // update the document
            // (document level operations are synchronized)
            applyChanges(n, update);
        }
        return oldNode;
    }

    @Nonnull
    @Override
    public Map createOrUpdate(Collection collection,
                                              UpdateOp update)
            throws MicroKernelException {
        return internalCreateOrUpdate(collection, update, false);
    }

    @Override
    public Map findAndUpdate(Collection collection,
                                             UpdateOp update)
            throws MicroKernelException {
        return internalCreateOrUpdate(collection, update, true);
    }

    private static boolean checkConditions(Map target,
                                           UpdateOp update) {
        for (Map.Entry change : update.changes.entrySet()) {
            Operation op = change.getValue();
            if (op.type == Operation.Type.CONTAINS_MAP_ENTRY) {
                String k = change.getKey();
                String[] kv = k.split("\\.");
                Object value = target.get(kv[0]);
                if (value == null) {
                    if (Boolean.TRUE.equals(op.value)) {
                        return false;
                    }
                } else {
                    if (value instanceof java.util.Collection) {
                        java.util.Collection col = (java.util.Collection) value;
                        if (Boolean.TRUE.equals(op.value)) {
                            if (!col.contains(kv[1])) {
                                return false;
                            }
                        } else {
                            if (col.contains(kv[1])) {
                                return false;
                            }
                        }
                    } else {
                        return false;
                    }
                }
            }
        }
        return true;
    }


    /**
     * Apply the changes to the in-memory map.
     * 
     * @param target the target map
     * @param update the changes to apply
     */
    public static void applyChanges(Map target, UpdateOp update) {
        for (Entry e : update.changes.entrySet()) {
            String k = e.getKey();
            String[] kv = k.split("\\.");
            Operation op = e.getValue();
            switch (op.type) {
            case SET: {
                target.put(k, op.value);
                break;
            }
            case INCREMENT: {
                Object old = target.get(k);
                Long x = (Long) op.value;
                if (old == null) {
                    old = 0L;
                }
                target.put(k, ((Long) old) + x);
                break;
            }
            case SET_MAP_ENTRY: {
                Object old = target.get(kv[0]);
                @SuppressWarnings("unchecked")
                Map m = (Map) old;
                if (m == null) {
                    m = Utils.newMap();
                    target.put(kv[0], m);
                }
                m.put(kv[1], op.value);
                break;
            }
            case REMOVE_MAP_ENTRY: {
                Object old = target.get(kv[0]);
                @SuppressWarnings("unchecked")
                Map m = (Map) old;
                if (m != null) {
                    m.remove(kv[1]);
                }
                break;
            }
            case SET_MAP: {
                Object old = target.get(kv[0]);
                @SuppressWarnings("unchecked")
                Map m = (Map) old;
                if (m == null) {
                    m = Utils.newMap();
                    target.put(kv[0], m);
                }
                m.put(kv[1], op.value);
                break;
            }
            case CONTAINS_MAP_ENTRY:
                // no effect
                break;
            }
        }
    }

    @Override
    public boolean create(Collection collection, List updateOps) {
        ConcurrentSkipListMap> map = getMap(collection);
        for (UpdateOp op : updateOps) {
            if (map.containsKey(op.key)) {
                return false;
            }
        }
        for (UpdateOp op : updateOps) {
            createOrUpdate(collection, op);
        }
        return true;
    }

    public String toString() {
        StringBuilder buff = new StringBuilder();
        buff.append("Nodes:\n");
        for (String p : nodes.keySet()) {
            buff.append("Path: ").append(p).append('\n');
            Map e = nodes.get(p);
            for (String prop : e.keySet()) {
                buff.append(prop).append('=').append(e.get(prop)).append('\n');
            }
            buff.append("\n");
        }
        return buff.toString();
    }

    @Override
    public void invalidateCache() {
        // there is no cache, so nothing to invalidate
    }

    @Override
    public void dispose() {
        // ignore
    }

    @Override
    public boolean isCached(Collection collection, String key) {
        return false;
    }

    @Override
    public void invalidateCache(Collection collection, String key) {
        // ignore
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy