Exemplo n.º 1
0
 private static void listUsersBy(final Environment env, final Store store, final String key) {
   env.executeInTransaction(
       new TransactionalExecutable() {
         @Override
         public void execute(@NotNull final Transaction txn) {
           final ArrayByteIterable emailEntry = stringToEntry(key);
           try (Cursor cursor = store.openCursor(txn)) {
             if (cursor.getSearchKey(emailEntry) != null) {
               boolean hasNext = true;
               int i = 0;
               for (; hasNext; ++i, hasNext = cursor.getNext()) {
                 if (!key.equalsIgnoreCase(entryToString(cursor.getKey()))) {
                   break;
                 }
                 System.out.println(entryToString(cursor.getValue()) + ' ' + key);
               }
               System.out.println("Total found: " + i);
             } else if (cursor.getSearchKeyRange(emailEntry) != null) {
               System.out.println(key + " not found, nearest candidates: ");
               boolean hasNext = true;
               for (; hasNext; hasNext = cursor.getNext()) {
                 final String currentKey = entryToString(cursor.getKey());
                 if (!currentKey.startsWith(key)) {
                   break;
                 }
                 System.out.println(entryToString(cursor.getValue()) + ' ' + currentKey);
               }
             } else {
               System.out.println("Nothing found");
             }
           }
         }
       });
 }
Exemplo n.º 2
0
 private static void registerNewUser(
     final Environment env,
     final Store users,
     final Store emails,
     final String username,
     final String email) {
   env.executeInTransaction(
       new TransactionalExecutable() {
         @Override
         public void execute(@NotNull final Transaction txn) {
           final ArrayByteIterable usernameEntry = stringToEntry(username);
           final ArrayByteIterable emailEntry = stringToEntry(email);
           final boolean exists;
           try (Cursor usersCursor = users.openCursor(txn)) {
             exists = usersCursor.getSearchBoth(usernameEntry, emailEntry);
             if (!exists) {
               users.put(txn, usernameEntry, emailEntry);
             }
           }
           if (!exists) {
             try (Cursor emailsCursor = emails.openCursor(txn)) {
               if (emailsCursor.getSearchBoth(emailEntry, usernameEntry)) {
                 throw new ExodusException("It can't be: users & emails are inconsistent!");
               }
               emails.put(txn, emailEntry, usernameEntry);
             }
           }
           System.out.println(
               (exists ? "User is already registered: " : "New user registered: ")
                   + username
                   + " "
                   + email);
         }
       });
 }