Example usage for java.util HashSet size

List of usage examples for java.util HashSet size

Introduction

In this page you can find the example usage for java.util HashSet size.

Prototype

public int size() 

Source Link

Document

Returns the number of elements in this set (its cardinality).

Usage

From source file:org.kchine.r.server.DirectJNI.java

private void fireVariableChangedEvents(long[] variablePointersBefore,
        HashMap<String, Object> clientProperties) {
    if (probedVariables.length > 0) {
        try {//from  ww  w  .  j  a v  a 2s  . c  o m
            final long[][] variablePointersAfter = new long[1][probedVariables.length];
            runR(new org.kchine.r.server.ExecutionUnit() {
                public void run(Rengine e) {
                    for (int i = 0; i < probedVariables.length; ++i) {
                        int[] exists = e.rniGetBoolArrayI(
                                e.rniEval(e.rniParse("exists('" + probedVariables[i] + "')", 1), 0));
                        if (exists[0] == 1) {
                            variablePointersAfter[0][i] = e.rniEval(e.rniParse(probedVariables[i], 1), 0);
                        }
                    }
                }
            });

            HashSet<String> changedVariablesHashSet = new HashSet<String>();
            for (int i = 0; i < probedVariables.length; ++i)
                if (variablePointersBefore[i] != variablePointersAfter[0][i])
                    changedVariablesHashSet.add(probedVariables[i]);
            if (changedVariablesHashSet.size() > 0) {
                HashMap<String, Object> attrs = new HashMap<String, Object>();
                attrs.put("variables", changedVariablesHashSet);
                notifyRActionListeners(new RConsoleAction("VARIABLES_CHANGE", attrs, clientProperties));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.clustercontrol.nodemap.session.NodeMapControllerBean.java

/**
 * fping?ping?????<BR>/*from   w  w w  .j a  v a 2s .  co  m*/
 * @param facilityId Ping?ID()collect?facilityID???
 * @return ping??
 * @throws HinemosUnknown
 * @throws NodeMapException 
 */
public List<String> pingToFacilityList(List<String> facilityList) throws HinemosUnknown, NodeMapException {

    String message = "";
    String messageOrg = "";
    //??????
    // hosts[] IP(String ??)
    // hostsv6[]  IPv6(String??)
    // node     IP????
    // target   nodo?
    HashSet<String> hosts = new HashSet<String>();
    HashSet<String> hostsv6 = new HashSet<String>();
    // ip?name
    Hashtable<String, List<String>> facilityNameTable = new Hashtable<>();

    String facilityId = null;
    int version = 4;
    String[] node;
    for (int index = 0; index < facilityList.size(); index++) {
        facilityId = facilityList.get(index);
        if (facilityId != null && !"".equals(facilityId)) {
            node = new String[2];
            try {

                // ??
                NodeInfo info = new RepositoryControllerBean().getNode(facilityId);

                if (info.getIpAddressVersion() != null) {
                    version = info.getIpAddressVersion();
                } else {
                    version = 4;
                }

                if (version == 6) {

                    InetAddress[] ip = InetAddress.getAllByName(info.getIpAddressV6());

                    if (ip.length != 1) {
                        //IPInetAddress??????1????????
                        //UnnownHostExcption
                        UnknownHostException e = new UnknownHostException();
                        m_log.info("pingToFacilityList() : " + e.getClass().getSimpleName() + ", "
                                + e.getMessage());
                        throw e;
                    }

                    node[0] = ip[0].getHostAddress();
                    if (node[0] != null && !node[0].equals("")) {
                        //IPHashSet?????
                        hostsv6.add(node[0]);
                    }
                } else {
                    node[0] = info.getIpAddressV4();
                    if (node[0] != null && !node[0].equals("")) {

                        //IPHashSet?????
                        hosts.add(node[0]);
                    }
                }
                if (node[0] != null && !node[0].equals("")) {
                    node[1] = info.getNodeName();
                    //target??????
                    List<String> facilitys = facilityNameTable.get(node[0]);
                    if (facilitys == null) {
                        facilitys = new ArrayList<>();
                    }
                    facilitys.add(facilityId);
                    facilityNameTable.put(node[0], facilitys);
                }
            } catch (FacilityNotFound e) {
                message = MessageConstant.MESSAGE_COULD_NOT_GET_NODE_ATTRIBUTES_PING.getMessage() + ","
                        + facilityId;
                messageOrg = e.getMessage();
                throw new NodeMapException(message + ", " + messageOrg, e);
            } catch (UnknownHostException e) {
                // ???
            }
        }
    }

    int runCount = 0;
    int runInterval = 0;
    int pingTimeout = 0;
    try {
        // [](default:1?19)
        String runCountKey = "nodemap.ping.runcount";
        runCount = HinemosPropertyUtil
                .getHinemosPropertyNum(runCountKey, Long.valueOf(PingRunCountConstant.TYPE_COUNT_01))
                .intValue();
        CommonValidator.validateInt(runCountKey, runCount, 1, 9);

        // [ms](default:1000?05000)
        String runIntervalKey = "nodemap.ping.runinterval";
        runInterval = HinemosPropertyUtil
                .getHinemosPropertyNum(runIntervalKey, Long.valueOf(PingRunIntervalConstant.TYPE_SEC_02))
                .intValue();
        CommonValidator.validateInt(runIntervalKey, runInterval, 0, 5 * 1000);

        // [ms](default:5000?13600000)
        String pintTimeoutKey = "nodemap.ping.timeout";
        pingTimeout = HinemosPropertyUtil
                .getHinemosPropertyNum(pintTimeoutKey, Long.valueOf(PingRunIntervalConstant.TYPE_SEC_05))
                .intValue();
        CommonValidator.validateInt(pintTimeoutKey, pingTimeout, 1, 60 * 60 * 1000);
    } catch (Exception e) {
        m_log.warn("pingToFacilityList() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
        throw new HinemosUnknown(e.getMessage(), e);
    }

    ReachAddressFping reachabilityFping = new ReachAddressFping(runCount, runInterval, pingTimeout);

    boolean result = true;
    boolean resultTmp = true;
    ArrayList<String> msgErr = new ArrayList<>();
    ArrayList<String> msgErrV6 = new ArrayList<>();
    Hashtable<String, PingResult> fpingResultSet = new Hashtable<String, PingResult>();
    Hashtable<String, PingResult> fpingResultSetV6 = new Hashtable<String, PingResult>();

    RunMonitorPing monitorPing = new RunMonitorPing();
    //IPv4???fping??
    if (hosts.size() != 0) {
        result = reachabilityFping.isReachable(hosts, 4);
        msgErr = reachabilityFping.getM_errMsg();
    }
    //IPv6???fping6??
    if (hostsv6.size() != 0) {
        resultTmp = reachabilityFping.isReachable(hostsv6, 6);
        msgErrV6 = reachabilityFping.getM_errMsg();
    }

    if (!result || !resultTmp) {
        return null;
    }
    List<String> retList = new ArrayList<>();
    fpingResultSet = monitorPing.wrapUpFping(msgErr, runCount, 4);
    fpingResultSetV6 = monitorPing.wrapUpFping(msgErrV6, runCount, 6);
    //IPv4????????IPv6?????
    m_log.debug("pingToFacilityList(): before fpingResultSet check");
    if (fpingResultSet.size() == 0) {
        m_log.debug("pingToFacilityList(): after fpingResultSet check");
        fpingResultSet = fpingResultSetV6;
    }
    //IPv4??????IPv6??
    else if (fpingResultSetV6.size() != 0) {
        fpingResultSet.putAll(fpingResultSetV6);
    }
    for (Map.Entry<String, List<String>> ipAdd : facilityNameTable.entrySet()) {
        PingResult pingResult = fpingResultSet.get(ipAdd.getKey());
        for (String facility : ipAdd.getValue()) {
            retList.add(facility + " : " + pingResult.getMesseageOrg());
        }
    }
    return retList;
}

From source file:org.hyperledger.fabric.sdk.Channel.java

/**
 * Send transaction to one of a specified set of orderers with the specified user context.
 * IF there are no event hubs or eventing peers this future returns immediately completed
 * indicating that orderer has accepted the transaction only.
 *
 * @param proposalResponses//from  w  ww . j  a  v  a 2 s . com
 * @param transactionOptions
 * @return Future allowing access to the result of the transaction invocation.
 */

public CompletableFuture<TransactionEvent> sendTransaction(Collection<ProposalResponse> proposalResponses,
        TransactionOptions transactionOptions) {
    try {

        if (null == transactionOptions) {
            throw new InvalidArgumentException("Parameter transactionOptions can't be null");
        }
        checkChannelState();
        User userContext = transactionOptions.userContext != null ? transactionOptions.userContext
                : client.getUserContext();
        userContextCheck(userContext);
        if (null == proposalResponses) {
            throw new InvalidArgumentException("sendTransaction proposalResponses was null");
        }

        List<Orderer> orderers = transactionOptions.orderers != null ? transactionOptions.orderers
                : new ArrayList<>(getOrderers());

        // make certain we have our own copy
        final List<Orderer> shuffeledOrderers = new ArrayList<>(orderers);

        if (transactionOptions.shuffleOrders) {
            Collections.shuffle(shuffeledOrderers);
        }

        if (config.getProposalConsistencyValidation()) {
            HashSet<ProposalResponse> invalid = new HashSet<>();
            int consistencyGroups = SDKUtils.getProposalConsistencySets(proposalResponses, invalid).size();

            if (consistencyGroups != 1 || !invalid.isEmpty()) {
                throw new IllegalArgumentException(format(
                        "The proposal responses have %d inconsistent groups with %d that are invalid."
                                + " Expected all to be consistent and none to be invalid.",
                        consistencyGroups, invalid.size()));

            }

        }

        List<FabricProposalResponse.Endorsement> ed = new LinkedList<>();
        FabricProposal.Proposal proposal = null;
        ByteString proposalResponsePayload = null;
        String proposalTransactionID = null;
        TransactionContext transactionContext = null;

        for (ProposalResponse sdkProposalResponse : proposalResponses) {
            ed.add(sdkProposalResponse.getProposalResponse().getEndorsement());
            if (proposal == null) {
                proposal = sdkProposalResponse.getProposal();
                proposalTransactionID = sdkProposalResponse.getTransactionID();
                if (proposalTransactionID == null) {
                    throw new InvalidArgumentException("Proposals with missing transaction ID");
                }
                proposalResponsePayload = sdkProposalResponse.getProposalResponse().getPayload();
                if (proposalResponsePayload == null) {
                    throw new InvalidArgumentException("Proposals with missing payload.");
                }
                transactionContext = sdkProposalResponse.getTransactionContext();
                if (transactionContext == null) {
                    throw new InvalidArgumentException("Proposals with missing transaction context.");
                }
            } else {
                final String transactionID = sdkProposalResponse.getTransactionID();
                if (transactionID == null) {
                    throw new InvalidArgumentException("Proposals with missing transaction id.");
                }
                if (!proposalTransactionID.equals(transactionID)) {
                    throw new InvalidArgumentException(
                            format("Proposals with different transaction IDs %s,  and %s",
                                    proposalTransactionID, transactionID));
                }
            }
        }

        TransactionBuilder transactionBuilder = TransactionBuilder.newBuilder();

        Payload transactionPayload = transactionBuilder.chaincodeProposal(proposal).endorsements(ed)
                .proposalResponsePayload(proposalResponsePayload).build();

        Envelope transactionEnvelope = createTransactionEnvelope(transactionPayload, transactionContext);

        NOfEvents nOfEvents = transactionOptions.nOfEvents;

        if (nOfEvents == null) {
            nOfEvents = NOfEvents.createNofEvents();
            Collection<Peer> eventingPeers = getEventingPeers();
            boolean anyAdded = false;
            if (!eventingPeers.isEmpty()) {
                anyAdded = true;
                nOfEvents.addPeers(eventingPeers);
            }
            Collection<EventHub> eventHubs = getEventHubs();
            if (!eventHubs.isEmpty()) {
                anyAdded = true;
                nOfEvents.addEventHubs(getEventHubs());
            }

            if (!anyAdded) {
                nOfEvents = NOfEvents.createNoEvents();
            }

        } else if (nOfEvents != NOfEvents.nofNoEvents) {
            StringBuilder issues = new StringBuilder(100);
            Collection<Peer> eventingPeers = getEventingPeers();
            nOfEvents.unSeenPeers().forEach(peer -> {
                if (peer.getChannel() != this) {
                    issues.append(format("Peer %s added to NOFEvents does not belong this channel. ",
                            peer.getName()));

                } else if (!eventingPeers.contains(peer)) {
                    issues.append(format("Peer %s added to NOFEvents is not a eventing Peer in this channel. ",
                            peer.getName()));
                }

            });
            nOfEvents.unSeenEventHubs().forEach(eventHub -> {
                if (!eventHubs.contains(eventHub)) {
                    issues.append(format("Eventhub %s added to NOFEvents does not belong this channel. ",
                            eventHub.getName()));
                }

            });

            if (nOfEvents.unSeenEventHubs().isEmpty() && nOfEvents.unSeenPeers().isEmpty()) {
                issues.append("NofEvents had no Eventhubs added or Peer eventing services.");
            }
            String foundIssues = issues.toString();
            if (!foundIssues.isEmpty()) {
                throw new InvalidArgumentException(foundIssues);
            }
        }

        final boolean replyonly = nOfEvents == NOfEvents.nofNoEvents
                || (getEventHubs().isEmpty() && getEventingPeers().isEmpty());

        CompletableFuture<TransactionEvent> sret;
        if (replyonly) { //If there are no eventhubs to complete the future, complete it
            // immediately but give no transaction event
            logger.debug(format(
                    "Completing transaction id %s immediately no event hubs or peer eventing services found in channel %s.",
                    proposalTransactionID, name));
            sret = new CompletableFuture<>();
        } else {
            sret = registerTxListener(proposalTransactionID, nOfEvents, transactionOptions.failFast);
        }

        logger.debug(format("Channel %s sending transaction to orderer(s) with TxID %s ", name,
                proposalTransactionID));
        boolean success = false;
        Exception lException = null; // Save last exception to report to user .. others are just logged.

        BroadcastResponse resp = null;
        Orderer failed = null;
        for (Orderer orderer : shuffeledOrderers) {
            if (failed != null) {
                logger.warn(format("Channel %s  %s failed. Now trying %s.", name, failed, orderer));
            }
            failed = orderer;
            try {

                if (null != diagnosticFileDumper) {
                    logger.trace(format("Sending to channel %s, orderer: %s, transaction: %s", name,
                            orderer.getName(), diagnosticFileDumper
                                    .createDiagnosticProtobufFile(transactionEnvelope.toByteArray())));
                }

                resp = orderer.sendTransaction(transactionEnvelope);
                lException = null; // no longer last exception .. maybe just failed.
                if (resp.getStatus() == Status.SUCCESS) {
                    success = true;
                    break;
                } else {
                    logger.warn(format("Channel %s %s failed. Status returned %s", name, orderer,
                            getRespData(resp)));
                }
            } catch (Exception e) {
                String emsg = format("Channel %s unsuccessful sendTransaction to orderer %s (%s)", name,
                        orderer.getName(), orderer.getUrl());
                if (resp != null) {

                    emsg = format("Channel %s unsuccessful sendTransaction to orderer %s (%s).  %s", name,
                            orderer.getName(), orderer.getUrl(), getRespData(resp));
                }

                logger.error(emsg);
                lException = new Exception(emsg, e);

            }

        }

        if (success) {
            logger.debug(format("Channel %s successful sent to Orderer transaction id: %s", name,
                    proposalTransactionID));
            if (replyonly) {
                sret.complete(null); // just say we're done.
            }
            return sret;
        } else {

            String emsg = format(
                    "Channel %s failed to place transaction %s on Orderer. Cause: UNSUCCESSFUL. %s", name,
                    proposalTransactionID, getRespData(resp));

            unregisterTxListener(proposalTransactionID);

            CompletableFuture<TransactionEvent> ret = new CompletableFuture<>();
            ret.completeExceptionally(
                    lException != null ? new Exception(emsg, lException) : new Exception(emsg));
            return ret;
        }
    } catch (Exception e) {

        CompletableFuture<TransactionEvent> future = new CompletableFuture<>();
        future.completeExceptionally(e);
        return future;

    }

}

From source file:dao.DirectoryDaoDb.java

/**
 * getDirBlobsFromTags - gets the directory blobs based on tags
 * @param dirtags - directory with tags/*w ww  . j av  a2  s  . c  o m*/
 */
public HashSet getDirBlobsFromTags(HashSet dirtags) throws BaseDaoException {
    if (dirtags != null && dirtags.size() > 0) {
        Iterator it1 = dirtags.iterator();
        while (it1.hasNext()) {
            Photo blob = (Photo) it1.next();
            if (blob == null)
                continue;
            String entryid = blob.getValue(DbConstants.ENTRYID);
            blob.setObject(DbConstants.PHOTO,
                    getPhoto(blob.getValue(DbConstants.ENTRYID), blob.getValue(DbConstants.DIRECTORY_ID)));
        }
    }
    return dirtags;
}

From source file:com.couchbase.lite.store.SQLiteViewStore.java

/**
 * Updates the indexes of one or more views in parallel.
 *
 * @param inputViews An array of ViewStore instances, always including the receiver.
 * @return Status OK if updated or NOT_MODIFIED if already up-to-date.
 * @throws CouchbaseLiteException//w w  w  .j a  v a 2  s . c  o  m
 */
@Override
@InterfaceAudience.Private
public Status updateIndexes(List<ViewStore> inputViews) throws CouchbaseLiteException {
    Log.v(Log.TAG_VIEW, "Re-indexing view: %s", name);
    if (getViewID() <= 0) {
        String msg = "getViewID() < 0";
        throw new CouchbaseLiteException(msg, new Status(Status.NOT_FOUND));
    }

    store.beginTransaction();
    boolean success = false;
    Cursor cursor = null;
    try {
        // If the view the update is for doesn't need any update, don't do anything:
        final long dbMaxSequence = store.getLastSequence();
        final long forViewLastSequence = getLastSequenceIndexed();
        if (forViewLastSequence >= dbMaxSequence) {
            success = true;
            return new Status(Status.NOT_MODIFIED);
        }

        // Check whether we need to update at all,
        // and remove obsolete emitted results from the 'maps' table:
        long minLastSequence = dbMaxSequence;
        final long[] viewLastSequence = new long[inputViews.size()];
        int deletedCount = 0;
        int i = 0;
        final HashSet<String> docTypes = new HashSet<String>();
        HashMap<String, String> viewDocTypes = null;
        boolean allDocTypes = false;
        final HashMap<Integer, Integer> viewTotalRows = new HashMap<Integer, Integer>();
        final ArrayList<SQLiteViewStore> views = new ArrayList<SQLiteViewStore>();
        final ArrayList<Mapper> mapBlocks = new ArrayList<Mapper>();

        for (ViewStore v : inputViews) {
            assert (v != null);
            SQLiteViewStore view = (SQLiteViewStore) v;
            ViewStoreDelegate delegate = view.getDelegate();
            Mapper map = delegate != null ? delegate.getMap() : null;
            if (map == null) {
                if (view == this) {
                    String msg = String.format(Locale.ENGLISH,
                            "Cannot index view %s: " + "no map block registered", view.getName());
                    Log.e(Log.TAG_VIEW, msg);
                    throw new CouchbaseLiteException(msg, new Status(Status.BAD_REQUEST));
                }
                Log.v(Log.TAG_VIEW, "    %s has no map block; skipping it", view.getName());
                continue;
            }

            views.add(view);
            mapBlocks.add(map);

            int viewID = view.getViewID();
            if (viewID <= 0) {
                String message = String.format(Locale.ENGLISH, "View '%s' not found in database",
                        view.getName());
                Log.e(Log.TAG_VIEW, message);
                throw new CouchbaseLiteException(message, new Status(Status.NOT_FOUND));
            }

            int totalRows = view.getTotalRows();
            viewTotalRows.put(viewID, totalRows);

            long last = view == this ? forViewLastSequence : view.getLastSequenceIndexed();
            viewLastSequence[i++] = last;
            if (last < 0) {
                String msg = String.format(Locale.ENGLISH, "last < 0 (%d)", last);
                throw new CouchbaseLiteException(msg, new Status(Status.INTERNAL_SERVER_ERROR));
            } else if (last < dbMaxSequence) {
                if (last == 0)
                    view.createIndex();
                minLastSequence = Math.min(minLastSequence, last);
                Log.v(Log.TAG_VIEW, "    %s last indexed at #%d", view.getName(), last);

                String docType = delegate.getDocumentType();
                if (docType != null) {
                    docTypes.add(docType);
                    if (viewDocTypes == null)
                        viewDocTypes = new HashMap<String, String>();
                    viewDocTypes.put(view.getName(), docType);
                } else {
                    allDocTypes = true;
                }

                int changes = 0;
                if (last == 0) {
                    changes = store.getStorageEngine().delete(view.queryString("maps_#"), null, null);
                } else {
                    store.optimizeSQLIndexes();
                    String[] args = { Long.toString(last), Long.toString(last) };
                    changes = store.getStorageEngine().delete(view.queryString("maps_#"),
                            "sequence IN (SELECT parent FROM revs "
                                    + "WHERE sequence>? AND +parent>0 AND +parent<=?)",
                            args);
                }

                // Update #deleted rows:
                deletedCount += changes;

                // Only count these deletes as changes if this isn't a view reset to 0
                if (last != 0) {
                    int newTotalRows = viewTotalRows.get(viewID) - changes;
                    viewTotalRows.put(viewID, newTotalRows);
                }
            }
        }

        if (minLastSequence == dbMaxSequence) {
            Log.v(Log.TAG_VIEW, "minLastSequence (%d) == dbMaxSequence (%d), nothing to do", minLastSequence,
                    dbMaxSequence);
            success = true;
            return new Status(Status.NOT_MODIFIED);
        }

        Log.v(Log.TAG_VIEW, "Updating indexes of (%s) from #%d to #%d ...", viewNames(views), minLastSequence,
                dbMaxSequence);

        // This is the emit() block, which gets called from within the user-defined map() block
        // that's called down below.
        final AtomicInteger insertedCount = new AtomicInteger(0);
        AbstractMapEmitBlock emitBlock = new AbstractMapEmitBlock() {
            @Override
            public void emit(Object key, Object value) {
                if (key == null) {
                    Log.w(Log.TAG_VIEW, "emit() called with nil key; ignoring");
                    return;
                }
                try {
                    curView.emit(key, value, this.sequence); // emit block's sequence
                    int curViewID = curView.getViewID();
                    viewTotalRows.put(curViewID, viewTotalRows.get(curViewID) + 1);
                } catch (Exception e) {
                    Log.e(Log.TAG_VIEW, "Error emitting", e);
                    throw new RuntimeException(e);
                }
            }
        };

        // Now scan every revision added since the last time the view was indexed:

        // NOTE: Below is original Query. In case query result uses a lot of memory,
        //       Android SQLiteDatabase causes null value column. Then it causes the missing
        //       _index data because following logic skip result if column is null.
        //       To avoid the issue, retrieving json field is isolated from original query.
        //       Because json field could be large, maximum size is 2MB.
        // StringBuffer sql = new StringBuffer( "SELECT revs.doc_id, sequence, docid, revid,
        // json, no_attachments, deleted FROM revs, docs WHERE sequence>? AND current!=0 ");

        boolean checkDocTypes = docTypes.size() > 1 || (allDocTypes && docTypes.size() > 0);
        StringBuilder sql = new StringBuilder(
                "SELECT revs.doc_id, sequence, docid, revid, no_attachments, deleted ");
        if (checkDocTypes)
            sql.append(", doc_type ");
        sql.append("FROM revs, docs WHERE sequence>? AND current!=0 ");
        if (minLastSequence == 0) {
            sql.append("AND deleted=0 ");
        }
        if (!allDocTypes && docTypes.size() > 0) {
            String docTypesString = getJoinedSQLQuotedStrings(docTypes.toArray(new String[docTypes.size()]));
            sql.append("AND doc_type IN (").append(docTypesString).append(") ");
        }
        // order result by deleted ASC so if multiple revs returned the non deleted are the first
        // NOTE: Views broken with concurrent update and delete
        // https://github.com/couchbase/couchbase-lite-java-core/issues/952
        sql.append("AND revs.doc_id = docs.doc_id ORDER BY revs.doc_id, deleted ASC, revid DESC");
        String[] selectArgs = { Long.toString(minLastSequence) };
        cursor = store.getStorageEngine().rawQuery(sql.toString(), selectArgs);

        boolean keepGoing = cursor.moveToNext(); // Go to first result row
        while (keepGoing) {
            // NOTE: skip row if 1st column is null
            // https://github.com/couchbase/couchbase-lite-java-core/issues/497
            if (cursor.isNull(0)) {
                keepGoing = cursor.moveToNext();
                continue;
            }

            long docID = cursor.getLong(0);

            // Reconstitute the document as a dictionary:
            long sequence = cursor.getLong(1);
            String docId = cursor.getString(2);
            if (docId.startsWith("_design/")) { // design docs don't get indexed!
                keepGoing = cursor.moveToNext();
                continue;
            }
            String revID = cursor.getString(3);
            boolean deleted = cursor.getInt(5) > 0;
            String docType = checkDocTypes ? cursor.getString(6) : null;

            // Skip rows with the same doc_id -- these are losing conflicts.
            // NOTE: Or Skip rows if 1st column is null
            // https://github.com/couchbase/couchbase-lite-java-core/issues/497
            ArrayList<String> conflicts = null;
            boolean isNull;
            while ((keepGoing = cursor.moveToNext())
                    && ((isNull = cursor.isNull(0)) || cursor.getLong(0) == docID)) {
                if (isNull)
                    continue;
                if (!deleted) {
                    if (conflicts == null)
                        conflicts = new ArrayList<String>();
                    conflicts.add(cursor.getString(3));
                }
            }

            long realSequence = sequence; // because sequence may be changed, below
            if (minLastSequence > 0) {
                // Find conflicts with documents from previous indexings.
                Cursor cursor2 = null;
                try {
                    String[] selectArgs2 = { Long.toString(docID), Long.toString(minLastSequence) };
                    cursor2 = store.getStorageEngine()
                            .rawQuery("SELECT revid, sequence FROM revs "
                                    + "WHERE doc_id=? AND sequence<=? AND current!=0 AND deleted=0 "
                                    + "ORDER BY revID DESC ", selectArgs2);

                    if (cursor2.moveToNext()) {
                        String oldRevID = cursor2.getString(0);
                        // This is the revision that used to be the 'winner'.
                        // Remove its emitted rows:
                        long oldSequence = cursor2.getLong(1);
                        String[] args = { Long.toString(oldSequence) };
                        for (SQLiteViewStore view : views) {
                            int changes = view.store.getStorageEngine().delete(view.queryString("maps_#"),
                                    "sequence=?", args);
                            deletedCount += changes;
                            int thisViewID = view.getViewID();
                            int newTotalRows = viewTotalRows.get(thisViewID) - changes;
                            viewTotalRows.put(thisViewID, newTotalRows);
                        }

                        String conflictRevID = oldRevID;
                        if (deleted || RevisionInternal.CBLCompareRevIDs(oldRevID, revID) > 0) {
                            // It still 'wins' the conflict, so it's the one that
                            // should be mapped [again], not the current revision!
                            conflictRevID = revID;
                            revID = oldRevID;
                            deleted = false;
                            sequence = oldSequence;
                        }

                        if (!deleted) {
                            // Conflict revisions:
                            if (conflicts == null)
                                conflicts = new ArrayList<String>();
                            conflicts.add(conflictRevID);
                            while (cursor2.moveToNext()) {
                                conflicts.add(cursor2.getString(0));
                            }
                        }
                    }
                } finally {
                    if (cursor2 != null) {
                        cursor2.close();
                    }
                }
            }

            if (deleted)
                continue;

            // Get json blob:
            String[] selectArgs3 = { Long.toString(sequence) };
            byte[] json = SQLiteUtils.byteArrayResultForQuery(store.getStorageEngine(),
                    "SELECT json FROM revs WHERE sequence=?", selectArgs3);

            // Get the document properties, to pass to the map function:
            Map<String, Object> curDoc = store.documentPropertiesFromJSON(json, docId, revID, false, sequence);

            if (curDoc == null) {
                Log.w(Log.TAG_VIEW, "Failed to parse JSON of doc %s rev %s", docID, revID);
                continue;
            }
            curDoc.put("_local_seq", sequence);

            if (conflicts != null)
                curDoc.put("_conflicts", conflicts);

            // Call the user-defined map() to emit new key/value pairs from this revision:
            i = -1;
            for (SQLiteViewStore view : views) {
                curView = view;
                ++i;
                if (viewLastSequence[i] < realSequence) {
                    if (checkDocTypes) {
                        String viewDocType = viewDocTypes.get(view.getName());
                        if (viewDocType != null && !viewDocType.equals(docType))
                            continue; // skip; view's documentType doesn't match this doc
                    }
                    Log.v(Log.TAG_VIEW, "#%d: map '%s' for view %s...", sequence, docID, view.getName());
                    try {
                        emitBlock.setSequence(sequence);
                        mapBlocks.get(i).map(curDoc, emitBlock);
                    } catch (Throwable e) {
                        String msg = String.format(Locale.ENGLISH, "Error when calling map block of view '%s'",
                                view.getName());
                        Log.e(Log.TAG_VIEW, msg, e);
                        throw new CouchbaseLiteException(msg, e, new Status(Status.CALLBACK_ERROR));
                    }
                }
            }
        }

        // Finally, record the last revision sequence number that was indexed and update #rows:
        for (SQLiteViewStore view : views) {
            view.finishCreatingIndex();
            int newTotalRows = viewTotalRows.get(view.getViewID());
            ContentValues updateValues = new ContentValues();
            updateValues.put("lastSequence", dbMaxSequence);
            updateValues.put("total_docs", newTotalRows);
            String[] whereArgs = { Integer.toString(view.getViewID()) };
            store.getStorageEngine().update("views", updateValues, "view_id=?", whereArgs);
        }
        Log.v(Log.TAG_VIEW, "...Finished re-indexing (%s) to #%d (deleted %d, added %d)", viewNames(views),
                dbMaxSequence, deletedCount, insertedCount.intValue());

        success = true;
        return new Status(Status.OK);
    } catch (SQLException ex) {
        throw new CouchbaseLiteException(ex, new Status(Status.DB_ERROR));
    } finally {
        curView = null;
        if (cursor != null)
            cursor.close();
        if (store != null)
            store.endTransaction(success);
    }
}

From source file:org.sakaiproject.lessonbuildertool.tool.beans.SimplePageBean.java

public boolean myStudentPageGroupsOk(SimplePageItem item) {
    Group group = null;/*from w  w  w.j  ava 2s . co m*/
    String groupId = null;
    if (item.isGroupOwned()) {
        // all groups we are a member of
        Collection<Group> groups = getCurrentSite().getGroupsWithMember(getCurrentUserId());

        String allowedString = item.getOwnerGroups();
        // if no group list specified, we're OK if user is in any groups
        if (allowedString == null || allowedString.length() == 0)
            return groups != null && groups.size() > 0;

        // otherwise have to check 
        HashSet<String> allowedIds = new HashSet<String>(Arrays.asList(allowedString.split(",")));
        HashSet<String> inIds = new HashSet<String>();
        for (Group g : groups)
            inIds.add(g.getId());

        // see if overlap between allowed and in
        inIds.retainAll(allowedIds);
        return inIds.size() > 0;
    }
    // if not group owned, always OK
    return true;
}

From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java

/**
 * THIS SHOULD RETURN HASHSET and not jsut Set because we add to it later (so it can't be Collections.emptySet())
 * //ww w . j a  v a  2 s.  c  om
 * @param theLastUpdated
 */
public static HashSet<Long> loadReverseIncludes(FhirContext theContext, EntityManager theEntityManager,
        Collection<Long> theMatches, Set<Include> theRevIncludes, boolean theReverseMode,
        DateRangeParam theLastUpdated) {
    if (theMatches.size() == 0) {
        return new HashSet<Long>();
    }
    if (theRevIncludes == null || theRevIncludes.isEmpty()) {
        return new HashSet<Long>();
    }
    String searchFieldName = theReverseMode ? "myTargetResourcePid" : "mySourceResourcePid";

    Collection<Long> nextRoundMatches = theMatches;
    HashSet<Long> allAdded = new HashSet<Long>();
    HashSet<Long> original = new HashSet<Long>(theMatches);
    ArrayList<Include> includes = new ArrayList<Include>(theRevIncludes);

    int roundCounts = 0;
    StopWatch w = new StopWatch();

    boolean addedSomeThisRound;
    do {
        roundCounts++;

        HashSet<Long> pidsToInclude = new HashSet<Long>();
        Set<Long> nextRoundOmit = new HashSet<Long>();

        for (Iterator<Include> iter = includes.iterator(); iter.hasNext();) {
            Include nextInclude = iter.next();
            if (nextInclude.isRecurse() == false) {
                iter.remove();
            }

            boolean matchAll = "*".equals(nextInclude.getValue());
            if (matchAll) {
                String sql;
                sql = "SELECT r FROM ResourceLink r WHERE r." + searchFieldName + " IN (:target_pids)";
                TypedQuery<ResourceLink> q = theEntityManager.createQuery(sql, ResourceLink.class);
                q.setParameter("target_pids", nextRoundMatches);
                List<ResourceLink> results = q.getResultList();
                for (ResourceLink resourceLink : results) {
                    if (theReverseMode) {
                        // if (theEverythingModeEnum.isEncounter()) {
                        // if (resourceLink.getSourcePath().equals("Encounter.subject") ||
                        // resourceLink.getSourcePath().equals("Encounter.patient")) {
                        // nextRoundOmit.add(resourceLink.getSourceResourcePid());
                        // }
                        // }
                        pidsToInclude.add(resourceLink.getSourceResourcePid());
                    } else {
                        pidsToInclude.add(resourceLink.getTargetResourcePid());
                    }
                }
            } else {

                List<String> paths;
                RuntimeSearchParam param = null;
                if (theContext.getVersion().getVersion() == FhirVersionEnum.DSTU1) {
                    paths = Collections.singletonList(nextInclude.getValue());
                } else {
                    String resType = nextInclude.getParamType();
                    if (isBlank(resType)) {
                        continue;
                    }
                    RuntimeResourceDefinition def = theContext.getResourceDefinition(resType);
                    if (def == null) {
                        ourLog.warn("Unknown resource type in include/revinclude=" + nextInclude.getValue());
                        continue;
                    }

                    String paramName = nextInclude.getParamName();
                    param = isNotBlank(paramName) ? def.getSearchParam(paramName) : null;
                    if (param == null) {
                        ourLog.warn("Unknown param name in include/revinclude=" + nextInclude.getValue());
                        continue;
                    }

                    paths = param.getPathsSplit();
                }

                String targetResourceType = defaultString(nextInclude.getParamTargetType(), null);
                for (String nextPath : paths) {
                    String sql;
                    boolean haveTargetTypesDefinedByParam = param != null && param.getTargets() != null
                            && param.getTargets().isEmpty() == false;
                    if (targetResourceType != null) {
                        sql = "SELECT r FROM ResourceLink r WHERE r.mySourcePath = :src_path AND r."
                                + searchFieldName
                                + " IN (:target_pids) AND r.myTargetResourceType = :target_resource_type";
                    } else if (haveTargetTypesDefinedByParam) {
                        sql = "SELECT r FROM ResourceLink r WHERE r.mySourcePath = :src_path AND r."
                                + searchFieldName
                                + " IN (:target_pids) AND r.myTargetResourceType in (:target_resource_types)";
                    } else {
                        sql = "SELECT r FROM ResourceLink r WHERE r.mySourcePath = :src_path AND r."
                                + searchFieldName + " IN (:target_pids)";
                    }
                    TypedQuery<ResourceLink> q = theEntityManager.createQuery(sql, ResourceLink.class);
                    q.setParameter("src_path", nextPath);
                    q.setParameter("target_pids", nextRoundMatches);
                    if (targetResourceType != null) {
                        q.setParameter("target_resource_type", targetResourceType);
                    } else if (haveTargetTypesDefinedByParam) {
                        q.setParameter("target_resource_types", param.getTargets());
                    }
                    List<ResourceLink> results = q.getResultList();
                    for (ResourceLink resourceLink : results) {
                        if (theReverseMode) {
                            Long pid = resourceLink.getSourceResourcePid();
                            pidsToInclude.add(pid);
                        } else {
                            Long pid = resourceLink.getTargetResourcePid();
                            pidsToInclude.add(pid);
                        }
                    }
                }
            }
        }

        if (theLastUpdated != null && (theLastUpdated.getLowerBoundAsInstant() != null
                || theLastUpdated.getUpperBoundAsInstant() != null)) {
            pidsToInclude = new HashSet<Long>(
                    filterResourceIdsByLastUpdated(theEntityManager, theLastUpdated, pidsToInclude));
        }
        for (Long next : pidsToInclude) {
            if (original.contains(next) == false && allAdded.contains(next) == false) {
                theMatches.add(next);
            }
        }

        pidsToInclude.removeAll(nextRoundOmit);

        addedSomeThisRound = allAdded.addAll(pidsToInclude);
        nextRoundMatches = pidsToInclude;
    } while (includes.size() > 0 && nextRoundMatches.size() > 0 && addedSomeThisRound);

    ourLog.info("Loaded {} {} in {} rounds and {} ms", new Object[] { allAdded.size(),
            theReverseMode ? "_revincludes" : "_includes", roundCounts, w.getMillisAndRestart() });

    return allAdded;
}

From source file:app.RunApp.java

/**
 * Create heatmap graph//from  w  w  w.  j a v  a  2 s  .c  om
 * 
 * @param jpanel Panel
 * @param coefficients Coefficients
 * @param list List
 * @param oldHeatmap Old heatmap
 * @return Heatmap
 */
private HeatMap createHeatmapGraph(JPanel jpanel, double[][] coefficients, ArrayList<AttributesPair> list,
        HeatMap oldHeatmap) {
    Color[] colors = new Color[256];

    for (int i = 0; i < colors.length; i++) {
        colors[i] = new Color(i, i, i);
    }

    HeatMap heatMap = null;

    double[][] newCoefs = coefficients.clone();

    for (int i = 0; i < newCoefs.length; i++) {
        for (int j = 0; j < newCoefs.length; j++) {
            if (newCoefs[i][j] < 0) {
                newCoefs[i][j] = 0;
            }
        }
    }

    if ((list != null) && (list.size() > 0)) {
        HashSet<Integer> selected = new HashSet<>();

        for (int i = 0; i < list.size(); i++) {
            selected.add(list.get(i).getAttribute1Index());
            selected.add(list.get(i).getAttribute2Index());
        }

        newCoefs = new double[selected.size()][selected.size()];

        List sortedSelected = new ArrayList(selected);
        Collections.sort(sortedSelected);

        for (int i = 0; i < sortedSelected.size(); i++) {
            for (int j = 0; j < sortedSelected.size(); j++) {
                newCoefs[i][j] = coefficients[(int) sortedSelected.get(i)][(int) sortedSelected.get(j)];
            }
        }
    }

    heatMap = new HeatMap(newCoefs, false, colors);

    if (oldHeatmap != null) {
        jpanel.remove(oldHeatmap);
    }

    jpanel.setLayout(new BorderLayout());
    jpanel.setPreferredSize(new Dimension(550, 425));
    jpanel.add(heatMap, BorderLayout.CENTER);

    jpanel.validate();
    jpanel.repaint();

    return heatMap;
}

From source file:gpl.pierrick.brihaye.aramorph.InMemoryDictionaryHandler.java

/** Loads a dictionary into a <CODE>Set</CODE> where the <PRE>key</PRE> is entry and its <PRE>value</PRE> is a
 * <CODE>List</CODE> (each entry can have multiple values)
 * @param set The set//from  ww w.ja v a  2s. c  o m
 * @param name A human-readable name
 * @param is The stream
 * @throws RuntimeException If a problem occurs when reading the dictionary
 */
private void loadDictionary(Map set, String name, InputStream is) throws RuntimeException { //TODO : should be static
    HashSet lemmas = new HashSet();
    int forms = 0;
    String lemmaID = "";
    System.out.print("Loading dictionary : " + name + " ");
    try {
        LineNumberReader IN = new LineNumberReader(new InputStreamReader(is, "ISO8859_1"));
        String line = null;
        while ((line = IN.readLine()) != null) {
            if ((IN.getLineNumber() % 1000) == 1)
                System.out.print(".");
            // new lemma
            if (line.startsWith(";; ")) {
                lemmaID = line.substring(3);
                // lemmaID's must be unique
                if (lemmas.contains(lemmaID))
                    throw new RuntimeException("Lemma " + lemmaID + "in " + name + " (line "
                            + IN.getLineNumber() + ") isn't unique");
                lemmas.add(lemmaID);
            }
            // comment
            else if (line.startsWith(";")) {
            } else {
                String split[] = line.split("\t", -1); //-1 to avoid triming of trail values

                //a little error-checking won't hurt :
                if (split.length != 4) {
                    throw new RuntimeException("Entry in " + name + " (line " + IN.getLineNumber()
                            + ") doesn't have 4 fields (3 tabs)");
                }
                String entry = split[0]; // get the entry for use as key
                String vocalization = split[1];
                String morphology = split[2];
                String glossPOS = split[3];

                String gloss;
                String POS;

                Pattern p;
                Matcher m;

                // two ways to get the POS info:
                // (1) explicitly, by extracting it from the gloss field:
                p = Pattern.compile(".*" + "<pos>(.+?)</pos>" + ".*");
                m = p.matcher(glossPOS);
                if (m.matches()) {
                    POS = m.group(1); //extract POS from glossPOS
                    gloss = glossPOS; //we clean up the gloss later (see below)
                }
                // (2) by deduction: use the morphology (and sometimes the voc and gloss) to deduce the appropriate POS
                else {
                    // we need the gloss to guess proper names
                    gloss = glossPOS;
                    // null prefix or suffix
                    if (morphology.matches("^(Pref-0|Suff-0)$")) {
                        POS = "";
                    } else if (morphology.matches("^F" + ".*")) {
                        POS = vocalization + "/FUNC_WORD";
                    } else if (morphology.matches("^IV" + ".*")) {
                        POS = vocalization + "/VERB_IMPERFECT";
                    } else if (morphology.matches("^PV" + ".*")) {
                        POS = vocalization + "/VERB_PERFECT";
                    } else if (morphology.matches("^CV" + ".*")) {
                        POS = vocalization + "/VERB_IMPERATIVE";
                    } else if (morphology.matches("^N" + ".*")) {
                        // educated guess (99% correct)
                        if (gloss.matches("^[A-Z]" + ".*")) {
                            POS = vocalization + "/NOUN_PROP";
                        }
                        // (was NOUN_ADJ: some of these are really ADJ's and need to be tagged manually)
                        else if (vocalization.matches(".*" + "iy~$")) {
                            POS = vocalization + "/NOUN";
                        } else
                            POS = vocalization + "/NOUN";
                    } else {
                        throw new RuntimeException(
                                "No POS can be deduced in " + name + " (line " + IN.getLineNumber() + ")");
                    }
                }

                // clean up the gloss: remove POS info and extra space, and convert upper-ASCII  to lower (it doesn't convert well to UTF-8)
                gloss = gloss.replaceFirst("<pos>.+?</pos>", "");
                gloss = gloss.trim();
                //TODO : we definitely need a translate() method in the java packages !
                gloss = gloss.replaceAll(";", "/"); //TODO : is it necessary ?
                gloss = gloss.replaceAll("", "A");
                gloss = gloss.replaceAll("", "A");
                gloss = gloss.replaceAll("", "A");
                gloss = gloss.replaceAll("", "A");
                gloss = gloss.replaceAll("", "A");
                gloss = gloss.replaceAll("", "A");
                gloss = gloss.replaceAll("", "C");
                gloss = gloss.replaceAll("", "E");
                gloss = gloss.replaceAll("", "E");
                gloss = gloss.replaceAll("", "E");
                gloss = gloss.replaceAll("", "E");
                gloss = gloss.replaceAll("", "I");
                gloss = gloss.replaceAll("", "I");
                gloss = gloss.replaceAll("", "I");
                gloss = gloss.replaceAll("", "I");
                gloss = gloss.replaceAll("", "N");
                gloss = gloss.replaceAll("", "O");
                gloss = gloss.replaceAll("", "O");
                gloss = gloss.replaceAll("", "O");
                gloss = gloss.replaceAll("", "O");
                gloss = gloss.replaceAll("", "O");
                gloss = gloss.replaceAll("", "U");
                gloss = gloss.replaceAll("", "U");
                gloss = gloss.replaceAll("", "U");
                gloss = gloss.replaceAll("", "U");
                gloss = gloss.replaceAll("", "a");
                gloss = gloss.replaceAll("", "a");
                gloss = gloss.replaceAll("", "a");
                gloss = gloss.replaceAll("", "a");
                gloss = gloss.replaceAll("", "a");
                gloss = gloss.replaceAll("", "a");
                gloss = gloss.replaceAll("", "c");
                gloss = gloss.replaceAll("", "e");
                gloss = gloss.replaceAll("", "e");
                gloss = gloss.replaceAll("", "e");
                gloss = gloss.replaceAll("", "e");
                gloss = gloss.replaceAll("", "i");
                gloss = gloss.replaceAll("", "i");
                gloss = gloss.replaceAll("", "i");
                gloss = gloss.replaceAll("", "i");
                gloss = gloss.replaceAll("", "n");
                gloss = gloss.replaceAll("", "o");
                gloss = gloss.replaceAll("", "o");
                gloss = gloss.replaceAll("", "o");
                gloss = gloss.replaceAll("", "o");
                gloss = gloss.replaceAll("", "o");
                gloss = gloss.replaceAll("", "u");
                gloss = gloss.replaceAll("", "u");
                gloss = gloss.replaceAll("", "u");
                gloss = gloss.replaceAll("", "u");
                gloss = gloss.replaceAll("", "AE");
                gloss = gloss.replaceAll("", "Sh");
                gloss = gloss.replaceAll("", "Zh");
                gloss = gloss.replaceAll("", "ss");
                gloss = gloss.replaceAll("", "ae");
                gloss = gloss.replaceAll("", "sh");
                gloss = gloss.replaceAll("", "zh");
                // note that although we read 4 fields from the dict we now save 5 fields in the hash table
                // because the info in last field, glossPOS, was split into two: gloss and POS
                DictionaryEntry de = new DictionaryEntry(entry, lemmaID, vocalization, morphology, gloss, POS);
                if (set.containsKey(entry)) {
                    ((Collection) set.get(entry)).add(de);
                } else
                    set.put(entry, de);
                forms++;
            }
        }
        IN.close();
        System.out.println();
        if (!"".equals(lemmaID))
            System.out.print(lemmas.size() + " lemmas and ");
        System.out.println(set.size() + " entries totalizing " + forms + " forms");
    } catch (IOException e) {
        throw new RuntimeException("Can not open : " + name);
    }
}

From source file:com.netflix.lipstick.Main.java

static int run(String args[], PigProgressNotificationListener listener) {
    int rc = 1;//from w w w  . j av  a  2 s .  c  o m
    boolean verbose = false;
    boolean gruntCalled = false;
    String logFileName = null;

    try {
        Configuration conf = new Configuration(false);
        GenericOptionsParser parser = new GenericOptionsParser(conf, args);
        conf = parser.getConfiguration();

        Properties properties = new Properties();
        PropertiesUtil.loadDefaultProperties(properties);
        properties.putAll(ConfigurationUtil.toProperties(conf));

        String[] pigArgs = parser.getRemainingArgs();

        boolean userSpecifiedLog = false;
        boolean checkScriptOnly = false;

        BufferedReader pin = null;
        boolean debug = false;
        boolean dryrun = false;
        boolean embedded = false;
        List<String> params = new ArrayList<String>();
        List<String> paramFiles = new ArrayList<String>();
        HashSet<String> optimizerRules = new HashSet<String>();

        CmdLineParser opts = new CmdLineParser(pigArgs);
        opts.registerOpt('4', "log4jconf", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('b', "brief", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('c', "check", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('d', "debug", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('e', "execute", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('f', "file", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('g', "embedded", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('h', "help", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('i', "version", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('l', "logfile", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('m', "param_file", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('p', "param", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('r', "dryrun", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('t', "optimizer_off", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('v', "verbose", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('w', "warning", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('x', "exectype", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('F', "stop_on_failure", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('M', "no_multiquery", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('P', "propertyFile", CmdLineParser.ValueExpected.REQUIRED);

        ExecMode mode = ExecMode.UNKNOWN;
        String file = null;
        String engine = null;
        ExecType execType = ExecType.MAPREDUCE;
        String execTypeString = properties.getProperty("exectype");
        if (execTypeString != null && execTypeString.length() > 0) {
            execType = ExecType.fromString(execTypeString);
        }

        // set up client side system properties in UDF context
        UDFContext.getUDFContext().setClientSystemProps(properties);

        char opt;
        while ((opt = opts.getNextOpt()) != CmdLineParser.EndOfOpts) {
            switch (opt) {
            case '4':
                String log4jconf = opts.getValStr();
                if (log4jconf != null) {
                    properties.setProperty(LOG4J_CONF, log4jconf);
                }
                break;

            case 'b':
                properties.setProperty(BRIEF, "true");
                break;

            case 'c':
                checkScriptOnly = true;
                break;

            case 'd':
                String logLevel = opts.getValStr();
                if (logLevel != null) {
                    properties.setProperty(DEBUG, logLevel);
                }
                debug = true;
                break;

            case 'e':
                mode = ExecMode.STRING;
                break;

            case 'f':
                mode = ExecMode.FILE;
                file = opts.getValStr();
                break;

            case 'g':
                embedded = true;
                engine = opts.getValStr();
                break;

            case 'F':
                properties.setProperty("stop.on.failure", "" + true);
                break;

            case 'h':
                String topic = opts.getValStr();
                if (topic != null)
                    if (topic.equalsIgnoreCase("properties"))
                        printProperties();
                    else {
                        System.out.println("Invalide help topic - " + topic);
                        usage();
                    }
                else
                    usage();
                return ReturnCode.SUCCESS;

            case 'i':
                System.out.println(getVersionString());
                return ReturnCode.SUCCESS;

            case 'l':
                // call to method that validates the path to the log file
                // and sets up the file to store the client side log file
                String logFileParameter = opts.getValStr();
                if (logFileParameter != null && logFileParameter.length() > 0) {
                    logFileName = validateLogFile(logFileParameter, null);
                } else {
                    logFileName = validateLogFile(logFileName, null);
                }
                userSpecifiedLog = true;
                properties.setProperty("pig.logfile", (logFileName == null ? "" : logFileName));
                break;

            case 'm':
                paramFiles.add(opts.getValStr());
                break;

            case 'M':
                // turns off multiquery optimization
                properties.setProperty("opt.multiquery", "" + false);
                break;

            case 'p':
                params.add(opts.getValStr());
                break;

            case 'r':
                // currently only used for parameter substitution
                // will be extended in the future
                dryrun = true;
                break;

            case 't':
                optimizerRules.add(opts.getValStr());
                break;

            case 'v':
                properties.setProperty(VERBOSE, "" + true);
                verbose = true;
                break;

            case 'w':
                properties.setProperty("aggregate.warning", "" + false);
                break;

            case 'x':
                try {
                    execType = ExecType.fromString(opts.getValStr());
                } catch (IOException e) {
                    throw new RuntimeException("ERROR: Unrecognized exectype.", e);
                }
                break;

            case 'P': {
                InputStream inputStream = null;
                try {
                    FileLocalizer.FetchFileRet localFileRet = FileLocalizer.fetchFile(properties,
                            opts.getValStr());
                    inputStream = new BufferedInputStream(new FileInputStream(localFileRet.file));
                    properties.load(inputStream);
                } catch (IOException e) {
                    throw new RuntimeException("Unable to parse properties file '" + opts.getValStr() + "'");
                } finally {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                        }
                    }
                }
            }
                break;

            default: {
                Character cc = Character.valueOf(opt);
                throw new AssertionError("Unhandled option " + cc.toString());
            }
            }
        }
        // create the context with the parameter
        PigContext pigContext = new PigContext(execType, properties);

        // create the static script state object
        String commandLine = LoadFunc.join((AbstractList<String>) Arrays.asList(args), " ");
        ScriptState scriptState = ScriptState.start(commandLine, pigContext);

        pigContext.getProperties().setProperty("pig.cmd.args", commandLine);

        if (logFileName == null && !userSpecifiedLog) {
            logFileName = validateLogFile(properties.getProperty("pig.logfile"), null);
        }

        pigContext.getProperties().setProperty("pig.logfile", (logFileName == null ? "" : logFileName));

        // configure logging
        configureLog4J(properties, pigContext);
        log.info(getVersionString().replace("\n", ""));

        if (logFileName != null) {
            log.info("Logging error messages to: " + logFileName);
        }

        if (!Boolean.valueOf(properties.getProperty(PROP_FILT_SIMPL_OPT, "false"))) {
            // turn off if the user has not explicitly turned on this
            // optimization
            optimizerRules.add("FilterLogicExpressionSimplifier");
        }

        if (optimizerRules.size() > 0) {
            pigContext.getProperties().setProperty("pig.optimizer.rules",
                    ObjectSerializer.serialize(optimizerRules));
        }

        if (properties.get("udf.import.list") != null)
            PigContext.initializeImportList((String) properties.get("udf.import.list"));

        PigContext.setClassLoader(pigContext.createCl(null));
        if (listener != null) {
            scriptState.registerListener(listener);
        }
        if (properties.getProperty("pig.listeners") != null) {
            for (String clsName : properties.getProperty("pig.listeners").split(":")) {
                // TODO: Is there a better/cleaner way to create these
                // listeners?
                scriptState.registerListener((PigProgressNotificationListener) PigContext.getClassLoader()
                        .loadClass(clsName).newInstance());
            }
        } else {
            scriptState.registerListener(new com.netflix.lipstick.listeners.LipstickPPNL());
        }

        // construct the parameter substitution preprocessor
        LipstickGrunt grunt = null;
        BufferedReader in;
        String substFile = null;

        paramFiles = fetchRemoteParamFiles(paramFiles, properties);
        pigContext.setParams(params);
        pigContext.setParamFiles(paramFiles);

        switch (mode) {

        case FILE: {
            String remainders[] = opts.getRemainingArgs();
            pigContext.getProperties().setProperty(PigContext.PIG_CMD_ARGS_REMAINDERS,
                    ObjectSerializer.serialize(remainders));
            FileLocalizer.FetchFileRet localFileRet = FileLocalizer.fetchFile(properties, file);
            if (localFileRet.didFetch) {
                properties.setProperty("pig.jars.relative.to.dfs", "true");
            }

            scriptState.setFileName(file);

            if (embedded) {
                return runEmbeddedScript(pigContext, localFileRet.file.getPath(), engine);
            } else {
                SupportedScriptLang type = determineScriptType(localFileRet.file.getPath());
                if (type != null) {
                    return runEmbeddedScript(pigContext, localFileRet.file.getPath(),
                            type.name().toLowerCase());
                }
            }

            in = new BufferedReader(new FileReader(localFileRet.file));

            // run parameter substitution preprocessor first
            substFile = file + ".substituted";

            pin = runParamPreprocessor(pigContext, in, substFile, debug || dryrun || checkScriptOnly);
            if (dryrun) {
                if (dryrun(substFile, pigContext)) {
                    log.info("Dry run completed. Substituted pig script is at " + substFile
                            + ". Expanded pig script is at " + file + ".expanded");
                } else {
                    log.info("Dry run completed. Substituted pig script is at " + substFile);
                }
                return ReturnCode.SUCCESS;
            }

            logFileName = validateLogFile(logFileName, file);
            pigContext.getProperties().setProperty("pig.logfile", logFileName);

            // Set job name based on name of the script
            pigContext.getProperties().setProperty(PigContext.JOB_NAME, "PigLatin:" + new File(file).getName());

            if (!debug) {
                new File(substFile).deleteOnExit();
            }

            scriptState.setScript(new File(file));

            grunt = new LipstickGrunt(pin, pigContext);
            gruntCalled = true;

            if (checkScriptOnly) {
                grunt.checkScript(substFile);
                System.err.println(file + " syntax OK");
                rc = ReturnCode.SUCCESS;
            } else {
                int results[] = grunt.exec();
                rc = getReturnCodeForStats(results);
            }

            return rc;
        }

        case STRING: {
            if (checkScriptOnly) {
                System.err.println("ERROR:" + "-c (-check) option is only valid "
                        + "when executing pig with a pig script file)");
                return ReturnCode.ILLEGAL_ARGS;
            }
            // Gather up all the remaining arguments into a string and pass
            // them into
            // grunt.
            StringBuffer sb = new StringBuffer();
            String remainders[] = opts.getRemainingArgs();
            for (int i = 0; i < remainders.length; i++) {
                if (i != 0)
                    sb.append(' ');
                sb.append(remainders[i]);
            }

            scriptState.setScript(sb.toString());

            in = new BufferedReader(new StringReader(sb.toString()));

            grunt = new LipstickGrunt(in, pigContext);
            gruntCalled = true;
            int results[] = grunt.exec();
            return getReturnCodeForStats(results);
        }

        default:
            break;
        }

        // If we're here, we don't know yet what they want. They may have
        // just
        // given us a jar to execute, they might have given us a pig script
        // to
        // execute, or they might have given us a dash (or nothing) which
        // means to
        // run grunt interactive.
        String remainders[] = opts.getRemainingArgs();
        if (remainders == null) {
            if (checkScriptOnly) {
                System.err.println("ERROR:" + "-c (-check) option is only valid "
                        + "when executing pig with a pig script file)");
                return ReturnCode.ILLEGAL_ARGS;
            }
            // Interactive
            mode = ExecMode.SHELL;
            ConsoleReader reader = new ConsoleReader(System.in, new OutputStreamWriter(System.out));
            reader.setDefaultPrompt("grunt> ");
            final String HISTORYFILE = ".pig_history";
            String historyFile = System.getProperty("user.home") + File.separator + HISTORYFILE;
            reader.setHistory(new History(new File(historyFile)));
            ConsoleReaderInputStream inputStream = new ConsoleReaderInputStream(reader);
            grunt = new LipstickGrunt(new BufferedReader(new InputStreamReader(inputStream)), pigContext);
            grunt.setConsoleReader(reader);
            gruntCalled = true;
            grunt.run();
            return ReturnCode.SUCCESS;
        } else {
            pigContext.getProperties().setProperty(PigContext.PIG_CMD_ARGS_REMAINDERS,
                    ObjectSerializer.serialize(remainders));

            // They have a pig script they want us to run.
            mode = ExecMode.FILE;

            FileLocalizer.FetchFileRet localFileRet = FileLocalizer.fetchFile(properties, remainders[0]);
            if (localFileRet.didFetch) {
                properties.setProperty("pig.jars.relative.to.dfs", "true");
            }

            scriptState.setFileName(remainders[0]);

            if (embedded) {
                return runEmbeddedScript(pigContext, localFileRet.file.getPath(), engine);
            } else {
                SupportedScriptLang type = determineScriptType(localFileRet.file.getPath());
                if (type != null) {
                    return runEmbeddedScript(pigContext, localFileRet.file.getPath(),
                            type.name().toLowerCase());
                }
            }

            in = new BufferedReader(new FileReader(localFileRet.file));

            // run parameter substitution preprocessor first
            substFile = remainders[0] + ".substituted";
            pin = runParamPreprocessor(pigContext, in, substFile, debug || dryrun || checkScriptOnly);
            if (dryrun) {
                if (dryrun(substFile, pigContext)) {
                    log.info("Dry run completed. Substituted pig script is at " + substFile
                            + ". Expanded pig script is at " + remainders[0] + ".expanded");
                } else {
                    log.info("Dry run completed. Substituted pig script is at " + substFile);
                }
                return ReturnCode.SUCCESS;
            }

            logFileName = validateLogFile(logFileName, remainders[0]);
            pigContext.getProperties().setProperty("pig.logfile", logFileName);

            if (!debug) {
                new File(substFile).deleteOnExit();
            }

            // Set job name based on name of the script
            pigContext.getProperties().setProperty(PigContext.JOB_NAME,
                    "PigLatin:" + new File(remainders[0]).getName());

            scriptState.setScript(localFileRet.file);

            grunt = new LipstickGrunt(pin, pigContext);
            gruntCalled = true;

            if (checkScriptOnly) {
                grunt.checkScript(substFile);
                System.err.println(remainders[0] + " syntax OK");
                rc = ReturnCode.SUCCESS;
            } else {
                int results[] = grunt.exec();
                rc = getReturnCodeForStats(results);
            }
            return rc;
        }

        // Per Utkarsh and Chris invocation of jar file via pig depricated.
    } catch (ParseException e) {
        usage();
        rc = ReturnCode.PARSE_EXCEPTION;
        PigStatsUtil.setErrorMessage(e.getMessage());
    } catch (org.apache.pig.tools.parameters.ParseException e) {
        // usage();
        rc = ReturnCode.PARSE_EXCEPTION;
        PigStatsUtil.setErrorMessage(e.getMessage());
    } catch (IOException e) {
        if (e instanceof PigException) {
            PigException pe = (PigException) e;
            rc = (pe.retriable()) ? ReturnCode.RETRIABLE_EXCEPTION : ReturnCode.PIG_EXCEPTION;
            PigStatsUtil.setErrorMessage(pe.getMessage());
            PigStatsUtil.setErrorCode(pe.getErrorCode());
        } else {
            rc = ReturnCode.IO_EXCEPTION;
            PigStatsUtil.setErrorMessage(e.getMessage());
        }

        if (!gruntCalled) {
            LogUtils.writeLog(e, logFileName, log, verbose, "Error before Pig is launched");
        }
    } catch (Throwable e) {
        rc = ReturnCode.THROWABLE_EXCEPTION;
        PigStatsUtil.setErrorMessage(e.getMessage());
        if (!gruntCalled) {
            LogUtils.writeLog(e, logFileName, log, verbose, "Error before Pig is launched");
        }
    } finally {
        // clear temp files
        FileLocalizer.deleteTempFiles();
        // PerformanceTimerFactory.getPerfTimerFactory().dumpTimers();
    }

    return rc;
}