Changeset: d9723aeb5161 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=d9723aeb5161
Modified Files:
        gdk/gdk_orderidx.c
Branch: Jul2017
Log Message:

Simplify code: just use BATsort.


diffs (103 lines):

diff --git a/gdk/gdk_orderidx.c b/gdk/gdk_orderidx.c
--- a/gdk/gdk_orderidx.c
+++ b/gdk/gdk_orderidx.c
@@ -162,72 +162,41 @@ persistOIDX(BAT *b)
 gdk_return
 BATorderidx(BAT *b, int stable)
 {
-       Heap *m;
-       oid *restrict mv;
-       oid seq;
-       BUN p, q;
-       BAT *bn = NULL;
-
        if (BATcheckorderidx(b))
                return GDK_SUCCEED;
-       MT_lock_set(&GDKhashLock(b->batCacheid));
-       if (b->torderidx) {
-               MT_lock_unset(&GDKhashLock(b->batCacheid));
-               return GDK_SUCCEED;
-       }
-       if ((m = createOIDXheap(b, stable)) == NULL) {
-               MT_lock_unset(&GDKhashLock(b->batCacheid));
-               return GDK_FAIL;
-       }
-
-       mv = (oid *) m->base + ORDERIDXOFF;
-
-       seq = b->hseqbase;
-       for (p = 0, q = BATcount(b); p < q; p++)
-               mv[p] = seq + p;
-
        if (!BATtdense(b)) {
-               /* we need to sort a copy of the column so as not to
-                * change the original */
-               bn = COLcopy(b, b->ttype, TRUE, TRANSIENT);
-               if (bn == NULL) {
-                       HEAPfree(m, 1);
-                       GDKfree(m);
-                       MT_lock_unset(&GDKhashLock(b->batCacheid));
+               BAT *on;
+               if (BATsort(NULL, &on, NULL, b, NULL, NULL, 0, stable) != 
GDK_SUCCEED)
                        return GDK_FAIL;
-               }
-               if (stable) {
-                       if (GDKssort(Tloc(bn, 0), mv,
-                                    bn->tvheap ? bn->tvheap->base : NULL,
-                                    BATcount(bn), Tsize(bn), SIZEOF_OID,
-                                    bn->ttype) != GDK_SUCCEED) {
-                               HEAPfree(m, 1);
-                               GDKfree(m);
-                               MT_lock_unset(&GDKhashLock(b->batCacheid));
-                               BBPunfix(bn->batCacheid);
-                               return GDK_FAIL;
+               assert(BATcount(b) == BATcount(on));
+               if (on->tdense) {
+                       /* if the order bat is dense, the input was
+                        * sorted and we don't need an order index */
+                       assert(b->tnosorted == 0);
+                       if (!b->tsorted) {
+                               b->tsorted = 1;
+                               b->tnosorted = 0;
+                               b->batDirtydesc = 1;
                        }
                } else {
-                       GDKqsort(Tloc(bn, 0), mv,
-                                bn->tvheap ? bn->tvheap->base : NULL,
-                                BATcount(bn), Tsize(bn), SIZEOF_OID,
-                                bn->ttype);
+                       /* BATsort quite possibly already created the
+                        * order index, but just to be sure... */
+                       MT_lock_set(&GDKhashLock(b->batCacheid));
+                       if (b->torderidx == NULL) {
+                               Heap *m;
+                               if ((m = createOIDXheap(b, stable)) == NULL) {
+                                       
MT_lock_unset(&GDKhashLock(b->batCacheid));
+                                       return GDK_FAIL;
+                               }
+                               memcpy((oid *) m->base + ORDERIDXOFF, Tloc(on, 
0), BATcount(on) * sizeof(oid));
+                               b->torderidx = m;
+                               b->batDirtydesc = 1;
+                               persistOIDX(b);
+                       }
+                       MT_lock_unset(&GDKhashLock(b->batCacheid));
                }
-               /* we must unfix after releasing the lock since we
-                * might get deadlock otherwise (we're holding a lock
-                * based on b->batCacheid; unfix tries to get a lock
-                * based on bn->batCacheid, usually but (crucially)
-                * not always a different lock) */
+               BBPunfix(on->batCacheid);
        }
-
-       b->torderidx = m;
-       b->batDirtydesc = TRUE;
-       persistOIDX(b);
-       MT_lock_unset(&GDKhashLock(b->batCacheid));
-
-       if (bn)
-               BBPunfix(bn->batCacheid);
-
        return GDK_SUCCEED;
 }
 
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to