Android Open Source - DKO In Memory Query






From Project

Back to project page DKO.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project DKO listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.kered.dko;
// w w  w . j  a v a2  s. c  o m
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.kered.dko.Constants.DB_TYPE;
import org.kered.dko.Constants.DIRECTION;
import org.kered.dko.Field.FK;
import org.kered.dko.Table.__Alias;
import org.kered.dko.Table.__PrimaryKey;

class InMemoryQuery<T extends Table> extends AbstractQuery<T> {

  List<T> cache = null;
  private List<Field<?>> selectFields;
  private Query<T> query;
  private boolean loaded = false;

  InMemoryQuery(final Query<T> query) {
    this(query, false);
  }

  InMemoryQuery(final Query<T> query, final boolean lazy) {
    super(query);
    this.query = query;
    if (!lazy) load();
  }

  private synchronized void load() {
    if (loaded) return;
    cache = new ArrayList<T>();
    this.selectFields = query.getSelectFields();
    for (final T t : query) {
      cache.add(t);
    }
    loaded = true;
  }

  private InMemoryQuery(final InMemoryQuery<T> q) {
    super(q);
    cache = new ArrayList<T>();
    loaded = true;
  }

  InMemoryQuery(final Class<T> type) {
    super(type);
    cache = new ArrayList<T>();
    loaded = true;
  }

  InMemoryQuery(final Class<T> type, final Iterable<T> items) {
    super(type);
    cache = new ArrayList<T>();
    for (final T t : items) {
      cache.add(t);
    }
    loaded = true;
  }

  @Override
  public Iterator<T> iterator() {
    if (!loaded) load();
    return Collections.unmodifiableList(cache).iterator();
  }

  @Override
  public Query<T> where(final Condition... conditions) {
    if (!loaded) load();
    final InMemoryQuery<T> q = new InMemoryQuery<T>(this);
    for (final T t : cache) {
      boolean include = true;
      for (final Condition c : conditions) {
        include &= c.matches(t);
      }
      if (include) q.cache.add(t);
    }
    return q;
  }

  @Override
  public long count() throws SQLException {
    if (!loaded) load();
    return cache.size();
  }

  @Override
  public Query<T> exclude(final Condition... conditions) {
    return null;
  }

  @Override
  public Query<T> orderBy(final Expression.OrderBy<?>... obes) {
    if (!loaded) load();
    final InMemoryQuery<T> q = new InMemoryQuery<T>(this);
    q.cache.addAll(cache);
    Collections.sort(q.cache, new Comparator<T>() {
      @SuppressWarnings({ "unchecked", "rawtypes" })
      @Override
      public int compare(final T o1, final T o2) {
        for (final Expression.OrderBy<?> obe : obes) {
          Field f = null;
          if (obe instanceof Field) {
            f = (Field) obe;
            final Comparable c1 = (Comparable) o1.get(f);
            final Object c2 = o2.get(f);
            if (c1 == null && c2 != null) return 1;
            final int c = c1.compareTo(c2);
            if (c != 0) return c;
          }
          else if (obe instanceof Field.OrderByField) {
            f = ((Field.OrderByField) obe).underlying;
            final Comparable c1 = (Comparable) o1.get(f);
            final Object c2 = o2.get(f);
            int mul = ((Field.OrderByField) obe).direction==DIRECTION.ASCENDING ? 1 : -1;
            if (c1 == null && c2 != null) return mul;
            final int c = c1.compareTo(c2) * mul;
            if (c != 0) return c;
          }
        }
        return 0;
      }});
    return q;
  }

  @Override
  public Query<T> limit(final long n) {
    if (!loaded) load();
    final InMemoryQuery<T> q = new InMemoryQuery<T>(this);
    q.cache = new ArrayList<T>();
    q.cache.addAll(cache.subList(0, Math.min((int)n, cache.size())));
    return q;
  }

  @Override
  public Query<T> distinct() {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Query<T> with(final FK... fields) {
    throw new UnsupportedOperationException("can't pre-pull FKs from in-memory queries");
  }

  @Override
  public Query<T> deferFields(final Field<?>... fields) {
    // do nothing
    return this;
  }

  @Override
  public Query<T> onlyFields(final Field<?>... fields) {
    // do nothing
    return this;
  }

  @Override
  public Query<T> deferFields(final Collection<Field<?>> fields) {
    // do nothing
    return this;
  }

  @Override
  public Query<T> onlyFields(final Collection<Field<?>> fields) {
    // do nothing
    return this;
  }

  @Override
  public int update() throws SQLException {
    throw new UnsupportedOperationException("can't update on an in-memory query");
  }

  @Override
  public int delete() throws SQLException {
    throw new UnsupportedOperationException("can't delete on an in-memory query");
  }

  @Override
  public Iterable<T> all() {
    if (!loaded) load();
    return Collections.unmodifiableList(cache);
  }

  @Override
  public Query<T> set(final Field<?> key, final Object value) {
    throw new UnsupportedOperationException("can't do this on an in-memory query");
  }

  @Override
  public Query<T> set(final Map<Field<?>, Object> values) {
    throw new UnsupportedOperationException("can't do this on an in-memory query");
  }

  @Override
  public Object insert() throws SQLException {
    throw new UnsupportedOperationException("can't insert on an in-memory query");
  }

  @Override
  public Query<T> use(final DataSource ds) {
    throw new UnsupportedOperationException("can't specify a DataSource " +
        "for an in-memory query");
  }

  @Override
  public Query<T> toMemory() {
    return this;
  }

  @SuppressWarnings({ "unchecked", "rawtypes" })
  @Override
  public Query<T> max() {
    throw new UnsupportedOperationException("max not supported yet on in-memory queries");
  }

  @Override
  public Query<T> min() {
    throw new UnsupportedOperationException("min not supported yet on in-memory queries");
  }

  @Override
  public Condition exists() {
    if (!loaded) load();
    if (cache.isEmpty()) return Condition.Literal.FALSE;
    else return Condition.Literal.TRUE;
  }

  @Override
  public DataSource getDataSource() {
    return null;
  }

  @Override
  public <S> Iterable<S> asIterableOf(final Field<S> field) {
    final List<S> ret = new ArrayList<S>();
    for (final T t : this) ret.add(t.get(field));
    return ret;
  }

  @SuppressWarnings("unchecked")
  @Override
  public T get(final __PrimaryKey<T> pk) {
    if (!loaded) load();
    if (cache==null || cache.size() == 0) return null;
    return get(Util.getPK(cache.get(0)).eq(pk));
  }

  @Override
  public Query<T> use(final Connection conn) {
    // do nothing
    return this;
  }

  @Override
  public List<Field<?>> getSelectFields() {
    if (!loaded) load();
    return selectFields;
  }

  @Override
  public Query<T> use(final DB_TYPE type) {
    // ignore
    return this;
  }

  @Override
  public Query<T> in(final Collection<T> set) {
    throw new UnsupportedOperationException("can't join on an in-memory query");
  }

  @Override
  public Query<T> avg() {
    // TODO Auto-generated method stub
    return null;
  }

}




Java Source Code List

.HelloWorld.java
org.kered.contactlensfinder.DB.java
org.kered.contactlensfinder.MainActivity.java
org.kered.contactlensfinder.ViewManufacturersActivity.java
org.kered.contactlensfinder.ViewProductsActivity.java
org.kered.contactlensfinder.ViewPropertiesActivity.java
org.kered.dko.AbstractQuery.java
org.kered.dko.Bulk.java
org.kered.dko.CSV.java
org.kered.dko.ClosableIterator.java
org.kered.dko.Condition.java
org.kered.dko.Constants.java
org.kered.dko.Context.java
org.kered.dko.DBQuery.java
org.kered.dko.DBRowIterator.java
org.kered.dko.Diff.java
org.kered.dko.DualIterator.java
org.kered.dko.Expression.java
org.kered.dko.Field.java
org.kered.dko.FilteringQuery.java
org.kered.dko.Function.java
org.kered.dko.InMemoryQuery.java
org.kered.dko.Join.java
org.kered.dko.LazyCacheIterable.java
org.kered.dko.LocalJoin.java
org.kered.dko.M.java
org.kered.dko.Main.java
org.kered.dko.MatryoshkaQuery.java
org.kered.dko.PeekableClosableIterator.java
org.kered.dko.PeekableIterator.java
org.kered.dko.QueryAddField.java
org.kered.dko.QueryFactory.java
org.kered.dko.QuerySnapshot.java
org.kered.dko.Query.java
org.kered.dko.SQLFunction.java
org.kered.dko.SelectAsMapIterable.java
org.kered.dko.SelectFromOAI.java
org.kered.dko.SelectSingleColumn.java
org.kered.dko.SoftJoinUtil.java
org.kered.dko.SoftJoin.java
org.kered.dko.SqlContext.java
org.kered.dko.Statistics.java
org.kered.dko.SubQueryField.java
org.kered.dko.TableInfo.java
org.kered.dko.TableWrapper.java
org.kered.dko.Table.java
org.kered.dko.TemporaryTableFactory.java
org.kered.dko.TmpTableBuilder.java
org.kered.dko.Tuple.java
org.kered.dko.UsageMonitor.java
org.kered.dko.UsageStats.java
org.kered.dko.Util.java
org.kered.dko.ant.ClassGenerator.java
org.kered.dko.ant.CodeGeneratorBase.java
org.kered.dko.ant.CodeGenerator.java
org.kered.dko.ant.DataSourceGenerator.java
org.kered.dko.ant.GsonGenerator.java
org.kered.dko.ant.JoinGenerator.java
org.kered.dko.ant.Main.java
org.kered.dko.ant.SchemaExtractorBase.java
org.kered.dko.ant.SchemaExtractor.java
org.kered.dko.ant.Util.java
org.kered.dko.datasource.CheapConnectionPoolingDataSource.java
org.kered.dko.datasource.ConnectionCountingDataSource.java
org.kered.dko.datasource.JDBCDriverDataSource.java
org.kered.dko.datasource.MatryoshkaDataSource.java
org.kered.dko.datasource.MirroredDataSource.java
org.kered.dko.datasource.ReflectedDataSource.java
org.kered.dko.datasource.SingleConnectionDataSource.java
org.kered.dko.datasource.SingleThreadedDataSource.java
org.kered.dko.datasource.UnClosableConnection.java
org.kered.dko.datasource.Util.java
org.kered.dko.json.CDL.java
org.kered.dko.json.CookieList.java
org.kered.dko.json.Cookie.java
org.kered.dko.json.HTTPTokener.java
org.kered.dko.json.HTTP.java
org.kered.dko.json.JSONArray.java
org.kered.dko.json.JSONException.java
org.kered.dko.json.JSONML.java
org.kered.dko.json.JSONObject.java
org.kered.dko.json.JSONString.java
org.kered.dko.json.JSONStringer.java
org.kered.dko.json.JSONTokener.java
org.kered.dko.json.JSONWriter.java
org.kered.dko.json.Pickle.java
org.kered.dko.json.XMLTokener.java
org.kered.dko.json.XML.java
org.kered.dko.junk.DerbyLoadTestSchema.java
org.kered.dko.junk.OracleCreateTestUser.java
org.kered.dko.junk.OracleLoadTestSchema.java
org.kered.dko.persistence.Util.java
org.kered.dko.util.DumpDatabase.java
sakila.Example0.java
sakila.Example1.java
sakila.Example2.java
sakila.Util.java