English 中文(简体)
我怎能避免蒙戈巴种族状况
原标题:how can i avoid mongodb race condition

我想用蒙戈德字更新一些数据 我的逻辑如下:

#find the specific document with "md5,time,size",
if collection.find({"src_md5":file_md5,"src_time":file_time,"src_size":file_size}).count() == 0:
    #if not found
    #   find the idx,if idx is not yet exist,set idx equa 1

    if collection.find({},{"idx":1}).count() == 0:
        idx = 1

    #if idx is alread there, sort idx and get the biggest idx
    else:
        idx = collection.find({},{"idx":1}).sort( idx ,-1).limit(5)[0][ idx ]
        idx = idx + 1

        #insert the info with idx
        if not self.insertFileInfo(collection,file_obj,file_md5,file_time,file_size,long(idx)):
            return None
#if the specific document with "md5,time,size" is found
else:
#just get the idx with the specific md5
    idx = collection.find({"src_md5":file_md5,"src_time":file_time,"src_size":file_size},{"idx":1})[0][ idx ]
    return None

i will run the above code in 4 machines,which means 4 process would update mongodb almost simultaneously,how can i ensure the atomic of the operations? my recored schema is

{"src_md5":"djapijfdakfiwqjfkasdj","src_size":2376498,"src_time":1338179291,"idx":1}
{"src_md5":"jdfipajkoijjipjefjidwpj","src_size":234876323,"src_time":1338123873,"idx":2}
{"src_md5":"djapojfkdasxkjipkjkf","src_size":3829874,"src_time":1338127634,"idx":3}

it s not a simple auto increment key,it should be increased when md5,size,time changed,and shuld be insert with them,as a record. i create a compound unique index on {"src_md5","src_time","src_size"},and create a unique index on {"idx"},but before i insert new info, i should get the idx alread exist,then increase it. there are two situation: 1,idx with the specific md5,size,time,if is already exist,just return the idx 2,if not exist, increase idx with 1

问题回答




相关问题
Get first unlocked row in MYSQL

When someone visits my website, he will see the page with the fewest pageviews. All pages, including a pageview counter, are stored in a MYSQL database. If a page is viewed, the pageview counter is ...

Simulating race conditions in RSpec unit tests

We have an asynchronous task that performs a potentially long-running calculation for an object. The result is then cached on the object. To prevent multiple tasks from repeating the same work, we ...

Python Service File Caching Apache Race Condition

I am writing a python service (pyamf) through which a user can access images. All images are stored on a central server. The python services will be running on satellite machines which have network ...

Javascript threading race condition

EDIT: I figured out the answer to the original YUI3 question I posted here, but it led to another one and instead of starting a new thread I thought I d just add it here. Please scroll down for the ...

Race between virtual function and pthread_create

When I try to create a class instance with a virtual method and pass it to pthread_create, I get a race condition, causing the caller to sometimes call the base method instead of the derived method ...

Nhibernate, multithreading, and race condition

I am having a problem with a race condition when saving to the database asynchronously using NHibernate. First an insert to the database is done asynchronously where the unique id is auto-generated. ...

热门标签