Skip to content

Commit

Permalink
write a line into test.file
Browse files Browse the repository at this point in the history
  • Loading branch information
huifer committed Sep 26, 2023
1 parent 1e5f1cc commit 870d5b4
Show file tree
Hide file tree
Showing 11 changed files with 620 additions and 0 deletions.
62 changes: 62 additions & 0 deletions docs/spring/cs0e5635d0-5c0d-11ee-b258-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.huifer.fzjh.service;

import com.huifer.fzjh.bean.RequestEntity;
import com.huifer.fzjh.bean.ServerWeight;
import com.huifer.fzjh.exception.LoadBalanceException;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 负载均衡算法实现: 平滑加权算轮询法
*/
@Slf4j
public class SmoothnessWeightRandomLoadBalance extends AbstractLoadBalance {
private int count = -1;
private RequestEntity requestEntity;

private List<ServerWeight> serverWeights;

public SmoothnessWeightRandomLoadBalance(RequestEntity requestEntity, List<ServerWeight> serverWeights) {
super(requestEntity, serverWeights);
this.count = serverWeights.size();
this.requestEntity = requestEntity;
this.serverWeights = serverWeights;
}

private static String getServiceIndex(HashMap<Integer, ServerWeight> serverMap) {
ServerWeight maxWeightServer = null;
int allWeight = serverMap.values().stream().mapToInt(ServerWeight::getWeight).sum();
for (Map.Entry<Integer, ServerWeight> item : serverMap.entrySet()) {
ServerWeight currentServer = item.getValue();
if (maxWeightServer == null || currentServer.getCurrentWeight() > maxWeightServer.getCurrentWeight()) {
maxWeightServer = currentServer;
}
}
maxWeightServer.setCurrentWeight(maxWeightServer.getCurrentWeight() - allWeight);
for (Map.Entry<Integer, ServerWeight> item : serverMap.entrySet()) {
ServerWeight currentServer = item.getValue();
currentServer.setCurrentWeight(currentServer.getCurrentWeight() + currentServer.getWeight());
}
return maxWeightServer.getIp() + ":" + maxWeightServer.getPort();
}

@Override
public String loadBalance() {
if (count < 0) {
throw new LoadBalanceException("机器数量不能小于0");
}

HashMap<Integer, ServerWeight> serverHashMap = new HashMap<>();
for (int i = 0; i < serverWeights.size(); i++) {
serverHashMap.put(i, serverWeights.get(i));
}
String hostPost = getServiceIndex(serverHashMap);
log.info("当前请求信息={},负载均衡计算后的机器hostPost={}", requestEntity, hostPost);
return hostPost;
}


}
79 changes: 79 additions & 0 deletions docs/spring/cs0e96e314-5c0d-11ee-b258-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright 2009-2019 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.submitted.ancestor_ref;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.Reader;

import static org.junit.jupiter.api.Assertions.assertEquals;

class AncestorRefTest {
private static SqlSessionFactory sqlSessionFactory;

@BeforeAll
static void setUp() throws Exception {
// create an SqlSessionFactory
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/ancestor_ref/mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}

// populate in-memory database
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/ancestor_ref/CreateDB.sql");
}

@Test
void testCircularAssociation() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
User user = mapper.getUserAssociation(1);
assertEquals("User2", user.getFriend().getName());
}
}

@Test
void testCircularCollection() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
User user = mapper.getUserCollection(2);
assertEquals("User2", user.getFriends().get(0).getName());
assertEquals("User3", user.getFriends().get(1).getName());
}
}

@Test
void testAncestorRef() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
Blog blog = mapper.selectBlog(1);
assertEquals("Author1", blog.getAuthor().getName());
assertEquals("Author2", blog.getCoAuthor().getName());
// author and coauthor should have a ref to blog
assertEquals(blog, blog.getAuthor().getBlog());
assertEquals(blog, blog.getCoAuthor().getBlog());
// reputation should point to it author? or fail but do not point to a random one
assertEquals(blog.getAuthor(), blog.getAuthor().getReputation().getAuthor());
assertEquals(blog.getCoAuthor(), blog.getCoAuthor().getReputation().getAuthor());
}
}
}
73 changes: 73 additions & 0 deletions docs/spring/cs0ed7924c-5c0d-11ee-b258-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright 2009-2019 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.executor.loader;

import javassist.util.proxy.Proxy;
import org.apache.ibatis.domain.blog.Author;
import org.apache.ibatis.executor.ExecutorException;
import org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.*;

class JavassistProxyTest extends SerializableProxyTest {

@BeforeAll
static void createProxyFactory() {
proxyFactory = new JavassistProxyFactory();
}

@Test
void shouldCreateAProxyForAPartiallyLoadedBean() throws Exception {
ResultLoaderMap loader = new ResultLoaderMap();
loader.addLoader("id", null, null);
Object proxy = proxyFactory.createProxy(author, loader, new Configuration(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
Author author2 = (Author) deserialize(serialize((Serializable) proxy));
assertTrue(author2 instanceof Proxy);
}

@Test
void shouldFailCallingAnUnloadedProperty() {
// yes, it must go in uppercase
HashMap<String, ResultLoaderMap.LoadPair> unloadedProperties = new HashMap<>();
unloadedProperties.put("ID", null);
Author author2 = (Author) ((JavassistProxyFactory) proxyFactory).createDeserializationProxy(author, unloadedProperties, new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
Assertions.assertThrows(ExecutorException.class, author2::getId);
}

@Test
void shouldLetCallALoadedProperty() {
Author author2 = (Author) ((JavassistProxyFactory) proxyFactory).createDeserializationProxy(author, new HashMap<>(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
assertEquals(999, author2.getId());
}

@Test
void shouldSerizalizeADeserlizaliedProxy() throws Exception {
Object proxy = ((JavassistProxyFactory) proxyFactory).createDeserializationProxy(author, new HashMap<>(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
Author author2 = (Author) deserialize(serialize((Serializable) proxy));
assertEquals(author, author2);
assertNotEquals(author.getClass(), author2.getClass());
}

}
105 changes: 105 additions & 0 deletions docs/spring/cs0f13ccda-5c0d-11ee-b258-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Copyright 2009-2019 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.cache;

import java.util.concurrent.locks.ReadWriteLock;

/**
* SPI for cache providers.
* <p>
* One instance of cache will be created for each namespace.
* <p>
* The cache implementation must have a constructor that receives the cache id as an String parameter.
* <p>
* MyBatis will pass the namespace as id to the constructor.
*
* <pre>
* public MyCache(final String id) {
* if (id == null) {
* throw new IllegalArgumentException("Cache instances require an ID");
* }
* this.id = id;
* initialize();
* }
* </pre>
* 二级缓存
* @author Clinton Begin
*/

public interface Cache {

/**
* @return The identifier of this cache
*/
String getId();

/**
* 放入数据
* @param key Can be any object but usually it is a {@link CacheKey}
* @param value The result of a select.
*/
void putObject(Object key, Object value);

/**
* 获取数据
* @param key The key
* @return The object stored in the cache.
*/
Object getObject(Object key);

/**
* 移除数据
* As of 3.3.0 this method is only called during a rollback
* for any previous value that was missing in the cache.
* This lets any blocking cache to release the lock that
* may have previously put on the key.
* A blocking cache puts a lock when a value is null
* and releases it when the value is back again.
* This way other threads will wait for the value to be
* available instead of hitting the database.
*
*
* @param key The key
* @return Not used
*/
Object removeObject(Object key);

/**
* 清空数据
* Clears this cache instance.
*/
void clear();

/**
* 有多少缓存数据
* Optional. This method is not called by the core.
*
* @return The number of elements stored in the cache (not its capacity).
*/
int getSize();

/**
* Optional. As of 3.2.6 this method is no longer called by the core.
* <p>
* Any locking needed by the cache must be provided internally by the cache provider.
*
* @return A ReadWriteLock
*/
default ReadWriteLock getReadWriteLock() {
return null;
}

}
Loading

0 comments on commit 870d5b4

Please sign in to comment.