Java Code Examples for com.google.common.collect.Table#row()

The following examples show how to use com.google.common.collect.Table#row() . 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: RedisPermissionsRepository.java    From fiat with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, UserPermission> getAllById() {
  Table<String, ResourceType, Response<Map<String, String>>> responseTable = getAllFromRedis();
  if (responseTable == null) {
    return new HashMap<>(0);
  }

  Map<String, UserPermission> allById = new HashMap<>(responseTable.rowKeySet().size());

  RawUserPermission rawUnrestricted = new RawUserPermission(responseTable.row(UNRESTRICTED));
  UserPermission unrestrictedUser = getUserPermission(UNRESTRICTED, rawUnrestricted);
  Set<String> adminSet = getAllAdmins();

  for (String userId : responseTable.rowKeySet()) {
    RawUserPermission rawUser = new RawUserPermission(responseTable.row(userId));
    rawUser.isAdmin = adminSet.contains(userId);
    UserPermission permission = getUserPermission(userId, rawUser);
    allById.put(userId, permission.merge(unrestrictedUser));
  }
  return allById;
}
 
Example 2
Source File: AstyanaxWriter.java    From blueflood with Apache License 2.0 6 votes vote down vote up
public void writeMetadata(Table<Locator, String, String> metaTable) throws ConnectionException {
    ColumnFamily cf = CassandraModel.CF_METRICS_METADATA;
    Timer.Context ctx = Instrumentation.getBatchWriteTimerContext(CassandraModel.CF_METRICS_METADATA_NAME);
    MutationBatch batch = keyspace.prepareMutationBatch();

    try {
        for (Locator locator : metaTable.rowKeySet()) {
            Map<String, String> metaRow = metaTable.row(locator);
            ColumnListMutation<String> mutation = batch.withRow(cf, locator);

            for (Map.Entry<String, String> meta : metaRow.entrySet()) {
                mutation.putColumn(meta.getKey(), meta.getValue(), StringMetadataSerializer.get(), null);
            }
        }
        try {
            batch.execute();
        } catch (ConnectionException e) {
            Instrumentation.markWriteError(e);
            log.error("Connection exception persisting metadata", e);
            throw e;
        }
    } finally {
        ctx.stop();
    }
}
 
Example 3
Source File: RedisCenterImpl.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
private List<AppCommandStats> getCommandStatsList(long appId, long collectTime,
                                                  Table<RedisConstant, String, Long> table) {
    Map<String, Long> commandMap = table.row(RedisConstant.Commandstats);
    List<AppCommandStats> list = new ArrayList<AppCommandStats>();
    if (commandMap == null) {
        return list;
    }
    for (String key : commandMap.keySet()) {
        String commandName = key.replace("cmdstat_", "");
        long callCount = MapUtils.getLong(commandMap, key, 0L);
        if (callCount == 0L) {
            continue;
        }
        AppCommandStats commandStats = new AppCommandStats();
        commandStats.setAppId(appId);
        commandStats.setCollectTime(collectTime);
        commandStats.setCommandName(commandName);
        commandStats.setCommandCount(callCount);
        commandStats.setModifyTime(new Date());
        list.add(commandStats);
    }
    return list;
}
 
Example 4
Source File: ToolchainResolutionFunction.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Finds the first platform from {@code availableExecutionPlatformKeys} that is present in {@code
 * resolvedToolchains} and has all required toolchain types.
 */
private static Optional<ConfiguredTargetKey> findExecutionPlatformForToolchains(
    ImmutableSet<ToolchainTypeInfo> requiredToolchainTypes,
    ImmutableList<ConfiguredTargetKey> availableExecutionPlatformKeys,
    Table<ConfiguredTargetKey, ToolchainTypeInfo, Label> resolvedToolchains) {
  for (ConfiguredTargetKey executionPlatformKey : availableExecutionPlatformKeys) {
    if (!resolvedToolchains.containsRow(executionPlatformKey)) {
      continue;
    }

    Map<ToolchainTypeInfo, Label> toolchains = resolvedToolchains.row(executionPlatformKey);
    if (!toolchains.keySet().containsAll(requiredToolchainTypes)) {
      // Not all toolchains are present, ignore this execution platform.
      continue;
    }

    return Optional.of(executionPlatformKey);
  }

  return Optional.empty();
}
 
Example 5
Source File: ModuleDependencySort.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Extract dependencies from modules to fill dependency graph.
 */
private static void processModules(final Table<String, Optional<Revision>, ModuleNodeImpl> moduleGraph,
        final Iterable<? extends Module> modules) {

    // Process nodes
    for (final Module momb : modules) {

        final String name = momb.getName();
        final Optional<Revision> rev = momb.getRevision();
        final Map<Optional<Revision>, ModuleNodeImpl> revs = moduleGraph.row(name);
        if (revs.containsKey(rev)) {
            throw new IllegalArgumentException(String.format("Module:%s with revision:%s declared twice", name,
                formatRevDate(rev)));
        }

        revs.put(rev, new ModuleNodeImpl(name, rev.orElse(null), momb));
    }
}
 
Example 6
Source File: MetadataIOIntegrationTest.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@Test
public void writeAllDatastaxReadAllAstyanax() throws IOException {

    Locator l0 = Locator.createLocatorFromPathComponents( getRandomTenantId(), "all.put.datastax.all.read.astyanax.l0" );
    Locator l1 = Locator.createLocatorFromPathComponents( getRandomTenantId(), "all.put.datastax.all.read.astyanax.l1" );

    Table<Locator, String, String> meta = HashBasedTable.create();
    meta.put( l0, CACHE_KEY, RollupType.GAUGE.toString() );
    meta.put( l1, CACHE_KEY, RollupType.SET.toString() );

    dMetadataIO.putAll( meta );

    Set<Locator> query = new HashSet<Locator>( Arrays.asList( l0, l1 ) );
    Table<Locator, String, String> result = astyanaxMetadataIO.getAllValues( query );

    assertEquals( 2, result.size() );

    Map<String, String> row = result.row( l0 );
    assertEquals( 1, row.size() );

    Map.Entry<String, String> entry = row.entrySet().iterator().next();
    assertEquals( CACHE_KEY, entry.getKey() );
    assertEquals( RollupType.GAUGE.toString(), entry.getValue() );

    Map<String, String> row2 = result.row( l1 );
    assertEquals( 1, row2.size() );

    Map.Entry<String, String> entry2 = row2.entrySet().iterator().next();
    assertEquals( CACHE_KEY, entry2.getKey() );
    assertEquals( RollupType.SET.toString(), entry2.getValue() );

}
 
Example 7
Source File: MetadataIOIntegrationTest.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@Test
public void writeAllAstyanaxReadAllDatastax() throws IOException {

    Locator l0 = Locator.createLocatorFromPathComponents( getRandomTenantId(), "all.put.astyanax.all.read.datastax.l0" );
    Locator l1 = Locator.createLocatorFromPathComponents( getRandomTenantId(), "all.put.astyanax.all.read.datastax.l1" );

    Table<Locator, String, String> meta = HashBasedTable.create();
    meta.put( l0, CACHE_KEY, RollupType.GAUGE.toString() );
    meta.put( l1, CACHE_KEY, RollupType.SET.toString() );

    astyanaxMetadataIO.putAll( meta );

    Set<Locator> query = new HashSet<Locator>( Arrays.asList( l0, l1 ) );
    Table<Locator, String, String> result = dMetadataIO.getAllValues( query );

    assertEquals( 2, result.size() );

    Map<String, String> row = result.row( l0 );
    assertEquals( 1, row.size() );

    Map.Entry<String, String> entry = row.entrySet().iterator().next();
    assertEquals( CACHE_KEY, entry.getKey() );
    assertEquals( RollupType.GAUGE.toString(), entry.getValue() );

    Map<String, String> row2 = result.row( l1 );
    assertEquals( 1, row2.size() );

    Map.Entry<String, String> entry2 = row2.entrySet().iterator().next();
    assertEquals( CACHE_KEY, entry2.getKey() );
    assertEquals( RollupType.SET.toString(), entry2.getValue() );

}
 
Example 8
Source File: AMetadataIOIntegrationTest.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@Test
public void testBatchedMetaWritesAndReads() throws Exception {
    final AMetadataIO metadataIO = new AMetadataIO();
    Table<Locator, String, String> metaTable = HashBasedTable.create();
    final Set<Locator> locators = new HashSet<Locator>();
    MetadataCache cache = MetadataCache.getInstance();

    for (int i = 0; i < 10; i++) {
        Locator loc = Locator.createLocatorFromDbKey(
                "12345.rackspace.monitoring.enities.enFoo.check_type.agent.cpu.check.chBar.metric.met" + i);
        locators.add(loc);
        metaTable.put(loc, "key", "value");
    }

    metadataIO.putAll(metaTable); // Writes batch to disk

    Thread.sleep(2000); // wait 2s for batch timer to kick in.

    // Read it back.
    Table<Locator, String, String> metaRead = AstyanaxReader.getInstance().getMetadataValues(locators);

    // Assert that we wrote meta for 10 different locators.
    Assert.assertTrue(metaRead.size() == 10);

    for (Locator locator : metaRead.rowKeySet()) {
        Map<String, String> metaMapForLocator = metaRead.row(locator);

        Assert.assertTrue(metaMapForLocator.size() == 1);
        Assert.assertTrue(metaMapForLocator.get("key").equals("value"));
    }
}
 
Example 9
Source File: DAbstractMetricsRW.java    From blueflood with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a list of {@link com.datastax.driver.core.ResultSetFuture} for each
 * {@link com.rackspacecloud.blueflood.types.Locator} to
 * {@link com.rackspacecloud.blueflood.outputs.formats.MetricData} object.
 *
 * @param resultSets
 * @param locatorIO
 * @param columnFamily
 * @return
 */
protected Map<Locator, MetricData> resultSetsToMetricData(Map<Locator, List<ResultSetFuture>> resultSets,
                                                          Map<Locator, DAbstractMetricIO> locatorIO,
                                                          String columnFamily,
                                                          Range range) {

    MetadataCache metadataCache = MetadataCache.getInstance();

    // iterate through all ResultSetFuture
    Map<Locator, MetricData> locatorMetricDataMap = new HashMap<Locator, MetricData>();
    for (Map.Entry<Locator, List<ResultSetFuture>> entry : resultSets.entrySet() ) {
        Locator locator = entry.getKey();
        List<ResultSetFuture> futures = entry.getValue();

        DAbstractMetricIO io = locatorIO.get(locator);

        // get ResultSets to a Table of locator, timestamp, rollup
        Table<Locator, Long, Object> locatorTimestampRollup = io.toLocatorTimestampValue(futures, locator, columnFamily, range);

        Map<Long, Object> tsRollupMap = locatorTimestampRollup.row( locator );

        // convert to Points and MetricData
        Points points = convertToPoints( tsRollupMap );

        // create MetricData
        MetricData metricData = new MetricData( points, metadataCache.getUnitString( locator ) );
        locatorMetricDataMap.put( locator, metricData );
    }
    return locatorMetricDataMap;
}
 
Example 10
Source File: GuavaTableUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenTable_whenRow_returnsSuccessfully() {
    final Table<String, String, Integer> universityCourseSeatTable = HashBasedTable.create();
    universityCourseSeatTable.put("Mumbai", "Chemical", 120);
    universityCourseSeatTable.put("Mumbai", "IT", 60);
    universityCourseSeatTable.put("Harvard", "Electrical", 60);
    universityCourseSeatTable.put("Harvard", "IT", 120);

    final Map<String, Integer> courseSeatMap = universityCourseSeatTable.row("Mumbai");

    assertThat(courseSeatMap).hasSize(2);
    assertThat(courseSeatMap.get("IT")).isEqualTo(60);
    assertThat(courseSeatMap.get("Chemical")).isEqualTo(120);
}
 
Example 11
Source File: ModuleDependencySort.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get imported module by its name and revision from moduleGraph.
 */
private static ModuleNodeImpl getModuleByNameAndRevision(
        final Table<String, Optional<Revision>, ModuleNodeImpl> moduleGraph, final String fromName,
        final Optional<Revision> fromRevision, final String toName, final Optional<Revision> toRevision) {

    final ModuleNodeImpl exact = moduleGraph.get(toName, toRevision);
    if (exact != null) {
        return exact;
    }

    // If revision is not specified in import, but module exists with different revisions, take first one
    if (toRevision.isEmpty()) {
        final Map<Optional<Revision>, ModuleNodeImpl> modulerevs = moduleGraph.row(toName);

        if (!modulerevs.isEmpty()) {
            final ModuleNodeImpl first = modulerevs.values().iterator().next();
            if (LOG.isTraceEnabled()) {
                LOG.trace("Import:{}:{} by module:{}:{} does not specify revision, using:{}:{}"
                        + " for module dependency sort", toName, formatRevDate(toRevision), fromName,
                        formatRevDate(fromRevision), first.getName(), formatRevDate(first.getRevision()));
            }
            return first;
        }
    }

    LOG.warn("Not existing module imported:{}:{} by:{}:{}", toName, formatRevDate(toRevision), fromName,
        formatRevDate(fromRevision));
    LOG.warn("Available models: {}", moduleGraph);
    throw new IllegalArgumentException(String.format("Not existing module imported:%s:%s by:%s:%s", toName,
        formatRevDate(toRevision), fromName, formatRevDate(fromRevision)));
}
 
Example 12
Source File: RedisCenterImpl.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
private void fillAccumulationMap(Map<RedisConstant, Map<String, Object>> infoMap,
                                 Table<RedisConstant, String, Long> table) {
    if (table == null || table.isEmpty()) {
        return;
    }
    Map<String, Object> accMap = infoMap.get(RedisConstant.DIFF);
    if (accMap == null) {
        accMap = new LinkedHashMap<String, Object>();
        infoMap.put(RedisConstant.DIFF, accMap);
    }
    for (RedisConstant constant : table.rowKeySet()) {
        Map<String, Long> rowMap = table.row(constant);
        accMap.putAll(rowMap);
    }
}
 
Example 13
Source File: ActionCheckpointSerde.java    From qmq with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] toBytes(final ActionCheckpoint state) {
    final StringBuilder data = new StringBuilder();
    data.append(VERSION_V3).append(NEWLINE);
    data.append(state.getOffset()).append(NEWLINE);

    final Table<String, String, ConsumerGroupProgress> progresses = state.getProgresses();
    for (final String subject : progresses.rowKeySet()) {
        final Map<String, ConsumerGroupProgress> groups = progresses.row(subject);
        data.append(SLASH_JOINER.join(subject, groups.size())).append(NEWLINE);

        for (final String group : groups.keySet()) {
            final ConsumerGroupProgress progress = groups.get(group);
            final Map<String, ConsumerProgress> consumers = progress.getConsumers();
            final int consumerCount = consumers == null ? 0 : consumers.size();

            data.append(SLASH_JOINER.join(group, boolean2Short(progress.isBroadcast()), progress.getPull(), consumerCount)).append(NEWLINE);

            if (consumerCount <= 0) {
                continue;
            }

            consumers.values().forEach(consumer -> {
                data.append(SLASH_JOINER.join(consumer.getConsumerId(), consumer.getPull(), consumer.getAck())).append(NEWLINE);
            });
        }
    }
    return data.toString().getBytes(Charsets.UTF_8);
}
 
Example 14
Source File: EMStep.java    From sequence-mining with GNU General Public License v3.0 4 votes vote down vote up
/** EM-step for structural EM */
static Tuple2<Double, Map<Integer, Double>> structuralEMStep(final TransactionDatabase transactions,
		final InferenceAlgorithm inferenceAlgorithm, final Sequence candidate) {
	final double noTransactions = transactions.size();

	// Calculate max. no. of candidate occurrences
	final int maxReps = transactions.getTransactionList().parallelStream().mapToInt(t -> t.repetitions(candidate))
			.max().getAsInt();
	final Map<Integer, Double> initProb = new HashMap<>();
	initProb.put(0, 0.);
	for (int occur = 1; occur <= maxReps; occur++)
		initProb.put(occur, 1.);

	// E-step (adding candidate to transactions that support it)
	final Map<Multiset.Entry<Sequence>, Long> coveringWithCounts = transactions.getTransactionList()
			.parallelStream().map(t -> {
				if (t.contains(candidate)) {
					t.addSequenceCache(candidate, initProb);
					final Multiset<Sequence> covering = inferenceAlgorithm.infer(t);
					t.setTempCachedCovering(covering);
					return covering.entrySet();
				}
				return t.getCachedCovering().entrySet();
			}).flatMap(Set::stream).collect(groupingBy(identity(), counting()));

	// M-step
	final Table<Sequence, Integer, Double> newSequences = coveringWithCounts.entrySet().parallelStream().collect(
			HashBasedTable::create,
			(t, e) -> t.put(e.getKey().getElement(), e.getKey().getCount(), e.getValue() / noTransactions),
			Table::putAll);
	newSequences.rowKeySet().parallelStream().forEach(seq -> {
		// Pad with zero counts for non-occurrences
		final int maxOccur = Collections.max(newSequences.row(seq).keySet());
		for (int occur = 1; occur <= maxOccur; occur++) {
			if (!newSequences.contains(seq, occur))
				newSequences.put(seq, occur, 0.);
		} // Add probabilities for zero occurrences
		double rowSum = 0;
		for (final Double count : newSequences.row(seq).values())
			rowSum += count;
		newSequences.put(seq, 0, 1 - rowSum);
	});

	// Get average cost (removing candidate from supported transactions)
	final double averageCost = transactions.getTransactionList().parallelStream().mapToDouble(t -> {
		double cost;
		if (t.contains(candidate))
			cost = t.getTempCachedCost(newSequences);
		else
			cost = t.getCachedCost(newSequences);
		t.removeSequenceCache(candidate);
		return cost;
	}).sum() / noTransactions;

	// Get candidate prob
	final Map<Integer, Double> prob = newSequences.row(candidate);

	return new Tuple2<Double, Map<Integer, Double>>(averageCost, prob);
}
 
Example 15
Source File: DiscretizationUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private Map<String, Object> match(InlineTable inlineTable, Map<String, FieldValue> values){
	Map<String, RowFilter> rowFilters = CacheUtil.getValue(inlineTable, DiscretizationUtil.rowFilterCache);

	Set<Integer> rows = null;

	Collection<Map.Entry<String, FieldValue>> entries = values.entrySet();
	for(Map.Entry<String, FieldValue> entry : entries){
		String key = entry.getKey();
		FieldValue value = entry.getValue();

		RowFilter rowFilter = rowFilters.get(key);
		if(rowFilter == null){
			throw new InvalidElementException(inlineTable);
		}

		SetMultimap<Object, Integer> valueRowsMap = rowFilter.getValueRowsMap(value.getDataType());

		Set<Integer> valueRows = valueRowsMap.get(FieldValueUtil.getValue(value));

		if(valueRows != null && !valueRows.isEmpty()){

			if(rows == null){
				rows = (entries.size() > 1 ? new HashSet<>(valueRows) : valueRows);
			} else

			{
				rows.retainAll(valueRows);
			} // End if

			if(rows.isEmpty()){
				return null;
			}
		} else

		{
			return null;
		}
	}

	if(rows != null && !rows.isEmpty()){
		Table<Integer, String, Object> content = InlineTableUtil.getContent(inlineTable);

		// "It is an error if the table entries used for matching are not unique"
		if(rows.size() != 1){
			throw new InvalidElementException(inlineTable);
		}

		Integer row = Iterables.getOnlyElement(rows);

		return content.row(row);
	}

	return null;
}
 
Example 16
Source File: RedisPermissionsRepository.java    From fiat with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, UserPermission> getAllByRoles(List<String> anyRoles) {
  if (anyRoles == null) {
    return getAllById();
  } else if (anyRoles.isEmpty()) {
    val unrestricted = get(UNRESTRICTED);
    if (unrestricted.isPresent()) {
      val map = new HashMap<String, UserPermission>();
      map.put(UNRESTRICTED, unrestricted.get());
      return map;
    }
    return new HashMap<>();
  }

  final Set<String> dedupedUsernames = new HashSet<>();
  for (String role : new HashSet<>(anyRoles)) {
    dedupedUsernames.addAll(scanSet(roleKey(role)));
  }
  dedupedUsernames.add(UNRESTRICTED);

  Table<String, ResourceType, Response<Map<String, String>>> responseTable =
      getAllFromRedis(dedupedUsernames);
  if (responseTable == null) {
    return new HashMap<>(0);
  }

  RawUserPermission rawUnrestricted = new RawUserPermission(responseTable.row(UNRESTRICTED));
  UserPermission unrestrictedUser = getUserPermission(UNRESTRICTED, rawUnrestricted);
  Set<String> adminSet = getAllAdmins();

  return dedupedUsernames.stream()
      .map(
          userId -> {
            RawUserPermission rawUser = new RawUserPermission(responseTable.row(userId));
            rawUser.isAdmin = adminSet.contains(userId);
            return getUserPermission(userId, rawUser);
          })
      .collect(
          Collectors.toMap(
              UserPermission::getId, permission -> permission.merge(unrestrictedUser)));
}
 
Example 17
Source File: JSONMatrixReader.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
private Pair<Iterator<Integer>, Supplier<SimpleCell[]>> parseToTable(CustomValue jsonNode) throws IOException {

        Table<Integer, String, SimpleCell> table = Reflection.newProxy(Table.class, new SafetyCheckInvocationHandler());

        for(CustomValue testCaseWrapper:jsonNode.getArrayValue()) {
            if (testCaseWrapper.getObjectValue().size() > 1) {
                throw new EPSCommonException("Too many nodes in testCase wrapper");
            }

            testCaseWrapper.getObjectValue().forEach((blockKey, commonBlock) -> {
                String commonBlockType = blockKey.getKey();
                AMLBlockBrace blockBrace = AMLBlockBrace.value(commonBlockType);

                Objects.requireNonNull(commonBlock, "'AML block' node must be presented");
                Objects.requireNonNull(blockBrace, "Unknown block type " + commonBlockType);

                int localRowCounter = tableRowCounter.getAndIncrement();
                table.put(localRowCounter, Column.Action.getName(), new SimpleCell(commonBlockType,blockKey.getLine()));

                commonBlock.getObjectValue().forEach((actionKey, actionNode) -> {
                    String reference = actionKey.getKey();

                    logger.debug("reading {}", reference);

                    if (actionNode.isObject()) {

                        int nestedCount = countNestedReferences(actionNode);
                        int target = tableRowCounter.get() + nestedCount;
                        table.put(target, Column.Reference.getName(), new SimpleCell(reference, actionKey.getLine()));
                        consumeNode(actionNode, table, target,  actionKey.getLine());
                        //FIXME will add additional empty row at last action
                        tableRowCounter.getAndIncrement();
                    } else if (!actionNode.isArray()) {
                        table.put(localRowCounter, reference,  new SimpleCell(actionNode.getSimpleValue().toString(), actionKey.getLine()));
                    } else{
                        throw new IllegalStateException(String.format("Invalid value type array %s found in block %s, number line %s", reference, commonBlockType, actionKey.getLine()));
                    }
                });

                table.put(tableRowCounter.getAndIncrement(), Column.Action.getName(), new SimpleCell(blockBrace.getInversed().getName(),blockKey.getLine()));
            });
        }

        Set<String> columns = ImmutableSet.<String>builder().addAll(table.columnKeySet()).add(Column.Id.getName()).build();
        columns.forEach(column -> table.put(0, column, new SimpleCell(column)));

        Iterator<Integer> rowIterator = dbg.DEBUG_SORT ? table.rowKeySet().iterator() : new TreeSet<>(table.rowKeySet()).iterator();

        Supplier<SimpleCell[]> supplier = SUPPLIER;
        //preserve header
        if (rowIterator.hasNext()) {

            supplier = () -> {
                int currentRow = rowIterator.next();
                Map<String, SimpleCell> row = new HashMap<>(table.row(currentRow));

                return columns.stream()
                        .map(key -> row.getOrDefault(key, new SimpleCell("")))
                        .toArray(SimpleCell[]::new);
            };
        }

        return new Pair<>(rowIterator, supplier);
    }
 
Example 18
Source File: ConsumerSequenceManager.java    From qmq with Apache License 2.0 4 votes vote down vote up
private boolean noPullLog(String subject, String group, String consumerId) {
    Table<String, String, PullLog> pullLogs = storage.allPullLogs();
    Map<String, PullLog> subscribers = pullLogs.row(consumerId);
    if (subscribers == null || subscribers.isEmpty()) return true;
    return subscribers.get(GroupAndSubject.groupAndSubject(subject, group)) == null;
}
 
Example 19
Source File: TaskAssignServiceImpl.java    From chronus with Apache License 2.0 4 votes vote down vote up
private Table<String, Integer, TaskAssignResultEntity> convertToNewAssignResultTable(Table<String, Integer, TaskAssignResultEntity> hisAssignTable, Table<String, Integer, TaskAssignResultEntity> todoTaskAssignResultTable, Table<String, String, TaskAssignResultEntity> needStartTaskTable, Table<String, String, TaskAssignResultEntity> needStopTaskTable, List<Node> tmpExecutorList, int executorMaxTaskNum) {
    Table<String, Integer, TaskAssignResultEntity> assignResultTable = HashBasedTable.create();

    for (Node node : tmpExecutorList) {
        Map<Integer, TaskAssignResultEntity> hisRow = hisAssignTable.row(node.getAddress());
        if (hisRow == null || hisRow.isEmpty()) {
            continue;
        }
        int currentNodeTotalTask = 0;
        for (int i = 0, hisSize = hisRow.size(); i < hisSize; i++) {
            TaskAssignResultEntity hisAssign = hisRow.remove(i);
            if (hisAssign == null) {
                break;
            }
            // 裁剪
            if (currentNodeTotalTask >= executorMaxTaskNum) {
                needStopTaskTable.put(node.getAddress(), getColumnKey(hisAssign), hisAssign);
                continue;
            }
            TaskAssignResultEntity todoAssignItem = todoTaskAssignResultTable.remove(hisAssign.getTaskName(), hisAssign.getSeqNo());
            if (todoAssignItem != null) {
                todoAssignItem.setExecutorAddress(node.getAddress());
                // 状态停止 需要移除
                if (hisAssign.getAssignNum() > todoAssignItem.getAssignNum()) {
                    needStopTaskTable.put(node.getAddress(), getColumnKey(hisAssign), hisAssign);
                } else if (hisAssign.getAssignNum() < todoAssignItem.getAssignNum()) {
                    // 状态启动 需要补充
                    needStartTaskTable.put(node.getAddress(), getColumnKey(todoAssignItem), todoAssignItem);
                } else if (!Objects.equals(hisAssign.getTaskItems(), todoAssignItem.getTaskItems())) {
                    // 任务项发生变更需要重新加载
                    needStopTaskTable.put(node.getAddress(), getColumnKey(hisAssign), hisAssign);
                    needStartTaskTable.put(node.getAddress(), getColumnKey(todoAssignItem), todoAssignItem);
                }
                assignResultTable.put(node.getAddress(), i, todoAssignItem);
                currentNodeTotalTask++;
            } else {
                //assignNum缩小 or 任务删除 需要移除
                needStopTaskTable.put(node.getAddress(), getColumnKey(hisAssign), hisAssign);
            }
        }
    }
    return assignResultTable;
}
 
Example 20
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
static
private Map<Integer, List<FieldValue>> loadInstanceValues(NearestNeighborModelEvaluator modelEvaluator){
	NearestNeighborModel nearestNeighborModel = modelEvaluator.getModel();

	Map<Integer, List<FieldValue>> result = new LinkedHashMap<>();

	Table<Integer, FieldName, FieldValue> table = modelEvaluator.getValue(NearestNeighborModelEvaluator.trainingInstanceCache, createTrainingInstanceLoader(modelEvaluator));

	KNNInputs knnInputs = nearestNeighborModel.getKNNInputs();

	Set<Integer> rowKeys = ImmutableSortedSet.copyOf(table.rowKeySet());
	for(Integer rowKey : rowKeys){
		List<FieldValue> values = new ArrayList<>();

		Map<FieldName, FieldValue> rowValues = table.row(rowKey);

		for(KNNInput knnInput : knnInputs){
			FieldValue value = rowValues.get(knnInput.getField());

			values.add(value);
		}

		result.put(rowKey, values);
	}

	return result;
}