Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBLLiveQuery not working properly for startKey and endKey values #1976

Closed
parvezq opened this issue Dec 7, 2017 · 5 comments
Closed

CBLLiveQuery not working properly for startKey and endKey values #1976

parvezq opened this issue Dec 7, 2017 · 5 comments
Labels
Milestone

Comments

@parvezq
Copy link

parvezq commented Dec 7, 2017

For a swift iOS application that is using a map/reduce block, the respective live query is not working properly. Below is the code excerpt:

Couchbase document has below field and type structure:
fID String
rStatus Bool
tID String
mDate: String with date entries as provided by CBLJSON.jsonObject(with: Date())

`if let aView: CBLView = self.database?.viewNamed("rView"){
aView.setMapBlock({ (doc, emit) in
if let fID = doc["fID"] as? String,
let tID = doc["tID"] as? String,
let rStatus = doc["rStatus"] as? NSNumber,
let dt = doc["mDate"] as? String, let date = CBLJSON.date(withJSONObject: dt){

                let timeStamp = dt.timeIntervalSince1970
                
                //Two times emit is to be able to get latest data based on grouping
	    emit([fID,rStatus,tID,timeStamp],doc)
                emit([tID,rStatus,fID,timeStamp],doc)
                
            }
        }, reduce: { (keys, values, rereduce) -> Any in
            var data = [String: Any]()
            if let valueArray = values as? [[String: Any]]{                    
                do{
                    let lIndex = valueArray.count - 1
                    if lIndex < 0{
                        return data
                    }
                    let lastObject = valueArray[lIndex]
                    data["fID"] = lastObject["fID"]
                    data["toId"] = lastObject["tID"]
                    data["rStatus"] = lastObject["rStatus"]
                    let mDate = lastObject["mDate"]
                    data["mDate"] = mDate
                    let date = CBLJSON.date(withJSONObject: mDate)
                    let tStamp = date?.timeIntervalSince1970
                    data["tStamp"] = tStamp
                }
                catch{
                    print("Encountered error: \(error).")
                }
            }
            //print("Data is: \(data)")
            return data
        }, version: "1")
    }`

The query setup code is as below:

`if let aView = self.database?.existingViewNamed("rView"){
aQuery = aView.createQuery().asLive()
if let query = aQuery{
var keysArray = Any
keysArray.append(fID) //Here fID and tID from document is a string beginning with letter G followed by two colons and then an integer value for example G::101, G::108, G::99 etc
query.startKey = keysArray

            keysArray.append([:])
            query.endKey = keysArray
            query.groupLevel = 3 //To group on basis of first three keys in the emitted keys
            query.addObserver(self, forKeyPath: "rows", options: [.new,.initial], context: nil)                
        }

}

`

I have ensured that the value specified in startKey and endKey do exists in the database and getting synced. The above query expects to return most recent data based on timeStamp and grouped too as per specified group level. The issue being faced is:

  1. The above query is not respecting the values specified in startKey and endKey fields and so is not returning complete data. Here by complete data I mean some documents gets excluded from the returned result set even though they are having correct values as specified by the startKey and endKey.
  2. I have noticed that keys array parameter of reduce block contains a collection with each row having four elements instead of three as per specified groupingLevel of 3.

Any idea, what is going wrong in here?


  • Version: Couchbase lite SDK version: 1.4.1
  • Client OS: iOS 11
  • Server: Couchbase Sync Gateway/1.4.1
@parvezq
Copy link
Author

parvezq commented Dec 7, 2017

The fID and tID fields of the document do not contains same string values but are different and having the same format.

@pasin
Copy link
Contributor

pasin commented Dec 11, 2017

@parvezq Can you enable QueryVerbose logging and post it here to see if the query does the reduce correctly?

@pasin
Copy link
Contributor

pasin commented Dec 11, 2017

Regarding the issue bout the keys array parameter, I have checked with @snej about this, and it's correct that there are four items in the keys array. Here is the info from @snej:

Jens Alfke: The way reduce is called doesn't depend on the groupLevel. Conceptually, reduce happens at indexing time (it does in CouchDB; it doesn't in CBL just because we didn't have a good way to store the reduced values.)
Jens Alfke: So the keys passed to reduce are always the same as the keys the map block emits.

@djpongh djpongh added this to the 1.4.x milestone Dec 15, 2017
@djpongh djpongh added the icebox label Dec 15, 2017
@snej
Copy link
Contributor

snej commented Jan 5, 2018

So is the resolution that this behaves correctly? Then should we close this?

@pasin
Copy link
Contributor

pasin commented Feb 6, 2018

We could reopen this issue again when there is new information.

@pasin pasin closed this as completed Feb 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants