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

org.s1.mongodb.table.MongoDBTable Maven / Gradle / Ivy

There is a newer version: 2.4.6
Show newest version
package org.s1.mongodb.table;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import org.s1.cluster.dds.beans.CollectionId;
import org.s1.cluster.dds.beans.Id;
import org.s1.cluster.dds.beans.StorageId;
import org.s1.mongodb.MongoDBConnectionHelper;
import org.s1.mongodb.MongoDBFormat;
import org.s1.mongodb.MongoDBQueryHelper;
import org.s1.mongodb.cluster.MongoDBDDS;
import org.s1.objects.MapMethod;
import org.s1.objects.Objects;
import org.s1.table.Table;
import org.s1.table.errors.AlreadyExistsException;
import org.s1.table.errors.MoreThanOneFoundException;
import org.s1.table.errors.NotFoundException;
import org.s1.user.AccessDeniedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @author Grigory Pykhov
 */
public abstract class MongoDBTable extends Table {

    private static final Logger LOG = LoggerFactory.getLogger(MongoDBTable.class);

    public abstract CollectionId getCollectionId();

    public void init() {
        //indexes
        checkIndexes();
    }
    public abstract List> getIndexes();

    protected String[][] getUniqueFields(){
        return null;
    }

    @Override
    protected void checkUnique(Map object, boolean isNew) throws AlreadyExistsException {
        String [][] uniqueFields = getUniqueFields();
        if(uniqueFields==null)
            return;
        List list = Objects.newArrayList(uniqueFields);
        list.add(new String[]{"id"});
        String id = Objects.get(object,"id");
        for(String [] uf:list){
            Map search = Objects.newSOHashMap();
            for(String f:uf){
                search.put(f, Objects.get(object, f));
            }
            if(isNew){

            }else{
                search = Objects.newSOHashMap("$and",Objects.newArrayList(
                        Objects.newSOHashMap("id",Objects.newSOHashMap("$ne",id)),
                        search
                ));
            }
            MongoDBQueryHelper.ensureNotExists(getCollectionId(), search);
        }
    }

    protected void checkIndexes() {
        //remove old indexes
        DBCollection coll = MongoDBConnectionHelper.getCollection(getCollectionId());
        List list = coll.getIndexInfo();
        List> indexes = new ArrayList>(getIndexes());
        indexes.add(Objects.newArrayList("id"));
        for(DBObject o: list){
            DBObject key = (DBObject)o.get("key");
            if(key.keySet().size()==1 && Objects.equals(key.get("_id"),1)){
                continue;
            }

            boolean exists = false;
            for (List b : indexes) {
                boolean same = false;
                if(key.keySet().size()==b.size()){
                    same = true;
                    for(String s:b){
                        if(!Objects.equals(1,key.get(s))){
                            same = false;
                        }
                    }
                }
                exists=same;
                if(exists)
                    break;
            }
            if(exists)
                continue;

            //removing
            if(LOG.isDebugEnabled())
                LOG.debug("Drop index "+o+" for collection "+getCollectionId().getCollection());
            coll.dropIndex((String)o.get("name"));
        }

        //add new
        for(List b:indexes){
            boolean exists = false;
            for(DBObject o: list){
                DBObject key = (DBObject)o.get("key");
                if(key.keySet().size()==1 && Objects.equals(1, key.get("id"))){
                    boolean same = false;
                    if(key.keySet().size()==b.size()){
                        same = true;
                        for(String s:b){
                            if(!Objects.equals(1,key.get(s))){
                                same = false;
                            }
                        }
                    }
                    exists=same;
                    if(exists)
                        break;
                }
            }
            if(exists)
                break;

            //add new index
            if(LOG.isDebugEnabled())
                LOG.debug("Ensure index "+b+" for collection "+getCollectionId().getCollection());
            DBObject i = new BasicDBObject();
            for(String f:b){
                i.put(f,1);
            }
            coll.ensureIndex(i);
        }

    }

    protected void prepareSearch(Map search){

    }

    protected void prepareSort(Map sort){

    }

    @MapMethod(names = {"search"})
    public long count(Map search) throws AccessDeniedException {
        checkAccess();
        if(search==null)
            search = Objects.newSOHashMap();
        prepareSearch(search);
        return MongoDBConnectionHelper.getCollection(getCollectionId()).count(MongoDBFormat.fromMap(search));
    }

    public List> list(Map search, Map sort, Map fields, int skip, int max) throws AccessDeniedException {
        return list(search,sort,fields,skip,max,null);
    }

    @MapMethod(names = {"search","sort","fields","skip","max","ctx"})
    public List> list(Map search, Map sort, Map fields, int skip, int max, Map ctx) throws AccessDeniedException {
        checkAccess();
        if(search==null)
            search = Objects.newSOHashMap();
        search = MongoDBFormat.escapeInjections(search);
        prepareSearch(search);
        if(sort==null)
            sort = Objects.newSOHashMap();
        prepareSort(sort);
        List> l = MongoDBQueryHelper.list(getCollectionId(),search,sort,fields,skip,max);
        for(Map m:l){
            enrichRecord(m,ctx);
        }
        return l;
    }

    @Override
    public Map collectionGet(String id) throws NotFoundException, MoreThanOneFoundException {
        return MongoDBQueryHelper.get(getCollectionId(), Objects.newSOHashMap("id",id));
    }

    @Override
    public void collectionAdd(String id, Map data) {
        MongoDBDDS.add(new Id(getCollectionId().getDatabase(), getCollectionId().getCollection(), id), data);
    }

    @Override
    public void collectionSet(String id, Map data) {
        MongoDBDDS.set(new Id(getCollectionId().getDatabase(), getCollectionId().getCollection(), id), data);
    }

    @Override
    public void collectionRemove(String id) {
        MongoDBDDS.remove(new Id(getCollectionId().getDatabase(), getCollectionId().getCollection(), id));
    }

    @Override
    public String getName() {
        return new StorageId(MongoDBDDS.class,getCollectionId().getDatabase(),getCollectionId().getCollection(),null).getLockName();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy