use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class StandardAuthManager method createProject.
@Override
public Id createProject(HugeProject project) {
E.checkArgument(!StringUtils.isEmpty(project.name()), "The name of project can't be null or empty");
return commit(() -> {
// Create project admin group
if (project.adminGroupId() == null) {
HugeGroup adminGroup = new HugeGroup("admin_" + project.name());
/*
* "creator" is a necessary parameter, other places are passed
* in "AuthManagerProxy", but here is the underlying module, so
* pass it directly here
*/
adminGroup.creator(project.creator());
Id adminGroupId = this.createGroup(adminGroup);
project.adminGroupId(adminGroupId);
}
// Create project op group
if (project.opGroupId() == null) {
HugeGroup opGroup = new HugeGroup("op_" + project.name());
// Ditto
opGroup.creator(project.creator());
Id opGroupId = this.createGroup(opGroup);
project.opGroupId(opGroupId);
}
// Create project target to verify permission
final String targetName = "project_res_" + project.name();
HugeResource resource = new HugeResource(ResourceType.PROJECT, project.name(), null);
HugeTarget target = new HugeTarget(targetName, this.graph.name(), "localhost:8080", ImmutableList.of(resource));
// Ditto
target.creator(project.creator());
Id targetId = this.targets.add(target);
project.targetId(targetId);
Id adminGroupId = project.adminGroupId();
Id opGroupId = project.opGroupId();
HugeAccess adminGroupWriteAccess = new HugeAccess(adminGroupId, targetId, HugePermission.WRITE);
// Ditto
adminGroupWriteAccess.creator(project.creator());
HugeAccess adminGroupReadAccess = new HugeAccess(adminGroupId, targetId, HugePermission.READ);
// Ditto
adminGroupReadAccess.creator(project.creator());
HugeAccess opGroupReadAccess = new HugeAccess(opGroupId, targetId, HugePermission.READ);
// Ditto
opGroupReadAccess.creator(project.creator());
this.access.add(adminGroupWriteAccess);
this.access.add(adminGroupReadAccess);
this.access.add(opGroupReadAccess);
return this.project.add(project);
});
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class StandardAuthManager method findUser.
@Override
public HugeUser findUser(String name) {
Id username = IdGenerator.of(name);
HugeUser user = this.usersCache.get(username);
if (user != null) {
return user;
}
List<HugeUser> users = this.users.query(P.NAME, name, 2L);
if (users.size() > 0) {
assert users.size() == 1;
user = users.get(0);
this.usersCache.update(username, user);
}
return user;
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class CachedGraphTransaction method queryEdgesFromBackend.
@Override
@Watched(prefix = "graphcache")
protected final Iterator<HugeEdge> queryEdgesFromBackend(Query query) {
RamTable ramtable = this.params().ramtable();
if (ramtable != null && ramtable.matched(query)) {
return ramtable.query(query);
}
if (!this.enableCacheEdge() || query.empty() || query.paging() || query.bigCapacity()) {
// Query all edges or query edges in paging, don't cache it
return super.queryEdgesFromBackend(query);
}
Id cacheKey = new QueryId(query);
Object value = this.edgesCache.get(cacheKey);
@SuppressWarnings("unchecked") Collection<HugeEdge> edges = (Collection<HugeEdge>) value;
if (value != null) {
for (HugeEdge edge : edges) {
if (edge.expired()) {
this.edgesCache.invalidate(cacheKey);
value = null;
}
}
}
if (value != null) {
// Not cached or the cache expired
return edges.iterator();
}
Iterator<HugeEdge> rs = super.queryEdgesFromBackend(query);
/*
* Iterator can't be cached, caching list instead
* there may be super node and too many edges in a query,
* try fetch a few of the head results and determine whether to cache.
*/
final int tryMax = 1 + MAX_CACHE_EDGES_PER_QUERY;
assert tryMax > MAX_CACHE_EDGES_PER_QUERY;
edges = new ArrayList<>(tryMax);
for (int i = 0; rs.hasNext() && i < tryMax; i++) {
edges.add(rs.next());
}
if (edges.size() == 0) {
this.edgesCache.update(cacheKey, Collections.emptyList());
} else if (edges.size() <= MAX_CACHE_EDGES_PER_QUERY) {
this.edgesCache.update(cacheKey, edges);
}
return new ExtendableIterator<>(edges.iterator(), rs);
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class CachedGraphTransaction method queryVerticesByIds.
@Watched(prefix = "graphcache")
private Iterator<HugeVertex> queryVerticesByIds(IdQuery query) {
if (query.idsSize() == 1) {
Id vertexId = query.ids().iterator().next();
HugeVertex vertex = (HugeVertex) this.verticesCache.get(vertexId);
if (vertex != null) {
if (!vertex.expired()) {
return QueryResults.iterator(vertex);
}
this.verticesCache.invalidate(vertexId);
}
Iterator<HugeVertex> rs = super.queryVerticesFromBackend(query);
vertex = QueryResults.one(rs);
if (vertex == null) {
return QueryResults.emptyIterator();
}
if (needCacheVertex(vertex)) {
this.verticesCache.update(vertex.id(), vertex);
}
return QueryResults.iterator(vertex);
}
IdQuery newQuery = new IdQuery(HugeType.VERTEX, query);
List<HugeVertex> vertices = new ArrayList<>();
for (Id vertexId : query.ids()) {
HugeVertex vertex = (HugeVertex) this.verticesCache.get(vertexId);
if (vertex == null) {
newQuery.query(vertexId);
} else if (vertex.expired()) {
newQuery.query(vertexId);
this.verticesCache.invalidate(vertexId);
} else {
vertices.add(vertex);
}
}
// Join results from cache and backend
ExtendableIterator<HugeVertex> results = new ExtendableIterator<>();
if (!vertices.isEmpty()) {
results.extend(vertices.iterator());
} else {
// Just use the origin query if find none from the cache
newQuery = query;
}
if (!newQuery.empty()) {
Iterator<HugeVertex> rs = super.queryVerticesFromBackend(newQuery);
// Generally there are not too much data with id query
ListIterator<HugeVertex> listIterator = QueryResults.toList(rs);
for (HugeVertex vertex : listIterator.list()) {
// Skip large vertex
if (needCacheVertex(vertex)) {
this.verticesCache.update(vertex.id(), vertex);
}
}
results.extend(listIterator);
}
return results;
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class HugeGraph method mapElId2Name.
default List<String> mapElId2Name(Collection<Id> ids) {
List<String> names = new ArrayList<>(ids.size());
for (Id id : ids) {
SchemaElement schema = this.edgeLabel(id);
names.add(schema.name());
}
return names;
}
Aggregations