Skip to content
This repository has been archived by the owner on Feb 8, 2019. It is now read-only.

[PIRK 71] Move Querier creation logic into QuerierFactory #105

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pirk.querier.wideskies;

import org.apache.pirk.utils.PIRException;

import java.util.Properties;

import static org.apache.pirk.querier.wideskies.QuerierProps.*;

/**
* Holds the various parameters related to creating a {@link Querier}.
*
* Created by ryan on 9/28/16.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop the author info.

*/
public class EncryptionPropertiesBuilder
{
private final Properties properties;

public static EncryptionPropertiesBuilder newBuilder() {
return new EncryptionPropertiesBuilder();
}

private EncryptionPropertiesBuilder() {
this.properties = new Properties();

setGeneralDefaults(properties);
setEncryptionDefaults(properties);
}

public EncryptionPropertiesBuilder numThreads(int numThreads) {
properties.setProperty(NUMTHREADS, String.valueOf(numThreads));
return this;
}

public EncryptionPropertiesBuilder bitSet(int bitSet) {
properties.setProperty(BITSET, String.valueOf(bitSet));
return this;
}

public EncryptionPropertiesBuilder queryType(String queryType) {
properties.setProperty(QUERYTYPE, queryType);
return this;
}

public EncryptionPropertiesBuilder hashBitSize(int hashBitSize) {
properties.setProperty(HASHBITSIZE, String.valueOf(hashBitSize));
return this;
}

public EncryptionPropertiesBuilder hashKey(String hashKey) {
properties.setProperty(HASHKEY, hashKey);
return this;
}

public EncryptionPropertiesBuilder dataPartitionBitSize(int dataPartitionBitSize) {
properties.setProperty(DATAPARTITIONSIZE, String.valueOf(dataPartitionBitSize));
return this;
}

public EncryptionPropertiesBuilder paillierBitSize(int paillierBitSize) {
properties.setProperty(PAILLIERBITSIZE, String.valueOf(paillierBitSize));
return this;
}

public EncryptionPropertiesBuilder certainty(int certainty) {
properties.setProperty(CERTAINTY, String.valueOf(certainty));
return this;
}

public EncryptionPropertiesBuilder embedSelector(boolean embedSelector) {
properties.setProperty(EMBEDSELECTOR, String.valueOf(embedSelector));
return this;
}

public EncryptionPropertiesBuilder useMemLookupTable(boolean useMemLookupTable) {
properties.setProperty(USEMEMLOOKUPTABLE, String.valueOf(useMemLookupTable));
return this;
}

public EncryptionPropertiesBuilder useHDFSLookupTable(boolean useHDFSLookupTable) {
properties.setProperty(USEHDFSLOOKUPTABLE, String.valueOf(useHDFSLookupTable));
return this;
}

public Properties build() throws PIRException
{
if(!validateQuerierEncryptionProperties(properties)) {
throw new PIRException("Encryption properties not valid. See log for details.");
}
return properties;
}

}
66 changes: 37 additions & 29 deletions src/main/java/org/apache/pirk/querier/wideskies/QuerierCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@
*/
package org.apache.pirk.querier.wideskies;

import java.io.File;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.*;
import org.apache.pirk.schema.data.DataSchemaLoader;
import org.apache.pirk.schema.query.QuerySchemaLoader;
import org.apache.pirk.utils.SystemConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
* Class for parsing the command line options for the QuerierDriver
*/
Expand All @@ -44,7 +41,6 @@ public class QuerierCLI

/**
* Create and parse allowable options
*
*/
public QuerierCLI(String[] args)
{
Expand Down Expand Up @@ -80,9 +76,8 @@ public QuerierCLI(String[] args)

/**
* Determine if an option was provided by the user via the CLI
*
* @param option
* - the option of interest
*
* @param option - the option of interest
* @return true if option was provided, false otherwise
*/
public boolean hasOption(String option)
Expand All @@ -92,9 +87,8 @@ public boolean hasOption(String option)

/**
* Obtain the argument of the option provided by the user via the CLI
*
* @param option
* - the option of interest
*
* @param option - the option of interest
* @return value of the argument of the option
*/
public String getOptionValue(String option)
Expand All @@ -104,7 +98,7 @@ public String getOptionValue(String option)

/**
* Method to parse and validate the options provided
*
*
* @return - true if valid, false otherwise
*/
private boolean parseOptions()
Expand All @@ -131,12 +125,28 @@ private boolean parseOptions()
// Validate properties
valid = QuerierProps.validateQuerierProperties();

// Load the new local query and data schemas
if (valid)
{
logger.info("loading schemas: dataSchemas = " + SystemConfiguration.getProperty("data.schemas") + " querySchemas = " + SystemConfiguration
.getProperty("query.schemas"));
try
{
DataSchemaLoader.initialize();
QuerySchemaLoader.initialize();

} catch (Exception e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be much better to handle the exception, or re-throw it, rather than consume and continue.
Why catch all Exception types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was moved out of the old QuerierProps.validateQuerierProperties() method. I decided it would be safe to move out since this was the only place that method was ever called, and this code doing initialization, not validation. But I'm not familiar enough yet with what the schema loaders are doing to understand how to handle those exceptions differently. Any suggestions?

{
e.printStackTrace();
}
}

return valid;
}

/**
* Create the options available for the DistributedTestDriver
*
*
* @return Apache's CLI Options object
*/
private Options createOptions()
Expand All @@ -163,22 +173,20 @@ private Options createOptions()
options.addOption(optionACTION);

// INPUTFILE
Option optionINPUTFILE = new Option("i", QuerierProps.INPUTFILE, true,
"required - Fully qualified file containing input "
+ "-- \n The input is either: \n (1) For Encryption: A query file - Contains the query selectors, one per line; "
+ "the first line must be the query number \n OR \n (2) For Decryption: A response file - Contains the serialized Response object");
Option optionINPUTFILE = new Option("i", QuerierProps.INPUTFILE, true, "required - Fully qualified file containing input "
+ "-- \n The input is either: \n (1) For Encryption: A query file - Contains the query selectors, one per line; "
+ "the first line must be the query number \n OR \n (2) For Decryption: A response file - Contains the serialized Response object");
optionINPUTFILE.setRequired(false);
optionINPUTFILE.setArgName(QuerierProps.INPUTFILE);
optionINPUTFILE.setType(String.class);
options.addOption(optionINPUTFILE);

// OUTPUTFILE
Option optionOUTPUTFILE = new Option("o", QuerierProps.OUTPUTFILE, true,
"required - Fully qualified file for the result output. "
+ "\n The output file specifies either: \n (1) For encryption: \n \t (a) A file to contain the serialized Querier object named: " + "<outputFile>-"
+ QuerierConst.QUERIER_FILETAG + " AND \n \t " + "(b) A file to contain the serialized Query object named: <outputFile>-"
+ QuerierConst.QUERY_FILETAG + "\n " + "OR \n (2) A file to contain the decryption results where each line is where each line "
+ "corresponds to one hit and is a JSON object with the schema QuerySchema");
Option optionOUTPUTFILE = new Option("o", QuerierProps.OUTPUTFILE, true, "required - Fully qualified file for the result output. "
+ "\n The output file specifies either: \n (1) For encryption: \n \t (a) A file to contain the serialized Querier object named: " + "<outputFile>-"
+ QuerierConst.QUERIER_FILETAG + " AND \n \t " + "(b) A file to contain the serialized Query object named: <outputFile>-" + QuerierConst.QUERY_FILETAG
+ "\n " + "OR \n (2) A file to contain the decryption results where each line is where each line "
+ "corresponds to one hit and is a JSON object with the schema QuerySchema");
optionOUTPUTFILE.setRequired(false);
optionOUTPUTFILE.setArgName(QuerierProps.OUTPUTFILE);
optionOUTPUTFILE.setType(String.class);
Expand Down Expand Up @@ -244,8 +252,8 @@ private Options createOptions()

// CERTAINTY
Option optionCERTAINTY = new Option("c", QuerierProps.CERTAINTY, true,
"required for encryption -- Certainty of prime generation for Paillier -- must be greater than or " + "equal to "
+ SystemConfiguration.getProperty("pir.primeCertainty") + "");
"required for encryption -- Certainty of prime generation for Paillier -- must be greater than or " + "equal to " + SystemConfiguration
.getProperty("pir.primeCertainty") + "");
optionCERTAINTY.setRequired(false);
optionCERTAINTY.setArgName(QuerierProps.CERTAINTY);
optionCERTAINTY.setType(String.class);
Expand Down