Java Code Examples for it.unimi.dsi.fastutil.longs.LongArrayList#isEmpty()

The following examples show how to use it.unimi.dsi.fastutil.longs.LongArrayList#isEmpty() . 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: SnapshotCodecV4.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
@Override
protected void encodeInProgress(BinaryEncoder encoder, Map<Long, TransactionManager.InProgressTx> inProgress)
    throws IOException {

  if (!inProgress.isEmpty()) {
    encoder.writeInt(inProgress.size());
    for (Map.Entry<Long, TransactionManager.InProgressTx> entry : inProgress.entrySet()) {
      encoder.writeLong(entry.getKey()); // tx id
      encoder.writeLong(entry.getValue().getExpiration());
      encoder.writeLong(entry.getValue().getVisibilityUpperBound());
      encoder.writeInt(entry.getValue().getType().ordinal());
      // write checkpoint tx IDs
      LongArrayList checkpointPointers = entry.getValue().getCheckpointWritePointers();
      if (!checkpointPointers.isEmpty()) {
        encoder.writeInt(checkpointPointers.size());
        for (int i = 0; i < checkpointPointers.size(); i++) {
          encoder.writeLong(checkpointPointers.getLong(i));
        }
      }
      encoder.writeInt(0);
    }
  }
  encoder.writeInt(0); // zero denotes end of list as per AVRO spec
}
 
Example 2
Source File: MRR.java    From StreamingRec with Apache License 2.0 5 votes vote down vote up
@Override
public void evaluate(Transaction transaction, LongArrayList recommendations,
		LongOpenHashSet userTransactions) {
	//if there is no ground truth, there is nothing to evaluate
	if (userTransactions == null || userTransactions.isEmpty()) {
		return;
	}
	
	// if the algorithm does not return any recommendations, count it as 0
	if (recommendations.isEmpty()) {
		results.add(0);
		return;
	}

	// calculate the MRR
	// if the algorithm retrieves less than k recommendations, we calculate
	// the real k value for this case
	int realK = Math.min(k, recommendations.size());
	// iterate over relevant items and calculate recall rank
	for (Long itemID : userTransactions) {
		for (int i = 0; i < realK; i++) {
			if (itemID == recommendations.getLong(i)) {
				results.add(1d/(i+1));
				return;
			}
		}
	}
	//nothing found -> count as zero
	results.add(0);
}
 
Example 3
Source File: TransactionManager.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
private boolean doInvalidate(long writePointer) {
  ChangeSet previousChangeSet = committingChangeSets.remove(writePointer);
  // remove from in-progress set, so that it does not get excluded in the future
  InProgressTx previous = inProgress.remove(writePointer);

  // This check is to prevent from invalidating committed transactions
  if (previous != null || previousChangeSet != null) {
    // add tx to invalids
    invalidTxList.add(writePointer);
    if (previous == null) {
      LOG.debug("Invalidating tx {} in committing change sets but not in-progress", writePointer);
    } else {
      // invalidate any checkpoint write pointers
      LongArrayList childWritePointers = previous.getCheckpointWritePointers();
      if (!childWritePointers.isEmpty()) {
        invalidTxList.addAll(childWritePointers);
        inProgress.keySet().removeAll(childWritePointers);
      }
    }

    String clientId = DEFAULT_CLIENTID;
    if (previous != null && previous.getClientId() != null) {
      clientId = previous.getClientId();
    }
    LOG.info("Tx invalid list: added tx {} belonging to client '{}' because of invalidate", writePointer, clientId);
    if (previous != null && !previous.isLongRunning()) {
      // tx was short-running: must move read pointer
      moveReadPointerIfNeeded(writePointer);
    }
    return true;
  }
  return false;
}
 
Example 4
Source File: PrecisionOrRecall.java    From StreamingRec with Apache License 2.0 4 votes vote down vote up
public void evaluate(Transaction transaction, LongArrayList recommendations, LongOpenHashSet userTransactions) {
	//if there is no ground truth, there is nothing to evaluate
	if (userTransactions == null || userTransactions.isEmpty()) {
		return;
	}
	// if the algorithm does not return any recommendations, count it as 0
	if (recommendations.isEmpty()) {
		results.add(0);
		return;
	}

	// if the algorithm retrieves less than k recommendations, we calculate
	// the real k value for this case
	int realK = Math.min(k, recommendations.size());

	// check duplicates
	LongOpenHashSet uniqueRecs = new LongOpenHashSet();
	for (int i = 0; i < realK; i++) {
		if (!uniqueRecs.add(recommendations.getLong(i))) {
			throw new RuntimeException("Duplicate recommendation.");
		}
	}

	// calculate the precision
	double result = 0;
	// iterate over relevant items and recommendations to calculate the
	// intersection
	for (LongIterator iterator = userTransactions.iterator(); iterator.hasNext();) {
		long itemID = iterator.nextLong();
		for (int i = 0; i < realK; i++) {
			if (itemID == recommendations.getLong(i)) {
				result++;
			}
		}
	}

	//determine the divider of the fraction (different for precision and recall)
	double divider;
	if(type == Type.Precision){
		divider = realK;
	}else if(type == Type.Recall){
		divider = userTransactions.size();
	}else{
		throw new RuntimeException("Neither precision nor recall defined.");
	}
	// store the precision/Recall
	results.add(result / divider);
}
 
Example 5
Source File: TransactionManager.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
private void doCommit(long transactionId, long writePointer, ChangeSet changes, long commitPointer,
                      boolean addToCommitted) {
  // In case this method is called when loading a previous WAL, we need to remove the tx from these sets
  committingChangeSets.remove(transactionId);
  if (addToCommitted && !changes.getChangeIds().isEmpty()) {
    // No need to add empty changes to the committed change sets, they will never trigger any conflict

    // Record the committed change set with the next writePointer as the commit time.
    // NOTE: we use current next writePointer as key for the map, hence we may have multiple txs changesets to be
    //       stored under one key
    ChangeSet committed = committedChangeSets.get(commitPointer);
    if (committed != null) {
      // NOTE: we modify the new set to prevent concurrent modification exception, as other threads (e.g. in
      // canCommit) use it unguarded
      changes.getChangeIds().addAll(committed.getChangeIds());
    }
    committedChangeSets.put(commitPointer, changes);
  }
  // remove from in-progress set, so that it does not get excluded in the future
  InProgressTx previous = inProgress.remove(transactionId);
  if (previous == null) {
    // tx was not in progress! perhaps it timed out and is invalid? try to remove it there.
    if (invalidTxList.remove(transactionId)) {
      LOG.info("Tx invalid list: removed committed tx {}", transactionId);
    }
  } else {
    LongArrayList checkpointPointers = previous.getCheckpointWritePointers();
    if (!checkpointPointers.isEmpty()) {
      // adjust the write pointer to be the last checkpoint of the tx and remove all checkpoints from inProgress
      writePointer = checkpointPointers.getLong(checkpointPointers.size() - 1);
      inProgress.keySet().removeAll(previous.getCheckpointWritePointers());
    }
  }
  // moving read pointer
  moveReadPointerIfNeeded(writePointer);

  // All committed change sets that are smaller than the earliest started transaction can be removed.
  // here we ignore transactions that have no timeout, they are long-running and don't participate in
  // conflict detection.
  // TODO: for efficiency, can we do this once per-log in replayLogs instead of once per edit?
  committedChangeSets.headMap(TxUtils.getFirstShortInProgress(inProgress)).clear();
}