Keep it for indexing materialized views
14341
def indexes(table, stream)
        if (indexes = @connection.indexes(table)).any?
          add_index_statements = indexes.map do |index|
            table_name = remove_prefix_and_suffix(index.table).inspect
            "  add_index #{([table_name] + index_parts(index)).join(', ')}"
          end

          stream.puts add_index_statements.sort.join("\n")
          stream.puts
        end
      end
69048
@TimerJ
    public TableMetadataBuilder addIndex(IndexMetadata indexMetadata) {
        indexes.put(indexMetadata.getName(), indexMetadata);
        return this;
    }
1213127
def store_many(self, sql, values):
        """"""
        cursor = self.get_cursor()
        cursor.executemany(sql, values)
        self.conn.commit()
850053
def index(self, format='html'):
        """"""
        # url('datastores')
        results = meta.Session.query(DataStore)
        serialized_datastores = [{'id': ds.id, 'text': ds.name, 'href': h.url_for(controller="datastores", action="show", id=ds.id), 'type': ds.type} for ds in results]
        # prevent JSON Array Cross-site Exploits (XSRF/CSRF)
        return {'datastores': serialized_datastores}
595181
def _put_into_indexes(self, obj):
        
        no_of_used_indexes = 0
        for field_name, db_index in list(self._get_db_fields(obj)):
            attr_value = getattr(obj, field_name)

            if attr_value is None:  # index only by set attributes
                continue

            container = db_index.get(attr_value, None)
            if container is None:
                container = OOTreeSet()
                db_index[attr_value] = container

            container.insert(obj)

            no_of_used_indexes += 1

        # make sure that atleast one `attr_value` was used
        if no_of_used_indexes <= 0:
            raise UnindexableObject(
                "You have to use atleast one of the identificators!"
            )
1359176
protected int persistHydrant(
      FireHydrant indexToPersist,
      DataSchema schema,
      Interval interval,
      Map<String, Object> metadataElems
  )
  {
    synchronized (indexToPersist) {
      if (indexToPersist.hasSwapped()) {
        log.info(
            "DataSource[%s], Interval[%s], Hydrant[%s] already swapped. Ignoring request to persist.",
            schema.getDataSource(), interval, indexToPersist
        );
        return 0;
      }

      log.info(
          "DataSource[%s], Interval[%s], Metadata [%s] persisting Hydrant[%s]",
          schema.getDataSource(),
          interval,
          metadataElems,
          indexToPersist
      );
      try {
        int numRows = indexToPersist.getIndex().size();

        final IndexSpec indexSpec = config.getIndexSpec();

        indexToPersist.getIndex().getMetadata().putAll(metadataElems);
        final File persistedFile = indexMerger.persist(
            indexToPersist.getIndex(),
            interval,
            new File(computePersistDir(schema, interval), String.valueOf(indexToPersist.getCount())),
            indexSpec,
            config.getSegmentWriteOutMediumFactory()
        );

        indexToPersist.swapSegment(
            new QueryableIndexSegment(indexIO.loadIndex(persistedFile), indexToPersist.getSegmentId())
        );
        return numRows;
      }
      catch (IOException e) {
        log.makeAlert("dataSource[%s] -- incremental persist failed", schema.getDataSource())
           .addData("interval", interval)
           .addData("count", indexToPersist.getCount())
           .emit();

        throw new RuntimeException(e);
      }
    }
  }
592435
function () {
            var data = this._data;
            if (data.selectedIndex == -1) {
                // no item is selected
                return [];
            } else if (data.selectedIndex != null) {
                return [data.selectedIndex];
            } else {
                var retVal = [];
                // we iterate over the view, so that we return indexes in the order visible to the user:
                var view = data.itemsView;
                view.refresh(); // in case there are changes in the view, refresh it
                var items = view.items;
                for (var idx = 0, length = items.length; idx < length; idx++) {
                    if (items[idx].value.selected) {
                        // retVal.push(idx);
                        retVal.push(items[idx].initIndex);
                    }
                }
                return retVal;
            }
        }
422736
def _cache_index(self, dbname, collection, index, cache_for):
        """"""
        now = datetime.datetime.utcnow()
        expire = datetime.timedelta(seconds=cache_for) + now

        with self.__index_cache_lock:
            if dbname not in self.__index_cache:
                self.__index_cache[dbname] = {}
                self.__index_cache[dbname][collection] = {}
                self.__index_cache[dbname][collection][index] = expire

            elif collection not in self.__index_cache[dbname]:
                self.__index_cache[dbname][collection] = {}
                self.__index_cache[dbname][collection][index] = expire

            else:
                self.__index_cache[dbname][collection][index] = expire
618782
private static List<AbstractExpression> getGbyExpressions(MaterializedViewInfo mv) {
        try {
            return AbstractExpression.fromJSONArrayString(mv.getGroupbyexpressionsjson(), null);
        } catch (JSONException e) {
            return new ArrayList<>();
        }
    }
666636
def select_indexes(self, indexes, flags=QItemSelectionModel.Select | QItemSelectionModel.Rows):
        

        return self.select_view_indexes(indexes, flags)
1222601
def to_index(self, index_type, index_name, includes=None):
        
        return IndexField(self.name, self.data_type, index_type, index_name, includes)
129231
def _apply_cached_indexes(self, cached_indexes, persist=False):
        """"""
        # cacheable_dict = {}
        for elt in ['valid_input_index', 'valid_output_index',
                    'index_array', 'distance_array']:
            val = cached_indexes[elt]
            if isinstance(val, tuple):
                val = cached_indexes[elt][0]
            elif isinstance(val, np.ndarray):
                val = da.from_array(val, chunks=CHUNK_SIZE)
            elif persist and isinstance(val, da.Array):
                cached_indexes[elt] = val = val.persist()
            setattr(self.resampler, elt, val)
481695
private static Index findBestMatchIndexForMatviewMinOrMax(MaterializedViewInfo matviewinfo,
            Table srcTable, List<AbstractExpression> groupbyExprs, AbstractExpression minMaxAggExpr) {
        CatalogMap<Index> allIndexes = srcTable.getIndexes();
        StmtTableScan tableScan = new StmtTargetTableScan(srcTable);

        // Candidate index. If we can find an index covering both group-by columns and aggExpr (optimal) then we will
        // return immediately.
        // If the index found covers only group-by columns (sub-optimal), we will first cache it here.
        Index candidate = null;
        for (Index index : allIndexes) {
            // indexOptimalForMinMax == true if the index covered both the group-by columns and the min/max aggExpr.
            boolean indexOptimalForMinMax = false;
            // If minMaxAggExpr is not null, the diff can be zero or one.
            // Otherwise, for a usable index, its number of columns must agree with that of the group-by columns.
            final int diffAllowance = minMaxAggExpr == null ? 0 : 1;

            // Get all indexed exprs if there is any.
            String expressionjson = index.getExpressionsjson();
            List<AbstractExpression> indexedExprs = null;
            if ( ! expressionjson.isEmpty() ) {
                try {
                    indexedExprs = AbstractExpression.fromJSONArrayString(expressionjson, tableScan);
                } catch (JSONException e) {
                    e.printStackTrace();
                    assert(false);
                    return null;
                }
            }
            // Get source table columns.
            List<Column> srcColumnArray = CatalogUtil.getSortedCatalogItems(srcTable.getColumns(), "index");
            MatViewIndexMatchingGroupby matchingCase = null;

            if (groupbyExprs == null) {
                // This means group-by columns are all simple columns.
                // It also means we can only access the group-by columns by colref.
                List<ColumnRef> groupbyColRefs =
                    CatalogUtil.getSortedCatalogItems(matviewinfo.getGroupbycols(), "index");
                if (indexedExprs == null) {
                    matchingCase = MatViewIndexMatchingGroupby.GB_COL_IDX_COL;

                    // All the columns in the index are also simple columns, EASY! colref vs. colref
                    List<ColumnRef> indexedColRefs =
                        CatalogUtil.getSortedCatalogItems(index.getColumns(), "index");
                    // The number of columns in index can never be less than that in the group-by column list.
                    // If minMaxAggExpr == null, they must be equal (diffAllowance == 0)
                    // Otherwise they may be equal (sub-optimal) or
                    // indexedColRefs.size() == groupbyColRefs.size() + 1 (optimal, diffAllowance == 1)
                    if (isInvalidIndexCandidate(indexedColRefs.size(), groupbyColRefs.size(), diffAllowance)) {
                        continue;
                    }

                    if (! isGroupbyMatchingIndex(matchingCase, groupbyColRefs, null, indexedColRefs, null, null)) {
                        continue;
                    }
                    if (isValidIndexCandidateForMinMax(indexedColRefs.size(), groupbyColRefs.size(), diffAllowance)) {
                        if(! isIndexOptimalForMinMax(matchingCase, minMaxAggExpr, indexedColRefs, null, srcColumnArray)) {
                            continue;
                        }
                        indexOptimalForMinMax = true;
                    }
                }
                else {
                    matchingCase = MatViewIndexMatchingGroupby.GB_COL_IDX_EXP;
                    // In this branch, group-by columns are simple columns, but the index contains complex columns.
                    // So it's only safe to access the index columns from indexedExprs.
                    // You can still get something from indexedColRefs, but they will be inaccurate.
                    // e.g.: ONE index column (a+b) will get you TWO separate entries {a, b} in indexedColRefs.
                    // In order to compare columns: for group-by columns: convert colref => col
                    //                              for    index columns: convert    tve => col
                    if (isInvalidIndexCandidate(indexedExprs.size(), groupbyColRefs.size(), diffAllowance)) {
                        continue;
                    }

                    if (! isGroupbyMatchingIndex(matchingCase, groupbyColRefs, null, null, indexedExprs, srcColumnArray)) {
                        continue;
                    }
                    if (isValidIndexCandidateForMinMax(indexedExprs.size(), groupbyColRefs.size(), diffAllowance)) {
                        if(! isIndexOptimalForMinMax(matchingCase, minMaxAggExpr, null, indexedExprs, null)) {
                            continue;
                        }
                        indexOptimalForMinMax = true;
                    }
                }
            }
            else {
                matchingCase = MatViewIndexMatchingGroupby.GB_EXP_IDX_EXP;
                // This means group-by columns have complex columns.
                // It's only safe to access the group-by columns from groupbyExprs.
                // AND, indexedExprs must not be null in this case. (yeah!)
                if ( indexedExprs == null ) {
                    continue;
                }
                if (isInvalidIndexCandidate(indexedExprs.size(), groupbyExprs.size(), diffAllowance)) {
                    continue;
                }

                if (! isGroupbyMatchingIndex(matchingCase, null, groupbyExprs, null, indexedExprs, null)) {
                    continue;
                }

                if (isValidIndexCandidateForMinMax(indexedExprs.size(), groupbyExprs.size(), diffAllowance)) {
                    if (! isIndexOptimalForMinMax(matchingCase, minMaxAggExpr, null, indexedExprs, null)) {
                        continue;
                    }
                    indexOptimalForMinMax = true;
                }
            }

            // NOW index at least covered all group-by columns (sub-optimal candidate)
            if (!index.getPredicatejson().isEmpty()) {
                // Additional check for partial indexes to make sure matview WHERE clause
                // covers the partial index predicate
                List<AbstractExpression> coveringExprs = new ArrayList<>();
                List<AbstractExpression> exactMatchCoveringExprs = new ArrayList<>();
                try {
                    String encodedPredicate = matviewinfo.getPredicate();
                    if (!encodedPredicate.isEmpty()) {
                        String predicate = Encoder.hexDecodeToString(encodedPredicate);
                        AbstractExpression matViewPredicate = AbstractExpression.fromJSONString(predicate, tableScan);
                        coveringExprs.addAll(ExpressionUtil.uncombineAny(matViewPredicate));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    assert(false);
                    return null;
                }
                String predicatejson = index.getPredicatejson();
                if ( ! predicatejson.isEmpty() &&
                        ! SubPlanAssembler.isPartialIndexPredicateCovered(
                                tableScan, coveringExprs,
                                predicatejson, exactMatchCoveringExprs)) {
                    // the partial index predicate does not match the MatView's
                    // where clause -- give up on this index
                    continue;
                }
            }
            // if the index already covered group by columns and the aggCol/aggExpr,
            // it is already the best index we can get, return immediately.
            if (indexOptimalForMinMax) {
                return index;
            }
            // otherwise wait to see if we can find something better!
            candidate = index;
        }
        return candidate;
    }
290936
def index(self, refresh=False):
        """"""
        es = connections.get_connection("default")
        index = self.__class__.search_objects.mapping.index
        doc_type = self.__class__.search_objects.mapping.doc_type
        es.index(index, doc_type,
                 id=self.pk,
                 body=self.to_dict(),
                 refresh=refresh)
348319
@Override
    public Status updateIndexes(List<ViewStore> inputViews) throws CouchbaseLiteException {
        synchronized (lockUpdateIndexes) {
            assert (inputViews != null);

            // workaround
            if (!inputViews.contains(this))
                inputViews.add(this);

            final ArrayList<View> views = new ArrayList<View>(inputViews.size());
            final ArrayList<Mapper> mapBlocks = new ArrayList<Mapper>(inputViews.size());
            final ArrayList<String> docTypes = new ArrayList<String>(inputViews.size());
            boolean useDocType = false;
            for (ViewStore v : inputViews) {
                ForestDBViewStore view = (ForestDBViewStore) v;
                ViewStoreDelegate delegate = view.getDelegate();
                Mapper map = delegate != null ? delegate.getMap() : null;
                if (map == null) {
                    Log.v(Log.TAG_VIEW, "    %s has no map block; skipping it", view.getName());
                    continue;
                }
                try {
                    view.openIndex();
                } catch (ForestException e) {
                    throw new CouchbaseLiteException(ForestBridge.err2status(e));
                }
                views.add(view._view);
                mapBlocks.add(map);
                String docType = delegate.getDocumentType();
                docTypes.add(docType);
                if (docType != null && !useDocType)
                    useDocType = true;
            }

            if (views.size() == 0) {
                Log.v(TAG, "    No input views to update the index");
                return new Status(Status.NOT_MODIFIED);
            }

            boolean success = false;
            Indexer indexer = null;
            try {
                indexer = new Indexer(views.toArray(new View[views.size()]));
                indexer.triggerOnView(this._view);
                DocumentIterator itr;
                try {
                    itr = indexer.iterateDocuments();
                    if (itr == null)
                        return new Status(Status.NOT_MODIFIED);
                } catch (ForestException e) {
                    if (e.code == FDBErrors.FDB_RESULT_SUCCESS)
                        return new Status(Status.NOT_MODIFIED);
                    else
                        throw new CouchbaseLiteException(ForestBridge.err2status(e));
                }
                // Now enumerate the docs:
                Document doc;
                while ((doc = itr.nextDocument()) != null) {
                    // For each updated document:
                    try {
                        String docType = useDocType ? doc.getType() : null;
                        // Skip design docs
                        boolean validDocToIndex =
                                !doc.deleted() && !doc.getDocID().startsWith("_design/");
                        // Read the document body:
                        Map<String, Object> body = ForestBridge.bodyOfSelectedRevision(doc);
                        body.put("_id", doc.getDocID());
                        body.put("_rev", doc.getRevID());
                        body.put("_local_seq", doc.getSequence());
                        if (doc.conflicted()) {
                            List<String> currentRevIDs = ForestBridge.getCurrentRevisionIDs(doc);
                            if (currentRevIDs != null && currentRevIDs.size() > 1)
                                body.put("_conflicts",
                                        currentRevIDs.subList(1, currentRevIDs.size()));
                        }
                        // Feed it to each view's map function:
                        for (int viewNumber = 0; viewNumber < views.size(); viewNumber++) {
                            if (!indexer.shouldIndex(doc, viewNumber))
                                continue;

                            boolean indexIt = validDocToIndex;
                            if (indexIt && useDocType) {
                                String viewDocType = docTypes.get(viewNumber);
                                if (viewDocType != null)
                                    indexIt = viewDocType.equals(docType);
                            }

                            if (indexIt)
                                emit(indexer, viewNumber, doc, body, mapBlocks.get(viewNumber));
                            else
                                emit(indexer, viewNumber, doc, body, null);
                        }
                    } finally {
                        doc.free();
                    }
                }
                success = true;
            } catch (ForestException e) {
                throw new CouchbaseLiteException(ForestBridge.err2status(e));
            } finally {
                if (indexer != null) {
                    try {
                        indexer.endIndex(success);
                    } catch (ForestException ex) {
                        Log.e(TAG, "Failed to call Indexer.endIndex(boolean)", ex);
                        if (success)
                            throw new CouchbaseLiteException(ForestBridge.err2status(ex));
                    }
                }
            }
            Log.v(TAG, "... Finished re-indexing (%s)", viewNames(inputViews));
            return new Status(Status.OK);
        }
    }
1020673
def _get_indexes(self, schema, **kwargs):
        """"""
        ret = {}
        query_str = []
        query_str.append('SELECT')
        query_str.append('  tbl.relname AS table_name, i.relname AS index_name, a.attname AS field_name,')
        query_str.append('  ix.indkey AS index_order, a.attnum AS field_num')
        query_str.append('FROM')
        query_str.append('  pg_class tbl, pg_class i, pg_index ix, pg_attribute a')
        query_str.append('WHERE')
        query_str.append('  tbl.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = tbl.oid')
        query_str.append('  AND a.attnum = ANY(ix.indkey) AND tbl.relkind = %s AND tbl.relname = %s')
        query_str.append('ORDER BY')
        query_str.append('  tbl.relname, i.relname')
        query_str = os.linesep.join(query_str)

        indexes = self.query(query_str, 'r', str(schema), **kwargs)

        # massage the data into more readable {index_name: fields} format
        for idict in indexes:
            if idict['index_name'] not in ret:
                ret[idict['index_name']] = list(map(int, idict['index_order'].split(' ')))

            i = ret[idict['index_name']].index(idict['field_num'])
            ret[idict['index_name']][i] = idict['field_name']

        return ret
1026834
public static MaterializedViewInfo getMaterializedViewInfo(Table tbl) {
        MaterializedViewInfo mvInfo = null;
        Table source = tbl.getMaterializer();
        if (source != null) {
            mvInfo = source.getViews().get(tbl.getTypeName());
        }
        return mvInfo;
    }