データストアのoffsetは1000以上では使えない

そういえばそういう制限もあった。
offset may not be above 1000 とかエラーを返してくれる。



制約に合わせて上のアプリを修正。
重要なのは

        statuses = Status.gql('ORDER BY id DESC').fetch(limit, (page - 1) * limit)

↑だとoffsetが1000以上になってしまうので、WHERE句で絞り込むのがいい。

        offset = (page - 1) * limit
        statuses = Status.gql('WHERE index >= :1 ORDER BY index DESC', offset).fetch(limit)

これで動く。




以下差分

 class Status(db.Model):
-    id      = db.IntegerProperty(required=True)
-    source  = db.StringProperty(required=True)
-    created = db.DateTimeProperty(required=True)
-    text    = db.TextProperty(required=True)
+    index    = db.IntegerProperty(required=True)
+    id       = db.IntegerProperty(required=True)
+    source   = db.StringProperty(required=True)
+    created  = db.DateTimeProperty(required=True)
+    text     = db.TextProperty(required=True)


-        statuses = Status.gql('ORDER BY id DESC').fetch(limit, (page - 1) * limit)
+        offset = (page - 1) * limit
+        statuses = Status.gql('WHERE index >= :1 ORDER BY index DESC', offset).fetch(limit)


-        sinceid = 1
-        statuses = Status.gql('ORDER BY id DESC LIMIT 1')
+        
+        maxindex = -1
+        sinceid  = 1
+        statuses = Status.gql('ORDER BY index DESC LIMIT 1')
         if statuses:
             for status in statuses:
-                sinceid = status.id
+                sinceid  = status.id
+                maxindex = status.index
                 break
-        logging.info('sinceid: %s' % sinceid)
+        logging.info('sinceid: %s'  % sinceid)
+        logging.info('maxindex: %s' % maxindex)
+
         statuses = getTimeLine(sinceid)
-        def addStatus(key, id, text, source, created):
+        def addStatus(key, index, id, text, source, created):
             obj = db.get( db.Key.from_path( "Status", key ) )
             if not obj:
                 obj = Status(key_name=key,
+                             index=index,
                              id=id,
                              text=text,
                              source=source,


         for status in statuses:
             try:
+                maxindex += 1
                 id  = status['id']
                 key = 's%s' % id
-                db.run_in_transaction(addStatus, key, id, status['text'], status['source'], status['created'])
+                db.run_in_transaction(addStatus, key, maxindex, id, status['text'], status['source'], status['created'])
+                logging.info( 'add status: %s(%s, %s)' % (status['text'], maxindex, id) )