/**
   * Change the session id. Note that this will change the session id for all contexts for which the
   * session id is in use.
   *
   * @see org.eclipse.jetty.nosql.NoSqlSessionManager#update(org.eclipse.jetty.nosql.NoSqlSession,
   *     java.lang.String, java.lang.String)
   */
  @Override
  protected void update(NoSqlSession session, String newClusterId, String newNodeId)
      throws Exception {
    __log.debug(
        "KVStoreSessionManager:update session {} to {}", session.getClusterId(), newClusterId);
    SortedMap<Key, ValueVersion> old_keyvalues =
        _kvstorehandler.multiGet(
            Key.createKey(Arrays.asList(__storeprefix, session.getClusterId())),
            null,
            Depth.PARENT_AND_DESCENDANTS);

    if (old_keyvalues != null && old_keyvalues.size() > 0) {
      this.kvstore_object_ops.clear();
      ValueVersion expiry_old_val = null;
      for (Entry<Key, ValueVersion> old_entry : old_keyvalues.entrySet()) {
        List<String> major_keypart = old_entry.getKey().getMajorPath();
        List<String> minor_keypart = old_entry.getKey().getMinorPath();
        if (old_entry
            .getKey()
            .toString()
            .startsWith(__storeprefix + "/" + session.getClusterId() + "/" + __EXPIRY)) {
          // got expiry value, save it for linkback
          expiry_old_val = old_entry.getValue();
        }
        this.kvstore_object_ops.add(
            this.kvstore_opfactory.createPut(
                Key.createKey(major_keypart, minor_keypart), old_entry.getValue().getValue()));

        major_keypart.set(1, newClusterId);
      }
      if (this.kvstore_object_ops.size() > 0) {
        _kvstorehandler.execute(this.kvstore_object_ops);
        this.kvstore_object_ops.clear();
        _kvstorehandler.multiDelete(
            Key.createKey(Arrays.asList(__storeprefix, session.getClusterId())),
            null,
            Depth.PARENT_AND_DESCENDANTS);
      }
      if (expiry_old_val != null) {
        _kvstorehandler.put(
            Key.createKey(
                Arrays.asList(
                    __expirydailyindexprefix,
                    new String(expiry_old_val.getValue().getValue()),
                    "UTF-8"),
                Arrays.asList(newClusterId)),
            Value.EMPTY_VALUE);
        _kvstorehandler.delete(
            Key.createKey(
                Arrays.asList(
                    __expirydailyindexprefix,
                    new String(expiry_old_val.getValue().getValue()),
                    "UTF-8"),
                Arrays.asList(session.getClusterId())));
      }
    }
  }
Exemple #2
0
  private void _storeFunctions(String what, boolean isPrintOutput) {

    _prepareKey();
    if (myKey == null) return;

    // +++TODO
    // errorMessage = _checkStore()

    // put & Co. return a Version, get returns a ValueVersion,
    // delete returns a boolean.
    try {
      if (what.equals("delete")) {
        boolean isSuccess = store.delete(myKey);
        if (isPrintOutput) System.out.println(isSuccess);
      } else if (what.equals("multiDelete")) {
        int isSuccess = store.multiDelete(myKey, null, null);
        if (isPrintOutput) System.out.println(isSuccess);
      } else if (what.equals("get")) {
        // store.get returns Null or the valueVersion.
        ValueVersion valueVersion = store.get(myKey);
        if (valueVersion != null) {
          // toString () from getValue ().getValue () does not work.
          String myValueString = new String(valueVersion.getValue().getValue());
          if (isPrintOutput) System.out.println(myValueString);
        } else {
          // If this is a test, assuming a proper key was put,
          // it did not pass it.
          if (isPrintOutput) {
            System.out.println("Key " + keysString + " could not be found.");
          }
          return;
        }
      } else if (what.equals("put")) {
        // put, putIfAbsent, putIfPresent.
        myValue = Value.createValue(valueString.getBytes());

        // Reflection is giving me too many troubles. So forget it.
        store.put(myKey, myValue);
      } else if (what.equals("putIfAbsent")) {
        myValue = Value.createValue(valueString.getBytes());
        store.putIfAbsent(myKey, myValue);
      } else if (what.equals("putIfPresent")) {
        myValue = Value.createValue(valueString.getBytes());
        store.putIfPresent(myKey, myValue);
      }
      positiveMessage = what + ": passed";
    } catch (Exception ex) {
      errorMessage = "ERROR in " + what + ": " + ex.toString();
      _printErrorMessage("False");
      return;
    }
    return;
  }
 public void testSetup() {
   EntityManager em = createEntityManager();
   // First clear old data from store.
   beginTransaction(em);
   KVStore store =
       ((OracleNoSQLConnection) em.unwrap(javax.resource.cci.Connection.class)).getStore();
   Iterator<Key> iterator = store.storeKeysIterator(Direction.UNORDERED, 0);
   while (iterator.hasNext()) {
     store.multiDelete(iterator.next(), null, null);
   }
   commitTransaction(em);
   beginTransaction(em);
   try {
     for (int index = 0; index < 10; index++) {
       existingOrder = new Order();
       existingOrder.id = this.nextId++;
       existingOrder.orderedBy = "ACME";
       existingOrder.address = new Address();
       existingOrder.address.city = "Ottawa";
       existingOrder.address.addressee = "Bob Jones";
       existingOrder.address.state = "CA";
       existingOrder.address.country = "Mexico";
       existingOrder.address.zipCode = "12345";
       LineItem line = new LineItem();
       line.itemName = "stuff";
       line.itemPrice = new BigDecimal("10.99");
       line.lineNumber = 1;
       line.quantity = 100;
       existingOrder.lineItems.add(line);
       line = new LineItem();
       line.itemName = "more stuff";
       line.itemPrice = new BigDecimal("20.99");
       line.lineNumber = 2;
       line.quantity = 50;
       existingOrder.lineItems.add(line);
       existingOrder.comments.add("priority order");
       existingOrder.comments.add("next day");
       em.persist(existingOrder);
     }
     commitTransaction(em);
   } finally {
     closeEntityManagerAndTransaction(em);
   }
   clearCache();
 }
  /**
   * Remove the per-context sub document for this session id.
   *
   * @see org.eclipse.jetty.nosql.NoSqlSessionManager#remove(org.eclipse.jetty.nosql.NoSqlSession)
   */
  @Override
  protected boolean remove(NoSqlSession session) {
    __log.debug(
        "KVStoreSessionManager:remove:session {} for context {}",
        session.getClusterId(),
        _contextId);

    /*
     * Check if the session exists and if it does remove the context
     * associated with this session
     */
    int deleted_count =
        _kvstorehandler.multiDelete(
            Key.createKey(
                Arrays.asList(__storeprefix, session.getClusterId()),
                Arrays.asList(__CONTEXT, _contextId)),
            null,
            Depth.PARENT_AND_DESCENDANTS);
    if (deleted_count > 0) return true;
    else return false;
  }
  /* ------------------------------------------------------------ */
  @Override
  protected synchronized Object save(
      NoSqlSession session, Object version, boolean activateAfterSave) {
    try {
      __log.debug("KVStoreSessionManager:save session {}", session.getClusterId());
      session.willPassivate();
      this.kvstore_object_ops.clear();
      if (this.kvstore_opfactory == null)
        this.kvstore_opfactory = this._kvstorehandler.getOperationFactory();
      /*
              // Form query for upsert
              BasicDBObject key = new BasicDBObject(__ID,session.getClusterId());

              // Form updates
              BasicDBObject update = new BasicDBObject();

              BasicDBObject sets = new BasicDBObject();
              BasicDBObject unsets = new BasicDBObject();
      */
      boolean upsert = false;
      boolean need_to_purge_attributes = false;
      // handle valid or invalid
      String new_valid = null;
      String new_expiry = null;
      String new_accessed = Base62Converter.longToLexiSortableBase62(session.getAccessed());
      if (session.isValid()) {
        new_valid = "1";
        long expiry =
            (session.getMaxInactiveInterval() > 0
                ? (session.getAccessed() + (1000L * getMaxInactiveInterval()))
                : 0);
        new_expiry = Base62Converter.longToLexiSortableBase62(expiry);
        __log.debug(
            "KVStoreSessionManager: calculated expiry {} for session {}", expiry, session.getId());
        // handle new or existing
        if (version == null) {
          __log.debug("New session");
          upsert = true;
          version = new Long(1);
          kvstore_object_ops.add(
              this.kvstore_opfactory.createPut(
                  Key.createKey(
                      Arrays.asList(__storeprefix, session.getClusterId()),
                      Arrays.asList(__CREATED)),
                  Value.createValue(
                      Base62Converter.fromBase10(session.getCreationTime()).getBytes("UTF-8"))));
          kvstore_object_ops.add(
              this.kvstore_opfactory.createPut(
                  Key.createKey(
                      Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__VALID)),
                  Value.createValue(new_valid.getBytes("UTF-8"))));
          kvstore_object_ops.add(
              this.kvstore_opfactory.createPut(
                  Key.createKey(
                      Arrays.asList(__storeprefix, session.getClusterId()),
                      Arrays.asList(__CONTEXT, _contextId, __VERSION)),
                  Value.createValue(
                      Base62Converter.fromBase10(((Long) version).longValue()).getBytes("UTF-8"))));
          kvstore_object_ops.add(
              this.kvstore_opfactory.createPut(
                  Key.createKey(
                      Arrays.asList(__storeprefix, session.getClusterId()),
                      Arrays.asList(__MAX_IDLE)),
                  Value.createValue(
                      Base62Converter.fromBase10(getMaxInactiveInterval()).getBytes("UTF-8"))));
          kvstore_object_ops.add(
              this.kvstore_opfactory.createPut(
                  Key.createKey(
                      Arrays.asList(__storeprefix, session.getClusterId()),
                      Arrays.asList(__EXPIRY)),
                  Value.createValue(new_expiry.getBytes("UTF-8"))));
        } else {
          version = new Long(((Number) version).longValue() + 1);
          kvstore_object_ops.add(
              this.kvstore_opfactory.createPut(
                  Key.createKey(
                      Arrays.asList(__storeprefix, session.getClusterId()),
                      Arrays.asList(__CONTEXT, _contextId, __VERSION)),
                  Value.createValue(
                      Base62Converter.fromBase10(((Long) version).longValue()).getBytes("UTF-8"))));

          // if max idle time and/or expiry is smaller for this context, then choose that for the
          // whole session doc
          ValueVersion vval_max_idle =
              this._kvstorehandler.get(
                  Key.createKey(
                      Arrays.asList(__storeprefix, session.getClusterId()),
                      Arrays.asList(__MAX_IDLE)));
          ValueVersion vval_expiry =
              this._kvstorehandler.get(
                  Key.createKey(
                      Arrays.asList(__storeprefix, session.getClusterId()),
                      Arrays.asList(__EXPIRY)));
          if (vval_max_idle != null && vval_max_idle.getValue().getValue() != null) {
            long currentMaxIdle =
                Base62Converter.toBase10(new String(vval_max_idle.getValue().getValue(), "UTF-8"));
            if (getMaxInactiveInterval() > 0 && getMaxInactiveInterval() < currentMaxIdle)
              kvstore_object_ops.add(
                  this.kvstore_opfactory.createPut(
                      Key.createKey(
                          Arrays.asList(__storeprefix, session.getClusterId()),
                          Arrays.asList(__MAX_IDLE)),
                      Value.createValue(
                          Base62Converter.fromBase10(getMaxInactiveInterval()).getBytes("UTF-8"))));
          }

          if (vval_expiry != null && vval_expiry.getValue().getValue() != null) {
            long currentExpiry =
                Base62Converter.toBase10(new String(vval_expiry.getValue().getValue(), "UTF-8"));
            if (expiry > 0 && expiry != currentExpiry) {
              new_expiry = Base62Converter.longToLexiSortableBase62(expiry);
              kvstore_object_ops.add(
                  this.kvstore_opfactory.createPut(
                      Key.createKey(
                          Arrays.asList(__storeprefix, session.getClusterId()),
                          Arrays.asList(__EXPIRY)),
                      Value.createValue(new_expiry.getBytes("UTF-8"))));
            }
          }
        }
        kvstore_object_ops.add(
            this.kvstore_opfactory.createPut(
                Key.createKey(
                    Arrays.asList(__storeprefix, session.getClusterId()),
                    Arrays.asList(__ACCESSED)),
                Value.createValue(new_accessed.getBytes("UTF-8"))));

        __log.debug(
            "session.isDirty=" + session.isDirty() + ";getSavePeriod=" + this.getSavePeriod());

        Set<String> names = session.takeDirty();
        if (isSaveAllAttributes() || upsert) {
          names.addAll(session.getNames()); // note dirty may include removed names
        }

        for (String name : names) {
          Object value = session.getAttribute(name);
          if (value == null)
            kvstore_object_ops.add(
                this.kvstore_opfactory.createDelete(
                    Key.createKey(
                        Arrays.asList(__storeprefix, session.getClusterId()),
                        Arrays.asList(__CONTEXT, _contextId, name))));
          else
            kvstore_object_ops.add(
                this.kvstore_opfactory.createPut(
                    Key.createKey(
                        Arrays.asList(__storeprefix, session.getClusterId()),
                        Arrays.asList(__CONTEXT, _contextId, name)),
                    Value.createValue(encodeValue(value))));
        }
      } else {
        need_to_purge_attributes = true;
        new_valid = "0";
        kvstore_object_ops.add(
            this.kvstore_opfactory.createPut(
                Key.createKey(
                    Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__VALID)),
                Value.createValue(new_valid.getBytes("UTF-8"))));
        kvstore_object_ops.add(
            this.kvstore_opfactory.createPut(
                Key.createKey(
                    Arrays.asList(__storeprefix, session.getClusterId()),
                    Arrays.asList(__INVALIDATED)),
                Value.createValue(
                    Base62Converter.fromBase10(System.currentTimeMillis()).getBytes("UTF-8"))));
        // unsets.put(getContextKey(),1);
      }

      // update indexes
      // delete old entries, if present
      ValueVersion old_valid =
          _kvstorehandler.get(
              Key.createKey(
                  Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__VALID)));
      ValueVersion old_accessed =
          _kvstorehandler.get(
              Key.createKey(
                  Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__ACCESSED)));
      ValueVersion old_expiry =
          _kvstorehandler.get(
              Key.createKey(
                  Arrays.asList(__storeprefix, session.getClusterId()), Arrays.asList(__EXPIRY)));

      if (old_valid != null
          && old_valid.getValue() != null
          && old_valid.getValue().getValue() != null
          && old_accessed != null
          && old_accessed.getValue() != null
          && old_accessed.getValue().getValue() != null) {
        __log.debug(
            "delete purgeindex entry for old_valid="
                + new String(old_valid.getValue().getValue(), "UTF-8")
                + ";old_accessed="
                + new String(old_accessed.getValue().getValue(), "UTF-8"));
        _kvstorehandler.delete(
            Key.createKey(
                Arrays.asList(
                    __purgeindexprefix,
                    new String(old_valid.getValue().getValue(), "UTF-8"),
                    new String(old_accessed.getValue().getValue(), "UTF-8")),
                Arrays.asList(session.getClusterId())));
      } else
        __log.debug(
            "nothing to delete from purge index, either accessed or valid keys are missing");
      __log.debug(
          "new_valid=" + new_valid + ";new_accessed=" + new_accessed + ";new_expiry=" + new_expiry);

      _kvstorehandler.put(
          Key.createKey(
              Arrays.asList(__purgeindexprefix, new_valid, new_accessed),
              Arrays.asList(session.getClusterId())),
          Value.EMPTY_VALUE);
      if (new_expiry != null) {
        if (old_expiry != null
            && old_expiry.getValue() != null
            && old_expiry.getValue().getValue() != null) {
          _kvstorehandler.delete(
              Key.createKey(
                  Arrays.asList(
                      __expirydailyindexprefix,
                      new String(old_expiry.getValue().getValue(), "UTF-8")),
                  Arrays.asList(session.getClusterId())));
        } else __log.debug("nothing to delete from expiry index, old expiry entry key is missing");

        _kvstorehandler.put(
            Key.createKey(
                Arrays.asList(__expirydailyindexprefix, new_expiry),
                Arrays.asList(session.getClusterId())),
            Value.EMPTY_VALUE);
      }
      // Done with indexes, do the upsert
      if (!kvstore_object_ops.isEmpty()) {
        _kvstorehandler.execute(kvstore_object_ops);
        kvstore_object_ops.clear();
      }
      if (need_to_purge_attributes) {
        /*
        __log.debug("before need to purge attrubutes");
           Iterator<Key> it = _kvstorehandler.multiGetKeysIterator(Direction.FORWARD, 10,
           		Key.createKey(Arrays.asList(
        				__storeprefix,
        				session.getClusterId())),
           		null,//new KeyRange("minorpart1",true,"minorpart1",true),
           		Depth.PARENT_AND_DESCENDANTS);
           while (it.hasNext()) {
           	Key key = it.next();
           	__log.debug("key="+key.toString());
           }
           */
        _kvstorehandler.multiDelete(
            Key.createKey(Arrays.asList(__storeprefix, session.getClusterId())),
            new KeyRange(__CONTEXT, true, __CONTEXT, true),
            Depth.PARENT_AND_DESCENDANTS);
        /*
        __log.debug("after need to purge attrubutes");
           Iterator<Key> it = _kvstorehandler.multiGetKeysIterator(Direction.FORWARD, 10,
           		Key.createKey(Arrays.asList(
        				__storeprefix,
        				session.getClusterId())),
           		null,//new KeyRange("minorpart1",true,"minorpart1",true),
           		Depth.PARENT_AND_DESCENDANTS);
           while (it.hasNext()) {
           	Key key = it.next();
           	__log.debug("key="+key.toString());
           }
           */

      }

      if (__log.isDebugEnabled()) __log.debug("KVStoreSessionManager:save:db.sessions.updated");
      if (activateAfterSave) session.didActivate();
      return version;
    } catch (Exception e) {
      LOG.warn(e);
    }
    return null;
  }