void setUserVector(String user, float[] vector) { Preconditions.checkArgument(vector.length == features); X.setVector(user, vector); try (AutoLock al = expectedUserIDsLock.autoWriteLock()) { expectedUserIDs.remove(user); } }
/** * @param id ID to remove feature vector for */ void removeVector(String id) { try (AutoLock al = lock.autoWriteLock()) { vectors.remove(id); recentIDs.remove(id); } }
@Override public void setVector(String id, float[] vector) { try (AutoLock al = lock.autoWriteLock()) { if (vectors.put(id, vector) == null) { // ID was actually new recentIDs.add(id); } } }
@Override public void retainRecentAndIDs(Collection<String> newModelIDs) { try (AutoLock al = lock.autoWriteLock()) { vectors.keySet().removeIf(key -> !newModelIDs.contains(key) && !recentIDs.contains(key)); recentIDs.clear(); } }
void setItemVector(String item, float[] vector) { Preconditions.checkArgument(vector.length == features); Y.setVector(item, vector); try (AutoLock al = expectedItemIDsLock.autoWriteLock()) { expectedItemIDs.remove(item); } // Not clear if it's too inefficient to clear and recompute YtY solver every time any bit // of Y changes, but it's the most correct cachedYTYSolver.setDirty(); }
public void setUserVector(String user, float[] vector) { Preconditions.checkArgument(vector.length == features); X.setVector(user, vector); try (AutoLock al = expectedUserIDsLock.autoWriteLock()) { expectedUserIDs.remove(user); } cachedXTXSolver.setDirty(); }
public void setItemVector(String item, float[] vector) { Preconditions.checkArgument(vector.length == features); Y.setVector(item, vector); try (AutoLock al = expectedItemIDsLock.autoWriteLock()) { expectedItemIDs.remove(item); } cachedYTYSolver.setDirty(); }
void addKnownItems(String user, Collection<String> items) { if (!items.isEmpty()) { MutableSet<String> knownItemsForUser = doGetKnownItems(user); if (knownItemsForUser == null) { try (AutoLock al = knownItemsLock.autoWriteLock()) { // Check again knownItemsForUser = knownItems.computeIfAbsent(user, k -> UnifiedSet.newSet()); } } synchronized (knownItemsForUser) { knownItemsForUser.addAll(items); } } }
public void retainRecentAndItemIDs(Collection<String> items) { Y.retainRecentAndIDs(items); try (AutoLock al = expectedItemIDsLock.autoWriteLock()) { expectedItemIDs.clear(); expectedItemIDs.addAll(items); Y.removeAllIDsFrom(expectedItemIDs); } }
public void retainRecentAndUserIDs(Collection<String> users) { X.retainRecentAndIDs(users); try (AutoLock al = expectedUserIDsLock.autoWriteLock()) { expectedUserIDs.clear(); expectedUserIDs.addAll(users); X.removeAllIDsFrom(expectedUserIDs); } }
/** * Retains only items that are expected to appear * in the upcoming model updates, or, that have arrived recently. This also clears the * recent known items data structure. * * @param items items that should be retained, which are coming in the new model updates */ void retainRecentAndItemIDs(Collection<String> items) { Y.retainRecentAndIDs(items); try (AutoLock al = expectedItemIDsLock.autoWriteLock()) { expectedItemIDs.clear(); expectedItemIDs.addAll(items); Y.removeAllIDsFrom(expectedItemIDs); } }
/** * Retains only users that are expected to appear * in the upcoming model updates, or, that have arrived recently. This also clears the * recent known users data structure. * * @param users users that should be retained, which are coming in the new model updates */ void retainRecentAndUserIDs(Collection<String> users) { X.retainRecentAndIDs(users); try (AutoLock al = expectedUserIDsLock.autoWriteLock()) { expectedUserIDs.clear(); expectedUserIDs.addAll(users); X.removeAllIDsFrom(expectedUserIDs); } }
@Override public void setVector(String item, float[] vector) { int newPartition = partitioner.applyAsInt(item, vector); // Exclusive update to mapping -- careful since other locks are acquired inside here try (AutoLock al = partitionMapLock.autoWriteLock()) { int existingPartition = partitionMap.getIfAbsent(item, Integer.MIN_VALUE); if (existingPartition >= 0 && existingPartition != newPartition) { // Move from one to the other partition, so first remove old entry partitions[existingPartition].removeVector(item); // Note that it's conceivable that a recommendation call sees *no* copy of this // item here in this brief window } // Then regardless put in new partition partitions[newPartition].setVector(item, vector); partitionMap.put(item, newPartition); } }
/** * Like {@link #retainRecentAndUserIDs(Collection)} and {@link #retainRecentAndItemIDs(Collection)} * but affects the known-items data structure. * * @param users users that should be retained, which are coming in the new model updates * @param items items that should be retained, which are coming in the new model updates */ void retainRecentAndKnownItems(Collection<String> users, Collection<String> items) { // Keep all users in the new model, or, that have been added since last model MutableSet<String> recentUserIDs = UnifiedSet.newSet(); X.addAllRecentTo(recentUserIDs); try (AutoLock al = knownItemsLock.autoWriteLock()) { knownItems.keySet().removeIf(key -> !users.contains(key) && !recentUserIDs.contains(key)); } // This will be easier to quickly copy the whole (smallish) set rather than // deal with locks below MutableSet<String> allRecentKnownItems = UnifiedSet.newSet(); Y.addAllRecentTo(allRecentKnownItems); Predicate<String> notKeptOrRecent = value -> !items.contains(value) && !allRecentKnownItems.contains(value); try (AutoLock al = knownItemsLock.autoReadLock()) { knownItems.values().forEach(knownItemsForUser -> { synchronized (knownItemsForUser) { knownItemsForUser.removeIf(notKeptOrRecent); } }); } }
@Test public void testWriteLock() { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); AutoReadWriteLock al = new AutoReadWriteLock(lock); assertFalse(lock.isWriteLocked()); try (AutoLock al2 = al.autoWriteLock()) { assertTrue(lock.isWriteLocked()); } assertFalse(lock.isWriteLocked()); }
/** * @param id ID to remove feature vector for */ void removeVector(String id) { try (AutoLock al = lock.autoWriteLock()) { vectors.remove(id); recentIDs.remove(id); } }
@Override public void retainRecentAndIDs(Collection<String> newModelIDs) { try (AutoLock al = lock.autoWriteLock()) { vectors.keySet().removeIf(key -> !newModelIDs.contains(key) && !recentIDs.contains(key)); recentIDs.clear(); } }
public void setItemVector(String item, float[] vector) { Preconditions.checkArgument(vector.length == features); Y.setVector(item, vector); try (AutoLock al = expectedItemIDsLock.autoWriteLock()) { expectedItemIDs.remove(item); } cachedYTYSolver.setDirty(); }
public void setUserVector(String user, float[] vector) { Preconditions.checkArgument(vector.length == features); X.setVector(user, vector); try (AutoLock al = expectedUserIDsLock.autoWriteLock()) { expectedUserIDs.remove(user); } cachedXTXSolver.setDirty(); }
public void retainRecentAndUserIDs(Collection<String> users) { X.retainRecentAndIDs(users); try (AutoLock al = expectedUserIDsLock.autoWriteLock()) { expectedUserIDs.clear(); expectedUserIDs.addAll(users); X.removeAllIDsFrom(expectedUserIDs); } }