From 58478d75cc3151c3f51aa8dde6330611b4d70564 Mon Sep 17 00:00:00 2001
From: Yoni Fogel <yoni@tokutek.com>
Date: Mon, 3 Dec 2007 02:18:48 +0000
Subject: [PATCH] Prevent DB->put from working on secondary indexes (unless
 called internally with DB_NO_ASSOCIATE flag set)

Modified test_db_secondary to verify this behavior.
Closes #106

git-svn-id: file:///svn/tokudb@888 c7de825b-a66e-492c-adef-691d508d4ae1
---
 src/tests/test_db_secondary.c | 5 ++++-
 src/ydb.c                     | 9 +++++----
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/tests/test_db_secondary.c b/src/tests/test_db_secondary.c
index a25267024cc..edee979ebe7 100644
--- a/src/tests/test_db_secondary.c
+++ b/src/tests/test_db_secondary.c
@@ -98,9 +98,12 @@ void insert_test() {
     data.size = sizeof(s);
     //Set up secondary key
     r = getname(sdbp, &key, &data, &skey);              CKERR(r);
-        
+
+    //Insert into primary.        
     r = dbp->put(dbp, null_txn, &key, &data, 0);        CKERR(r);
     
+    //Make certain we fail to insert into secondary.
+    r = sdbp->put(sdbp, null_txn, &key, &data, 0);      assert(r == EINVAL);
 
     /* Try to get it from primary. */
     r = dbp->get(dbp, null_txn, &key, &testdata, 0);    CKERR(r);
diff --git a/src/ydb.c b/src/ydb.c
index 6ecd463c28f..868dcf4afa9 100644
--- a/src/ydb.c
+++ b/src/ydb.c
@@ -793,7 +793,7 @@ static int do_associated_inserts (DB_TXN *txn, DBT *key, DBT *data, DB *secondar
 	return EINVAL; // We aren't ready for this
     }
 #endif
-    r = secondary->put(secondary, txn, &idx, key, 0);
+    r = secondary->put(secondary, txn, &idx, key, DB_NO_ASSOCIATE);
     if (idx.flags & DB_DBT_APPMALLOC) {
 	free(idx.data);
     }
@@ -803,9 +803,10 @@ static int do_associated_inserts (DB_TXN *txn, DBT *key, DBT *data, DB *secondar
 static int toku_db_put(DB * db, DB_TXN * txn, DBT * key, DBT * data, u_int32_t flags) {
     int r;
 
-    if (flags != 0)
-        return EINVAL;
-
+    if (db->i->primary == 0 && flags != 0) return EINVAL;
+    //Cannot put directly into a secondary.
+    if (db->i->primary != 0 && flags != DB_NO_ASSOCIATE) return EINVAL;
+        
     unsigned int brtflags;
     r = toku_brt_get_flags(db->i->brt, &brtflags); assert(r == 0);
     unsigned int nodesize;
-- 
2.30.9