Key Design

If your application does not perform transaction-protected operations on multiple records, then you do not need to give your keys any special consideration just because of slices. In this case, use keys designed exactly as you would if you were not using slices. The internal hashing algorithm will automatically distribute your records evenly across your slices based on the data that the hash finds in your keys.

However, if you do want to perform atomic operations on multiple records, then you do need to give your key design some thought. This is because the slice feature distributes your records across unique sub-environments. A transaction can not cross environments, so you must design your keys so that those records which you want to operation upon in a single transaction are all placed in the same slice. You do this by identifying the portion of your key with is slice-relevant.

For example, suppose your application contains information about corporate personnel. To adequately represent each employee in the corporation, you have the following record types:

  • Contact information, including the employee’s name, organization name, office number, phone number, and email address.

  • A photograph of the employee (stored as an external file).

  • The employee’s public key for digital signatures.

For a non-sliced database, it would be enough to create keys using employee ID and record type. That is, for an employee with ID 6591, your database would contain three keys:

  1. 6591;Contact

  2. 6591;Image

  3. 6591;Signature

However, for a sliced database, there is no guarantee that the three records using these keys would be placed in the same slice. If they are not in the same slice, then put and get operations for all three records cannot be wrapped in a single transaction. Without a single transaction, you cannot operated on these three records atomically.

Therefore, to support a sliced database, you need to identify a portion of the key that will be identical between records that you want to operate upon within a single transaction. In this simple example, you can use the employee’s ID to accomplish this. That is, you must identify the slice-relevant portion of the key so that DB knows what to use when dividing records across slices.

To identify the slice-relevant portion of the key, you define a callback, wich you then set using the DB->set_slice_callback() method.

  1. int
  2. construct_slice_dbt(const DB *db, const DBT *key, DBT *slice)
  3. {
  4. /*
  5. * key is the source key.
  6. *
  7. * slice is the DBT that we use to identify the slice-relevant
  8. * portion the data in key.
  9. *
  10. * This example does not require lifting disjointed portions of
  11. * key to create a slice key, so we can get away with simply
  12. * setting slice->size to highlight the interesting portion of
  13. * key's data.
  14. *
  15. */
  16. slice->data = key->data;
  17. slice->size = 4;
  18. return (0);
  19. }
  20. ...
  21. /*
  22. * Opening the environment is routine so we skip it here for
  23. * the sake of brevity. Assume we have created and opened an
  24. * environment handle, dbenv.
  25. */
  26. DB *dbp = NULL;
  27. DB_TXN *txn = NULL;
  28. u_int32_t open_flags;
  29. int ret;
  30. ret = db_create(&dbp, dbenv, 0);
  31. if (ret != 0) {
  32. /* db_create failed. handle error here */
  33. }
  34. open_flags = DB_CREATE | DB_THREAD | DB_AUTO_COMMIT;
  35. /* Open the database with slice support. */
  36. open_flags |= DB_SLICED;
  37. /* Set the callback before opening the database */
  38. ret = dbp->set_slice_callback(dbp, construct_slice_dbt);
  39. if (ret != 0) {
  40. /* callback set failed. handle error here */
  41. }
  42. ret = dbp->open(dbp,
  43. NULL, /* Txn pointer */
  44. "mydb.db", /* Database file name */
  45. NULL, /* Logical database name */
  46. DB_BTREE, /* Database is using btree access method */
  47. open_flags, /* Open flags */
  48. 0); /* File mode. Using defaults. */
  49. if (ret != 0) {
  50. /* db open failed. handle error here */
  51. }

After that, you can read and write to your database exactly as you would if you were using a non-sliced database. Your threaded workload will scale across cores much better than if you use a non-sliced environment.