Ejemplo n.º 1
0
  @Override
  public PrimitiveIntIterator nodeGetLabels(long nodeId) {
    try {
      final long[] labels = parseLabelsField(nodeStore.getRecord(nodeId)).get(nodeStore);
      return new PrimitiveIntIterator() {
        private int cursor;

        @Override
        public boolean hasNext() {
          return cursor < labels.length;
        }

        @Override
        public int next() {
          if (!hasNext()) {
            throw new NoSuchElementException();
          }
          return safeCastLongToInt(labels[cursor++]);
        }
      };

    } catch (
        InvalidRecordException
            e) { // TODO Might hide invalid dynamic record problem. It's here because this method
      // might get called with a nodeId that doesn't exist.
      return PrimitiveIntCollections.emptyIterator();
    }
  }
  @Test
  public void correctlySaysNodeIsDeleted() throws Exception {
    // Given
    state.nodeDoDelete(1l);
    Node node = mock(Node.class);
    when(node.getId()).thenReturn(1l);
    when(ops.nodeGetAllProperties(1l)).thenReturn(IteratorUtil.<DefinedProperty>emptyIterator());
    when(ops.nodeGetLabels(1l)).thenReturn(PrimitiveIntCollections.emptyIterator());

    // When & Then
    assertThat(snapshot().isDeleted(node), equalTo(true));
  }
 public static PrimitiveIntIterator nodeGetLabels(
     StoreReadLayer storeLayer, ReadableTxState txState, long nodeId)
     throws EntityNotFoundException {
   if (txState.nodeIsDeletedInThisTx(nodeId)) {
     return PrimitiveIntCollections.emptyIterator();
   }
   if (txState.nodeIsAddedInThisTx(nodeId)) {
     return PrimitiveIntCollections.toPrimitiveIterator(
         txState.nodeStateLabelDiffSets(nodeId).getAdded().iterator());
   }
   return txState.nodeStateLabelDiffSets(nodeId).augment(storeLayer.nodeGetLabels(nodeId));
 }
  @Test
  public void shouldListAddedLabels() throws Exception {
    // Given
    state.nodeDoAddLabel(2, 1l);
    when(ops.labelGetName(2)).thenReturn("theLabel");
    when(ops.nodeGetLabels(1l)).thenReturn(PrimitiveIntCollections.emptyIterator());

    // When
    Iterable<LabelEntry> labelEntries = snapshot().assignedLabels();

    // Then
    LabelEntry entry = single(labelEntries);
    assertThat(entry.label().name(), equalTo("theLabel"));
    assertThat(entry.node().getId(), equalTo(1l));
  }
  @Test
  public void showsDeletedNodes() throws Exception {
    // Given
    state.nodeDoDelete(1l);
    state.nodeDoDelete(2l);
    when(ops.nodeGetAllProperties(1l)).thenReturn(IteratorUtil.<DefinedProperty>emptyIterator());
    when(ops.nodeGetAllProperties(2l))
        .thenReturn(asList(Property.stringProperty(1, "p")).iterator());
    when(ops.nodeGetLabels(1l)).thenReturn(PrimitiveIntCollections.emptyIterator());
    when(ops.nodeGetLabels(2l)).thenReturn(PrimitiveIntCollections.iterator(15));
    when(ops.propertyKeyGetName(1)).thenReturn("key");
    when(ops.labelGetName(15)).thenReturn("label");

    // When & Then
    TxStateTransactionDataSnapshot snapshot = snapshot();
    assertThat(idList(snapshot.deletedNodes()), equalTo(asList(1l, 2l)));
    assertThat(single(snapshot.removedLabels()).label().name(), equalTo("label"));
    assertThat(single(snapshot.removedNodeProperties()).key(), equalTo("key"));
  }
  @Override
  public PrimitiveIntIterator nodeGetLabels(KernelStatement state, long nodeId)
      throws EntityNotFoundException {
    if (state.hasTxStateWithChanges()) {
      if (state.txState().nodeIsDeletedInThisTx(nodeId)) {
        return PrimitiveIntCollections.emptyIterator();
      }

      if (state.txState().nodeIsAddedInThisTx(nodeId)) {
        return PrimitiveIntCollections.toPrimitiveIterator(
            state.txState().nodeStateLabelDiffSets(nodeId).getAdded().iterator());
      }

      return state
          .txState()
          .nodeStateLabelDiffSets(nodeId)
          .augment(storeLayer.nodeGetLabels(nodeId));
    }

    return storeLayer.nodeGetLabels(nodeId);
  }
  @Override
  public PrimitiveIntIterator nodeGetRelationshipTypes(KernelStatement state, long nodeId)
      throws EntityNotFoundException {
    if (state.hasTxStateWithChanges() && state.txState().nodeModifiedInThisTx(nodeId)) {
      ReadableTxState tx = state.txState();
      if (tx.nodeIsDeletedInThisTx(nodeId)) {
        return PrimitiveIntCollections.emptyIterator();
      }

      if (tx.nodeIsAddedInThisTx(nodeId)) {
        return tx.nodeRelationshipTypes(nodeId);
      }

      Set<Integer> types = new HashSet<>();

      // Add types in the current transaction
      PrimitiveIntIterator typesInTx = tx.nodeRelationshipTypes(nodeId);
      while (typesInTx.hasNext()) {
        types.add(typesInTx.next());
      }

      // Augment with types stored on disk, minus any types where all rels of that type are deleted
      // in current tx.
      PrimitiveIntIterator committedTypes = storeLayer.nodeGetRelationshipTypes(nodeId);
      while (committedTypes.hasNext()) {
        int current = committedTypes.next();
        if (!types.contains(current) && nodeGetDegree(state, nodeId, Direction.BOTH, current) > 0) {
          types.add(current);
        }
      }

      return PrimitiveIntCollections.toPrimitiveIterator(types.iterator());
    } else {
      return storeLayer.nodeGetRelationshipTypes(nodeId);
    }
  }
Ejemplo n.º 8
0
 public PrimitiveIntIterator relationshipTypes() {
   if (hasAddedRelationships()) {
     return relationshipsAdded.relationshipTypes();
   }
   return PrimitiveIntCollections.emptyIterator();
 }