[ASTERIXDB-2730][STO] Safely Update Flush Pointer in GVBC

- user model changes: no
- storage format changes: no
- interface changes: no

Details:
- Ensure at least one primary index is registered before
  updating the flush pointer in GlobalVirtualBufferCache.
  Otherwise, reset the pointer to zero.

Change-Id: Ib0b3c9c7cfcf7b6a3c2f404f42d1d4802cf12b28
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/6285
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Luo Chen <cluo8@uci.edu>
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/context/GlobalVirtualBufferCache.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/context/GlobalVirtualBufferCache.java
index 6e97d64..5177b25 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/context/GlobalVirtualBufferCache.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/context/GlobalVirtualBufferCache.java
@@ -123,7 +123,9 @@
             int pos = primaryIndexes.indexOf(index);
             if (pos >= 0) {
                 primaryIndexes.remove(index);
-                if (flushPtr > pos) {
+                if (primaryIndexes.isEmpty()) {
+                    flushPtr = 0;
+                } else if (flushPtr > pos) {
                     // If the removed index is before flushPtr, we should decrement flushPtr by 1 so that
                     // it still points to the same index.
                     flushPtr = (flushPtr - 1) % primaryIndexes.size();