1 8 9 package com.sleepycat.persist; 10 11 import java.util.Map ; 12 import java.util.SortedMap ; 13 14 import com.sleepycat.bind.EntityBinding; 15 import com.sleepycat.bind.EntryBinding; 16 import com.sleepycat.collections.StoredSortedMap; 17 import com.sleepycat.je.Cursor; 18 import com.sleepycat.je.Database; 19 import com.sleepycat.je.DatabaseEntry; 20 import com.sleepycat.je.DatabaseException; 21 import com.sleepycat.je.Environment; 22 import com.sleepycat.je.LockMode; 23 import com.sleepycat.je.OperationStatus; 24 import com.sleepycat.je.Transaction; 25 import com.sleepycat.persist.impl.PersistEntityBinding; 26 import com.sleepycat.persist.impl.PersistKeyAssigner; 27 import com.sleepycat.persist.model.Entity; 28 import com.sleepycat.persist.model.PrimaryKey; 29 30 200 public class PrimaryIndex<PK,E> extends BasicIndex<PK,E> { 201 202 private Class <E> entityClass; 203 private EntityBinding entityBinding; 204 private SortedMap <PK,E> map; 205 private PersistKeyAssigner keyAssigner; 206 207 232 public PrimaryIndex(Database database, 233 Class <PK> keyClass, 234 EntryBinding keyBinding, 235 Class <E> entityClass, 236 EntityBinding entityBinding) 237 throws DatabaseException { 238 239 super(database, keyClass, keyBinding, 240 new EntityValueAdapter(entityClass, entityBinding, false)); 241 242 this.entityClass = entityClass; 243 this.entityBinding = entityBinding; 244 245 if (entityBinding instanceof PersistEntityBinding) { 246 keyAssigner = 247 ((PersistEntityBinding) entityBinding).getKeyAssigner(); 248 } 249 } 250 251 256 public Database getDatabase() { 257 return db; 258 } 259 260 265 public Class <PK> getKeyClass() { 266 return keyClass; 267 } 268 269 274 public EntryBinding getKeyBinding() { 275 return keyBinding; 276 } 277 278 283 public Class <E> getEntityClass() { 284 return entityClass; 285 } 286 287 292 public EntityBinding getEntityBinding() { 293 return entityBinding; 294 } 295 296 311 public E put(E entity) 312 throws DatabaseException { 313 314 return put(null, entity); 315 } 316 317 333 public E put(Transaction txn, E entity) 334 throws DatabaseException { 335 336 DatabaseEntry keyEntry = new DatabaseEntry(); 337 DatabaseEntry dataEntry = new DatabaseEntry(); 338 assignKey(entity, keyEntry); 339 340 boolean autoCommit = false; 341 Environment env = db.getEnvironment(); 342 if (transactional && 343 txn == null && 344 env.getThreadTransaction() == null) { 345 txn = env.beginTransaction(null, null); 346 autoCommit = true; 347 } 348 349 boolean failed = true; 350 Cursor cursor = db.openCursor(txn, null); 351 try { 352 while (true) { 353 OperationStatus status = 354 cursor.getSearchKey(keyEntry, dataEntry, LockMode.RMW); 355 if (status == OperationStatus.SUCCESS) { 356 E existing = 357 (E) entityBinding.entryToObject(keyEntry, dataEntry); 358 entityBinding.objectToData(entity, dataEntry); 359 cursor.put(keyEntry, dataEntry); 360 failed = false; 361 return existing; 362 } else { 363 entityBinding.objectToData(entity, dataEntry); 364 status = cursor.putNoOverwrite(keyEntry, dataEntry); 365 if (status != OperationStatus.KEYEXIST) { 366 failed = false; 367 return null; 368 } 369 } 370 } 371 } finally { 372 cursor.close(); 373 if (autoCommit) { 374 if (failed) { 375 txn.abort(); 376 } else { 377 txn.commit(); 378 } 379 } 380 } 381 } 382 383 397 public void putNoReturn(E entity) 398 throws DatabaseException { 399 400 putNoReturn(null, entity); 401 } 402 403 418 public void putNoReturn(Transaction txn, E entity) 419 throws DatabaseException { 420 421 DatabaseEntry keyEntry = new DatabaseEntry(); 422 DatabaseEntry dataEntry = new DatabaseEntry(); 423 assignKey(entity, keyEntry); 424 entityBinding.objectToData(entity, dataEntry); 425 426 db.put(txn, keyEntry, dataEntry); 427 } 428 429 444 public boolean putNoOverwrite(E entity) 445 throws DatabaseException { 446 447 return putNoOverwrite(null, entity); 448 } 449 450 466 public boolean putNoOverwrite(Transaction txn, E entity) 467 throws DatabaseException { 468 469 DatabaseEntry keyEntry = new DatabaseEntry(); 470 DatabaseEntry dataEntry = new DatabaseEntry(); 471 assignKey(entity, keyEntry); 472 entityBinding.objectToData(entity, dataEntry); 473 474 OperationStatus status = db.putNoOverwrite(txn, keyEntry, dataEntry); 475 476 return (status == OperationStatus.SUCCESS); 477 } 478 479 483 private void assignKey(E entity, DatabaseEntry keyEntry) 484 throws DatabaseException { 485 486 if (keyAssigner != null) { 487 if (!keyAssigner.assignPrimaryKey(entity, keyEntry)) { 488 entityBinding.objectToKey(entity, keyEntry); 489 } 490 } else { 491 entityBinding.objectToKey(entity, keyEntry); 492 } 493 } 494 495 499 500 public E get(PK key) 501 throws DatabaseException { 502 503 return get(null, key, null); 504 } 505 506 public E get(Transaction txn, PK key, LockMode lockMode) 507 throws DatabaseException { 508 509 DatabaseEntry keyEntry = new DatabaseEntry(); 510 DatabaseEntry dataEntry = new DatabaseEntry(); 511 keyBinding.objectToEntry(key, keyEntry); 512 513 OperationStatus status = db.get(txn, keyEntry, dataEntry, lockMode); 514 515 if (status == OperationStatus.SUCCESS) { 516 return (E) entityBinding.entryToObject(keyEntry, dataEntry); 517 } else { 518 return null; 519 } 520 } 521 522 public Map <PK,E> map() { 523 return sortedMap(); 524 } 525 526 public synchronized SortedMap <PK,E> sortedMap() { 527 if (map == null) { 528 map = new StoredSortedMap(db, keyBinding, entityBinding, true); 529 } 530 return map; 531 } 532 } 533 | Popular Tags |