If I have a state with an attribute, e.g.:
class Test(val timestamp: Double, val linearId = UniqueIdentifier()): LinearState
I can execute a vault query based on the linearId with LinearStateQueryCriteria. Is there a way to define query criteria for the attributes of a State? For example, get every state where the timestamp attribute is equal to x or even in a range between y and z?
This would work if your State is a QueryableState. You could use a VaultCustomQueryCriteria to get the desired result as shown below:
QueryCriteria generalCriteria = new VaultQueryCriteria(Vault.StateStatus.ALL);
FieldInfo attributeCurrency = getField("currency", CashSchemaV1.PersistentCashState.class);
FieldInfo attributeQuantity = getField("pennies", CashSchemaV1.PersistentCashState.class);
CriteriaExpression currencyIndex = Builder.equal(attributeCurrency, "USD");
CriteriaExpression quantityIndex = Builder.greaterThanOrEqual(attributeQuantity, 10L);
QueryCriteria customCriteria2 = new VaultCustomQueryCriteria(quantityIndex);
QueryCriteria customCriteria1 = new VaultCustomQueryCriteria(currencyIndex);
QueryCriteria criteria = generalCriteria.and(customCriteria1).and(customCriteria2);
Vault.Page<ContractState> results = vaultService.queryBy(Cash.State.class, criteria);
Refer here for more details: https://docs.corda.net/api-vault-query.html
Refer here if you want to learn about QueryableState:
https://medium.com/corda/persisting-corda-states-in-custom-database-tables-using-queryablestate-dedaa18b7050
Related
I have a Service class that i am trying to unit test. The service class is as follows -:
class BtoService #Inject()(db: GrDbConnection,
businessService: BusinessService,
vertexIdGenerator: VertexIdGenerator) {
def updateBto(id: String, updateBTOReq: UpdateBTORequest) = {
implicit val g = db.g
val btoVertex = g
.V(
vertexIdGenerator.vertexId(VertexLabels.BTO, id, PropertyLabels.BTO_ID))
.headOption() match {
case Some(value) => value
case None => throw BtoDoesNotExistException(s"BTO $id Does not exists")
}
So while testing this class, i create a mock of injected services(BusinessService, GrDbConnection) -:
val db: GrDbConnection = mock[GrDbConnection]
val businessService: BusinessService = mock[BusinessService]
val vertexIdGenerator: VertexIdGenerator = mock[VertexIdGenerator]
val btoService: BtoService = new BtoService(db, businessService, vertexIdGenerator)
val g : ScalaGraph = EmptyGraph
.instance()
.asScala()
btoService.updateBto("101",updateBTORequest)
Mockito.when(db.g).thenReturn(g)
The GrDbConnection.scala has the defined db.g -:
val g: ScalaGraph = EmptyGraph
.instance()
.asScala
.configure(_.withRemote(connection))
here connection has the necessary details to connect to the actuall db.
Since i can return an empty scala graph using Mockito.when().thenReturn(), i preferred not to use .configure() option in my test class.
My real problem that i face is that, i am not able to add a vertex to the test graph, I need to add a btoModel as the vertex to the graph since, in service class :
val btoVertex = g.V(vertex......()).headOption() - returns a GremlinScala.Aux[scala.vertex, Hnil]
How do i proceed with that?
please contact me at - nilay0016#gmail.com for more info.
i have stored multiple values in session using dictionary like below
var get_values = new Dictionary();
get_values["name"] = model.Name; ;
get_values["address"] = model.Address;
get_values["phone"] = model.Phone;
get_values["email"] = model.Email;
Session["sess_values"] = get_values;
now i want to get values from session one by one. my question is how to get values from session like name, address etc.?
just like any normal dictionary
//you get your sesssion
var sessionDictionary =Session["sess_values"] as Dictionary<string, string>;
//then get the value, example for name
var TheName = sessionDictionary["name"];
Given this code:
var NewComm = CRM.CreateRecord("Communication");
NewComm("Comm_ChannelId") = Request.Form("chanId");
NewComm("Comm_Type") = "Appointment";
NewComm("Comm_DateTime") = Request.Form("initialHour");
NewComm("Comm_Status") = "Pending";
NewComm("Comm_Priority") = "Normal";
NewComm("Comm_Action") = "Meeting";
NewComm("Comm_SecTerr") = Request.Form("secTerr");
NewComm("Comm_Subject") = "No Subject";
NewComm("Comm_Note") = "No notes";
NewComm.SaveChanges();
There is a method of the CreateRecord Object to retrieve the ID of the recently created record?
Once you have created the new record, the Id becomes available in the object. Using your example above, you can simply get the Id with this code:
NewComm.SaveChanges();
var CommId = NewComm("Comm_CommunicationId");
This applies to any record type with the same method.
Six Ticks Support
I am trying to use a DynamoDB table to store this data:
DartsPlayerInsultTable
CustomerId String
PlayerId String
PlayerInsult String
Using the method (concept, not code) described here:
https://java.awsblog.com/post/Tx3GYZEVGO924K4/The-DynamoDBMapper-Local-Secondary-Indexes-and-You
here:
http://mobile.awsblog.com/post/TxTCW7KW8BGZAF/Amazon-DynamoDB-on-Mobile-Part-4-Local-Secondary-Indexes
and here:
http://labs.journwe.com/2013/12/15/dynamodb-secondary-indexes/comment-page-1/#comment-116
I want to have multiple insult records per customer-player.
CustomerId is my Hash Key
PlayerId is my Range Key
and I a trying to use PlayerInsult in a key so that
a second PlayerInsult value inserts a second record
rather than replacing the existing one.
Have tried both Global and Secondary indexes for this,
but if I try to add a row with a new insult, it still
replaces the insult with the same customer-player key
rather than adding a new one.
Any suggestions on the best approach to use for this is
DynanoDB? Do I need to create a hybrid column for a range-key?
Trying to keep this simple...
class func createDartsPlayerInsultTable() -> BFTask {
let dynamoDB = AWSDynamoDB.defaultDynamoDB()
let hashKeyAttributeDefinition = AWSDynamoDBAttributeDefinition()
hashKeyAttributeDefinition.attributeName = "CustomerId"
hashKeyAttributeDefinition.attributeType = AWSDynamoDBScalarAttributeType.S
let hashKeySchemaElement = AWSDynamoDBKeySchemaElement()
hashKeySchemaElement.attributeName = "CustomerId"
hashKeySchemaElement.keyType = AWSDynamoDBKeyType.Hash
let rangeKeyAttributeDefinition = AWSDynamoDBAttributeDefinition()
rangeKeyAttributeDefinition.attributeName = "PlayerId"
rangeKeyAttributeDefinition.attributeType = AWSDynamoDBScalarAttributeType.S
let rangeKeySchemaElement = AWSDynamoDBKeySchemaElement()
rangeKeySchemaElement.attributeName = "PlayerId"
rangeKeySchemaElement.keyType = AWSDynamoDBKeyType.Range
/*
let indexRangeKeyAttributeDefinition = AWSDynamoDBAttributeDefinition()
indexRangeKeyAttributeDefinition.attributeName = "PlayerInsult"
indexRangeKeyAttributeDefinition.attributeType = AWSDynamoDBScalarAttributeType.S
let rangeKeySchemaElement = AWSDynamoDBKeySchemaElement()
rangeKeySchemaElement.attributeName = "PlayerId"
rangeKeySchemaElement.keyType = AWSDynamoDBKeyType.Range
let indexRangeKeyElement = AWSDynamoDBKeySchemaElement()
indexRangeKeyElement.attributeName = "PlayerInsult"
indexRangeKeyElement.keyType = AWSDynamoDBIndexRangeKeyType.
*/
//Add non-key attributes
let playerInsultAttrDef = AWSDynamoDBAttributeDefinition()
playerInsultAttrDef.attributeName = "PlayerInsult"
playerInsultAttrDef.attributeType = AWSDynamoDBScalarAttributeType.S
let provisionedThroughput = AWSDynamoDBProvisionedThroughput()
provisionedThroughput.readCapacityUnits = 5
provisionedThroughput.writeCapacityUnits = 5
// CREATE GLOBAL SECONDARY INDEX
/*
let gsi = AWSDynamoDBGlobalSecondaryIndex()
let gsiArray = NSMutableArray()
let gsiHashKeySchema = AWSDynamoDBKeySchemaElement()
gsiHashKeySchema.attributeName = "PlayerId"
gsiHashKeySchema.keyType = AWSDynamoDBKeyType.Hash
let gsiRangeKeySchema = AWSDynamoDBKeySchemaElement()
gsiRangeKeySchema.attributeName = "PlayerInsult"
gsiRangeKeySchema.keyType = AWSDynamoDBKeyType.Range
let gsiProjection = AWSDynamoDBProjection()
gsiProjection.projectionType = AWSDynamoDBProjectionType.All;
gsi.keySchema = [gsiHashKeySchema,gsiRangeKeySchema];
gsi.indexName = "PlayerInsult";
gsi.projection = gsiProjection;
gsi.provisionedThroughput = provisionedThroughput;
gsiArray .addObject(gsi)
*/
// CREATE LOCAL SECONDARY INDEX
let lsi = AWSDynamoDBLocalSecondaryIndex()
let lsiArray = NSMutableArray()
let lsiHashKeySchema = AWSDynamoDBKeySchemaElement()
lsiHashKeySchema.attributeName = "CustomerId"
lsiHashKeySchema.keyType = AWSDynamoDBKeyType.Hash
let lsiRangeKeySchema = AWSDynamoDBKeySchemaElement()
lsiRangeKeySchema.attributeName = "PlayerInsult"
lsiRangeKeySchema.keyType = AWSDynamoDBKeyType.Range
let lsiProjection = AWSDynamoDBProjection()
lsiProjection.projectionType = AWSDynamoDBProjectionType.All;
lsi.keySchema = [lsiHashKeySchema,lsiRangeKeySchema];
lsi.indexName = "PlayerInsult";
lsi.projection = lsiProjection;
//lsi.provisionedThroughput = provisionedThroughput;
lsiArray .addObject(lsi)
//Create TableInput
let createTableInput = AWSDynamoDBCreateTableInput()
createTableInput.tableName = DartsPlayerInsultTableName;
createTableInput.attributeDefinitions = [hashKeyAttributeDefinition, rangeKeyAttributeDefinition, playerInsultAttrDef]
//createTableInput.attributeDefinitions = [hashKeyAttributeDefinition, rangeKeyAttributeDefinition]
createTableInput.keySchema = [hashKeySchemaElement, rangeKeySchemaElement]
createTableInput.provisionedThroughput = provisionedThroughput
//createTableInput.globalSecondaryIndexes = gsiArray as [AnyObject]
createTableInput.localSecondaryIndexes = lsiArray as [AnyObject]
return dynamoDB.createTable(createTableInput).continueWithSuccessBlock({ (var task:BFTask!) -> AnyObject! in
if ((task.result) != nil) {
// Wait for up to 4 minutes until the table becomes ACTIVE.
let describeTableInput = AWSDynamoDBDescribeTableInput()
describeTableInput.tableName = DartsPlayerInsultTableName;
task = dynamoDB.describeTable(describeTableInput)
for var i = 0; i < 16; i++ {
task = task.continueWithSuccessBlock({ (task:BFTask!) -> AnyObject! in
let describeTableOutput:AWSDynamoDBDescribeTableOutput = task.result as! AWSDynamoDBDescribeTableOutput
let tableStatus = describeTableOutput.table.tableStatus
if tableStatus == AWSDynamoDBTableStatus.Active {
return task
}
sleep(15)
return dynamoDB .describeTable(describeTableInput)
})
}
}
return task
})
}
Putting this as an answer and not another comment in case it gets long...
It sounds like the average user's insults might fit into a single record. With the disclaimer that I know absolutely nothing about swift, this might at least be something relatively simple. Keep your customer and player keys. Before you persist the insults, turn the whole list into one big string using whatever version of join("|") swift has. When you fetch the record, do a split("|") to get your list back. (Just be a little judicious with your choice of separators, I'm only using "|" as an example, you don't want to choose something that might appear in an insult...)
There's going to be that one user with enough insults to take you over the 400kb object limit. Set a max list size constant in your code -- when you turn your lists into strings to persist them to dynamo, check the player's list length against that limit. If you exceed it, break your list into chunks of that size and use hash and range keys like ("foo", "bar"), ("foo", "bar1"), ("foo", "bar2"), etc. Yes, the first one does not have a bucket number at the end...
When you query for the data, just do a straight query first and assume you'll be in the good case (just "foo" and "bar", no other buckets). When you unpack that first list, check its length. If it's equal to your max list size constant, you know that you got a "bad" user and need to do a range query. That second one can use the hash key "foo" and the range "bar" to "bar9999". You will fetch back all those buckets with that range query. Unpack and concatenate all the lists.
This is a little gory, but it should also ultimately be straight ahead to code up. Hopefully it's still simple enough to hook into the patterns you mentioned.
What I decided to do was make a conventional dynamodb table with just one hash key, but the new hash key is a combined string of:
CustomerId + "|" + PlayerId
It is not too hard to maintain synchrony between players and insults tables because once a player is inserted into the player table, modifying the player name results in a new row being inserted. Thus, insults do not need to be modified if the player name changes. You only need to cleanup insults if a player is deleted.
This update behavior is just the way dynamodb works if you make Player name a hash key, which I did to insure they were unique.
I am converting a SQL query into broker API functionality. The query basically retrieves custom meta data based on key and value filters. The issue is when I am joining two criteria using or criteria the query.executequery takes forever and the control never returns. The code that I am using is as below
PublicationCriteria pubCriteria = new PublicationCriteria(80);
//1st query
CustomMetaKeyCriteria keyCriteria1 = new CustomMetaKeyCriteria("PublicationType");
CustomMetaValueCriteria valueCriteria11 = new CustomMetaValueCriteria("Report", Criteria.Like);
CustomMetaValueCriteria valueCriteria12 = new CustomMetaValueCriteria("Video", Criteria.Like);
Criteria valueCriteria1 = CriteriaFactory.Or(valueCriteria11, valueCriteria12);
Criteria criteria1 =CriteriaFactory.And(keyCriteria1, valueCriteria1);
//2nd query
CustomMetaKeyCriteria keyCriteria2 = new CustomMetaKeyCriteria("Tags");
CustomMetaValueCriteria valueCriteria21 = new CustomMetaValueCriteria("tcm:80-20641", Criteria.Equal);
CustomMetaValueCriteria valueCriteria22 = new CustomMetaValueCriteria("tcm:80-20645", Criteria.Equal);
Criteria valueCriteria2 = CriteriaFactory.Or(valueCriteria21, valueCriteria22);
Criteria criteria2 = CriteriaFactory.And(keyCriteria2, valueCriteria2);
Criteria querycriteria = CriteriaFactory.Or(criteria1, criteria2);
Criteria finalCriteria = CriteriaFactory.And(pubCriteria, querycriteria);
Query query = new Query(criteria2);
query.SetResultFilter(new LimitFilter(10));
var n = query.ExecuteQuery();
I have tried using new orcriteria and passing the criteria as array but this also didn't work.
Couple of weeks back I tried the same, it worked for me. I have put my findings here. http://vadalis.com/custom-meta-query-from-tridionbroker-database/
Note : my broker database is very small.