<commit_before>package ca.omny.documentdb;

import com.couchbase.client.CouchbaseClient;
import com.couchbase.client.protocol.views.Query;
import com.couchbase.client.protocol.views.Stale;
import com.couchbase.client.protocol.views.View;
import com.couchbase.client.protocol.views.ViewResponse;
import com.couchbase.client.protocol.views.ViewRow;
import com.google.gson.Gson;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import javax.enterprise.inject.Alternative;
import net.spy.memcached.util.StringUtils;

@Alternative
public class CouchQuerier implements IDocumentQuerier {

    CouchbaseClient client;

    public CouchQuerier() {
        this.client = CouchFactory.getClient();
    }

    public CouchQuerier(CouchbaseClient client) {
        this.client = client;
    }
    
    @Override
    public String getKey(String... keyParts) {
        String key = StringUtils.join(Arrays.asList(keyParts), "::");
        return key;
    }

    @Override
    public <T extends Object> T get(String key, Class<T> type) {
        Object o = client.get(key);
        if (o != null) {
            String value = o.toString();
            try {
                return getGson().fromJson(value, type);
            } catch (com.google.gson.JsonSyntaxException syntaxException) {
            }
            if (type.equals(String.class)) {
                return (T) value;
            }
        }

        return null;
    }

    @Override
    public void set(String key, Object value) {
        String gsonValue = this.getGson().toJson(value);
        client.set(key, gsonValue);
    }

    @Override
    public void set(String key, Object value, int expires) {
        String gsonValue = this.getGson().toJson(value);
        client.set(key, expires, gsonValue);
    }
    
    @Override
    public Object getRaw(String key) {
        return client.get(key);
    }
    
    @Override
    public void setRaw(String key, Object value) {
        client.set(key, value);
    }

    @Override
    public <T extends Object> List<T> getRange(String prefix, Class<T> type) {
        return this.getRange(prefix, type, true);
    }
    
    @Override
    public <T extends Object> List<T> getRange(String prefix, Class<T> type, boolean allowStale) {
        return this.getRange("all", "all", prefix, type, allowStale);
    }
    
    private <T extends Object> List<T> getRange(String designDocument, String viewName,String prefix, Class<T> type, boolean allowStale) {
        View view = client.getView(designDocument,viewName);
        Query query = new Query();
        query.setIncludeDocs(true);
        query.setRangeStart(prefix + "::");
        query.setRangeEnd(prefix + ":Z");
        if (!allowStale) {
            query.setStale(Stale.FALSE);
        }
        ViewResponse response = client.query(view, query);
        List<T> list = new LinkedList<T>();
        Gson g = this.getGson();
        for (ViewRow row : response) {
            if (row.getDocument() != null) {
                if (type.equals(String.class)) {
                    list.add((T)row.getDocument().toString());
                } else {
                    list.add(g.fromJson(row.getDocument().toString(), type));
                }
            }
        }
        return list;
    }
    
    public List<String> getKeysInRange(String prefix, boolean allowStale) {
        return this.getKeysInRange(prefix+"/", prefix+"0", allowStale);
    }
    
    public List<String> getKeysInRange(String start, String end, boolean allowStale) {
        return this.getKeysInRange("all", "all", start, end, allowStale, 0);
    }
    
     private List<String> getKeysInRange(String designDocument, String viewName,String start, String end, boolean allowStale, int groupLevel) {
        View view = client.getView(designDocument,viewName);
        Query query = new Query();
        query.setIncludeDocs(false);
        query.setRangeStart(start);
        query.setRangeEnd(end);
        if(groupLevel>0) {
            query.setGroupLevel(groupLevel);
        }
        if (!allowStale) {
            query.setStale(Stale.FALSE);
        }
        ViewResponse response = client.query(view, query);
        List<String> list = new LinkedList<String>();
        Gson g = this.getGson();
        for (ViewRow row : response) {
            list.add(row.getId());
        }
        return list;
    }

    /**
     * Does a multi-get and returns the keys received in the order they're
     * received
     *
     * @param <T>
     * @param keys
     * @param type
     * @return
     */
    @Override
    public <T extends Object> Collection<KeyValuePair<T>> multiGet(Collection<String> keys, Class<T> type) {
        Map<String, Object> bulk = client.getBulk(keys);
        Gson g = this.getGson();
        Collection<KeyValuePair<T>> results = new LinkedList<KeyValuePair<T>>();
        for (String key : keys) {
            if (bulk.containsKey(key)) {
                Object o = bulk.get(key);
                System.out.println(o);
                T value = g.fromJson(o.toString(), type);
                KeyValuePair<T> pair = new KeyValuePair<T>(key, value);
                results.add(pair);
            }
        }
        return results;
    }

    @Override
    public <T extends Object> Collection<T> multiGetCollection(Collection<String> keys, Class<T> type) {
        Map<String, Object> bulk = client.getBulk(keys);
        Gson g = this.getGson();
        Collection<T> results = new LinkedList<T>();
        for (String key : keys) {
            if (bulk.containsKey(key)) {
                Object o = bulk.get(key);
                System.out.println(o);
                T value = g.fromJson(o.toString(), type);
                results.add(value);
            }
        }
        return results;
    }

    @Override
    public Future<Boolean> delete(String key) {
        return client.delete(key);
    }

    @Override
    public void deleteAll(String prefix) {
        View view = client.getView("all", "all");
        Query query = new Query();
        query.setIncludeDocs(false);
        query.setRangeStart(prefix + "::");
        query.setRangeEnd(prefix + ":Z");
        ViewResponse response = client.query(view, query);
        for (ViewRow row : response) {
            this.delete(row.getId());
        }
    }

    private Gson getGson() {
        Gson g = new Gson();
        return g;
    }

    @Override
    public void finalize() {
        client.shutdown();
    }
}
<commit_msg>remove reference to DI in DB lib<commit_after>package ca.omny.documentdb;

import com.couchbase.client.CouchbaseClient;
import com.couchbase.client.protocol.views.Query;
import com.couchbase.client.protocol.views.Stale;
import com.couchbase.client.protocol.views.View;
import com.couchbase.client.protocol.views.ViewResponse;
import com.couchbase.client.protocol.views.ViewRow;
import com.google.gson.Gson;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import net.spy.memcached.util.StringUtils;

public class CouchQuerier implements IDocumentQuerier {

    CouchbaseClient client;

    public CouchQuerier() {
        this.client = CouchFactory.getClient();
    }

    public CouchQuerier(CouchbaseClient client) {
        this.client = client;
    }
    
    @Override
    public String getKey(String... keyParts) {
        String key = StringUtils.join(Arrays.asList(keyParts), "::");
        return key;
    }

    @Override
    public <T extends Object> T get(String key, Class<T> type) {
        Object o = client.get(key);
        if (o != null) {
            String value = o.toString();
            try {
                return getGson().fromJson(value, type);
            } catch (com.google.gson.JsonSyntaxException syntaxException) {
            }
            if (type.equals(String.class)) {
                return (T) value;
            }
        }

        return null;
    }

    @Override
    public void set(String key, Object value) {
        String gsonValue = this.getGson().toJson(value);
        client.set(key, gsonValue);
    }

    @Override
    public void set(String key, Object value, int expires) {
        String gsonValue = this.getGson().toJson(value);
        client.set(key, expires, gsonValue);
    }
    
    @Override
    public Object getRaw(String key) {
        return client.get(key);
    }
    
    @Override
    public void setRaw(String key, Object value) {
        client.set(key, value);
    }

    @Override
    public <T extends Object> List<T> getRange(String prefix, Class<T> type) {
        return this.getRange(prefix, type, true);
    }
    
    @Override
    public <T extends Object> List<T> getRange(String prefix, Class<T> type, boolean allowStale) {
        return this.getRange("all", "all", prefix, type, allowStale);
    }
    
    private <T extends Object> List<T> getRange(String designDocument, String viewName,String prefix, Class<T> type, boolean allowStale) {
        View view = client.getView(designDocument,viewName);
        Query query = new Query();
        query.setIncludeDocs(true);
        query.setRangeStart(prefix + "::");
        query.setRangeEnd(prefix + ":Z");
        if (!allowStale) {
            query.setStale(Stale.FALSE);
        }
        ViewResponse response = client.query(view, query);
        List<T> list = new LinkedList<T>();
        Gson g = this.getGson();
        for (ViewRow row : response) {
            if (row.getDocument() != null) {
                if (type.equals(String.class)) {
                    list.add((T)row.getDocument().toString());
                } else {
                    list.add(g.fromJson(row.getDocument().toString(), type));
                }
            }
        }
        return list;
    }
    
    public List<String> getKeysInRange(String prefix, boolean allowStale) {
        return this.getKeysInRange(prefix+"/", prefix+"0", allowStale);
    }
    
    public List<String> getKeysInRange(String start, String end, boolean allowStale) {
        return this.getKeysInRange("all", "all", start, end, allowStale, 0);
    }
    
     private List<String> getKeysInRange(String designDocument, String viewName,String start, String end, boolean allowStale, int groupLevel) {
        View view = client.getView(designDocument,viewName);
        Query query = new Query();
        query.setIncludeDocs(false);
        query.setRangeStart(start);
        query.setRangeEnd(end);
        if(groupLevel>0) {
            query.setGroupLevel(groupLevel);
        }
        if (!allowStale) {
            query.setStale(Stale.FALSE);
        }
        ViewResponse response = client.query(view, query);
        List<String> list = new LinkedList<String>();
        Gson g = this.getGson();
        for (ViewRow row : response) {
            list.add(row.getId());
        }
        return list;
    }

    /**
     * Does a multi-get and returns the keys received in the order they're
     * received
     *
     * @param <T>
     * @param keys
     * @param type
     * @return
     */
    @Override
    public <T extends Object> Collection<KeyValuePair<T>> multiGet(Collection<String> keys, Class<T> type) {
        Map<String, Object> bulk = client.getBulk(keys);
        Gson g = this.getGson();
        Collection<KeyValuePair<T>> results = new LinkedList<KeyValuePair<T>>();
        for (String key : keys) {
            if (bulk.containsKey(key)) {
                Object o = bulk.get(key);
                System.out.println(o);
                T value = g.fromJson(o.toString(), type);
                KeyValuePair<T> pair = new KeyValuePair<T>(key, value);
                results.add(pair);
            }
        }
        return results;
    }

    @Override
    public <T extends Object> Collection<T> multiGetCollection(Collection<String> keys, Class<T> type) {
        Map<String, Object> bulk = client.getBulk(keys);
        Gson g = this.getGson();
        Collection<T> results = new LinkedList<T>();
        for (String key : keys) {
            if (bulk.containsKey(key)) {
                Object o = bulk.get(key);
                System.out.println(o);
                T value = g.fromJson(o.toString(), type);
                results.add(value);
            }
        }
        return results;
    }

    @Override
    public Future<Boolean> delete(String key) {
        return client.delete(key);
    }

    @Override
    public void deleteAll(String prefix) {
        View view = client.getView("all", "all");
        Query query = new Query();
        query.setIncludeDocs(false);
        query.setRangeStart(prefix + "::");
        query.setRangeEnd(prefix + ":Z");
        ViewResponse response = client.query(view, query);
        for (ViewRow row : response) {
            this.delete(row.getId());
        }
    }

    private Gson getGson() {
        Gson g = new Gson();
        return g;
    }

    @Override
    public void finalize() {
        client.shutdown();
    }
}

{'fraction_non_alphanumeric': 0.11919975657994827, 'fraction_numerical': 0.00045641259698767686, 'mean_word_length': 2.1948967193195625, 'pattern_counts': {'":': 10, '<': 81, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5221942', 'n_tokens_mistral': 3897, 'n_tokens_neox': 3834, 'n_words': 883}