mps = entry.getValue();
128 | for (MsgPool mp : mps) {
129 | mp.deleteMsgPool();
130 | }
131 | }
132 | for (DummyContext ctx : eqhs) {
133 | ctx.getEqh().close();
134 | }
135 | msgPools.clear();
136 | eqhs.clear();
137 | }
138 |
139 | /**
140 | * Thread to keep accelio context alive
141 | */
142 | private static final class DummyContext implements Runnable {
143 | private EventQueueHandler eqh;
144 |
145 | public DummyContext(EventQueueHandler ctx) {
146 | eqh = ctx;
147 | }
148 |
149 | public void run() {
150 | eqh.runEventLoop(EventQueueHandler.INFINITE_EVENTS, EventQueueHandler.INFINITE_DURATION);
151 | synchronized (eqh) {
152 | eqh.notifyAll();
153 | }
154 | }
155 |
156 | public EventQueueHandler getEqh() {
157 | synchronized (eqh) {
158 | eqh.breakEventLoop();
159 | try {
160 | eqh.wait();
161 | } catch (InterruptedException e) {
162 | LOG.error("eqh cache was interrupted while in wait");
163 | }
164 | }
165 | return eqh;
166 | }
167 | }
168 | }
169 |
--------------------------------------------------------------------------------
/src/java/org/accelio/jxio/jxioConnection/impl/MultiBufOutputStream.java:
--------------------------------------------------------------------------------
1 | /*
2 | ** Copyright (C) 2013 Mellanox Technologies
3 | **
4 | ** Licensed under the Apache License, Version 2.0 (the "License");
5 | ** you may not use this file except in compliance with the License.
6 | ** You may obtain a copy of the License at:
7 | **
8 | ** http://www.apache.org/licenses/LICENSE-2.0
9 | **
10 | ** Unless required by applicable law or agreed to in writing, software
11 | ** distributed under the License is distributed on an "AS IS" BASIS,
12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13 | ** either express or implied. See the License for the specific language
14 | ** governing permissions and limitations under the License.
15 | **
16 | */
17 | package org.accelio.jxio.jxioConnection.impl;
18 |
19 | import java.io.IOException;
20 | import java.io.OutputStream;
21 | import java.nio.ByteBuffer;
22 |
23 | import org.apache.commons.logging.Log;
24 | import org.apache.commons.logging.LogFactory;
25 |
26 | import org.accelio.jxio.jxioConnection.impl.BufferSupplier;
27 |
28 | public class MultiBufOutputStream extends OutputStream {
29 |
30 | private static final Log LOG = LogFactory.getLog(MultiBufOutputStream.class);
31 | private final BufferSupplier supplier;
32 | private ByteBuffer outBuf = null;
33 | private boolean connectionOpen = true;
34 |
35 | public MultiBufOutputStream(BufferSupplier sup) {
36 | supplier = sup;
37 | }
38 |
39 | @Override
40 | public synchronized void write(int b) throws IOException {
41 | byte[] temp = new byte[1];
42 | write(temp, 0, 1);
43 | }
44 |
45 | @Override
46 | public synchronized void write(byte[] b, int off, int len) throws IOException {
47 | int totalWrite = 0;
48 | int bytesWrite;
49 |
50 | if (!connectionOpen) {
51 | throw new IOException("Stream closed.");
52 | }
53 | if (b == null) {
54 | throw new NullPointerException();
55 | }
56 | // bounds check
57 | if (len < 0 || off < 0 || off + len > b.length) {
58 | throw new ArrayIndexOutOfBoundsException();
59 | }
60 | if (outBuf == null) {
61 | outBuf = supplier.getNextBuffer();
62 | }
63 | while (totalWrite < len) {
64 | // bring new buffer if full
65 | if (!outBuf.hasRemaining()) {
66 | outBuf = supplier.getNextBuffer();
67 | }
68 | bytesWrite = Math.min(len - totalWrite, outBuf.remaining());
69 | outBuf.put(b, off, bytesWrite);
70 | totalWrite += bytesWrite;
71 | off += bytesWrite;
72 | }
73 | }
74 |
75 | @Override
76 | public synchronized void flush() throws IOException {
77 | supplier.flush();
78 | outBuf = null;
79 | }
80 |
81 | @Override
82 | public void close() throws IOException {
83 | if (connectionOpen) {
84 | connectionOpen = false;
85 | flush();
86 | }
87 | }
88 |
89 | /*
90 | * // FOR TESTING!
91 | * public synchronized long skip(long n) throws IOException {
92 | * long numSkipped = 0;
93 | * long skip;
94 | *
95 | * if (outBuf == null) {
96 | * outBuf = bufManager.getNextBuffer();
97 | * }
98 | * while (numSkipped < n) {
99 | * // bring new buffer if empty
100 | * if (!outBuf.hasRemaining()) {
101 | * outBuf = bufManager.getNextBuffer();
102 | * }
103 | * skip = Math.min(n - numSkipped, outBuf.remaining());
104 | * outBuf.position(outBuf.position() + (int) skip);
105 | * numSkipped += skip;
106 | * }
107 | * return numSkipped;
108 | * }
109 | */
110 | }
111 |
--------------------------------------------------------------------------------
/src/java/org/accelio/jxio/jxioConnection/impl/MultiBuffInputStream.java:
--------------------------------------------------------------------------------
1 | /*
2 | ** Copyright (C) 2013 Mellanox Technologies
3 | **
4 | ** Licensed under the Apache License, Version 2.0 (the "License");
5 | ** you may not use this file except in compliance with the License.
6 | ** You may obtain a copy of the License at:
7 | **
8 | ** http://www.apache.org/licenses/LICENSE-2.0
9 | **
10 | ** Unless required by applicable law or agreed to in writing, software
11 | ** distributed under the License is distributed on an "AS IS" BASIS,
12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13 | ** either express or implied. See the License for the specific language
14 | ** governing permissions and limitations under the License.
15 | **
16 | */
17 | package org.accelio.jxio.jxioConnection.impl;
18 |
19 | import java.io.IOException;
20 | import java.io.InputStream;
21 | import java.nio.ByteBuffer;
22 |
23 | import org.accelio.jxio.jxioConnection.impl.BufferSupplier;
24 |
25 | //inspired from SocketInputStream
26 | public class MultiBuffInputStream extends InputStream {
27 |
28 | private final BufferSupplier supplier;
29 | private ByteBuffer inputBuff = null;
30 | private boolean eof = false;
31 | private boolean connectionOpen = true;
32 |
33 | public MultiBuffInputStream(BufferSupplier sup) {
34 | supplier = sup;
35 | }
36 |
37 | @Override
38 | public synchronized int read() throws IOException {
39 | byte[] temp = new byte[1];
40 | int n = read(temp, 0, 1);
41 | if (n <= 0) {
42 | ;
43 | return -1;
44 | }
45 | return temp[0] & 0xFF;
46 | }
47 |
48 | @Override
49 | public synchronized int read(byte[] b, int off, int len) throws IOException {
50 | int totalRead = 0;
51 | int bytesRead;
52 |
53 | if (!connectionOpen) {
54 | throw new IOException("Stream closed.");
55 | }
56 | if (eof) {
57 | return -1;
58 | }
59 | if (b == null) {
60 | throw new NullPointerException();
61 | }
62 | // bounds check
63 | if (len <= 0 || off < 0 || off + len > b.length) {
64 | if (len == 0) {
65 | return 0;
66 | }
67 | throw new ArrayIndexOutOfBoundsException();
68 | }
69 | try {
70 | if (inputBuff == null) {
71 | inputBuff = supplier.getNextBuffer();
72 | }
73 | while (totalRead < len) {
74 | // bring new buffer if empty
75 | if (!inputBuff.hasRemaining()) {
76 | inputBuff = supplier.getNextBuffer();
77 | }
78 | bytesRead = Math.min(len - totalRead, inputBuff.remaining());
79 | inputBuff.get(b, off, bytesRead);
80 | totalRead += bytesRead;
81 | off += bytesRead;
82 | if (inputBuff.limit() <= inputBuff.position() && inputBuff.position() != inputBuff.capacity()) {
83 | eof = true;
84 | return totalRead;
85 | }
86 | }
87 | } catch (IOException e) {
88 | // no buffer available because the other side closed the connection
89 | eof = true;
90 | }
91 | return totalRead;
92 | }
93 |
94 | @Override
95 | public synchronized int available() throws IOException {
96 | if (!connectionOpen) {
97 | throw new IOException("Stream closed.");
98 | }
99 | return inputBuff.remaining();
100 | }
101 |
102 | @Override
103 | public void close() throws IOException {
104 | connectionOpen = false;
105 | }
106 |
107 | @Override
108 | public synchronized long skip(long n) throws IOException {
109 | long numSkipped = 0;
110 | long skip;
111 |
112 | if (!connectionOpen) {
113 | throw new IOException("Stream closed.");
114 | }
115 | if (eof || n <= 0) {
116 | return 0;
117 | }
118 | if (inputBuff == null) {
119 | inputBuff = supplier.getNextBuffer();
120 | }
121 | while (numSkipped < n) {
122 | // bring new buffer if empty
123 | if (!inputBuff.hasRemaining()) {
124 | inputBuff = supplier.getNextBuffer();
125 | }
126 | skip = Math.min(n - numSkipped, inputBuff.remaining());
127 | inputBuff.position((int) (inputBuff.position() + skip));
128 | numSkipped += skip;
129 | }
130 | return numSkipped;
131 | }
132 |
133 | }
134 |
--------------------------------------------------------------------------------
/src/java/org/accelio/jxio/jxioConnection/impl/SimpleConnection.java:
--------------------------------------------------------------------------------
1 | /*
2 | ** Copyright (C) 2013 Mellanox Technologies
3 | **
4 | ** Licensed under the Apache License, Version 2.0 (the "License");
5 | ** you may not use this file except in compliance with the License.
6 | ** You may obtain a copy of the License at:
7 | **
8 | ** http://www.apache.org/licenses/LICENSE-2.0
9 | **
10 | ** Unless required by applicable law or agreed to in writing, software
11 | ** distributed under the License is distributed on an "AS IS" BASIS,
12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13 | ** either express or implied. See the License for the specific language
14 | ** governing permissions and limitations under the License.
15 | **
16 | */
17 | package org.accelio.jxio.jxioConnection.impl;
18 |
19 | import java.net.ConnectException;
20 | import java.net.URI;
21 |
22 | import org.apache.commons.logging.Log;
23 | import org.apache.commons.logging.LogFactory;
24 |
25 | import org.accelio.jxio.Msg;
26 | import org.accelio.jxio.MsgPool;
27 | import org.accelio.jxio.EventName;
28 | import org.accelio.jxio.EventReason;
29 | import org.accelio.jxio.ClientSession;
30 | import org.accelio.jxio.EventQueueHandler;
31 | import org.accelio.jxio.exceptions.JxioGeneralException;
32 | import org.accelio.jxio.exceptions.JxioQueueOverflowException;
33 | import org.accelio.jxio.exceptions.JxioSessionClosedException;
34 | import org.accelio.jxio.jxioConnection.impl.JxioResourceManager;
35 | import org.accelio.jxio.jxioConnection.impl.SimpleConnection;
36 |
37 | public abstract class SimpleConnection {
38 |
39 | private static final Log LOG = LogFactory.getLog(SimpleConnection.class.getCanonicalName());
40 | private boolean established = false;
41 | protected EventQueueHandler eqh;
42 | protected final ClientSession cs;
43 | protected MsgPool msgPool;
44 | protected EventName connectErrorType = null;
45 | protected boolean close = false;
46 | protected Msg msg = null;
47 |
48 | public SimpleConnection(URI uri, int msgIn, int msgOut, int msgCount) throws ConnectException {
49 | eqh = JxioResourceManager.getEqh();
50 | msgPool = JxioResourceManager.getMsgPool(msgCount, msgIn, msgOut);
51 | long startTime = System.nanoTime();
52 | cs = new ClientSession(eqh, uri, new ClientCallbacks());
53 | eqh.runEventLoop(1, EventQueueHandler.INFINITE_DURATION); // session established event
54 | if (!established) {
55 | throw new ConnectException(this.toString() + " could not connect to " + uri.getHost() + " on port "
56 | + uri.getPort() + ", got " + connectErrorType);
57 | }
58 | long endTime = System.nanoTime();
59 |
60 | LOG.info(cs.toString() + " session established with host " + uri.getHost() + ", time taken to open: "
61 | + (endTime - startTime));
62 | }
63 |
64 | public class ClientCallbacks implements ClientSession.Callbacks {
65 |
66 | public void onMsgError(Msg msg, EventReason reason) {
67 | if (reason != EventReason.MSG_FLUSHED) {
68 | LOG.info(SimpleConnection.this.toString() + " onMsgErrorCallback, " + reason);
69 | }
70 | msgPool.releaseMsg(msg);
71 | }
72 |
73 | public void onSessionEstablished() {
74 | established = true;
75 | }
76 |
77 | public void onSessionEvent(EventName event, EventReason reason) {
78 | LOG.info(SimpleConnection.this.toString() + " onSessionEvent " + event);
79 | if (event == EventName.SESSION_CLOSED || event == EventName.SESSION_ERROR
80 | || event == EventName.SESSION_REJECT) { // normal exit
81 | connectErrorType = event;
82 | boolean needToClean = !eqh.getInRunEventLoop();
83 | if (close) {
84 | needToClean = false;
85 | }
86 | close = true;
87 | eqh.breakEventLoop();
88 | if (needToClean) {
89 | releaseResources();
90 | }
91 | }
92 | }
93 |
94 | public void onResponse(Msg m) {
95 | msg = m;
96 | if (close) {
97 | msgPool.releaseMsg(m);
98 | msg = null;
99 | }
100 | }
101 | }
102 |
103 | public void disconnect() {
104 | if (close) return;
105 | close = true;
106 | if (LOG.isDebugEnabled()) {
107 | LOG.debug(this.toString() + " jxioConnection disconnect, msgPool.count()" + msgPool.count()
108 | + " msgPool.capacity() " + msgPool.capacity());
109 | }
110 | handleLastMsg();
111 | while (msgPool.count() < msgPool.capacity()) {
112 | eqh.runEventLoop(msgPool.capacity() - msgPool.count(), EventQueueHandler.INFINITE_DURATION);
113 | }
114 | if (LOG.isDebugEnabled()) {
115 | LOG.debug(this.toString() + "disconnecting,got all msgs back, closing session");
116 | }
117 | closeStream();
118 | cs.close();
119 | eqh.runEventLoop(EventQueueHandler.INFINITE_EVENTS, EventQueueHandler.INFINITE_DURATION);
120 | releaseResources();
121 | }
122 |
123 | public void releaseResources() {
124 | if (LOG.isDebugEnabled()) {
125 | LOG.debug(this.toString() + " Releasing resources");
126 | }
127 | if (eqh != null)
128 | JxioResourceManager.returnEqh(eqh);
129 | if (msgPool != null)
130 | JxioResourceManager.returnMsgPool(msgPool);
131 | eqh = null;
132 | msgPool = null;
133 | }
134 |
135 | public void sendMsg() {
136 | if (msg != null) {
137 | try {
138 | cs.sendRequest(msg);
139 | } catch (JxioSessionClosedException e) {
140 | LOG.debug(this.toString() + " Error sending message: " + e.toString());
141 | msgPool.releaseMsg(msg);
142 | } catch (JxioQueueOverflowException e) {
143 | LOG.error(this.toString() + " Error sending message: " + e.toString());
144 | msgPool.releaseMsg(msg);
145 | } catch (JxioGeneralException e) {
146 | LOG.error(this.toString() + " Error sending message: " + e.toString());
147 | msgPool.releaseMsg(msg);
148 | }
149 | msg = null;
150 | }
151 | }
152 |
153 | public abstract void closeStream();
154 |
155 | public abstract void handleLastMsg();
156 | }
157 |
--------------------------------------------------------------------------------
/src/java/org/apache/lucene/facet/taxonomy/LRUHashMap.java:
--------------------------------------------------------------------------------
1 | package org.apache.lucene.facet.taxonomy;
2 |
3 | /*
4 | * Licensed to the Apache Software Foundation (ASF) under one or more
5 | * contributor license agreements. See the NOTICE file distributed with
6 | * this work for additional information regarding copyright ownership.
7 | * The ASF licenses this file to You under the Apache License, Version 2.0
8 | * (the "License"); you may not use this file except in compliance with
9 | * the License. You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | *
19 | * Taken from lucene-facet-4.9.0
20 | */
21 |
22 | import java.util.LinkedHashMap;
23 | import java.util.Map;
24 |
25 | /**
26 | * LRUHashMap is an extension of Java's HashMap, which has a bounded size();
27 | * When it reaches that size, each time a new element is added, the least
28 | * recently used (LRU) entry is removed.
29 | *
30 | * Java makes it very easy to implement LRUHashMap - all its functionality is
31 | * already available from {@link java.util.LinkedHashMap}, and we just need to
32 | * configure that properly.
33 | *
34 | * Note that like HashMap, LRUHashMap is unsynchronized, and the user MUST
35 | * synchronize the access to it if used from several threads. Moreover, while
36 | * with HashMap this is only a concern if one of the threads is modifies the
37 | * map, with LURHashMap every read is a modification (because the LRU order
38 | * needs to be remembered) so proper synchronization is always necessary.
39 | *
40 | * With the usual synchronization mechanisms available to the user, this
41 | * unfortunately means that LRUHashMap will probably perform sub-optimally under
42 | * heavy contention: while one thread uses the hash table (reads or writes), any
43 | * other thread will be blocked from using it - or even just starting to use it
44 | * (e.g., calculating the hash function). A more efficient approach would be not
45 | * to use LinkedHashMap at all, but rather to use a non-locking (as much as
46 | * possible) thread-safe solution, something along the lines of
47 | * java.util.concurrent.ConcurrentHashMap (though that particular class does not
48 | * support the additional LRU semantics, which will need to be added separately
49 | * using a concurrent linked list or additional storage of timestamps (in an
50 | * array or inside the entry objects), or whatever).
51 | *
52 | * @lucene.experimental
53 | */
54 | public class LRUHashMap extends LinkedHashMap {
55 |
56 | private static final long serialVersionUID = 1L;
57 | private int maxSize;
58 |
59 | /**
60 | * Create a new hash map with a bounded size and with least recently
61 | * used entries removed.
62 | * @param maxSize
63 | * the maximum size (in number of entries) to which the map can grow
64 | * before the least recently used entries start being removed.
65 | * Setting maxSize to a very large value, like
66 | * {@link Integer#MAX_VALUE} is allowed, but is less efficient than
67 | * using {@link java.util.HashMap} because our class needs
68 | * to keep track of the use order (via an additional doubly-linked
69 | * list) which is not used when the map's size is always below the
70 | * maximum size.
71 | */
72 | public LRUHashMap(int maxSize) {
73 | super(16, 0.75f, true);
74 | this.maxSize = maxSize;
75 | }
76 |
77 | /**
78 | * Return the max size
79 | */
80 | public int getMaxSize() {
81 | return maxSize;
82 | }
83 |
84 | /**
85 | * setMaxSize() allows changing the map's maximal number of elements
86 | * which was defined at construction time.
87 | *
88 | * Note that if the map is already larger than maxSize, the current
89 | * implementation does not shrink it (by removing the oldest elements);
90 | * Rather, the map remains in its current size as new elements are
91 | * added, and will only start shrinking (until settling again on the
92 | * give maxSize) if existing elements are explicitly deleted.
93 | */
94 | public void setMaxSize(int maxSize) {
95 | this.maxSize = maxSize;
96 | }
97 |
98 | // We override LinkedHashMap's removeEldestEntry() method. This method
99 | // is called every time a new entry is added, and if we return true
100 | // here, the eldest element will be deleted automatically. In our case,
101 | // we return true if the size of the map grew beyond our limit - ignoring
102 | // what is that eldest element that we'll be deleting.
103 | @Override
104 | protected boolean removeEldestEntry(Map.Entry eldest) {
105 | return size() > maxSize;
106 | }
107 |
108 | @SuppressWarnings("unchecked")
109 | @Override
110 | public LRUHashMap clone() {
111 | return (LRUHashMap) super.clone();
112 | }
113 |
114 | }
115 |
--------------------------------------------------------------------------------
/src/lib/commons-logging-1.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/accelio/JXIO/a3331e1843a0f384770ca7475a53bff08b96006d/src/lib/commons-logging-1.2.jar
--------------------------------------------------------------------------------
/src/lib/commons-logging.jar:
--------------------------------------------------------------------------------
1 | commons-logging-1.2.jar
--------------------------------------------------------------------------------
/src/lib/log4j-1.2.15.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/accelio/JXIO/a3331e1843a0f384770ca7475a53bff08b96006d/src/lib/log4j-1.2.15.jar
--------------------------------------------------------------------------------