Delete Ancestor Tree in Google Cloud Datastore - google-cloud-datastore

How do I delete an entire ancestor tree via the API (Python protocol buffer) in Google Cloud Datastore?
For example, if I have entities stored in this structure: grandparent/parent/child, how can I delete the grandparent, and in so doing delete all the children and grandchildren of that "node"?
If I submit a delete request on the grandparent's key, the grandparent entity is deleted, but its children and grandchildren remain, and their path is still grandparent/parent/child, even though the grandparent entity was deleted.

Deleting a parent entity will not delete any of the child entities. However, you can use an ancestor query to find the keys of all of the child entities and delete them as part of a single transaction. The steps would be:
Begin transaction.
Run a keys-only kindless ancestor query on the parent entity's key.
Add each of the keys returned by #2 to the list of keys to delete.
Commit the transaction.
Here's a partial code snippet:
# Create a transactional RunQueryRequest.
req = datastore.RunQueryRequest()
req.read_options.transaction = txn # From previous BeginTransactionRequest.
query = req.query
# Add an ancestor filter.
key_filter = query.filter.property_filter
key_filter.property.name = '__key__'
key_filter.operator = datastore.PropertyFilter.HAS_ANCESTOR
path_element = key_filter.value.key_value.path_element.add()
path_element.kind = 'ParentKind'
path_element.name = 'parent-name'
# Make it a keys-only query.
query.projection.add().property.name = '__key__'
batch = self.datastore.run_query(req)
for entity_result in batch:
# Add entity_result.entity.key to CommitRequest...

Related

Firestore rule to only add/remove one item of array

To optimize usage, I have a Firestore collection with only one document, consisting in a single field, which is an array of strings.
This is what the data looks like in the collection. Just one document with one field, which is an array:
On the client side, the app is simply retrieving the entire status document, picking one at random, and then sending the entire array back minus the one it picked
var all = await metaRef.doc("status").get();
List tokens=all['all'];
var r=new Random();
int numar=r.nextInt(tokens.length);
var ales=tokens[numar];
tokens.removeAt(numar);
metaRef.doc("status").set({"all":tokens});
Then it tries to do some stuff with the string, which may fail or succeed. If it succeeds, then no more writing to the database, but if it fails it fetches that array again, adds the string back and pushes it:
var all = await metaRef.doc("status").get();
List tokens=all['all'];
List<String> toate=(tokens.map((element) => element as String).toList());
toate.add(ales.toString());
metaRef.doc("status").set({"all":toate});
You can use the methods associated with the Set object.
Here is an example to check that only 1 item was removed:
allow update: if checkremoveonlyoneitem()
function checkremoveonlyoneitem() {
let set = resource.data.array.toSet();
let setafter = request.resource.data.array.toSet();
return set.size() == setafter.size() + 1
&& set.intersection(setafter).size() == 1;
}
Then you can check that only one item was added. And you should also add additional checks in case the array does not exist on your doc.
If you are not sure about how the app performs the task i.e., successfully or not, then I guess it is nice idea to implement this logic in the client code. You can just make a simple conditional block which deletes the field from the document if the operation succeeds, either due to offline condition or any other issue. You can find the following sample from the following document regarding how to do it. Like this, with just one write you can delete the field which the user picks without updating the whole document.
city_ref = db.collection(u'cities').document(u'BJ')
city_ref.update({
u'capital': firestore.DELETE_FIELD
})snippets.py

Firebase,iOS: Appending key-value pair into a child node

I have the following User Table structure in Firebase
As you can see in the user that I have opened, I have a Posts section, inside this post section holds the Id's all articles which have been posted by this user.
The issue I am facing is as follows:
When the user creates a new article it's saved within the Posts Table, after the save I return the newly generated ID which I then pass on to the user table, I trying to insert the newly created ID into the post section of the user, so I assumed the URL would be something like this:
Users/{UserId}/Posts
However all this does it create a new section called posts, it doesn't actually insert the record into the given area.
My code which isn't working is as follows:
let linkPost = [childautoID: true]
FIRDatabase.database().reference().child("Users/\(UserId)/Posts").child(UserId).setValue(linkPost)
FYI the two id's that are currently inside Posts I added manually.
I've also tried the following:
FIRDatabase.database().reference().child("Users/\(UserId)/Posts").setValue(linkPost)
However all this does it remove all existing Id's and then inserts the new id.
I prefer something like this. This automatically append the data without fetching first
FIRDatabase.database().reference().child("Users/\(UserId)/Posts").child(UserId).setValue(true)
To append a key-value pair in Firebase Database child node use this :-
Make a Firebase Database Reference to the Posts node of that currentUser FIRDatabase.database().reference().child("Users").child(FIRAuth.auth()!.currentUser!.uid).child("Posts")
Check if Posts node exists in your user's DB, If not then create one by :- parentRef.setValue([postId : "True"]) in else block.
But if Posts node does exist retrieve it as a NSMutableDictionary , set the new object to it, and then store the updated Dictionary to that node.
func storePostsToDB(postID :String!, userID : String! = FIRAuth.auth()!.currentUser!.uid){
let parentRef = FIRDatabase.database().reference().child("Users").child(userID).child("Posts")
parentRef.observeSingleEventOfType(.Value, withBlock: {(friendsList) in
if friendsList.exists(){
if let listDict = friendsList.value as? NSMutableDictionary{
listDict.setObject("True", forKey: postID)
parentRef.setValue(listDict)
}
}else{
parentRef.setValue([postID : "True"])
}
})
}
Calling the function:-
storePostsToDB("yourPostID")// If you want to store in the currentUser DB
storePostsToDB("yourPostID", userID : otherUserID)//If you want to store the post in some other users database with uid `otherUserID`

react create dynamic tree recursively

thanks for reading and helping. Here is my situation so far:
I have much data in database, each piece of data has id, parentid(which means you can find the id of its parent using this parentid ), name, description.
I want to create a dynamic tree using react,but I do not know how many levels of nodes I have. Each node represents for an id in database. An user clicks on a node A on this tree, the children nodes whose parentid equals to A's id will show/hide.
I do not intend to retrieve all the data because it will take long time. Now I am able to get one node's children by sending request and get response.body:
getChildren(id){
ajax.get('http://localhost:8080/configmgmt/code/category/retriveTree/' + id)
.end((error, response) => {
if (!error && response) {
console.dir(response.body );
this.setState(subdata:response.body});
} else {
console.log('There was an error fetching from database', error);
}
}
);
}
in render part, I wrote:
{this.state.subdata.map((rb,index)=>{
return <li><div>{rb.name}</label></div></li>})
}
Here comes the question, I still do not know how to create the tree recursively(because any node might has its children nodes ). how to do this when we can only get a node's children nodes from the database?
I would do your task in two steps:
Create a structure for an augmented tree with loading status flags. It should have a structure like this (this is pseudocode):
class Node {
loaded: boolean,
expanded: boolean,
children: list<Node>
}
Create a component for this:
If node isn't expanded don't render its children
If use clicks on expand sign
If children are loaded, do nothing, just change the expanded field
If children aren't loaded
set expanded to true
initiate ajax request
as soon as the request completes, set loaded to true, and assign children
Creating a component which recursively uses itself isn't a problem. If you don't know how to do this read here: how to render child components in react.js recursively

NHibernate - Duplicate Records with lazily mapped collection

All,
I have an entity, that has several collections,- each collection is mapped lazily. When I run a criteria query, I get duplicate results for my root entity in the result set. How's that possible when all my collections are mapped lazily!
I verified, my collections, load lazily.
Here's my mapping:
Root entity 'Project':
[Bag(0, Lazy = CollectionLazy.True, Inverse = true, Cascade = "all-delete-orphan")]
[Key(1, Column = "job_id")]
[OneToMany(2, ClassType = typeof(ProjectPlan))]
public virtual IList<ProjectPlan> PlanList
{
get { return _planList; }
set { _planList = value; }
}
The criteria query is:
ICriteria criteria = session.Session.CreateCriteria<Entities.Project>()
.Add(Restrictions.Eq(Entities.Project.PROP_STATUS, !Entities.Project.STATUS_DELETED_FLAG));
.CreateAlias(Entities.Project.PROP_PLANLIST, "p")
.Add(Restrictions.Eq("p.County", 'MIDDLSEX'))
.setFirstResult(start).setMaxResults(pageSize)
.List<Entities.Project>();
I know, I can correct this problem w/ Distinct result transformer, I just want to know if this is normal behavior on lazy collections.
EDIT: I found the cause of this,- when looking at the raw SQL, the join, and where clause are correct but what baffles me is the generated Select clause,- it not only contains columns from the project entity (root entity) but also columns from the project plans entity which causes the issue I described above. I am not at work right now, but I'll try to do this: .SetProjection(Projections.RootEntity()), so I only get Project's columns in the select clause.
One way, how to solve this (I'd say so usual scenario) is: 1) not use fetching collections inside of the query and 2) use batch fetching, as a part of the mapping
So, we will always be querying the root entity. That will give us a flat result set, which can be correctly used for paging.
To get the collection data for each recieved row, and to avoid 1 + N issue (goign for collection of each record) we will use 19.1.5. Using batch fetching
The mapping would be like this
[Bag(0, Lazy = CollectionLazy.True
, Inverse = true
, Cascade = "all-delete-orphan"
, BatchSize = 25)] // Or something similar to batch-size="25"
[Key(1, Column = "job_id")]
[OneToMany(2, ClassType = typeof(ProjectPlan))]
public virtual IList<ProjectPlan> PlanList
{
...
Some other similar QA (with the almost same details)
How to Eager Load Associations without duplication in NHibernate?
NHibernate QueryOver with Fetch resulting multiple sql queries and db hits
Is this the right way to eager load child collections in NHibernate
And we still can filter over the collection items! but we have to use subqueries, an example Query on HasMany reference

Objectify NotFoundException

Why do these 2 fetches result in a NotFoundException?
ofy().load().type(MyClass.class).id(myClassInstance.getId()).safeGet();
ofy().load().key(myClassInstance.getKey()).safeGet();
But this query returns an entity:
ofy().load().type(MyClass.class).filter("fieldName",myClassInstance.getUserId()).first().get();
Additional info:
MyClass contains a #Parent and #Id field
You aren't specifying the parent key when loading by key.
ofy().load().type(MyClass.class).id(myClassInstance.getId()).safeGet();
// should be:
ofy().load().type(MyClass.class).parent(myClassInstance.getParent()).id(myClassInstance.getId()).safeGet();
For the second line, I suspect your implementation of getKey() is flawed and missing the parent key. The query works because the query is not a key lookup; it just returns whatever is in the index for the property.
Remember that ids are only unique for a particular parent. The unique identifier for an entity is {parent, id}. Read https://code.google.com/p/objectify-appengine/wiki/Concepts carefully.

Resources