Java Code Examples for javax.jdo.Query#setUnique()

The following examples show how to use javax.jdo.Query#setUnique() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdoUserClusterStore.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
public TransientUserClusters getTransientClusters(long checkpoint) {
	final PersistenceManager pm = getPM();
	Query query = pm.newQuery( "javax.jdo.query.SQL","select max(t_id) from user_clusters_transient");
	query.setResultClass(Long.class);
	query.setUnique(true);
	Long lastId =  (Long) query.execute();
	if (lastId != null && lastId > checkpoint)
	{
		if (logger.isDebugEnabled())
			logger.debug("Loading new transient clusters as checkpoint is "+checkpoint+" and found checkpoint is " + lastId);
		query = pm.newQuery( "javax.jdo.query.SQL","select user_id,t.cluster_id,weight,lastupdate,group_id from user_clusters_transient t, cluster_update, cluster_group where t.cluster_id=cluster_group.cluster_id and t.t_id>? order by user_id asc");
		query.setResultClass(UserCluster.class);
		List<UserCluster> clusters =  (List<UserCluster>) query.execute(checkpoint);
		return new TransientUserClusters(lastId,new ArrayList<>(clusters));
	}
	else
		return new TransientUserClusters(lastId,new ArrayList<UserCluster>());
}
 
Example 2
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
private void dropSentryRoleCore(PersistenceManager pm, String roleName)
    throws SentryNoSuchObjectException {
  String lRoleName = roleName.trim().toLowerCase();
  Query query = pm.newQuery(MSentryRole.class);
  query.setFilter("this.roleName == t");
  query.declareParameters("java.lang.String t");
  query.setUnique(true);
  MSentryRole sentryRole = (MSentryRole) query.execute(lRoleName);
  if (sentryRole == null) {
    throw new SentryNoSuchObjectException("Role: " + lRoleName + " doesn't exist");
  } else {
    pm.retrieve(sentryRole);
    int numPrivs = sentryRole.getPrivileges().size();
    sentryRole.removePrivileges();
    // with SENTRY-398 generic model
    sentryRole.removeGMPrivileges();
    privCleaner.incPrivRemoval(numPrivs);
    pm.deletePersistent(sentryRole);
  }
}
 
Example 3
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
private MSentryPrivilege getMSentryPrivilege(TSentryPrivilege tPriv, PersistenceManager pm) {
  Query query = pm.newQuery(MSentryPrivilege.class);
  query.setFilter("this.serverName == \"" + toNULLCol(safeTrimLower(tPriv.getServerName())) + "\" "
      + "&& this.dbName == \"" + toNULLCol(safeTrimLower(tPriv.getDbName())) + "\" "
      + "&& this.tableName == \"" + toNULLCol(safeTrimLower(tPriv.getTableName())) + "\" "
      + "&& this.columnName == \"" + toNULLCol(safeTrimLower(tPriv.getColumnName())) + "\" "
      + "&& this.URI == \"" + toNULLCol(safeTrim(tPriv.getURI())) + "\" "
      + "&& this.grantOption == grantOption "
      + "&& this.action == \"" + toNULLCol(safeTrimLower(tPriv.getAction())) + "\"");
  query.declareParameters("Boolean grantOption");
  query.setUnique(true);
  Boolean grantOption = null;
  if (tPriv.getGrantOption().equals(TSentryGrantOption.TRUE)) {
    grantOption = true;
  } else if (tPriv.getGrantOption().equals(TSentryGrantOption.FALSE)) {
    grantOption = false;
  }
  Object obj = query.execute(grantOption);
  if (obj != null) {
    return (MSentryPrivilege) obj;
  }
  return null;
}
 
Example 4
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
private void alterSentryRoleRevokePrivilegeCore(PersistenceManager pm,
    String roleName, TSentryPrivilege tPrivilege)
    throws SentryNoSuchObjectException, SentryInvalidInputException {
  Query query = pm.newQuery(MSentryRole.class);
  query.setFilter("this.roleName == t");
  query.declareParameters("java.lang.String t");
  query.setUnique(true);
  MSentryRole mRole = (MSentryRole) query.execute(roleName);
  if (mRole == null) {
    throw new SentryNoSuchObjectException("Role: " + roleName + " doesn't exist");
  } else {
    query = pm.newQuery(MSentryPrivilege.class);
    MSentryPrivilege mPrivilege = getMSentryPrivilege(tPrivilege, pm);
    if (mPrivilege == null) {
      mPrivilege = convertToMSentryPrivilege(tPrivilege);
    } else {
      mPrivilege = (MSentryPrivilege) pm.detachCopy(mPrivilege);
    }

    Set<MSentryPrivilege> privilegeGraph = Sets.newHashSet();
    if (mPrivilege.getGrantOption() != null) {
      privilegeGraph.add(mPrivilege);
    } else {
      MSentryPrivilege mTure = new MSentryPrivilege(mPrivilege);
      mTure.setGrantOption(true);
      privilegeGraph.add(mTure);
      MSentryPrivilege mFalse = new MSentryPrivilege(mPrivilege);
      mFalse.setGrantOption(false);
      privilegeGraph.add(mFalse);
    }
    // Get the privilege graph
    populateChildren(pm, Sets.newHashSet(roleName), mPrivilege, privilegeGraph);
    for (MSentryPrivilege childPriv : privilegeGraph) {
      revokePrivilegeFromRole(pm, tPrivilege, mRole, childPriv);
    }
    pm.makePersistent(mRole);
  }
}
 
Example 5
Source File: TestSentryRole.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
private MSentryRole getMSentryRole(PersistenceManager pm, String roleName) {
  Query query = pm.newQuery(MSentryRole.class);
  query.setFilter("this.roleName == t");
  query.declareParameters("java.lang.String t");
  query.setUnique(true);
  MSentryRole sentryRole = (MSentryRole) query.execute(roleName);
  return sentryRole;
}
 
Example 6
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
public Set<MSentryRole> getRolesForGroups(PersistenceManager pm, Set<String> groups) {
  Set<MSentryRole> result = new HashSet<MSentryRole>();
  Query query = pm.newQuery(MSentryGroup.class);
  query.setFilter("this.groupName == t");
  query.declareParameters("java.lang.String t");
  query.setUnique(true);
  for (String group : groups) {
    MSentryGroup sentryGroup = (MSentryGroup) query.execute(group.trim());
    if (sentryGroup != null) {
      result.addAll(sentryGroup.getRoles());
    }
  }
  return result;
}
 
Example 7
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
public Set<String> getRoleNamesForGroups(Set<String> groups) {
  Set<String> result = new HashSet<String>();
  boolean rollbackTransaction = true;
  PersistenceManager pm = null;
  try {
    pm = openTransaction();
    Query query = pm.newQuery(MSentryGroup.class);
    query.setFilter("this.groupName == t");
    query.declareParameters("java.lang.String t");
    query.setUnique(true);
    for (String group : groups) {
      MSentryGroup sentryGroup = (MSentryGroup) query.execute(group.trim());
      if (sentryGroup != null) {
        for (MSentryRole role : sentryGroup.getRoles()) {
          result.add(role.getRoleName());
        }
      }
    }
    rollbackTransaction = false;
    commitTransaction(pm);
    return result;
  } finally {
    if (rollbackTransaction) {
      rollbackTransaction(pm);
    }
  }
}
 
Example 8
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
MSentryRole getMSentryRoleByName(String roleName)
    throws SentryNoSuchObjectException {
  boolean rollbackTransaction = true;
  PersistenceManager pm = null;
  roleName = roleName.trim().toLowerCase();
  try {
    pm = openTransaction();
    Query query = pm.newQuery(MSentryRole.class);
    query.setFilter("this.roleName == t");
    query.declareParameters("java.lang.String t");
    query.setUnique(true);
    MSentryRole sentryRole = (MSentryRole) query.execute(roleName);
    if (sentryRole == null) {
      throw new SentryNoSuchObjectException("Role: " + roleName + " doesn't exist");
    } else {
      pm.retrieve(sentryRole);
    }
    rollbackTransaction = false;
    commitTransaction(pm);
    return sentryRole;
  } finally {
    if (rollbackTransaction) {
      rollbackTransaction(pm);
    }
  }
}
 
Example 9
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
public CommitContext alterSentryRoleDeleteGroups(String roleName,
    Set<TSentryGroup> groupNames)
        throws SentryNoSuchObjectException {
  boolean rollbackTransaction = true;
  PersistenceManager pm = null;
  roleName = roleName.trim().toLowerCase();
  try {
    pm = openTransaction();
    Query query = pm.newQuery(MSentryRole.class);
    query.setFilter("this.roleName == t");
    query.declareParameters("java.lang.String t");
    query.setUnique(true);
    MSentryRole role = (MSentryRole) query.execute(roleName);
    if (role == null) {
      throw new SentryNoSuchObjectException("Role: " + roleName + " doesn't exist");
    } else {
      query = pm.newQuery(MSentryGroup.class);
      query.setFilter("this.groupName == t");
      query.declareParameters("java.lang.String t");
      query.setUnique(true);
      List<MSentryGroup> groups = Lists.newArrayList();
      for (TSentryGroup tGroup : groupNames) {
        String groupName = tGroup.getGroupName().trim();
        MSentryGroup group = (MSentryGroup) query.execute(groupName);
        if (group != null) {
          group.removeRole(role);
          groups.add(group);
        }
      }
      pm.makePersistentAll(groups);
      CommitContext commit = commitUpdateTransaction(pm);
      rollbackTransaction = false;
      return commit;
    }
  } finally {
    if (rollbackTransaction) {
      rollbackTransaction(pm);
    }
  }
}
 
Example 10
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
private void alterSentryRoleAddGroupsCore(PersistenceManager pm, String roleName,
    Set<TSentryGroup> groupNames) throws SentryNoSuchObjectException {
  String lRoleName = roleName.trim().toLowerCase();
  Query query = pm.newQuery(MSentryRole.class);
  query.setFilter("this.roleName == t");
  query.declareParameters("java.lang.String t");
  query.setUnique(true);
  MSentryRole role = (MSentryRole) query.execute(lRoleName);
  if (role == null) {
    throw new SentryNoSuchObjectException("Role: " + lRoleName + " doesn't exist");
  } else {
    query = pm.newQuery(MSentryGroup.class);
    query.setFilter("this.groupName == t");
    query.declareParameters("java.lang.String t");
    query.setUnique(true);
    List<MSentryGroup> groups = Lists.newArrayList();
    for (TSentryGroup tGroup : groupNames) {
      String groupName = tGroup.getGroupName().trim();
      MSentryGroup group = (MSentryGroup) query.execute(groupName);
      if (group == null) {
        group = new MSentryGroup(groupName, System.currentTimeMillis(), Sets.newHashSet(role));
      }
      group.appendRole(role);
      groups.add(group);
    }
    pm.makePersistentAll(groups);
  }
}
 
Example 11
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Integer getItemCluster(long itemId) {
	Collection<Integer> res = new ArrayList<>();
	Query query = pm.newQuery( "javax.jdo.query.SQL", "select cluster_id from item_clusters where item_id="+itemId);
	query.setResultClass(Integer.class);
	query.setUnique(true);
	return (Integer) query.execute();
}
 
Example 12
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
/**
Get the MSentry object from roleName
Note: Should be called inside a transaction
 */
public MSentryRole getMSentryRole(PersistenceManager pm, String roleName) {
  Query query = pm.newQuery(MSentryRole.class);
  query.setFilter("this.roleName == t");
  query.declareParameters("java.lang.String t");
  query.setUnique(true);
  MSentryRole sentryRole = (MSentryRole) query.execute(roleName);
  return sentryRole;
}
 
Example 13
Source File: DelegateSentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
/**
 * The role is global in the generic model, such as the role may be has more than one component
 * privileges, so delete role will remove all privileges related to it.
 */
@Override
public CommitContext dropRole(String component, String role, String requestor)
    throws SentryNoSuchObjectException {
  boolean rollbackTransaction = true;
  PersistenceManager pm = null;
  role = toTrimmedLower(role);
  try {
    pm = openTransaction();
    Query query = pm.newQuery(MSentryRole.class);
    query.setFilter("this.roleName == t");
    query.declareParameters("java.lang.String t");
    query.setUnique(true);
    MSentryRole sentryRole = (MSentryRole) query.execute(role);
    if (sentryRole == null) {
      throw new SentryNoSuchObjectException("Role: " + role + " doesn't exist");
    } else {
      pm.retrieve(sentryRole);
      sentryRole.removeGMPrivileges();
      sentryRole.removePrivileges();
      pm.deletePersistent(sentryRole);
    }
    CommitContext commit = commitUpdateTransaction(pm);
    rollbackTransaction = false;
    return commit;
  } finally {
    if (rollbackTransaction) {
      rollbackTransaction(pm);
    }
  }
}
 
Example 14
Source File: JdoUserClusterStore.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public long getCurrentTimestamp() {
	final PersistenceManager pm = getPM();
	Query query = pm.newQuery( "javax.jdo.query.SQL","select lastupdate from cluster_update");
	query.setResultClass(Long.class);
	query.setUnique(true);
	return (Long) query.execute();
}
 
Example 15
Source File: JdoUserClusterStore.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public int getNumUsersWithClusters() {
	final PersistenceManager pm = getPM();
	Query query = pm.newQuery( "javax.jdo.query.SQL","select count(distinct user_id) from user_clusters");
	query.setResultClass(Integer.class);
	query.setUnique(true);
	return (Integer) query.execute();
}
 
Example 16
Source File: JdoClusterCountStore.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
/**
 * timestamp and time is ignore for db counts - the db value for these is used. They are assumed to be up-todate with clusters.
 */
@Override
public double getCount(int clusterId, long itemId,long timestamp) {
	final PersistenceManager pm = getPM();
	Query query = pm.newQuery( "javax.jdo.query.SQL", "select count from cluster_counts where id=? and item_id=?" );
	query.setResultClass(Double.class);
	query.setUnique(true);
	Double count = (Double) query.execute(clusterId, itemId);
	if (count != null)
		return count;
	else
		return 0D;
}
 
Example 17
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public Integer getDimensionForAttrName(long itemId, String name) {
	Query query = pm.newQuery( "javax.jdo.query.SQL", "select dim_id from dimension natural join item_map_enum join item_attr on (item_attr.attr_id=item_map_enum.attr_id) where item_attr.name=? and item_map_enum.item_id=?");
	query.setResultClass(Integer.class);
	query.setUnique(true);
	return (Integer) query.execute(name,itemId);
}
 
Example 18
Source File: PrivilegeOperatePersistence.java    From incubator-sentry with Apache License 2.0 4 votes vote down vote up
private MSentryGMPrivilege getPrivilege(MSentryGMPrivilege privilege, PersistenceManager pm) {
  Query query = pm.newQuery(MSentryGMPrivilege.class);
  query.setFilter(MSentryGMPrivilege.toQuery(privilege));
  query.setUnique(true);
  return (MSentryGMPrivilege)query.execute();
}