Search in sources :

Example 1 with DataReader

use of edu.uci.ics.textdb.storage.DataReader in project textdb by TextDB.

the class PlanStore method getPlan.

/**
     * Retrieves a plan by given name from plan store.
     *
     * @param planName, the name of the plan.
     * @Return ITuple, the tuple consisting of fields of the plan.
     * @throws TextDBException
     */
public Tuple getPlan(String planName) throws TextDBException {
    Query q = new TermQuery(new Term(PlanStoreConstants.NAME, planName));
    DataReader reader = relationManager.getTableDataReader(PlanStoreConstants.TABLE_NAME, q);
    reader.open();
    Tuple inputTuple = null;
    while ((inputTuple = reader.getNextTuple()) != null) {
        IField nameField = inputTuple.getField(PlanStoreConstants.NAME);
        if (nameField.getValue().toString().equals(planName)) {
            reader.close();
            return inputTuple;
        }
    }
    reader.close();
    return null;
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) DataReader(edu.uci.ics.textdb.storage.DataReader) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) IField(edu.uci.ics.textdb.api.field.IField) Tuple(edu.uci.ics.textdb.api.tuple.Tuple)

Example 2 with DataReader

use of edu.uci.ics.textdb.storage.DataReader in project textdb by TextDB.

the class PlanStoreResource method getAllQueryPlans.

@GET
public QueryPlanListBean getAllQueryPlans() {
    ArrayList<QueryPlanBean> queryPlans = new ArrayList<>();
    try {
        // Getting an iterator for the plan store
        DataReader reader = planStore.getPlanIterator();
        reader.open();
        // Iterating through the stored plans, and mapping them to a QueryPlanRequest object
        Tuple tuple;
        while ((tuple = reader.getNextTuple()) != null) {
            String name = tuple.getField(PlanStoreConstants.NAME).getValue().toString();
            String description = tuple.getField(PlanStoreConstants.DESCRIPTION).getValue().toString();
            String logicalPlanJson = tuple.getField(PlanStoreConstants.LOGICAL_PLAN_JSON).getValue().toString();
            queryPlans.add(new QueryPlanBean(name, description, mapper.readValue(logicalPlanJson, LogicalPlan.class)));
        }
    } catch (TextDBException e) {
        e.printStackTrace();
        throw new TextdbWebException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new TextdbWebException("fail to parse json:\n" + e.getMessage());
    }
    return new QueryPlanListBean(queryPlans);
}
Also used : QueryPlanListBean(edu.uci.ics.textdb.web.response.planstore.QueryPlanListBean) DataReader(edu.uci.ics.textdb.storage.DataReader) TextdbWebException(edu.uci.ics.textdb.web.TextdbWebException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TextDBException(edu.uci.ics.textdb.api.exception.TextDBException) QueryPlanBean(edu.uci.ics.textdb.web.response.planstore.QueryPlanBean) Tuple(edu.uci.ics.textdb.api.tuple.Tuple)

Example 3 with DataReader

use of edu.uci.ics.textdb.storage.DataReader in project textdb by TextDB.

the class WordCountIndexSource method computeWordCount.

private void computeWordCount() throws TextDBException {
    try {
        HashMap<String, Integer> wordCountMap = new HashMap<>();
        DataReader dataReader = RelationManager.getRelationManager().getTableDataReader(predicate.getTableName(), new MatchAllDocsQuery());
        dataReader.open();
        IndexReader luceneIndexReader = dataReader.getLuceneIndexReader();
        for (int i = 0; i < luceneIndexReader.numDocs(); i++) {
            Terms termVector = luceneIndexReader.getTermVector(i, predicate.getAttribute());
            TermsEnum termsEnum = termVector.iterator();
            while (termsEnum.next() != null) {
                String key = termsEnum.term().utf8ToString();
                wordCountMap.put(key, wordCountMap.get(key) == null ? ((int) termsEnum.totalTermFreq()) : wordCountMap.get(key) + ((int) termsEnum.totalTermFreq()));
            }
        }
        luceneIndexReader.close();
        dataReader.close();
        sortedWordCountMap = wordCountMap.entrySet().stream().sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue())).collect(Collectors.toList());
        wordCountIterator = sortedWordCountMap.iterator();
    } catch (IOException e) {
        throw new DataFlowException(e);
    }
}
Also used : DataReader(edu.uci.ics.textdb.storage.DataReader) HashMap(java.util.HashMap) IndexReader(org.apache.lucene.index.IndexReader) Terms(org.apache.lucene.index.Terms) DataFlowException(edu.uci.ics.textdb.api.exception.DataFlowException) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermsEnum(org.apache.lucene.index.TermsEnum)

Example 4 with DataReader

use of edu.uci.ics.textdb.storage.DataReader in project textdb by TextDB.

the class PlanStoreTest method testPlanIterator.

@Test
public void testPlanIterator() throws TextDBException {
    List<String> validPlans = new ArrayList<>();
    validPlans.add(logicalPlanJson1);
    validPlans.add(logicalPlanJson2);
    List<String> expectedPlans = new ArrayList<>();
    String planNamePrefix = "plan_";
    for (int i = 0; i < 100; i++) {
        String plan = validPlans.get(i % 2);
        expectedPlans.add(plan);
        planStore.addPlan(planNamePrefix + i, "basic plan " + i, plan);
    }
    DataReader reader = planStore.getPlanIterator();
    reader.open();
    Tuple tuple;
    String[] returnedPlans = new String[expectedPlans.size()];
    while ((tuple = reader.getNextTuple()) != null) {
        String planName = tuple.getField(PlanStoreConstants.NAME).getValue().toString();
        int planIdx = Integer.parseInt(planName.split("_")[1]);
        String logicalPlanJson = tuple.getField(PlanStoreConstants.LOGICAL_PLAN_JSON).getValue().toString();
        returnedPlans[planIdx] = logicalPlanJson;
    }
    reader.close();
    for (int i = 0; i < expectedPlans.size(); i++) {
        assertPlanEquivalence(expectedPlans.get(i), returnedPlans[i]);
    }
}
Also used : DataReader(edu.uci.ics.textdb.storage.DataReader) ArrayList(java.util.ArrayList) Tuple(edu.uci.ics.textdb.api.tuple.Tuple) Test(org.junit.Test)

Aggregations

DataReader (edu.uci.ics.textdb.storage.DataReader)4 Tuple (edu.uci.ics.textdb.api.tuple.Tuple)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 DataFlowException (edu.uci.ics.textdb.api.exception.DataFlowException)1 TextDBException (edu.uci.ics.textdb.api.exception.TextDBException)1 IField (edu.uci.ics.textdb.api.field.IField)1 TextdbWebException (edu.uci.ics.textdb.web.TextdbWebException)1 QueryPlanBean (edu.uci.ics.textdb.web.response.planstore.QueryPlanBean)1 QueryPlanListBean (edu.uci.ics.textdb.web.response.planstore.QueryPlanListBean)1 HashMap (java.util.HashMap)1 IndexReader (org.apache.lucene.index.IndexReader)1 Term (org.apache.lucene.index.Term)1 Terms (org.apache.lucene.index.Terms)1 TermsEnum (org.apache.lucene.index.TermsEnum)1 Query (org.apache.lucene.search.Query)1 TermQuery (org.apache.lucene.search.TermQuery)1 Test (org.junit.Test)1