Package com.higherfrequencytrading.chronicle

Examples of com.higherfrequencytrading.chronicle.Excerpt


                    tsc.useUnsafe(USE_UNSAFE);
                    final IntIndexedChronicle tsc2 = new IntIndexedChronicle(basePath2);
                    tsc2.useUnsafe(USE_UNSAFE);
                    tsc2.clear();

                    Excerpt excerpt = tsc.createExcerpt();
                    Excerpt excerpt2 = tsc2.createExcerpt();
                    for (int i = 0; i < RUNS; i++) {
                        do {
                            BusyWaiter.pause();
                        } while (!excerpt.index(i));

                        char type = excerpt.readChar();
                        if ('T' != type)
                            assertEquals('T', type);
                        int n = excerpt.readInt();
                        if (i != n)
                            assertEquals(i, n);
                        excerpt.readChars(sb);
                        excerpt.readLong();
                        excerpt.readDouble();
                        excerpt.finish();

                        excerpt2.startExcerpt(6);
                        excerpt2.writeChar('R');
                        excerpt2.writeInt(n);
                        excerpt2.finish();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    al2.release();
                }
            }
        });
        t.start();

        al.bind();
        Excerpt excerpt = tsc.createExcerpt();
        Excerpt excerpt2 = tsc2.createExcerpt();
        long start = System.nanoTime();
        int i2 = 0;
        for (int i = 0; i < RUNS; i++) {
            excerpt.startExcerpt(34);
            excerpt.writeChar('T');
            excerpt.writeInt(i);
            excerpt.writeChars("Hello");
            excerpt.writeLong(0L);
            excerpt.writeDouble(0.0);
            excerpt.finish();

            while (excerpt2.index(i2)) {
                char type = excerpt2.readChar();
                if ('R' != type)
                    assertEquals('R', type);
                int n = excerpt2.readInt();
                if (i2 != n)
                    assertEquals(i2, n);
                excerpt2.finish();
                i2++;
            }
        }

        for (; i2 < RUNS; i2++) {
            do {
                BusyWaiter.pause();
            } while (!excerpt2.index(i2));
            char type = excerpt2.readChar();
            if ('R' != type)
                assertEquals('R', type);
            int n = excerpt2.readInt();
            if (i2 != n)
                assertEquals(i2, n);
            excerpt2.finish();
        }

        t.join();
        tsc.close();
        tsc2.close();
View Full Code Here


    public void onException(@NotNull String message, @NotNull Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        t.printStackTrace(pw);
        String text = sw.toString();
        Excerpt excerpt = startExceprt(2 + message.length() + 2 + text.length(), 'X');
        excerpt.writeUTF(message);
        excerpt.writeUTF(text);
        excerpt.finish();
    }
View Full Code Here

        @Override
        public void run() {
            try {
                long index = readIndex(socket);
                Excerpt excerpt = chronicle.createExcerpt();
                ByteBuffer bb = TcpUtil.createBuffer(1, chronicle.byteOrder()); // minimum size
                if (closed) {
                    return;
                }
                do {
                    while (!excerpt.index(index))
                        pause(delayNS);
                    int size = excerpt.capacity();
                    int remaining = size + TcpUtil.HEADER_SIZE;

                    bb.clear();
                    bb.putLong(index);
                    bb.putInt(size);
                    while (remaining > 0) {
                        int size2 = Math.min(remaining, bb.capacity());
                        bb.limit(size2);
                        excerpt.read(bb);
                        bb.flip();
                        remaining -= bb.remaining();
                        IOTools.writeAll(socket, bb);
                    }
                    if (bb.remaining() > 0) throw new EOFException("Failed to send index=" + index);
View Full Code Here

    public SocketGateway(final InetSocketAddress address, @NotNull Chronicle outbound, @NotNull Chronicle inbound, @NotNull WaitingThread waitingThread) {
        this.address = address;
        this.outbound = outbound;
        this.inbound = inbound;

        Excerpt out = outbound.createExcerpt();
        out.index(out.size());
        outboundReader = new GatewayEntryReader(out, true) {
            final ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1 << 20);

            @Override
            protected void onEntry(long writeTimeNS, long writeTimeMS, long readTimeMS, int length, char type, @NotNull Excerpt excerpt) {
                if (type == 'X') return;

                byteBuffer.position(0);
                byteBuffer.limit(length);
                excerpt.read(byteBuffer);
                byteBuffer.flip();
                try {
                    assert socket != null;
                    IOTools.writeAll(socket, byteBuffer);
                } catch (IOException e) {
                    inboundWriter.onException("Failed to write", e);
                }
            }
        };
        Excerpt in = inbound.createExcerpt();
        in.index(in.size());
        inboundWriter = new GatewayEntryWriter(in);

        waitingThread.add(this);
    }
View Full Code Here

                    bb.clear();
                    assert socket != null;
                    if (socket.read(bb) < 0)
                        return outboundReader.readEntry();
                    bb.flip();
                    Excerpt excerpt = inboundWriter.startExceprt(bb.remaining(), 'I');
                    excerpt.write(bb);
                    excerpt.finish();
                    break;
            }
        } catch (IOException e) {
            inboundWriter.onException("Failed while " + state, e);
            state = State.PAUSING;
View Full Code Here

//        String tmp = System.getProperty("user.home");

        String pePath = tmp + "/demo/pe";
        IndexedChronicle pe2gw = new IndexedChronicle(pePath);
        pe2gw.useUnsafe(true);
        Excerpt excerpt = pe2gw.createExcerpt();
        final Pe2GwWriter pe2GwWriter = new Pe2GwWriter(excerpt);

        Gw2PeEvents listener = new PEEvents(pe2GwWriter);
        Gw2PeReader[] readers = new Gw2PeReader[2];
        IndexedChronicle[] gw2pe = new IndexedChronicle[readers.length];
        for (int i = 0; i < readers.length; i++) {
            int sourceId = i + 1;
            String gw2pePath = tmp + "/demo/gw2pe" + sourceId;
            gw2pe[i] = new IndexedChronicle(gw2pePath);
            gw2pe[i].useUnsafe(true);
            readers[i] = new Gw2PeReader(sourceId, gw2pe[i].createExcerpt(), listener);
        }

        long prevProcessed = 0, count = 0;
        //noinspection InfiniteLoopStatement
        do {
            boolean readOne = false;
            for (Gw2PeReader reader : readers) {
                readOne |= reader.readOne();
            }
            if (readOne) {
                // did something
                count = 0;
            } else if (count++ > 1000000) {
                // do something else like pause.
                long processed = excerpt.index() + 1;
                if (prevProcessed != processed) {
                    System.out.printf("Processed %,d requests%n", processed);
                    prevProcessed = processed;
                }
            }
View Full Code Here

        @Override
        public void run() {
            try {
                long index = readIndex(socket);
                Excerpt excerpt = chronicle.createExcerpt();
                ByteBuffer bb = TcpUtil.createBuffer(1, chronicle.byteOrder()); // minimum size
                long sendInSync = 0;
                boolean first = true;
                OUTER:
                while (!closed) {
                    while (!excerpt.index(index)) {
//                        System.out.println("Waiting for " + index);
                        long now = System.currentTimeMillis();
                        if (sendInSync <= now && !first) {
                            bb.clear();
                            bb.putInt(IN_SYNC_LEN);
                            bb.flip();
                            IOTools.writeAll(socket, bb);
                            sendInSync = now + HEARTBEAT_INTERVAL_MS;
                        }
                        pause();
                        if (closed) break OUTER;
                    }
                    pauseReset();
//                    System.out.println("Writing " + index);
                    final int size = excerpt.capacity();
                    int remaining;

                    bb.clear();
                    if (first) {
//                        System.out.println("wi " + index);
                        bb.putLong(index);
                        first = false;
                        remaining = size + TcpUtil.HEADER_SIZE;
                    } else {
                        remaining = size + 4;
                    }
                    bb.putInt(size);
                    // for large objects send one at a time.
                    if (size > bb.capacity() / 2) {
                        while (remaining > 0) {
                            int size2 = Math.min(remaining, bb.capacity());
                            bb.limit(size2);
                            excerpt.read(bb);
                            bb.flip();
//                        System.out.println("w " + ChronicleTools.asString(bb));
                            remaining -= bb.remaining();
                            IOTools.writeAll(socket, bb);
                        }
                    } else {
                        bb.limit(remaining);
                        excerpt.read(bb);
                        int count = 1;
                        while (excerpt.index(index + 1) && count++ < MAX_MESSAGE) {
                            if (excerpt.remaining() + 4 >= bb.capacity() - bb.position())
                                break;
                            // if there is free space, copy another one.
                            int size2 = excerpt.capacity();
//                            System.out.println("W+ "+size);
                            bb.limit(bb.position() + size2 + 4);
                            bb.putInt(size2);
                            excerpt.read(bb);

                            index++;
                        }

                        bb.flip();
View Full Code Here

    @Test
    public void testReadMessages() throws Exception {
        String path = TMP + "/testReadMessages";
        ChronicleTools.deleteOnExit(path);
        IndexedChronicle chronicle = new IndexedChronicle(path);
        Excerpt excerpt = chronicle.createExcerpt();

        FixSocketReader fsr = new FixSocketReader(null, excerpt) {
            final byte[] bytes = S.getBytes("US-ASCII");
            int start = 0;

            @Override
            protected void readMoreData(@NotNull ByteBuffer buffer) throws IOException {
                int end = Math.min(bytes.length, start + 256);
                buffer.put(bytes, start, end - start);
                start = end;
                if (end == bytes.length)
                    close();
            }
        };

        FixDecoder fd = new FixDecoder(fsr);
        FixDecodeListener listener = new FixDecodeListener() {
            @Override
            public void onField(int fid, @NotNull Excerpt value) {
                String text = value.parseUTF(StopCharTesters.FIX_TEXT);
                System.out.println("\t" + fid + "=" + text);
            }

            @Override
            public void onEndOfMessage() {
                System.out.println("EOM");
            }

            @Override
            public void onEndOfBatch() {
                System.out.println("EOB");
            }
        };
        while (!fsr.isClosed()) {
            fd.readMessages(listener);
        }
        excerpt.index(-1);
        int count = 0;
        while (excerpt.nextIndex()) {
            int length = (int) excerpt.readStopBit();
            String fix = ChronicleTools.asString(excerpt, excerpt.position(), length);
            assertTrue(fix.startsWith("8=FIX"));
            assertTrue(fix.endsWith(".")); // ^A is replaced with .
            assertEquals(8, fix.length() - fix.lastIndexOf(".10="));
            int pos = -1;
            while ((pos = fix.indexOf(".10=", pos + 1)) > 0)
View Full Code Here

    @Test
    public void testReadMessagesPerf() throws Exception {
        String path = TMP + "/testReadMessages";
        ChronicleTools.deleteOnExit(path);
        IndexedChronicle chronicle = new IndexedChronicle(path);
        Excerpt excerpt = chronicle.createExcerpt();
        final int[] count = {0};
        FixSocketReader fsr = new FixSocketReader(null, excerpt) {
            final byte[] bytes = S.getBytes("US-ASCII");
            int start = 0;
View Full Code Here

        deleteOnExit(basePath);

        IndexedChronicle tsc = new IndexedChronicle(basePath);
        tsc.useUnsafe(false /*USE_UNSAFE*/);

        Excerpt excerpt = tsc.createExcerpt();

        List objects = Arrays.asList(1, 1L, "one", new Date(1));

        excerpt.startExcerpt(293);
        // a new ObjectOutputStream is required for each record as they are not reusable :(
        ObjectOutputStream coos = new ObjectOutputStream(excerpt.outputStream());
        coos.writeObject(objects);
        coos.close();

        excerpt.index(0);
        assertEquals(293, excerpt.remaining());

        // a new ObjectInputStream is required for each record as they are not reusable :(
        ObjectInputStream cois = new ObjectInputStream(excerpt.inputStream());
        List objects2 = (List) cois.readObject();
        assertEquals(objects, objects2);
    }
View Full Code Here

TOP

Related Classes of com.higherfrequencytrading.chronicle.Excerpt

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.