What is the Groovy way to get collection items without items in subcollection. For example:
collection:
[1,2,3,4,5,6]
subcollection:
[1,5,6]
the result should be:
[2,3,4]
EDIT:
It looks I'm doing something wrong. This is part of my code:
def report = Report.get(params.report.id)
def user = User.get(params.user.id)
List<User> availableUsers = []
availableUsers = User.findAllByCompany(company))
List<User> addedUsers = []
addedUsers = (List<User>) session["addedUsers"] ?: []
addedUsers << user
session["addedUsers"] = null
session["addedUsers"] = addedUsers
availableUsers = availableUsers - addedUsers
This code is only removing last user in addedUsers list.
availableUsers: [John, Jack, Jim]
addedUsers: [John, Jack]
availableUsers - addedUsers: [John, Jim]
Every time only the last item in addedUsers gets removed. I'm guessing I'm missing something obvious but I cant find it.
Did you try the obvious:
result = [ 1, 2, 3, 4, 5, 6 ] - [ 1, 5, 6 ]
Because that is what works...
Edit
Storing domain object in the session will result in the object being different between hibernate transactions, better to store the id in the session and get the Users fresh each time (or write this functionality into the domain if it needs persisting), something like:
addedUsers = session["addedUsers"].collect { User.get( it.id ) } ?: []
Related
I am working on a simple database procedure in Kotlin using Room, and I can't explain why the process is so slow, mostly on the Android Studio emulator.
The table I am working on is this:
#Entity(tableName = "folders_items_table", indices = arrayOf(Index(value = ["folder_name"]), Index(value = ["item_id"])))
data class FoldersItems(
#PrimaryKey(autoGenerate = true)
var uid: Long = 0L,
#ColumnInfo(name = "folder_name")
var folder_name: String = "",
#ColumnInfo(name = "item_id")
var item_id: String = ""
)
And what I am just trying to do is this: checking if a combination folder/item is already present, insert a new record. If not, ignore it. on the emulator, it takes up to 7-8 seconds to insert 100 records. On a real device, it is much faster, but still, it takes around 3-4 seconds which is not acceptable for just 100 records. It looks like the "insert" query is particularly slow.
Here is the procedure that makes what I have just described (inside a coroutine):
val vsmFoldersItems = FoldersItems()
items.forEach{
val itmCk = database.checkFolderItem(item.folder_name, it)
if (itmCk == 0L) {
val newFolderItemHere = vsmFoldersItems.copy(
folder_name = item.folder_name,
item_id = it
)
database.insertFolderItems(newFolderItemHere)
}
}
the variable "items" is an array of Strings.
Here is the DAO definitions of the above-called functions:
#Query("SELECT uid FROM folders_items_table WHERE folder_name = :folder AND item_id = :item")
fun checkFolderItem(folder: String, item: String): Long
#Insert
suspend fun insertFolderItems(item: FoldersItems)
Placing the loop inside a single transaction should significantly reduce the time taken.
The reason is that each transaction (by default each SQL statement that makes a change to the database) will result in a disk write. So that's 100 disk writes for your loop.
If you begin a transaction before the loop and then set the transaction successful when the loop is completed and then end the transaction a single disk write is required.
What I am unsure of is exactly how to do this when using a suspended function (not that familiar with Kotlin).
As such I'd suggest either dropping the suspend or having another Dao for use within loops.
Then have something like :-
val vsmFoldersItems = FoldersItems()
your_RoomDatabase.beginTransaction()
items.forEach{
val itmCk = database.checkFolderItem(item.folder_name, it)
if (itmCk == 0L) {
val newFolderItemHere = vsmFoldersItems.copy(
folder_name = item.folder_name,
item_id = it
)
database.insertFolderItems(newFolderItemHere)
}
}
your_RoomDatabase.setTransactionSuccessful() //<<<<<<< IF NOT set then ALL updates will be rolled back
your_RoomDatabase.endTransaction()
You may wish to refer to:-
https://developer.android.com/reference/androidx/room/RoomDatabase
You may wish to especially refer to runInTransaction
My rank command is working fine, iv been trying to add the ability to !rank #user. As you can see i have code there to grab the mentioned user, so it will display the mentioned users name and profile pic but my points (because they are requested from message.author) I'm just unsure how i should get the points from the database for the mentioned user. Any help or advice would be amazing, thanks!
(my database is SQLite)
const member = message.mentions.members.first() || message.member || message.guild.members.cache.get(args[0])
score = bot.getScore.get(message.author.id, message.guild.id);
if (!score) {
score = {
id: `${message.guild.id}-${message.author.id}`,
user: message.author.id,
guild: message.guild.id,
points: 0,
level: 1,
};
}
let curxp = score.points;
let curlvl = score.level;
let nxtLvlXp = curlvl * 300;
let difference = nxtLvlXp - curxp;
const embed = new Discord.RichEmbed()
.setTitle("XP / LEVEL")
.setDescription(member.user.tag)
.setThumbnail(member.user.displayAvatarURL)
.setColor(cyan)
.addField('**' + "Level" + '**', curlvl, true)
.addField('**' + "XP" + '**', curxp, true)
.setFooter(`${difference} XP til next level up`, bot.user.displayAvatarURL);
return message.channel.send({ embed });
You pretty much already have it
Instead of using message.author.id for the first argument why not just use the member variable which gives the final member?
Also you should have message.guild.members.cache.get(args[0]) before message.member, since message.member will always exist unless in a DM Channel.
const member = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member;
score = bot.getScore.get(member.id, message.guild.id);
I have a collection with over 1000 json documents. I want to now delete all the documents in that collection.
Is there any way of doing it from the portal using a query?
PS: I know I can delete the collection and re-create it or use a c# application to do it.
No, there is no equivalent to the SQL DELETE expression in DocumentDB so there is no way to do it with a query. Here is a stored procedure (CoffeeScript) that does this for me when I want to delete based upon a query.
deleteSomeDocuments = (memo) ->
collection = getContext().getCollection()
unless memo?
memo = {}
if memo.returnDeleted
unless memo.deleted?
memo.deleted = []
else
memo.returnDeleted = false
stillQueuingOperations = true
query = () ->
if stillQueuingOperations
responseOptions =
pageSize: memo.remaining
setBody()
if memo.filterQuery?
memo.stillQueueing = collection.queryDocuments(collection.getSelfLink(), memo.filterQuery, responseOptions, onReadDocuments)
else
memo.stillQueueing = collection.readDocuments(collection.getSelfLink(), responseOptions, onReadDocuments)
onReadDocuments = (err, resources, options) ->
if err
throw err
if resources.length isnt memo.remaining
throw new Error("Expected memo.remaining (#{memo.remaining}) and the number of rows returned (#{resources.length}) to match. They don't.")
memo.stillQueueing = true
while memo.remaining > 0 and memo.stillQueueing
oldDocument = resources[memo.remaining - 1]
documentLink = oldDocument._self
etag = oldDocument._etag
options = {etag} # Sending the etag per best practice, but not handling it if there is conflict.
getContext().getResponse().setBody(memo)
memo.stillQueueing = collection.deleteDocument(documentLink, options)
if memo.stillQueueing
if memo.returnDeleted
memo.deleted.push(oldDocument)
memo.remaining--
setBody = () ->
getContext().getResponse().setBody(memo)
query()
return memo
exports.deleteSomeDocuments = deleteSomeDocuments
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.
In OpenERP I have 3 models, A, B and C. If you start on the form for A, there is a tree list of children B. when you click on one of those B children that form has a tree list of children C. I need the C children to be associated to both parents A and B but they will only associate themselves with B despite having many2one fields for both A and B. How can I force this association?
I have looked at solutions using active_id and default_get without success. The context object seems useful for this but I see no way of dynamically setting it with ids for both parent objects as I drill down from A to C navigating through the forms. I don't understand why context is not normally used for holding the context in this manner like in various web frameworks.
To clarify, when I edit object A and from it's edit form create an instance of B and from B's edit form create an instance of C how do I make the C associated to A and B?
Should I just execute a simple update with subselect query in C's create method to make this happen or will that break the ORM?
Looking at the transaction data when I save a new C instance (document) I see that everything I need in terms of ids is within the transaction but I don't know how to access and manipulate the values as I need to e.g.
{
"jsonrpc":"2.0",
"method":"call",
"params":{
"model":"dbe.vendor",
"method":"write",
"args":[
[
3
],
{
"application":[
[
4,
2,
false
],
[
1,
21,
{
"documents":[
[
4,
37,
false
],
[
4,
35,
false
],
[
4,
46,
false
],
[
4,
36,
false
],
[
0,
false,
{
"state":"new",
"name":"order of precendence test",
"description":"TESTING",
"type_of":7,
"locked":false,
"note":false,
"datas":false,
"datas_fname":false,
"type":"binary",
"application_id":false,
"certification_id":false,
"vendor_id":3,
"message_follower_ids":false,
"message_ids":false
}
]
]
}
]
]
}
],
"kwargs":{
"context":{
"lang":"en_US",
"tz":"EST",
"uid":7
}
},
"session_id":"303ae4c1bd9d49079c4efc9e06e0184f",
"context":{
"lang":"en_US",
"tz":"EST",
"uid":7
}
},
"id":"r138"
}
NOTE: I manually inserted vendor_id because it is a required field but it is the field I want to automatically populated.
Perhaps I am not literate enough with OpenERP to make this association happen properly or there isn't enough relevant documentation or the ORM isn't designed for this use (or any combinations of these) but I need this to work as per my requirements and now it does. I apologize if it is offensive in any manner. Criticisms and advice always welcome.
def create(self, cr, uid, vals, context=None):
"""Creates a new dbe.document instance. Called by both internal app and client.
documents are associated to dbe.vendor, dbe.application and dbe.certification.
#param vals: All dbe.document field values as a dictionary.
#return ID of new dbe.document instance.
"""
if context is None:
context={}
doc_id = None
association_id = None
# vendor_id is passed with vals when documents are created from the client side.
new_vendor_id = vals.get('vendor_id',False)
if new_vendor_id:
doc_id = super(dbe_document,self).create(cr, uid, vals, context=context)
_logger.debug("<CREATE> DBE Document (%d) created for vendor #%d by user %d", doc_id, new_vendor_id, uid)
else: # within OpenERP we cannot get vendor_id from context so we do it caveman style.
doc_id = super(dbe_document,self).create(cr, uid, vals, context=context)
association_id = self.read(cr, uid, doc_id, ['application_id', 'certification_id'], context=context)
if association_id['application_id']:
application_id = association_id['application_id']
application_obj = self.pool.get('dbe.application')
application = application_obj.browse(cr, uid, application_id)
new_vendor_id = application.vendor_id.id
elif association_id['certification_id']: # since there is no application maybe its a certification related doc....
certification_id = association_id['certification_id'][0]
certification_obj = self.pool.get('dbe.certification')
certification = certification_obj.browse(cr, uid, certification_id)
new_vendor_id = certification.vendor_id.id
else:
raise osv.except_osv(_('ValidateError'), _('<CREATE> A DBE Document cannot be created without an application or certification - Association Missing!'))
if new_vendor_id: # brute-force association to dbe.vendor.
vals.update({'vendor_id': new_vendor_id})
self.write(cr, uid, doc_id, vals, context)
_logger.debug("<CREATE> DBE Document (%d) created for vendor #%d by user %d", doc_id, new_vendor_id, uid)
else: # too bad - no vendor, no doc.
_logger.debug("<CREATE> DBE Document (%d) removed because vendor_id missing for user %d", doc_id, uid)
raise osv.except_osv(_('ValidateError'), _('<CREATE> A DBE Document cannot be created without selecting a Vendor - Vendor Id Missing!'))
return doc_id
better to use fields.related to make such association where you can do refer "A" from "B" in form of "C".