简体   繁体   中英

GDataDB: Concurrent connections & Google Docs

I use GDataDB as online INI file to save user's settings on "clouds". I use single spreadsheet like below:

 username   create     update       expire          amount
 3600001    20120303   20120303     20180303        9
 3600001    20120303   20120303     20160303        9
 3600020    20120301   20120303     20190505        14

I wonder what happens at concurrent writes/updates. I mean is it safe? What do you think?

Here are the codes: As you see it's a low chance to access the file at the same time but I ask for future reference, to see it can be used or not on heavy -not to much- read-write operations. Or can I implement a lock mechanism?

        public class License
        {
            public string username { get; set; }
            public string create { get; set; }
            public string update { get; set; }
            public string expire { get; set; }
            public double amount { get; set; }
        }



        private static void Main(string[] args) {


            string myaccount = "xxxxxxxxxx@gmail.com";
            string mypass = "xxxxxxxxxxxxxxxx";
            string spreadsheet = "licence";

            string username = "03600001";
            double amount = 9.0d; //bucks
            int extend = 1; //year

            // create the DatabaseClient passing my Gmail or Google Apps credentials
            IDatabaseClient client = new DatabaseClient(myaccount, mypass);

            // get or create the database. This is the spreadsheet file 
            IDatabase db = client.GetDatabase(spreadsheet) ?? client.CreateDatabase(spreadsheet);

            // get or create the table. This is a worksheet in the file 
            // note I am using my Person object so it knows what my schema needs to be  
            // for my data. It will create a header row with the property names 
            ITable<License> table = db.GetTable<License>(spreadsheet) ?? db.CreateTable<License>(spreadsheet);




            var license = new License();


            IList<IRow<License>> rows = table.FindStructured(String.Format("username={0}", username)).OrderByDescending(o => o.Element.expire).Take(1).ToList();

            if (rows == null || rows.Count == 0)
            {
                //add new
                license.username = username;
                license.create = DateTime.Now.ToString("yyyyMMdd");
                license.update = license.create;
                license.expire = DateTime.Now.AddYears(extend).ToString("yyyyMMdd");
                license.amount = amount;

                table.Add(license);
            }
            else
            {
                //update
                IRow<License> row = rows[0];

                DateTime expire = DateTime.ParseExact(row.Element.expire, "yyyyMMdd", null);

                row.Element.expire = expire.AddYears(extend).ToString("yyyyMMdd");
                row.Element.update = DateTime.Now.ToString("yyyyMMdd");
                row.Element.amount = amount;

                row.Update();
            }

          }

As far as I know there should be no problem. Of course, since there is no built-in locking mechanisms, the last update to a particular cell will win, unless you implement a locking mechanism yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM