-ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
- const char *rec_buf,
- size_t rec_len,
- const char *recid_buf, size_t recid_len,
- int action)
- /* 1 = insert. Fail it already exists */
- /* 2 = replace. Fail it does not exist */
- /* 3 = delete. Fail if does not exist */
- /* 4 = update. Insert/replace */
-{
- ZEBRA_RES res;
- SYSNO sysno = 0;
- char *rinfo = 0;
- char recid_z[256];
- ASSERTZH;
- assert(action>0 && action <=4);
- assert(rec_buf);
-
- yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
- zh->errCode = 0;
-
- if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
- return ZEBRA_FAIL;
-
- memcpy (recid_z, recid_buf, recid_len);
- recid_z[recid_len] = 0;
-
- if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
- return ZEBRA_FAIL;
-
- rinfo = dict_lookup (zh->reg->matchDict, recid_z);
- if (rinfo)
- {
- if (action == 1) /* fail if insert */
- {
- zebra_end_trans(zh);
- return ZEBRA_FAIL;
- }
-
- memcpy (&sysno, rinfo+1, sizeof(sysno));
- }
- else
- {
- if (action == 2 || action == 3) /* fail if delete or update */
- {
- zebra_end_trans(zh);
- return ZEBRA_FAIL;
- }
- action = 1; /* make it an insert (if it's an update).. */
- }
- res = buffer_extract_record (zh, rec_buf, rec_len,
- action == 3 ? 1 : 0 /* delete flag */,
- 0, /* test mode */
- 0, /* recordType */
- &sysno,
- 0, /* match */
- 0, /* fname */
- 0, /* force update */
- 1 /* allow update */
- );
- if (action == 1)
- {
- dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
- }
- else if (action == 3)
- {
- dict_delete (zh->reg->matchDict, recid_z);
- }
- zebra_end_trans(zh);
- return res;
-}
-