Couchbase
  • Why NoSQL?
  • Couchbase Server
  • Download
  • Resources
  • Careers
Home | Forums | SDKs | SDKs

View & Lock

4 replies [Last post]
  • Login or register to post comments
Thu, 12/27/2012 - 03:51
hhtu
Offline
Joined: 12/27/2012
Groups: None

Hi, I'm new to here, and I use PHP to connect to Couchbase.

I have a problem by using "view" function.

Because I notice that there is a getAndLock function which can lock read data, but how do I lock data from "view" function? Or I should not use view to lock those data? than what should I do for this purpose? - has some criteria on those data and need to lock while read.

Thanks for help.

Top
  • Login or register to post comments
Thu, 12/27/2012 - 09:44
ingenthr
Offline
Joined: 03/16/2010
Groups:

The best approach would be to iterate over the view and lock the items specified by the id in the row returned from the view, if you need locking.

For what it's worth, it's often better to refactor the problem to use CAS operations or some other approach instead of locking. Maybe you can describe what you're trying to achieve and we can assist with the right approach?

Top
  • Login or register to post comments
Thu, 12/27/2012 - 20:46
hhtu
Offline
Joined: 12/27/2012
Groups: None

Thanks for the answer, actually I have thought about it but still can't solve my problem.

What I want to do is like: select * from table, I know this could be done in php - view function, and I also want to add limit constrain, like 1000 records, here is my basic requirement.

And for lock purpose, I want to use more than one computer to retrieve those data, ex: machine 1 asks for 1000 data, machine 2 asks for 800 data, machine 3 asks for 900 data, and all machines have to retrieve different data. Maybe I can get view result and use getAndLock on those keys, but I think it is too late, because if there are more than one computer to use view operation, this will be useless, data will be overlapped, and if I'm not luck, machine 1's 1000 data may contain machine 2's all 800 data, and more computer, more collisions, this is what I want to avoid.

Top
  • Login or register to post comments
Thu, 12/27/2012 - 23:28
ingenthr
Offline
Joined: 03/16/2010
Groups:

Is this for some kind of job queue like processing?

One thought would be to process in batches by setting items along side the current items. For example, if you set an item, indicating what the first range is and then add items with key and no value for each of the items to be processed along with an expiration. The "key" can match the real key but with something additional indicating it's to be processed.

When a node processing the item comes along to process it, it can set it with another value or remove it, after removing or changing the range to be processed to the next range.

Now, if items are being added or removed concurrently, you'll have a challenge, but that could be mitigated with other application level changes to add the markers.

I don't think locks are good for what you're trying to achieve. If the lock expires, how will you know if it's been processed or the lock holder just died?

This probably isn't a perfect answer, but I don't fully grasp what you're trying to achieve. Hopefully it helps get you the right direction.

Top
  • Login or register to post comments
Wed, 01/02/2013 - 01:25
hhtu
Offline
Joined: 12/27/2012
Groups: None

Ya, it's like to build a job queue, and I solve this purpose in a other way.
First, I add a document, and key is "jobs_count", value is "numOfJobs"

$Jobs->numOfJobs = 0;
$cb->set("jobs_count", json_encode($Jobs));

Second, I numbering all my jobs as key name. ex: jobs00001, jobs00002, jobs00003...etc

If there is a connection ask for 3000 jobs, it needs to retrieve "job_count" first(getAndLock),
and gets numOfJobs ex: 5000, then retrieves jobs05001 ~ jobs08000 key.
After finishing this retrieve, update "job_count"(cas) to 8000.

I hope I use it right ~ :)

thanks for help me & Happy New Year

Top
  • Login or register to post comments
  • Login or register to post comments
  • Login
  • Register

Company

  • About Us
  • Leadership
  • Customers
  • Partners
  • Contact Us

Product

  • Couchbase Server
  • Couchbase SDKs
  • Use Cases
  • Documentation
  • Forums

Open Source

  • Couchbase Project
  • Couchbase vs. CouchDB

Commercial

  • Subscriptions & Support
  • Training & Services

News

  • Blog
  • Newsletter
  • Press Releases
  • Buzz

Follow Us

    
  • Customer Login
  • Terms of Service
  • Privacy Policy
  • Trademark Policy
  • Site Map

© 2013 COUCHBASE All rights reserved.

Sign in to Couchbase Community

close
  • Create new account
  • Request new password
You are logging into the Forums, Wiki and Issue Tracker