Search in sources :

Example 1 with IDField

use of edu.uci.ics.textdb.api.field.IDField in project textdb by TextDB.

the class PlanStore method addPlan.

/**
     * Adds a Logical Plan JSON to the plan store.
     *
     * @param planName, the name of the plan.
     * @param description, the description of the plan.
     * @param logicalPlanJson, the logical plan JSON string
     * @Return IDField, the id field of the plan stored.
     * @throws TextDBException, when there are null fields or the given name is invalid or there is an existing plan with same name.
     */
public IDField addPlan(String planName, String description, String logicalPlanJson) throws TextDBException {
    if (planName == null || description == null || logicalPlanJson == null) {
        throw new TextDBException("Arguments cannot be null when adding a plan");
    }
    if (!PlanStoreConstants.VALID_PLAN_NAME.matcher(planName).find()) {
        throw new TextDBException("Plan name is not valid. It can only contain alphanumeric characters, " + "underscore, and hyphen.");
    }
    if (getPlan(planName) != null) {
        throw new TextDBException("A plan with the same name already exists");
    }
    try {
        // Converting the JSON String to a JSON Node to minimize space usage and to check validity of JSON string
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readValue(logicalPlanJson, JsonNode.class);
        logicalPlanJson = objectMapper.writeValueAsString(jsonNode);
    } catch (IOException e) {
        throw new StorageException("logical plan json is an invalid json string: " + logicalPlanJson);
    }
    Tuple tuple = new Tuple(PlanStoreConstants.SCHEMA_PLAN, new StringField(planName), new StringField(description), new StringField(logicalPlanJson));
    DataWriter dataWriter = relationManager.getTableDataWriter(PlanStoreConstants.TABLE_NAME);
    dataWriter.open();
    IDField id = dataWriter.insertTuple(tuple);
    dataWriter.close();
    return id;
}
Also used : IDField(edu.uci.ics.textdb.api.field.IDField) StringField(edu.uci.ics.textdb.api.field.StringField) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) TextDBException(edu.uci.ics.textdb.api.exception.TextDBException) StorageException(edu.uci.ics.textdb.api.exception.StorageException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Tuple(edu.uci.ics.textdb.api.tuple.Tuple) DataWriter(edu.uci.ics.textdb.storage.DataWriter)

Example 2 with IDField

use of edu.uci.ics.textdb.api.field.IDField in project textdb by TextDB.

the class PlanStore method updatePlanInternal.

/**
     * Updates both plan description and plan json of a plan with the given plan name.
     * If description is null, it will not update plan description.
     * If plan json is NULL, it will not update the plan's JSON file.
     *
     * @param planName, the name of the plan.
     * @param description, the new description of the plan.
     * @param logicalPlanJson, the new plan json string.
     * @throws TextDBException
     */
private void updatePlanInternal(String planName, String description, String logicalPlanJson) throws TextDBException {
    Tuple existingPlan = getPlan(planName);
    if (existingPlan == null) {
        return;
    }
    // Checking if an updated description or logical plan JSON string has been provided
    if (description == null && logicalPlanJson == null) {
        return;
    }
    // Checking if the logical plan JSON string needs to be updated
    if (logicalPlanJson != null) {
        // Compressing and checking the validity of the logical plan JSON string
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readValue(logicalPlanJson, JsonNode.class);
            logicalPlanJson = objectMapper.writeValueAsString(jsonNode);
        } catch (IOException e) {
            throw new StorageException("logical plan json is an invalid json string: " + logicalPlanJson);
        }
    }
    // Getting the fields in order for performing the update
    IDField idField = (IDField) existingPlan.getField(SchemaConstants._ID);
    IField descriptionField = description != null ? new StringField(description) : existingPlan.getField(PlanStoreConstants.DESCRIPTION);
    IField logicalPlanJsonField = logicalPlanJson != null ? new StringField(logicalPlanJson) : existingPlan.getField(PlanStoreConstants.LOGICAL_PLAN_JSON);
    // Creating a tuple out of all the fields
    Tuple newTuple = new Tuple(PlanStoreConstants.SCHEMA_PLAN, new StringField(planName), descriptionField, logicalPlanJsonField);
    // Writing the updated tuple
    DataWriter dataWriter = relationManager.getTableDataWriter(PlanStoreConstants.TABLE_NAME);
    dataWriter.open();
    dataWriter.updateTuple(newTuple, idField);
    dataWriter.close();
}
Also used : IDField(edu.uci.ics.textdb.api.field.IDField) StringField(edu.uci.ics.textdb.api.field.StringField) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) IField(edu.uci.ics.textdb.api.field.IField) StorageException(edu.uci.ics.textdb.api.exception.StorageException) Tuple(edu.uci.ics.textdb.api.tuple.Tuple) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataWriter(edu.uci.ics.textdb.storage.DataWriter)

Example 3 with IDField

use of edu.uci.ics.textdb.api.field.IDField in project textdb by TextDB.

the class PlanStore method deletePlan.

/**
     * Removess a plan by given name from plan store.
     *
     * @param planName, the name of the plan.
     * @throws TextDBException
     */
public void deletePlan(String planName) throws TextDBException {
    Tuple plan = getPlan(planName);
    if (plan == null) {
        return;
    }
    IDField idField = (IDField) plan.getField(SchemaConstants._ID);
    DataWriter dataWriter = relationManager.getTableDataWriter(PlanStoreConstants.TABLE_NAME);
    dataWriter.open();
    dataWriter.deleteTupleByID(idField);
    dataWriter.close();
}
Also used : IDField(edu.uci.ics.textdb.api.field.IDField) Tuple(edu.uci.ics.textdb.api.tuple.Tuple) DataWriter(edu.uci.ics.textdb.storage.DataWriter)

Example 4 with IDField

use of edu.uci.ics.textdb.api.field.IDField in project textdb by TextDB.

the class NlpSplitTest method test2.

@Test
public void test2() throws TextDBException, ParseException {
    TupleSourceOperator tupleSource = new TupleSourceOperator(NlpSplitTestConstants.getOneToManyTestTuple(), NlpSplitTestConstants.SPLIT_SCHEMA);
    NlpSplitOperator sentence_list = new NlpSplitOperator(new NlpSplitPredicate(NLPOutputType.ONE_TO_MANY, NlpSplitTestConstants.TEXT, PropertyNameConstants.NLP_OUTPUT_TYPE));
    TupleSink tupleSink = new TupleSink();
    sentence_list.setInputOperator(tupleSource);
    tupleSink.setInputOperator(sentence_list);
    tupleSink.open();
    List<Tuple> results = tupleSink.collectAllTuples();
    tupleSink.close();
    Assert.assertTrue(TestUtils.equals(NlpSplitTestConstants.getOneToManyResultTuple(), results));
    Set<IDField> compset = new HashSet<IDField>();
    for (Tuple result : results) {
        Assert.assertFalse(compset.contains(result.getField(SchemaConstants._ID)));
        compset.add(result.getField(SchemaConstants._ID));
    }
}
Also used : TupleSink(edu.uci.ics.textdb.exp.sink.tuple.TupleSink) IDField(edu.uci.ics.textdb.api.field.IDField) TupleSourceOperator(edu.uci.ics.textdb.exp.source.tuple.TupleSourceOperator) Tuple(edu.uci.ics.textdb.api.tuple.Tuple) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with IDField

use of edu.uci.ics.textdb.api.field.IDField in project textdb by TextDB.

the class SimilarityJoinTest method test1.

/*
     * Tests the Similarity Join Predicate on two similar words:
     *   Donald J. Trump
     *   Donald Trump
     * Under the condition of similarity (NormalizedLevenshtein) > 0.8, these two words should match.
     *
     */
@Test
public void test1() throws TextDBException {
    JoinTestHelper.insertToTable(NEWS_TABLE_OUTER, JoinTestConstants.getNewsTuples().get(0));
    JoinTestHelper.insertToTable(NEWS_TABLE_INNER, JoinTestConstants.getNewsTuples().get(1));
    String trumpRegex = "[Dd]onald.{1,5}[Tt]rump";
    RegexMatcher regexMatcherInner = JoinTestHelper.getRegexMatcher(JoinTestHelper.NEWS_TABLE_INNER, trumpRegex, JoinTestConstants.NEWS_BODY);
    RegexMatcher regexMatcherOuter = JoinTestHelper.getRegexMatcher(JoinTestHelper.NEWS_TABLE_OUTER, trumpRegex, JoinTestConstants.NEWS_BODY);
    SimilarityJoinPredicate similarityJoinPredicate = new SimilarityJoinPredicate(JoinTestConstants.NEWS_BODY, 0.8);
    List<Tuple> results = JoinTestHelper.getJoinDistanceResults(regexMatcherInner, regexMatcherOuter, similarityJoinPredicate, Integer.MAX_VALUE, 0);
    Schema joinInputSchema = Utils.addAttributeToSchema(JoinTestConstants.NEWS_SCHEMA, SchemaConstants.SPAN_LIST_ATTRIBUTE);
    Schema resultSchema = similarityJoinPredicate.generateOutputSchema(joinInputSchema, joinInputSchema);
    List<Span> resultSpanList = Arrays.asList(new Span("inner_" + JoinTestConstants.NEWS_BODY, 5, 20, trumpRegex, "Donald J. Trump", -1), new Span("outer_" + JoinTestConstants.NEWS_BODY, 18, 30, trumpRegex, "Donald Trump", -1));
    Tuple resultTuple = new Tuple(resultSchema, new IDField(UUID.randomUUID().toString()), new IntegerField(2), new TextField("Alternative Facts and the Costs of Trump-Branded Reality"), new TextField("When Donald J. Trump swore the presidential oath on Friday, he assumed " + "responsibility not only for the levers of government but also for one of " + "the United States’ most valuable assets, battered though it may be: its credibility. " + "The country’s sentimental reverence for truth and its jealously guarded press freedoms, " + "while never perfect, have been as important to its global standing as the strength of " + "its military and the reliability of its currency. It’s the bedrock of that " + "American exceptionalism we’ve heard so much about for so long."), new IntegerField(1), new TextField("UCI marchers protest as Trump begins his presidency"), new TextField("a few hours after Donald Trump was sworn in Friday as the nation’s 45th president, " + "a line of more than 100 UC Irvine faculty members and students took to the campus " + "in pouring rain to demonstrate their opposition to his policies on immigration and " + "other issues and urge other opponents to keep organizing during Trump’s presidency."), new ListField<>(resultSpanList));
    Assert.assertTrue(TestUtils.equals(Arrays.asList(resultTuple), results));
}
Also used : IDField(edu.uci.ics.textdb.api.field.IDField) SimilarityJoinPredicate(edu.uci.ics.textdb.exp.join.SimilarityJoinPredicate) Schema(edu.uci.ics.textdb.api.schema.Schema) TextField(edu.uci.ics.textdb.api.field.TextField) RegexMatcher(edu.uci.ics.textdb.exp.regexmatcher.RegexMatcher) IntegerField(edu.uci.ics.textdb.api.field.IntegerField) Span(edu.uci.ics.textdb.api.span.Span) Tuple(edu.uci.ics.textdb.api.tuple.Tuple) Test(org.junit.Test)

Aggregations

IDField (edu.uci.ics.textdb.api.field.IDField)11 Tuple (edu.uci.ics.textdb.api.tuple.Tuple)10 Test (org.junit.Test)6 StringField (edu.uci.ics.textdb.api.field.StringField)5 Schema (edu.uci.ics.textdb.api.schema.Schema)5 StorageException (edu.uci.ics.textdb.api.exception.StorageException)3 Attribute (edu.uci.ics.textdb.api.schema.Attribute)3 Span (edu.uci.ics.textdb.api.span.Span)3 DataWriter (edu.uci.ics.textdb.storage.DataWriter)3 IOException (java.io.IOException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IField (edu.uci.ics.textdb.api.field.IField)2 IntegerField (edu.uci.ics.textdb.api.field.IntegerField)2 TextField (edu.uci.ics.textdb.api.field.TextField)2 SimilarityJoinPredicate (edu.uci.ics.textdb.exp.join.SimilarityJoinPredicate)2 RegexMatcher (edu.uci.ics.textdb.exp.regexmatcher.RegexMatcher)2 TextDBException (edu.uci.ics.textdb.api.exception.TextDBException)1 ListField (edu.uci.ics.textdb.api.field.ListField)1 TupleSink (edu.uci.ics.textdb.exp.sink.tuple.TupleSink)1