1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2004,2008 Oracle. All rights reserved.
*
* $Id: ToManyExample.java,v 1.10 2008/05/27 15:30:31 mark Exp $
*/
package je;
import java.io.File;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.bind.tuple.StringBinding;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.ForeignKeyDeleteAction;
import com.sleepycat.je.ForeignMultiKeyNullifier;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.SecondaryConfig;
import com.sleepycat.je.SecondaryCursor;
import com.sleepycat.je.SecondaryDatabase;
import com.sleepycat.je.SecondaryMultiKeyCreator;
import com.sleepycat.je.Transaction;
/**
* An example of using many-to-many and one-to-many secondary indices.
*/
public class ToManyExample {
private Environment env;
private Database catalogDb;
private Database animalDb;
private Database personDb;
private SecondaryDatabase personByEmail;
private SecondaryDatabase personByAnimal;
private EntryBinding<String> keyBinding;
private EntryBinding<Person> personBinding;
private EntryBinding<Animal> animalBinding;
/**
* Runs the example program, given a single "-h HOME" argument.
*/
public static void main(String[] args) {
if (args.length != 2 || !"-h".equals(args[0])) {
System.out.println("Usage: java " +
ToManyExample.class.getName() +
" -h ENV_HOME");
System.exit(1);
}
String homeDir = args[1];
try {
ToManyExample example = new ToManyExample(homeDir);
example.exec();
example.close();
} catch (DatabaseException e) {
e.printStackTrace();
}
}
/**
* Opens the environment and all databases.
*/
private ToManyExample(String homeDir) throws DatabaseException {
/* Open the environment. */
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);
envConfig.setTransactional(true);
env = new Environment(new File(homeDir), envConfig);
/* Open/create all databases in a transaction. */
Transaction txn = env.beginTransaction(null, null);
try {
/* A standard (no duplicates) database config. */
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
dbConfig.setTransactional(true);
/* The catalog is used for the serial binding. */
catalogDb = env.openDatabase(txn, "catalog", dbConfig);
StoredClassCatalog catalog = new StoredClassCatalog(catalogDb);
personBinding = new SerialBinding(catalog, null);
animalBinding = new SerialBinding(catalog, null);
keyBinding = new StringBinding();
/* Open the person and animal primary DBs. */
animalDb = env.openDatabase(txn, "animal", dbConfig);
personDb = env.openDatabase(txn, "person", dbConfig);
/*
* A standard secondary config; duplicates, key creators and key
* nullifiers are specified below.
*/
SecondaryConfig secConfig = new SecondaryConfig();
secConfig.setAllowCreate(true);
secConfig.setTransactional(true);
/*
* Open the secondary database for personByEmail. This is a
* one-to-many index because duplicates are not configured.
*/
secConfig.setSortedDuplicates(false);
secConfig.setMultiKeyCreator(new EmailKeyCreator());
personByEmail = env.openSecondaryDatabase(txn, "personByEmail",
personDb, secConfig);
/*
* Open the secondary database for personByAnimal. This is a
* many-to-many index because duplicates are configured. Foreign
* key constraints are specified to ensure that all animal keys
* exist in the animal database.
*/
secConfig.setSortedDuplicates(true);
secConfig.setMultiKeyCreator(new AnimalKeyCreator());
secConfig.setForeignMultiKeyNullifier(new AnimalKeyNullifier());
secConfig.setForeignKeyDatabase(animalDb);
secConfig.setForeignKeyDeleteAction(ForeignKeyDeleteAction.NULLIFY);
personByAnimal = env.openSecondaryDatabase(txn, "personByAnimal",
personDb, secConfig);
txn.commit();
} catch (DatabaseException e) {
txn.abort();
throw e;
} catch (RuntimeException e) {
txn.abort();
throw e;
}
}
/**
* Closes all databases and the environment.
*/
private void close() throws DatabaseException {
if (personByEmail != null) {
personByEmail.close();
}
if (personByAnimal != null) {
personByAnimal.close();
}
if (catalogDb != null) {
catalogDb.close();
}
if (personDb != null) {
personDb.close();
}
if (animalDb != null) {
animalDb.close();
}
if (env != null) {
env.close();
}
}
/**
* Adds, updates, prints and deletes Person records with many-to-many and
* one-to-many secondary indices.
*/
private void exec()
throws DatabaseException {
System.out.println
("\nInsert some animals.");
Animal dogs = insertAndPrintAnimal("dogs", true);
Animal fish = insertAndPrintAnimal("fish", false);
Animal horses = insertAndPrintAnimal("horses", true);
Animal donkeys = insertAndPrintAnimal("donkeys", true);
System.out.println
("\nInsert a new empty person.");
Person kathy = new Person();
kathy.name = "Kathy";
putPerson(kathy);
printPerson("Kathy");
System.out.println
("\nAdd favorites/addresses and update the record.");
kathy.favoriteAnimals.add(horses.name);
kathy.favoriteAnimals.add(dogs.name);
kathy.favoriteAnimals.add(fish.name);
kathy.emailAddresses.add("kathy@kathy.com");
kathy.emailAddresses.add("kathy@yahoo.com");
putPerson(kathy);
printPerson("Kathy");
System.out.println
("\nChange favorites and addresses and update the person record.");
kathy.favoriteAnimals.remove(fish.name);
kathy.favoriteAnimals.add(donkeys.name);
kathy.emailAddresses.add("kathy@gmail.com");
kathy.emailAddresses.remove("kathy@yahoo.com");
putPerson(kathy);
printPerson("Kathy");
System.out.println
("\nInsert another person with some of the same favorites.");
Person mark = new Person();
mark.favoriteAnimals.add(dogs.name);
mark.favoriteAnimals.add(horses.name);
mark.name = "Mark";
putPerson(mark);
printPerson("Mark");
System.out.println
("\nPrint by favorite animal index.");
printByIndex(personByAnimal);
System.out.println
("\nPrint by email address index.");
printByIndex(personByEmail);
System.out.println
("\nDelete 'dogs' and print again by favorite animal index.");
deleteAnimal(dogs.name);
printPerson("Kathy");
printPerson("Mark");
printByIndex(personByAnimal);
System.out.println
("\nDelete both records and print again (should print nothing).");
deletePerson("Kathy");
deletePerson("Mark");
printPerson("Kathy");
printPerson("Mark");
printByIndex(personByAnimal);
printByIndex(personByEmail);
}
/**
* Inserts an animal record and prints it. Uses auto-commit.
*/
private Animal insertAndPrintAnimal(String name, boolean furry)
throws DatabaseException {
Animal animal = new Animal();
animal.name = name;
animal.furry = furry;
DatabaseEntry key = new DatabaseEntry();
keyBinding.objectToEntry(name, key);
DatabaseEntry data = new DatabaseEntry();
animalBinding.objectToEntry(animal, data);
OperationStatus status = animalDb.putNoOverwrite(null, key, data);
if (status == OperationStatus.SUCCESS) {
System.out.println(animal);
} else {
System.out.println("Animal was not inserted: " + name +
" (" + status + ')');
}
return animal;
}
/**
* Deletes an animal. Uses auto-commit.
*/
private boolean deleteAnimal(String name)
throws DatabaseException {
DatabaseEntry key = new DatabaseEntry();
keyBinding.objectToEntry(name, key);
OperationStatus status = animalDb.delete(null, key);
return status == OperationStatus.SUCCESS;
}
/**
* Gets a person by name and prints it.
*/
private void printPerson(String name)
throws DatabaseException {
DatabaseEntry key = new DatabaseEntry();
keyBinding.objectToEntry(name, key);
DatabaseEntry data = new DatabaseEntry();
OperationStatus status = personDb.get(null, key, data, null);
if (status == OperationStatus.SUCCESS) {
Person person = personBinding.entryToObject(data);
person.name = keyBinding.entryToObject(key);
System.out.println(person);
} else {
System.out.println("Person not found: " + name);
}
}
/**
* Prints all person records by a given secondary index.
*/
private void printByIndex(SecondaryDatabase secDb)
throws DatabaseException {
DatabaseEntry secKey = new DatabaseEntry();
DatabaseEntry priKey = new DatabaseEntry();
DatabaseEntry priData = new DatabaseEntry();
SecondaryCursor cursor = secDb.openSecondaryCursor(null, null);
try {
while (cursor.getNext(secKey, priKey, priData, null) ==
OperationStatus.SUCCESS) {
Person person = personBinding.entryToObject(priData);
person.name = keyBinding.entryToObject(priKey);
System.out.println("Index key [" +
keyBinding.entryToObject(secKey) +
"] maps to primary key [" +
person.name + ']');
}
} finally {
cursor.close();
}
}
/**
* Inserts or updates a person. Uses auto-commit.
*/
private void putPerson(Person person)
throws DatabaseException {
DatabaseEntry key = new DatabaseEntry();
keyBinding.objectToEntry(person.name, key);
DatabaseEntry data = new DatabaseEntry();
personBinding.objectToEntry(person, data);
personDb.put(null, key, data);
}
/**
* Deletes a person. Uses auto-commit.
*/
private boolean deletePerson(String name)
throws DatabaseException {
DatabaseEntry key = new DatabaseEntry();
keyBinding.objectToEntry(name, key);
OperationStatus status = personDb.delete(null, key);
return status == OperationStatus.SUCCESS;
}
/**
* A person object.
*/
@SuppressWarnings("serial")
private static class Person implements Serializable {
/** The primary key. */
private transient String name;
/** A many-to-many set of keys. */
private Set<String> favoriteAnimals = new HashSet<String>();
/** A one-to-many set of keys. */
private Set<String> emailAddresses = new HashSet<String>();
public String toString() {
return "Person {" +
"\n Name: " + name +
"\n FavoriteAnimals: " + favoriteAnimals +
"\n EmailAddresses: " + emailAddresses +
"\n}";
}
}
/**
* An animal object.
*/
@SuppressWarnings("serial")
private static class Animal implements Serializable {
/** The primary key. */
private transient String name;
/** A non-indexed property. */
private boolean furry;
public String toString() {
return "Animal {" +
"\n Name: " + name +
"\n Furry: " + furry +
"\n}";
}
}
/**
* Returns the set of email addresses for a person. This is an example
* of a multi-key creator for a to-many index.
*/
private class EmailKeyCreator implements SecondaryMultiKeyCreator {
public void createSecondaryKeys(SecondaryDatabase secondary,
DatabaseEntry primaryKey,
DatabaseEntry primaryData,
Set<DatabaseEntry> results)
throws DatabaseException {
Person person = personBinding.entryToObject(primaryData);
copyKeysToEntries(person.emailAddresses, results);
}
}
/**
* Returns the set of favorite animals for a person. This is an example
* of a multi-key creator for a to-many index.
*/
private class AnimalKeyCreator implements SecondaryMultiKeyCreator {
public void createSecondaryKeys(SecondaryDatabase secondary,
DatabaseEntry primaryKey,
DatabaseEntry primaryData,
Set<DatabaseEntry> results)
throws DatabaseException {
Person person = personBinding.entryToObject(primaryData);
copyKeysToEntries(person.favoriteAnimals, results);
}
}
/**
* A utility method to copy a set of keys (Strings) into a set of
* DatabaseEntry objects.
*/
private void copyKeysToEntries(Set<String> keys,
Set<DatabaseEntry> entries) {
for (Iterator<String> i = keys.iterator(); i.hasNext();) {
DatabaseEntry entry = new DatabaseEntry();
keyBinding.objectToEntry(i.next(), entry);
entries.add(entry);
}
}
/**
* Removes a given key from the set of favorite animals for a person. This
* is an example of a nullifier for a to-many index. The nullifier is
* called when an animal record is deleted because we configured this
* secondary with ForeignKeyDeleteAction.NULLIFY.
*/
private class AnimalKeyNullifier implements ForeignMultiKeyNullifier {
public boolean nullifyForeignKey(SecondaryDatabase secondary,
DatabaseEntry primaryKey,
DatabaseEntry primaryData,
DatabaseEntry secKey)
throws DatabaseException {
Person person = personBinding.entryToObject(primaryData);
String key = keyBinding.entryToObject(secKey);
if (person.favoriteAnimals.remove(key)) {
personBinding.objectToEntry(person, primaryData);
return true;
} else {
return false;
}
}
}
}
|