Search in sources :

Example 11 with CouchbaseEntryManager

use of io.jans.orm.couchbase.impl.CouchbaseEntryManager in project jans by JanssenProject.

the class CouchbaseConcurentSessionUpdateSample method main.

public static void main(String[] args) throws InterruptedException {
    // Prepare sample connection details
    CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
    final CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
    try {
        // Create Couchbase entry manager
        String sessionId = "xyzcyzxy-a41a-45ad-8a83-61485dbad561";
        final String sessionDn = "uniqueIdentifier=" + sessionId + ",ou=session,o=jans";
        final String userDn = "inum=@!E8F2.853B.1E7B.ACE2!0001!39A4.C163!0000!A8F2.DE1E.D7FB,ou=people,o=jans";
        final SimpleSessionState simpleSessionState = new SimpleSessionState();
        simpleSessionState.setDn(sessionDn);
        simpleSessionState.setId(sessionId);
        simpleSessionState.setLastUsedAt(new Date());
        couchbaseEntryManager.persist(simpleSessionState);
        System.out.println("Persisted");
        int threadCount = 500;
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount, daemonThreadFactory());
        for (int i = 0; i < threadCount; i++) {
            final int count = i;
            executorService.execute(new Runnable() {

                @Override
                public void run() {
                    final SimpleSessionState simpleSessionStateFromCouchbase = couchbaseEntryManager.find(SimpleSessionState.class, sessionDn);
                    String beforeUserDn = simpleSessionStateFromCouchbase.getUserDn();
                    String randomUserDn = count % 2 == 0 ? userDn : "";
                    try {
                        simpleSessionStateFromCouchbase.setUserDn(randomUserDn);
                        simpleSessionStateFromCouchbase.setLastUsedAt(new Date());
                        couchbaseEntryManager.merge(simpleSessionStateFromCouchbase);
                        System.out.println("Merged thread: " + count + ", userDn: " + randomUserDn + ", before userDn: " + beforeUserDn);
                    } catch (Throwable e) {
                        System.out.println("ERROR !!!, thread: " + count + ", userDn: " + randomUserDn + ", before userDn: " + beforeUserDn + ", error:" + e.getMessage());
                    // e.printStackTrace();
                    }
                }
            });
        }
        Thread.sleep(5000L);
    } finally {
        couchbaseEntryManager.destroy();
    }
}
Also used : SimpleSessionState(io.jans.orm.couchbase.model.SimpleSessionState) ExecutorService(java.util.concurrent.ExecutorService) CouchbaseEntryManager(io.jans.orm.couchbase.impl.CouchbaseEntryManager) Date(java.util.Date)

Example 12 with CouchbaseEntryManager

use of io.jans.orm.couchbase.impl.CouchbaseEntryManager in project jans by JanssenProject.

the class CouchbaseCustomObjectAttributesSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
    // Create SQL entry manager
    CouchbaseEntryManager sqlEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
    // Add dummy user
    SimpleUser newUser = new SimpleUser();
    newUser.setDn(String.format("inum=%s,ou=people,o=jans", System.currentTimeMillis()));
    newUser.setUserId("sample_user_" + System.currentTimeMillis());
    newUser.setUserPassword("test");
    newUser.getCustomAttributes().add(new CustomObjectAttribute("address", Arrays.asList("London", "Texas", "Kiev")));
    newUser.getCustomAttributes().add(new CustomObjectAttribute("jansGuid", "test_value"));
    newUser.getCustomAttributes().add(new CustomObjectAttribute("birthdate", new Date()));
    newUser.getCustomAttributes().add(new CustomObjectAttribute("jansActive", false));
    // Require cusom attribute in table with age: INT type
    newUser.getCustomAttributes().add(new CustomObjectAttribute("scimCustomThird", 18));
    newUser.setUserRole(UserRole.ADMIN);
    newUser.setMemberOf(Arrays.asList("group_1", "group_2", "group_3"));
    sqlEntryManager.persist(newUser);
    LOG.info("Added User '{}' with uid '{}' and key '{}'", newUser, newUser.getUserId(), newUser.getDn());
    // Find added dummy user
    SimpleUser foundUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
    LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser, foundUser.getUserId(), foundUser.getDn());
    LOG.info("Custom attributes '{}'", foundUser.getCustomAttributes());
    for (CustomObjectAttribute customAttribute : foundUser.getCustomAttributes()) {
        if (customAttribute.getValue() instanceof Date) {
            LOG.info("Found date custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
        } else if (customAttribute.getValue() instanceof Integer) {
            LOG.info("Found integer custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
        } else if (customAttribute.getValue() instanceof Boolean) {
            LOG.info("Found boolean custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
        } else if (customAttribute.getValues().size() > 1) {
            LOG.info("Found list custom attribute '{}' with value '{}', multiValued: {}", customAttribute.getName(), customAttribute.getValues(), customAttribute.isMultiValued());
        }
    }
    for (Iterator<CustomObjectAttribute> it = foundUser.getCustomAttributes().iterator(); it.hasNext(); ) {
        CustomObjectAttribute attr = (CustomObjectAttribute) it.next();
        if (StringHelper.equalsIgnoreCase(attr.getName(), "jansGuid")) {
            attr.setValue("");
            break;
        }
    }
    sqlEntryManager.merge(foundUser);
    // Find updated dummy user
    SimpleUser foundUser2 = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
    LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser2, foundUser2.getUserId(), foundUser2.getDn());
    LOG.info("Custom attributes after merge '{}'", foundUser2.getCustomAttributes());
    for (CustomObjectAttribute customAttribute : foundUser2.getCustomAttributes()) {
        if (customAttribute.getValue() instanceof Date) {
            LOG.info("Found date custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
        } else if (customAttribute.getValue() instanceof Integer) {
            LOG.info("Found integer custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
        } else if (customAttribute.getValue() instanceof Boolean) {
            LOG.info("Found boolean custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
        } else if (customAttribute.getValues().size() > 1) {
            LOG.info("Found list custom attribute '{}' with value '{}', multiValued: {}", customAttribute.getName(), customAttribute.getValues(), customAttribute.isMultiValued());
        }
    }
    // Find added dummy user by numeric attribute
    Filter filter = Filter.createGreaterOrEqualFilter("scimCustomThird", 16);
    List<SimpleUser> foundUsers = sqlEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filter);
    if (foundUsers.size() > 0) {
        foundUser = foundUsers.get(0);
        LOG.info("Found User '{}' by filter '{}' with uid '{}' and key '{}'", foundUser, filter, foundUser, foundUser);
    } else {
        LOG.error("Can't find User by filter '{}'", filter);
    }
}
Also used : CustomObjectAttribute(io.jans.orm.model.base.CustomObjectAttribute) SimpleUser(io.jans.orm.couchbase.model.SimpleUser) Filter(io.jans.orm.search.filter.Filter) CouchbaseEntryManager(io.jans.orm.couchbase.impl.CouchbaseEntryManager) Date(java.util.Date)

Example 13 with CouchbaseEntryManager

use of io.jans.orm.couchbase.impl.CouchbaseEntryManager in project jans by JanssenProject.

the class CouchbaseUpateMissingEntrySample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    CouchbaseEntryManagerSample sqlSampleEntryManager = new CouchbaseEntryManagerSample();
    // Create SQL entry manager
    CouchbaseEntryManager sqlEntryManager = sqlSampleEntryManager.createCouchbaseEntryManager();
    String sessionId = UUID.randomUUID().toString();
    final String sessionDn = "uniqueIdentifier=" + sessionId + ",ou=session,o=jans";
    final SimpleSessionState simpleSessionState = new SimpleSessionState();
    simpleSessionState.setDn(sessionDn);
    simpleSessionState.setId(sessionId);
    simpleSessionState.setLastUsedAt(new Date());
    try {
        sqlEntryManager.merge(simpleSessionState);
        System.out.println("Updated");
    } catch (EntryPersistenceException ex) {
        LOG.info("Failed to update, root case exception: {}", ex.getCause().getClass(), ex);
    }
}
Also used : SimpleSessionState(io.jans.orm.couchbase.model.SimpleSessionState) CouchbaseEntryManager(io.jans.orm.couchbase.impl.CouchbaseEntryManager) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) Date(java.util.Date)

Example 14 with CouchbaseEntryManager

use of io.jans.orm.couchbase.impl.CouchbaseEntryManager in project jans by JanssenProject.

the class CouchbaseUserSearchSample method main.

public static void main(String[] args) throws InterruptedException {
    // Prepare sample connection details
    CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
    final CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
    int countUsers = 1000000;
    int threadCount = 200;
    int threadIterationCount = 10;
    long totalStart = System.currentTimeMillis();
    try {
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount, daemonThreadFactory());
        for (int i = 0; i < threadCount; i++) {
            activeCount.incrementAndGet();
            final int count = i;
            executorService.execute(new Runnable() {

                @Override
                public void run() {
                    long start = System.currentTimeMillis();
                    for (int j = 0; j < threadIterationCount; j++) {
                        long userUid = Math.round(Math.random() * countUsers);
                        String uid = String.format("user%06d", userUid);
                        try {
                            Filter filter = Filter.createEqualityFilter(Filter.createLowercaseFilter("uid"), StringHelper.toLowerCase(uid));
                            // Filter filter = Filter.createEqualityFilter("uid", uid);
                            List<SimpleUser> foundUsers = couchbaseEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filter);
                            if (foundUsers.size() > 0) {
                                successResult.incrementAndGet();
                            } else {
                                LOG.warn("Failed to find user: " + uid);
                                failedResult.incrementAndGet();
                            }
                        } catch (Throwable e) {
                            errorResult.incrementAndGet();
                            System.out.println("ERROR !!!, thread: " + count + ", uid: " + uid + ", error:" + e.getMessage());
                            e.printStackTrace();
                        }
                    }
                    long end = System.currentTimeMillis();
                    long duration = end - start;
                    LOG.info("Thread " + count + " execution time: " + duration);
                    totalTime.addAndGet(duration);
                    activeCount.decrementAndGet();
                }
            });
        }
        while (activeCount.get() != 0) {
            Thread.sleep(1000L);
        }
    } finally {
        couchbaseEntryManager.destroy();
    }
    long totalEnd = System.currentTimeMillis();
    long duration = totalEnd - totalStart;
    LOG.info("Total execution time: " + duration + " after execution: " + (threadCount * threadIterationCount));
    System.out.println(String.format("successResult: '%d', failedResult: '%d', errorResult: '%d'", successResult.get(), failedResult.get(), errorResult.get()));
}
Also used : SimpleUser(io.jans.orm.couchbase.model.SimpleUser) Filter(io.jans.orm.search.filter.Filter) ExecutorService(java.util.concurrent.ExecutorService) CouchbaseEntryManager(io.jans.orm.couchbase.impl.CouchbaseEntryManager) List(java.util.List)

Example 15 with CouchbaseEntryManager

use of io.jans.orm.couchbase.impl.CouchbaseEntryManager in project jans by JanssenProject.

the class ManualCouchbaseEntryManagerTest method createCouchbaseEntryManager.

public static CouchbaseEntryManager createCouchbaseEntryManager() throws IOException {
    CouchbaseEntryManagerFactory couchbaseEntryManagerFactory = new CouchbaseEntryManagerFactory();
    couchbaseEntryManagerFactory.create();
    CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerFactory.createEntryManager(loadProperties());
    System.out.println("Created CouchbaseEntryManager: " + couchbaseEntryManager);
    return couchbaseEntryManager;
}
Also used : CouchbaseEntryManagerFactory(io.jans.orm.couchbase.impl.CouchbaseEntryManagerFactory) CouchbaseEntryManager(io.jans.orm.couchbase.impl.CouchbaseEntryManager)

Aggregations

CouchbaseEntryManager (io.jans.orm.couchbase.impl.CouchbaseEntryManager)16 Filter (io.jans.orm.search.filter.Filter)8 SimpleUser (io.jans.orm.couchbase.model.SimpleUser)5 Date (java.util.Date)5 CouchbaseEntryManagerFactory (io.jans.orm.couchbase.impl.CouchbaseEntryManagerFactory)3 CustomAttribute (io.jans.orm.model.base.CustomAttribute)3 CustomObjectAttribute (io.jans.orm.model.base.CustomObjectAttribute)3 SimpleClient (io.jans.orm.couchbase.model.SimpleClient)2 SimpleSession (io.jans.orm.couchbase.model.SimpleSession)2 SimpleSessionState (io.jans.orm.couchbase.model.SimpleSessionState)2 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 Test (org.testng.annotations.Test)2 Lookup (com.couchbase.client.core.message.kv.subdoc.multi.Lookup)1 Bucket (com.couchbase.client.java.Bucket)1 JsonDocument (com.couchbase.client.java.document.JsonDocument)1 SubdocOptionsBuilder (com.couchbase.client.java.subdoc.SubdocOptionsBuilder)1 SimpleAttribute (io.jans.orm.couchbase.model.SimpleAttribute)1 SimpleCustomStringUser (io.jans.orm.couchbase.model.SimpleCustomStringUser)1