PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/datasets/twitter/twitter-ingestion/src/main/java/io/lumify/twitter/loaders/UserVertexLoader.java

https://gitlab.com/zaverichintan/lumify
Java | 106 lines | 63 code | 26 blank | 17 comment | 6 complexity | 8d1e5c08974e17834fc6922778b82938 MD5 | raw file
  1. package io.lumify.twitter.loaders;
  2. import static com.google.common.base.Preconditions.checkNotNull;
  3. import io.lumify.core.model.properties.LumifyProperties;
  4. import io.lumify.core.model.user.UserRepository;
  5. import io.lumify.core.model.workQueue.WorkQueueRepository;
  6. import io.lumify.twitter.TwitterOntology;
  7. import java.util.concurrent.TimeUnit;
  8. import org.securegraph.Authorizations;
  9. import org.securegraph.Graph;
  10. import org.securegraph.Vertex;
  11. import org.securegraph.VertexBuilder;
  12. import com.google.common.base.Strings;
  13. import com.google.common.cache.Cache;
  14. import com.google.common.cache.CacheBuilder;
  15. import com.google.inject.Inject;
  16. /**
  17. * Responsible for loading Twitter user vertex information to/from the underlying data store
  18. */
  19. public class UserVertexLoader {
  20. private final Cache<String, Vertex> userVertexCache = CacheBuilder.newBuilder().expireAfterWrite(15, TimeUnit.MINUTES).build();
  21. private final Graph graph;
  22. private final WorkQueueRepository workQueueRepository;
  23. private final UserRepository userRepository;
  24. private final Authorizations authorizations;
  25. /**
  26. *
  27. * @param secureGraph The underlying graph data store instance, not null
  28. * @param workQueueRepo The work queue used to store pending operations, not null
  29. * @param userRepo The system user repository used for retrieving users known to the system, not null
  30. */
  31. @Inject
  32. public UserVertexLoader(final Graph secureGraph, final WorkQueueRepository workQueueRepo, final UserRepository userRepo) {
  33. graph = checkNotNull(secureGraph);
  34. workQueueRepository = checkNotNull(workQueueRepo);
  35. userRepository = checkNotNull(userRepo);
  36. authorizations = userRepository.getAuthorizations(userRepository.getSystemUser());
  37. }
  38. /**
  39. * Loads the vertex corresponding to the provided vertex details. If the vertex cannot be found,
  40. * a new one will be created.
  41. * @param userDetails The details corresponding to the vertex of interest, not null
  42. * @return The vertex corresponding to the details provided
  43. */
  44. public Vertex loadVertex(final UserVertexDetails userDetails) {
  45. checkNotNull(userDetails);
  46. final String profileImageUrl = userDetails.getProfileImageUrl();
  47. String vertexId = "TWITTER_USER_" + userDetails.getId();
  48. Vertex userVertex = userVertexCache.getIfPresent(vertexId);
  49. if( userVertex != null ) {
  50. return userVertex;
  51. }
  52. userVertex = graph.getVertex(vertexId, authorizations);
  53. if( userVertex == null ) {
  54. userVertex = createTwitterUserVertex(userDetails, vertexId);
  55. workQueueRepository.pushGraphPropertyQueue(userVertex, LumifyProperties.TITLE.getProperty(userVertex));
  56. if( !Strings.isNullOrEmpty(profileImageUrl) ) {
  57. workQueueRepository.pushGraphPropertyQueue(userVertex, TwitterOntology.PROFILE_IMAGE_URL.getProperty(userVertex));
  58. }
  59. workQueueRepository.pushGraphPropertyQueue(userVertex, TwitterOntology.SCREEN_NAME.getProperty(userVertex));
  60. }
  61. userVertexCache.put(vertexId, userVertex);
  62. return userVertex;
  63. }
  64. private Vertex createTwitterUserVertex(final UserVertexDetails userDetails, final String vertexId) {
  65. final VertexBuilder vertexBuilder = graph.prepareVertex(vertexId, LoaderConstants.EMPTY_VISIBILITY);
  66. // Set core ontology properties
  67. LumifyProperties.CONCEPT_TYPE.setProperty(vertexBuilder, TwitterOntology.CONCEPT_TYPE_USER, LoaderConstants.EMPTY_VISIBILITY);
  68. LumifyProperties.SOURCE.addPropertyValue(vertexBuilder, LoaderConstants.MULTI_VALUE_KEY, LoaderConstants.SOURCE_NAME, LoaderConstants.EMPTY_VISIBILITY);
  69. LumifyProperties.TITLE.addPropertyValue(vertexBuilder, LoaderConstants.MULTI_VALUE_KEY, userDetails.getName(), LoaderConstants.EMPTY_VISIBILITY);
  70. // Add tweet properties
  71. TwitterOntology.SCREEN_NAME.addPropertyValue(vertexBuilder, LoaderConstants.MULTI_VALUE_KEY, userDetails.getScreenName(), LoaderConstants.EMPTY_VISIBILITY);
  72. final String profileImageUrl = userDetails.getProfileImageUrl();
  73. if( !Strings.isNullOrEmpty(profileImageUrl) ) {
  74. TwitterOntology.PROFILE_IMAGE_URL.addPropertyValue(vertexBuilder, LoaderConstants.MULTI_VALUE_KEY, profileImageUrl, LoaderConstants.EMPTY_VISIBILITY);
  75. }
  76. final Vertex userVertex = vertexBuilder.save(authorizations);
  77. graph.flush();
  78. return userVertex;
  79. }
  80. }