Creates the hbase row.

@param m
the m
@param entity
the entity
@param rowId
the row id
@param relations
the relations
@return the hBase row
@throws IOException
Signals that an I/O exception has occurred.
761150
public HBaseRow createHbaseRow(EntityMetadata m, Object entity, Object rowId, List<RelationHolder> relations)
            throws IOException
    {
        MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel(
                m.getPersistenceUnit());
        EntityType entityType = metaModel.entity(m.getEntityClazz());
        Set<Attribute> attributes = entityType.getAttributes();
        if (metaModel.isEmbeddable(m.getIdAttribute().getBindableJavaType()))
        {
            rowId = KunderaCoreUtils.prepareCompositeKey(m, rowId);
        }
        HBaseRow hbaseRow = new HBaseRow(rowId, new ArrayList<HBaseCell>());
        // handle attributes and embeddables
        createCellsAndAddToRow(entity, metaModel, attributes, hbaseRow, m, -1, "");
        // handle relations
        if (relations != null && !relations.isEmpty())
        {
            hbaseRow.addCells(getRelationCell(m, rowId, relations));
        }
        // handle inheritence
        String discrColumn = ((AbstractManagedType) entityType).getDiscriminatorColumn();
        String discrValue = ((AbstractManagedType) entityType).getDiscriminatorValue();
        if (discrColumn != null && discrValue != null)
        {
            hbaseRow.addCell(new HBaseCell(m.getTableName(), discrColumn, discrValue));
        }
        return hbaseRow;
    }
1367948
private Relationship getOrCreateRelationshipWithUniqueFactory(Object entity, EntityMetadata m,
            GraphDatabaseService graphDb)
    {
        Object id = PropertyAccessorHelper.getObject(entity, (Field) m.getIdAttribute().getJavaMember());
        final String idFieldName = m.getIdAttribute().getName();

        UniqueFactory<Relationship> factory = new UniqueFactory.UniqueRelationshipFactory(graphDb, m.getIndexName())
        {

            @Override
            protected Relationship create(Map<String, Object> paramMap)
            {
                return null;
            }

            @Override
            protected void initialize(Relationship relationship, Map<String, Object> properties)
            {
                relationship.setProperty(idFieldName, properties.get(idFieldName));
            }
        };

        return factory.getOrCreate(idFieldName, id);
    }
272376
@Override
    public <E> List<E> getForeignKeysFromJoinTable(String schemaName, String joinTableName, Object rowKey,
            String inverseJoinColumnName)
    {
        List<E> foreignKeys = new ArrayList<E>();

        HTableInterface hTable = null;

        // Load raw data from Join Table in HBase
        try
        {
            hTable = gethTable(schemaName);

            List<HBaseData> results = hbaseReader.LoadData(hTable, joinTableName, rowKey, getFilter(joinTableName));

            // assuming rowKey is not null.
            if (results != null && !results.isEmpty())
            {

                HBaseData data = results.get(0);

                Map<String, byte[]> hbaseValues = data.getColumns();
                Set<String> columnNames = hbaseValues.keySet();

                for (String columnName : columnNames)
                {
                    if (columnName.startsWith(inverseJoinColumnName) && data.getColumnFamily().equals(joinTableName))
                    {
                        byte[] columnValue = data.getColumnValue(columnName);

                        // TODO : Because no attribute class is present, so
                        // cannot be done.
                        String hbaseColumnValue = Bytes.toString(columnValue);

                        foreignKeys.add((E) hbaseColumnValue);
                    }
                }
            }
        }
        catch (IOException e)
        {
            return foreignKeys;
        }
        finally
        {
            try
            {
                if (hTable != null)
                {
                    puthTable(hTable);
                }
            }
            catch (IOException e)
            {

                // Do nothing.
            }
        }
        return foreignKeys;
    }
769770
protected void computeEntityViaColumns(EntityMetadata m, boolean isRelation, List<String> relations,
        List<Object> entities, Map<ByteBuffer, List<Column>> qResults) {
        MetamodelImpl metaModel =
            (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel(m.getPersistenceUnit());

        EntityType entityType = metaModel.entity(m.getEntityClazz());

        List<AbstractManagedType> subManagedType = ((AbstractManagedType) entityType).getSubManagedType();

        for (ByteBuffer key : qResults.keySet()) {
            onColumn(m, isRelation, relations, entities, qResults.get(key), subManagedType, key);
        }
    }
360427
@Override
  public Clustering<MeanModel> run(Database database, Relation<V> relation) {
    // Database objects to process
    final DBIDs ids = relation.getDBIDs();
    // Choose initial means
    double[][] means = initializer.chooseInitialMeans(database, relation, k, getDistanceFunction());
    // Setup cluster assignment store
    List<ModifiableDBIDs> clusters = new ArrayList<>();
    for(int i = 0; i < k; i++) {
      clusters.add(DBIDUtil.newHashSet(relation.size() / k + 2));
    }

    // Meta data storage
    final WritableDataStore<Meta> metas = initializeMeta(relation, means);
    // Perform the initial assignment
    ArrayModifiableDBIDs tids = initialAssignment(clusters, metas, ids);
    // Recompute the means after the initial assignment
    means = means(clusters, means, relation);
    // Refine the result via k-means like iterations
    means = refineResult(relation, means, clusters, metas, tids);

    // Wrap result
    Clustering<MeanModel> result = new Clustering<>("k-Means Samesize Clustering", "kmeans-samesize-clustering");
    for(int i = 0; i < clusters.size(); i++) {
      result.addToplevelCluster(new Cluster<>(clusters.get(i), new MeanModel(means[i])));
    }
    return result;
  }
49212
protected void createIndexesOnColumns(EntityMetadata m, String tableName, List<Column> columns, Class columnType) {
        Object pooledConnection = null;
        try {
            Cassandra.Client api = null;
            pooledConnection = getConnection();
            api = (org.apache.cassandra.thrift.Cassandra.Client) getConnection(pooledConnection);
            KsDef ksDef = api.describe_keyspace(m.getSchema());
            List<CfDef> cfDefs = ksDef.getCf_defs();

            // Column family definition on which secondary index creation is
            // required
            CfDef columnFamilyDefToUpdate = null;
            boolean isUpdatable = false;
            for (CfDef cfDef : cfDefs) {
                if (cfDef.getName().equals(tableName)) {
                    columnFamilyDefToUpdate = cfDef;
                    break;
                }
            }

            if (columnFamilyDefToUpdate == null) {
                log.error("Join table {} not available.", tableName);
                throw new PersistenceException("table" + tableName + " not found!");
            }
            // create a column family, in case it is not already available.

            // Get list of indexes already created
            List<ColumnDef> columnMetadataList = columnFamilyDefToUpdate.getColumn_metadata();
            List<String> indexList = new ArrayList<String>();

            if (columnMetadataList != null) {
                for (ColumnDef columnDef : columnMetadataList) {
                    indexList.add(new StringAccessor().fromBytes(String.class, columnDef.getName()));
                }
                // need to set them to null else it is giving problem on update
                // column family and trying to add again existing indexes.
                // columnFamilyDefToUpdate.column_metadata = null;
            }

            // Iterate over all columns for creating secondary index on them
            for (Column column : columns) {

                ColumnDef columnDef = new ColumnDef();

                columnDef.setName(column.getName());
                columnDef.setValidation_class(CassandraValidationClassMapper.getValidationClass(columnType, false));
                columnDef.setIndex_type(IndexType.KEYS);

                // Add secondary index only if it's not already created
                // (if already created, it would be there in column family
                // definition)
                if (!indexList.contains(new StringAccessor().fromBytes(String.class, column.getName()))) {
                    isUpdatable = true;
                    columnFamilyDefToUpdate.addToColumn_metadata(columnDef);
                }
            }

            // Finally, update column family with modified column family
            // definition
            if (isUpdatable) {
                columnFamilyDefToUpdate.setKey_validation_class(CassandraValidationClassMapper
                    .getValidationClass(m.getIdAttribute().getJavaType(), isCql3Enabled(m)));
                api.system_update_column_family(columnFamilyDefToUpdate);
            }

        } catch (Exception e) {
            log.warn("Could not create secondary index on column family {}, Caused by: . ", tableName, e);

        } finally {
            releaseConnection(pooledConnection);
        }
    }
46840
protected List<Object> onCounterColumn(EntityMetadata m, boolean isRelation, List<String> relations,
        List<KeySlice> ks) {
        List<Object> entities;

        if (m.getType().isSuperColumnFamilyMetadata()) {
            if (log.isInfoEnabled()) {
                log.info("On counter column for super column family of entity {}.", m.getEntityClazz());
            }

            // TODO:: change it. remove column or super column helper
            Map<byte[], List<CounterSuperColumn>> results = new HashMap<byte[], List<CounterSuperColumn>>();

            List<CounterSuperColumn> counterColumns = null;
            for (KeySlice slice : ks) {
                counterColumns = new ArrayList<CounterSuperColumn>(slice.getColumnsSize());
                for (ColumnOrSuperColumn column : slice.columns) {
                    counterColumns.add(column.counter_super_column);
                }

                results.put(slice.getKey(), counterColumns);
            }

            entities = new ArrayList<Object>(results.size());

            for (byte[] key : results.keySet()) {
                Object e = null;
                Object id = PropertyAccessorHelper.getObject(m.getIdAttribute().getJavaType(), key);
                List<CounterSuperColumn> counterSuperColumns = results.get(key);
                ThriftRow tr = new ThriftRow(id, m.getTableName(), new ArrayList<Column>(0),
                    new ArrayList<SuperColumn>(0), new ArrayList<CounterColumn>(0), counterSuperColumns);
                e = getDataHandler().populateEntity(tr, m, KunderaCoreUtils.getEntity(e), relations, isRelation);
                entities.add(e);
            }
        } else {
            if (log.isInfoEnabled()) {
                log.info("On counter column for column family of entity {}", m.getEntityClazz());
            }

            Map<byte[], List<CounterColumn>> results = new HashMap<byte[], List<CounterColumn>>();

            List<CounterColumn> counterColumns = null;

            for (KeySlice slice : ks) {
                counterColumns = new ArrayList<CounterColumn>(slice.getColumnsSize());
                for (ColumnOrSuperColumn column : slice.columns) {
                    counterColumns.add(column.counter_column);
                }

                results.put(slice.getKey(), counterColumns);
            }

            entities = new ArrayList<Object>(results.size());

            for (byte[] key : results.keySet()) {
                Object e = null;
                Object id = PropertyAccessorHelper.getObject(m.getIdAttribute().getJavaType(), key);

                List<CounterColumn> columns = results.get(key);
                ThriftRow tr = new ThriftRow(id, m.getTableName(), new ArrayList<Column>(0),
                    new ArrayList<SuperColumn>(0), columns, new ArrayList<CounterSuperColumn>(0));
                e = getDataHandler().populateEntity(tr, m, KunderaCoreUtils.getEntity(e), relations, isRelation);

                if (e != null) {
                    entities.add(e);
                }
            }
        }
        return entities;
    }
995274
private void onRelationalAttributes(List<RelationHolder> rlHolders, Row row, Table schemaTable)
    {
        // Iterate over relations
        if (rlHolders != null && !rlHolders.isEmpty())
        {
            for (RelationHolder rh : rlHolders)
            {
                String relationName = rh.getRelationName();
                Object valueObj = rh.getRelationValue();

                if (!StringUtils.isEmpty(relationName) && valueObj != null)
                {
                    if (valueObj != null)
                    {
                        NoSqlDBUtils.add(schemaTable.getField(relationName), row, valueObj, relationName);
                        KunderaCoreUtils.printQuery(
                                "Add relation: relation name:" + relationName + "relation value:" + valueObj,
                                showQuery);
                    }
                }
            }
        }
    }
1253851
public Map<String, Object> createRelationshipProperties(EntityMetadata entityMetadata,
            EntityMetadata targetEntityMetadata, Object relationshipEntity)
    {
        MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel(
                entityMetadata.getPersistenceUnit());
        EntityType entityType = metaModel.entity(relationshipEntity.getClass());
        Map<String, Object> relationshipProperties = new HashMap<String, Object>();

        Set<Attribute> attributes = entityType.getSingularAttributes();
        for (Attribute attribute : attributes)
        {
            Field f = (Field) attribute.getJavaMember();
            if (!f.getType().equals(entityMetadata.getEntityClazz())
                    && !f.getType().equals(targetEntityMetadata.getEntityClazz()))
            {
                String relPropertyName = ((AbstractAttribute) attribute).getJPAColumnName();
                Object value = PropertyAccessorHelper.getObject(relationshipEntity, f);
                relationshipProperties.put(relPropertyName, toNeo4JProperty(value));
            }
        }
        return relationshipProperties;
    }
770048
public Clustering<PrototypeModel<O>> run(Relation<O> relation) {
    RangeQuery<O> rq = relation.getRangeQuery(getDistanceFunction(), threshold);

    ModifiableDBIDs seen = DBIDUtil.newHashSet(relation.size());
    Clustering<PrototypeModel<O>> clustering = new Clustering<>("Prototype clustering", "prototype-clustering");

    int queries = 0;
    FiniteProgress prog = LOG.isVerbose() ? new FiniteProgress("Leader clustering", relation.size(), LOG) : null;
    for(DBIDIter it = relation.iterDBIDs(); it.valid() && seen.size() < relation.size(); it.advance()) {
      if(seen.contains(it)) {
        continue;
      }
      DoubleDBIDList res = rq.getRangeForDBID(it, threshold);
      ++queries;
      ModifiableDBIDs ids = DBIDUtil.newArray(res.size());
      for(DBIDIter cand = res.iter(); cand.valid(); cand.advance()) {
        if(seen.add(cand)) {
          LOG.incrementProcessed(prog);
          ids.add(cand);
        }
      }
      assert (ids.size() > 0 && ids.contains(it));
      PrototypeModel<O> mod = new SimplePrototypeModel<>(relation.get(it));
      clustering.addToplevelCluster(new Cluster<>(ids, mod));
    }
    LOG.statistics(new LongStatistic(this.getClass().getName() + ".queries", queries));
    LOG.ensureCompleted(prog);
    return clustering;
  }
281539
@Override
    public List<EnhanceEntity> populateRelation(EntityMetadata m, Client client, int maxResults)
    {
        // TODO: maxresults to be taken care after work on pagination.

        List<EnhanceEntity> ls = null;
        List<String> relationNames = m.getRelationNames();
        boolean isParent = m.isParent();
        if (!isParent)
        {
            // if it is not a parent.
            String sqlQuery = null;
            if (MetadataUtils.useSecondryIndex(((ClientBase) client).getClientMetadata()))
            {
                sqlQuery = getSqlQueryFromJPA(m, relationNames, null);
            }
            else
            {
                // prepare lucene query and find.
                Set<String> rSet = fetchDataFromLucene(m.getEntityClazz(), client);
                if (rSet != null && !rSet.isEmpty())
                {
                    filter = "WHERE";
                }
                sqlQuery = getSqlQueryFromJPA(m, relationNames, rSet);
            }
            // call client with relation name list and convert to sql query.

            ls = populateEnhanceEntities(m, relationNames, client, sqlQuery);
        }
        else
        {
            if (MetadataUtils.useSecondryIndex(((ClientBase) client).getClientMetadata()))
            {
                try
                {
                    List entities = ((HibernateClient) client).find(getSqlQueryFromJPA(m, relationNames, null),
                            new ArrayList<String>(), m);
                    ls = new ArrayList<EnhanceEntity>(entities.size());
                    ls = transform(m, ls, entities);
                }
                catch (Exception e)
                {
                    log.error("Error while executing handleAssociation for RDBMS, Caused by {}.", e);
                    throw new QueryHandlerException(e);
                }
            }
            else
            {
                ls = onAssociationUsingLucene(m, client, ls);
            }
        }

        return ls;
    }
994372
private void parseRelations(String persistenceUnit, List<TableInfo> tableInfos, EntityMetadata entityMetadata,
            TableInfo tableInfo, List<Relation> relations)
    {
        for (Relation relation : relations)
        {
            if (relation != null)
            {
                Class entityClass = relation.getTargetEntity();
                EntityMetadata targetEntityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata,
                        entityClass);
                if (targetEntityMetadata == null)
                {
                    log.error("Persistence unit for class : " + entityClass + " is not loaded");
                    throw new SchemaGenerationException(
                            "Persistence unit for class : " + entityClass + " is not loaded");
                }
                ForeignKey relationType = relation.getType();

                // if relation type is one to many or join by primary key
                if (targetEntityMetadata != null && relationType.equals(ForeignKey.ONE_TO_MANY)
                        && relation.getJoinColumnName(kunderaMetadata) != null)
                {
                    // if self association
                    if (targetEntityMetadata.equals(entityMetadata))
                    {
                        tableInfo.addColumnInfo(getJoinColumn(tableInfo, relation.getJoinColumnName(kunderaMetadata),
                                entityMetadata.getIdAttribute().getJavaType()));
                    }
                    else
                    {
                        String pu = targetEntityMetadata.getPersistenceUnit();
                        Type targetEntityType = targetEntityMetadata.getType();
                        Class idClass = targetEntityMetadata.getIdAttribute().getJavaType();
                        String idName = ((AbstractAttribute) targetEntityMetadata.getIdAttribute()).getJPAColumnName();
                        TableInfo targetTableInfo = new TableInfo(targetEntityMetadata.getTableName(),
                                targetEntityType.name(), idClass, idName);

                        // In case of different persistence unit. case for poly
                        // glot
                        // persistence.
                        if (!pu.equals(persistenceUnit))
                        {
                            List<TableInfo> targetTableInfos = getSchemaInfo(pu);

                            addJoinColumnToInfo(relation.getJoinColumnName(kunderaMetadata), targetTableInfo,
                                    targetTableInfos, entityMetadata);

                            // add for newly discovered persistence unit.
                            puToSchemaMetadata.put(pu, targetTableInfos);
                        }
                        else
                        {
                            addJoinColumnToInfo(relation.getJoinColumnName(kunderaMetadata), targetTableInfo,
                                    tableInfos, entityMetadata);
                        }
                    }
                }
                // if relation type is one to one or many to one.
                else if (relation.isUnary() && relation.getJoinColumnName(kunderaMetadata) != null)
                {
                    if (!relation.isJoinedByPrimaryKey())
                    {
                        tableInfo.addColumnInfo(getJoinColumn(tableInfo, relation.getJoinColumnName(kunderaMetadata),
                                targetEntityMetadata.getIdAttribute().getJavaType()));
                    }
                }
                // if relation type is many to many and relation via join table.
                else if ((relationType.equals(ForeignKey.MANY_TO_MANY)) && (entityMetadata.isRelationViaJoinTable()))
                {
                    JoinTableMetadata joinTableMetadata = relation.getJoinTableMetadata();
                    String joinTableName = joinTableMetadata != null ? joinTableMetadata.getJoinTableName() : null;
                    String joinColumnName = joinTableMetadata != null
                            ? (String) joinTableMetadata.getJoinColumns().toArray()[0] : null;
                    String inverseJoinColumnName = joinTableMetadata != null
                            ? (String) joinTableMetadata.getInverseJoinColumns().toArray()[0] : null;
                    if (joinTableName != null)
                    {
                        TableInfo joinTableInfo = new TableInfo(joinTableName, Type.COLUMN_FAMILY.name(), String.class,
                                "key");
                        if (!tableInfos.isEmpty() && !tableInfos.contains(joinTableInfo) || tableInfos.isEmpty())
                        {
                            joinTableInfo.addColumnInfo(getJoinColumn(joinTableInfo, joinColumnName,
                                    entityMetadata.getIdAttribute().getJavaType()));
                            joinTableInfo.addColumnInfo(getJoinColumn(joinTableInfo, inverseJoinColumnName,
                                    targetEntityMetadata.getIdAttribute().getJavaType()));

                            // // Do not delete above lines. Currently join
                            // table
                            // columns are of type string only.
                            // // It needs to be fixed later.
                            // joinTableInfo.addColumnInfo(getJoinColumn(joinTableInfo,
                            // joinColumnName, String.class));
                            // joinTableInfo.addColumnInfo(getJoinColumn(joinTableInfo,
                            // inverseJoinColumnName, String.class));

                            tableInfos.add(joinTableInfo);
                        }
                    }
                }
            }
        }
    }
1324416
public Relation<K,V> create(Relation<K,V> r) {
	// default implementation
	Relation<K,V> r2 = this.create();
	for(K key : r.keys()) {
	    r2.addAll(key, r.getValues(key));
	}
	return r2;
    }
979238
@Override
    public void writeData(String tableName, EntityMetadata m, Object entity, Object rowId,
            List<RelationHolder> relations, boolean showQuery) throws IOException
    {
        HBaseRow hbaseRow = createHbaseRow(m, entity, rowId, relations);
        writeHbaseRowInATable(tableName, hbaseRow);
    }
255368
@Override
    public List<HBaseData> LoadData(HTableInterface hTable, String columnFamily, Object rowKey, Filter filter,
            String... columns) throws IOException
    {
        List<HBaseData> results = new ArrayList<HBaseData>();
        if (scanner == null)
        {

            // only in case of find by id
            Scan scan = null;
            if (rowKey != null)
            {
                byte[] rowKeyBytes = HBaseUtils.getBytes(rowKey);
                Get g = new Get(rowKeyBytes);

                // TODO: After more than one column family for 1 table. this
                // should work. currently failing for embeddable entities.
                if (columnFamily != null)
                {
                    g.addFamily(Bytes.toBytes(columnFamily));
                }

                if (filter != null)
                {
                    g.setFilter(filter);
                }
                Result result = hTable.get(g);

                if (result != null && result.list() != null)
                {
                    HBaseData data = null;
                    for (KeyValue value : result.list())
                    {
                        data = new HBaseData(columnFamily != null ? columnFamily : new String(value.getFamily()),
                                value.getRow());
                        break;
                    }

                    if (data != null)
                    {
                        data.setColumns(result.list());
                        results.add(data);
                    }

                }
                return results;

                // scan = new Scan(g);
            }
            else
            {
                scan = new Scan();
            }
            setScanCriteria(filter, columnFamily, null, scan, columns);
            scanner = hTable.getScanner(scan);
            resultsIter = scanner.iterator();
        }
        return scanResults(columnFamily, results);
    }
1150031
protected Cluster<SubspaceModel> makeCluster(Relation<V> relation, DBIDs C, long[] D) {
    DBIDs ids = DBIDUtil.newHashSet(C); // copy, also to lose distance values!
    Cluster<SubspaceModel> cluster = new Cluster<>(ids);
    cluster.setModel(new SubspaceModel(new Subspace(D), Centroid.make(relation, ids).getArrayRef()));
    return cluster;
  }
185600
@Override
    public List<Object> findParentEntityFromJoinTable(EntityMetadata parentMetadata, String joinTableName,
            String joinColumnName, String inverseJoinColumnName, Object childId)
    {
        throw new PersistenceException("Not applicable for HBase");
    }