Search in sources :

Example 16 with Triple

use of org.apache.commons.lang3.tuple.Triple in project mlib by myshzzx.

the class SpringExporter method proxySock.

public static <T> T proxySock(String host, int port, Class<T> type, @Nullable String beanName) {
    ThreadLocal<Triple<Socket, ObjectInputStream, ObjectOutputStream>> tt = ThreadLocal.withInitial(() -> Triple.of(null, null, null));
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(type);
    enhancer.setCallback((InvocationHandler) (o, method, args) -> {
        Triple<Socket, ObjectInputStream, ObjectOutputStream> tsoo = tt.get();
        Socket socket = tsoo.getLeft();
        ObjectOutputStream out = tsoo.getRight();
        ObjectInputStream in = tsoo.getMiddle();
        if (socket == null || socket.isClosed()) {
            socket = new Socket(host, port);
            out = new ObjectOutputStream(socket.getOutputStream());
            in = new ObjectInputStream(socket.getInputStream());
            tt.set(Triple.of(socket, in, out));
        }
        out.writeObject(new Invoke(type, beanName, method.getDeclaringClass(), method.getName(), method.getParameterTypes(), args));
        out.flush();
        Result r = (Result) in.readObject();
        return r.getResult();
    });
    return (T) enhancer.create();
}
Also used : Triple(org.apache.commons.lang3.tuple.Triple) Enhancer(org.springframework.cglib.proxy.Enhancer) Socket(java.net.Socket) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) ObjectInputStream(java.io.ObjectInputStream) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ServerSocket(java.net.ServerSocket) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ObjectOutputStream(java.io.ObjectOutputStream) Serializer(mysh.util.Serializer) Triple(org.apache.commons.lang3.tuple.Triple) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Logger(org.slf4j.Logger) InvocationHandler(org.springframework.cglib.proxy.InvocationHandler) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) ApplicationContext(org.springframework.context.ApplicationContext) Serializable(java.io.Serializable) Base64(java.util.Base64) JSON(com.alibaba.fastjson.JSON) HttpGet(org.apache.http.client.methods.HttpGet) JSONObject(com.alibaba.fastjson.JSONObject) HttpClients(org.apache.http.impl.client.HttpClients) ApplicationContextAware(org.springframework.context.ApplicationContextAware) Enhancer(org.springframework.cglib.proxy.Enhancer) ObjectOutputStream(java.io.ObjectOutputStream) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ObjectInputStream(java.io.ObjectInputStream)

Example 17 with Triple

use of org.apache.commons.lang3.tuple.Triple in project incubator-pulsar by apache.

the class CompactedTopicTest method buildCompactedLedger.

/**
 * Build a compacted ledger, and return the id of the ledger, the position of the different
 * entries in the ledger, and a list of gaps, and the entry which should be returned after the gap.
 */
private Triple<Long, List<Pair<MessageIdData, Long>>, List<Pair<MessageIdData, Long>>> buildCompactedLedger(BookKeeper bk, int count) throws Exception {
    LedgerHandle lh = bk.createLedger(1, 1, Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE, Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD);
    List<Pair<MessageIdData, Long>> positions = new ArrayList<>();
    List<Pair<MessageIdData, Long>> idsInGaps = new ArrayList<>();
    AtomicLong ledgerIds = new AtomicLong(10L);
    AtomicLong entryIds = new AtomicLong(0L);
    CompletableFuture.allOf(IntStream.range(0, count).mapToObj((i) -> {
        List<MessageIdData> idsInGap = new ArrayList<MessageIdData>();
        if (r.nextInt(10) == 1) {
            long delta = r.nextInt(10) + 1;
            idsInGap.add(MessageIdData.newBuilder().setLedgerId(ledgerIds.get()).setEntryId(entryIds.get() + 1).build());
            ledgerIds.addAndGet(delta);
            entryIds.set(0);
        }
        long delta = r.nextInt(5);
        if (delta != 0) {
            idsInGap.add(MessageIdData.newBuilder().setLedgerId(ledgerIds.get()).setEntryId(entryIds.get() + 1).build());
        }
        MessageIdData id = MessageIdData.newBuilder().setLedgerId(ledgerIds.get()).setEntryId(entryIds.addAndGet(delta + 1)).build();
        @Cleanup RawMessage m = new RawMessageImpl(id, Unpooled.EMPTY_BUFFER);
        CompletableFuture<Void> f = new CompletableFuture<>();
        ByteBuf buffer = m.serialize();
        lh.asyncAddEntry(buffer, (rc, ledger, eid, ctx) -> {
            if (rc != BKException.Code.OK) {
                f.completeExceptionally(BKException.create(rc));
            } else {
                positions.add(Pair.of(id, eid));
                idsInGap.forEach((gid) -> idsInGaps.add(Pair.of(gid, eid)));
                f.complete(null);
            }
        }, null);
        buffer.release();
        return f;
    }).toArray(CompletableFuture[]::new)).get();
    lh.close();
    return Triple.of(lh.getId(), positions, idsInGaps);
}
Also used : IntStream(java.util.stream.IntStream) RawMessageImpl(org.apache.pulsar.client.impl.RawMessageImpl) RawMessage(org.apache.pulsar.client.api.RawMessage) Cleanup(lombok.Cleanup) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) AfterMethod(org.testng.annotations.AfterMethod) ArrayList(java.util.ArrayList) Unpooled(io.netty.buffer.Unpooled) Lists(com.google.common.collect.Lists) Pair(org.apache.commons.lang3.tuple.Pair) Assert(org.testng.Assert) ByteBuf(io.netty.buffer.ByteBuf) Triple(org.apache.commons.lang3.tuple.Triple) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) BeforeMethod(org.testng.annotations.BeforeMethod) BookKeeper(org.apache.bookkeeper.client.BookKeeper) BKException(org.apache.bookkeeper.client.BKException) Sets(com.google.common.collect.Sets) AtomicLong(java.util.concurrent.atomic.AtomicLong) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest) List(java.util.List) AsyncLoadingCache(com.github.benmanes.caffeine.cache.AsyncLoadingCache) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) Collections(java.util.Collections) RawMessageImpl(org.apache.pulsar.client.impl.RawMessageImpl) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) AtomicLong(java.util.concurrent.atomic.AtomicLong) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) List(java.util.List) RawMessage(org.apache.pulsar.client.api.RawMessage) Pair(org.apache.commons.lang3.tuple.Pair)

Example 18 with Triple

use of org.apache.commons.lang3.tuple.Triple in project incubator-pulsar by apache.

the class CompactedTopicTest method testEntryLookup.

@Test
public void testEntryLookup() throws Exception {
    BookKeeper bk = pulsar.getBookKeeperClientFactory().create(this.conf, null);
    Triple<Long, List<Pair<MessageIdData, Long>>, List<Pair<MessageIdData, Long>>> compactedLedgerData = buildCompactedLedger(bk, 500);
    List<Pair<MessageIdData, Long>> positions = compactedLedgerData.getMiddle();
    List<Pair<MessageIdData, Long>> idsInGaps = compactedLedgerData.getRight();
    LedgerHandle lh = bk.openLedger(compactedLedgerData.getLeft(), Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE, Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD);
    long lastEntryId = lh.getLastAddConfirmed();
    AsyncLoadingCache<Long, MessageIdData> cache = CompactedTopicImpl.createCache(lh, 50);
    MessageIdData firstPositionId = positions.get(0).getLeft();
    Pair<MessageIdData, Long> lastPosition = positions.get(positions.size() - 1);
    // check ids before and after ids in compacted ledger
    Assert.assertEquals(CompactedTopicImpl.findStartPoint(new PositionImpl(0, 0), lastEntryId, cache).get(), Long.valueOf(0));
    Assert.assertEquals(CompactedTopicImpl.findStartPoint(new PositionImpl(Long.MAX_VALUE, 0), lastEntryId, cache).get(), Long.valueOf(CompactedTopicImpl.NEWER_THAN_COMPACTED));
    // entry 0 is never in compacted ledger due to how we generate dummy
    Assert.assertEquals(CompactedTopicImpl.findStartPoint(new PositionImpl(firstPositionId.getLedgerId(), 0), lastEntryId, cache).get(), Long.valueOf(0));
    // check next id after last id in compacted ledger
    Assert.assertEquals(CompactedTopicImpl.findStartPoint(new PositionImpl(lastPosition.getLeft().getLedgerId(), lastPosition.getLeft().getEntryId() + 1), lastEntryId, cache).get(), Long.valueOf(CompactedTopicImpl.NEWER_THAN_COMPACTED));
    // shuffle to make cache work hard
    Collections.shuffle(positions, r);
    Collections.shuffle(idsInGaps, r);
    // Check ids we know are in compacted ledger
    for (Pair<MessageIdData, Long> p : positions) {
        PositionImpl pos = new PositionImpl(p.getLeft().getLedgerId(), p.getLeft().getEntryId());
        Long got = CompactedTopicImpl.findStartPoint(pos, lastEntryId, cache).get();
        Assert.assertEquals(got, Long.valueOf(p.getRight()));
    }
    // Check ids we know are in the gaps of the compacted ledger
    for (Pair<MessageIdData, Long> gap : idsInGaps) {
        PositionImpl pos = new PositionImpl(gap.getLeft().getLedgerId(), gap.getLeft().getEntryId());
        Assert.assertEquals(CompactedTopicImpl.findStartPoint(pos, lastEntryId, cache).get(), Long.valueOf(gap.getRight()));
    }
}
Also used : MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) AtomicLong(java.util.concurrent.atomic.AtomicLong) BookKeeper(org.apache.bookkeeper.client.BookKeeper) ArrayList(java.util.ArrayList) List(java.util.List) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 19 with Triple

use of org.apache.commons.lang3.tuple.Triple in project sqlg by pietermartin.

the class BaseSqlDialect method flushEdgeGlobalUniqueIndexes.

@Override
public void flushEdgeGlobalUniqueIndexes(SqlgGraph sqlgGraph, Map<MetaEdge, Pair<SortedSet<String>, Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>>>> edgeCache) {
    for (MetaEdge metaEdge : edgeCache.keySet()) {
        Pair<SortedSet<String>, Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>>> triples = edgeCache.get(metaEdge);
        Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>> edgeMap = triples.getRight();
        Map<String, PropertyColumn> propertyColumnMap = sqlgGraph.getTopology().getPropertiesFor(metaEdge.getSchemaTable().withPrefix(EDGE_PREFIX));
        Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>> rows = triples.getRight();
        for (Map.Entry<String, PropertyColumn> propertyColumnEntry : propertyColumnMap.entrySet()) {
            PropertyColumn propertyColumn = propertyColumnEntry.getValue();
            for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
                StringBuilder sql = new StringBuilder();
                sql.append("INSERT INTO ");
                sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA));
                sql.append(".");
                sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + globalUniqueIndex.getName()));
                sql.append(" (");
                PropertyType propertyType = propertyColumn.getPropertyType();
                String[] sqlDefinitions = sqlgGraph.getSqlDialect().propertyTypeToSqlDefinition(propertyType);
                int count = 1;
                for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
                    if (count++ > 1) {
                        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE + propertyType.getPostFixes()[count - 2]));
                    } else {
                        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE));
                    }
                    sql.append(",");
                }
                sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID));
                sql.append(",");
                sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME));
                sql.append(") VALUES ( ");
                count = 1;
                // noinspection Duplicates
                for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
                    if (count++ > 1) {
                        sql.append("?");
                    } else {
                        sql.append("?");
                    }
                    sql.append(", ");
                }
                sql.append("?, ?)");
                if (sqlgGraph.getSqlDialect().needsSemicolon()) {
                    sql.append(";");
                }
                if (logger.isDebugEnabled()) {
                    logger.debug(sql.toString());
                }
                Connection conn = sqlgGraph.tx().getConnection();
                try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
                    for (Map.Entry<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>> rowEntry : rows.entrySet()) {
                        SqlgEdge sqlgEdge = rowEntry.getKey();
                        Map<String, Object> parameterValueMap = rowEntry.getValue().getRight();
                        Object value = parameterValueMap.get(propertyColumn.getName());
                        List<Pair<PropertyType, Object>> typeAndValues = new ArrayList<>();
                        typeAndValues.add(Pair.of(propertyColumn.getPropertyType(), value));
                        typeAndValues.add(Pair.of(PropertyType.STRING, sqlgEdge.id().toString()));
                        typeAndValues.add(Pair.of(PropertyType.STRING, propertyColumn.getName()));
                        SqlgUtil.setKeyValuesAsParameterUsingPropertyColumn(sqlgGraph, true, 1, preparedStatement, typeAndValues);
                        preparedStatement.addBatch();
                    }
                    preparedStatement.executeBatch();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) Pair(org.apache.commons.lang3.tuple.Pair) Triple(org.apache.commons.lang3.tuple.Triple) GlobalUniqueIndex(org.umlg.sqlg.structure.topology.GlobalUniqueIndex)

Example 20 with Triple

use of org.apache.commons.lang3.tuple.Triple in project sqlg by pietermartin.

the class SqlgSqlExecutor method executeDropQuery.

public static void executeDropQuery(SqlgGraph sqlgGraph, SchemaTableTree rootSchemaTableTree, LinkedList<SchemaTableTree> distinctQueryStack) {
    List<Triple<DROP_QUERY, String, SchemaTable>> sqls = rootSchemaTableTree.constructDropSql(distinctQueryStack);
    for (Triple<DROP_QUERY, String, SchemaTable> sqlPair : sqls) {
        DROP_QUERY dropQuery = sqlPair.getLeft();
        String sql = sqlPair.getMiddle();
        SchemaTable deletedSchemaTable = sqlPair.getRight();
        switch(dropQuery) {
            case ALTER:
                executeDropQuery(sqlgGraph, sql, new LinkedList<>(), deletedSchemaTable);
                break;
            case EDGE:
                LinkedList<SchemaTableTree> tmp = new LinkedList<>(distinctQueryStack);
                tmp.removeLast();
                executeDropQuery(sqlgGraph, sql, tmp, deletedSchemaTable);
                break;
            case NORMAL:
                executeDropQuery(sqlgGraph, sql, distinctQueryStack, deletedSchemaTable);
                break;
            case TRUNCATE:
                executeDropQuery(sqlgGraph, sql, new LinkedList<>(), deletedSchemaTable);
                break;
            default:
                throw new IllegalStateException("Unknown DROP_QUERY " + dropQuery.toString());
        }
    }
}
Also used : Triple(org.apache.commons.lang3.tuple.Triple) SchemaTableTree(org.umlg.sqlg.sql.parse.SchemaTableTree) SchemaTable(org.umlg.sqlg.structure.SchemaTable) LinkedList(java.util.LinkedList)

Aggregations

Triple (org.apache.commons.lang3.tuple.Triple)51 ArrayList (java.util.ArrayList)20 ImmutableTriple (org.apache.commons.lang3.tuple.ImmutableTriple)18 List (java.util.List)13 Pair (org.apache.commons.lang3.tuple.Pair)10 Test (org.junit.Test)8 Collectors (java.util.stream.Collectors)7 java.util (java.util)6 Organization (alfio.model.user.Organization)5 Event (alfio.model.Event)4 Ticket (alfio.model.Ticket)4 TicketReservation (alfio.model.TicketReservation)4 IOException (java.io.IOException)4 BlockPos (net.minecraft.util.math.BlockPos)4 Mutable (org.apache.commons.lang3.mutable.Mutable)4 Triple (org.apache.hyracks.algebricks.common.utils.Triple)4 alfio.model (alfio.model)3 TicketCategory (alfio.model.TicketCategory)3 TemplateManager (alfio.util.TemplateManager)3 LockedInodePath (alluxio.master.file.meta.LockedInodePath)3