├── .github └── workflows │ └── maven.yml ├── .gitignore ├── LICENSE ├── README.md ├── doc ├── build.sh ├── header │ ├── java.txt │ ├── shell.txt │ └── xml.txt └── todo.md ├── pom.xml ├── release.md ├── security.md └── src ├── main └── java │ └── com │ └── geektcp │ └── common │ └── mosheh │ ├── buffer │ └── BufferBuilder.java │ ├── cache │ ├── AbstractCache.java │ ├── AbstractCacheTree.java │ ├── Cache.java │ ├── Store.java │ ├── common │ │ ├── LruCache.java │ │ ├── SimpleCache.java │ │ └── WeakCache.java │ └── tiny │ │ ├── CacheBuilder.java │ │ ├── listener │ │ ├── TinyListener.java │ │ ├── TinyReferenceEntry.java │ │ ├── TinyRemovalCause.java │ │ ├── TinyRemovalNotification.java │ │ └── ValueReference.java │ │ ├── loader │ │ ├── CacheLoader.java │ │ └── TinyLoader.java │ │ ├── loading │ │ ├── InvalidateCache.java │ │ ├── LoadingCache.java │ │ └── implement │ │ │ └── TinyLoadingCache.java │ │ ├── local │ │ └── TinyCache.java │ │ └── storage │ │ ├── AbstractCacheFactory.java │ │ ├── CacheFactory.java │ │ ├── cache │ │ ├── CacheTree.java │ │ └── TreeCacheFactory.java │ │ └── key │ │ ├── AbstractKey.java │ │ ├── LocalKey.java │ │ └── NodeKey.java │ ├── checker │ └── Preconditions.java │ ├── collection │ ├── ArrayStack.java │ ├── ListQueue.java │ ├── ListStack.java │ ├── Lists.java │ ├── ThyQueue.java │ └── ThyStack.java │ ├── concurrent │ ├── cas │ │ └── UnSafeBuilder.java │ ├── lock │ │ ├── Lock.java │ │ ├── LockBuilder.java │ │ ├── SynchronizedLock.java │ │ └── ThyLock.java │ ├── single │ │ └── ThreadBuilder.java │ └── thread │ │ ├── able │ │ ├── ThyCallable.java │ │ └── ThyRunnable.java │ │ ├── executor │ │ ├── ThyExecutor.java │ │ ├── TinyExecutorBuilder.java │ │ └── service │ │ │ ├── TinyCompletionService.java │ │ │ ├── TinyExecutor.java │ │ │ ├── TinyExecutorService.java │ │ │ ├── TinyRejectedHandler.java │ │ │ ├── TinyScheduledService.java │ │ │ └── impl │ │ │ ├── TinyAbstractService.java │ │ │ ├── delegated │ │ │ ├── TinyDelegatedExecutor.java │ │ │ ├── TinyFinalizeExecutor.java │ │ │ └── TinyScheduledExecutor.java │ │ │ └── extend │ │ │ ├── TinyDelayedWorkQueue.java │ │ │ ├── TinyScheduledThreadPoolExecutor.java │ │ │ └── TinyThreadPoolExecutor.java │ │ └── factory │ │ ├── ThyThread.java │ │ └── ThyThreadFactory.java │ ├── constant │ ├── CommonStatus.java │ └── Status.java │ ├── exception │ ├── BaseException.java │ ├── ExecutorException.java │ ├── MoshehException.java │ └── StatusException.java │ ├── generator │ └── IdGenerator.java │ ├── parser │ ├── Expression.java │ ├── GqlParser.java │ ├── Parser.java │ ├── SearchParser.java │ ├── SqlParser.java │ └── SyntaxParser.java │ ├── socket │ ├── buffer │ │ └── DataOutputBuffer.java │ ├── builder │ │ ├── MoshehClientBuilder.java │ │ └── MoshehServerBuilder.java │ ├── comparator │ │ ├── BinaryComparable.java │ │ ├── RawComparator.java │ │ ├── Writable.java │ │ ├── WritableComparable.java │ │ └── WritableComparator.java │ ├── constant │ │ └── Status.java │ ├── server │ │ ├── MoshehServer.java │ │ ├── call │ │ │ └── MoshehCall.java │ │ ├── config │ │ │ └── MoshehConfig.java │ │ ├── connection │ │ │ └── MoshehConnection.java │ │ ├── listener │ │ │ └── MoshehListener.java │ │ ├── responder │ │ │ └── MoshehResponder.java │ │ └── worker │ │ │ └── MoshehWorker.java │ ├── text │ │ ├── ConnectionBody.java │ │ ├── ConnectionHeader.java │ │ └── Text.java │ └── util │ │ ├── StringUtils.java │ │ └── WritableUtils.java │ ├── sort │ ├── Bubble.java │ ├── Heap.java │ ├── HeapSort.java │ ├── Insertion.java │ ├── MergeSort.java │ ├── MergeSortDown.java │ ├── MergeSortUp.java │ ├── QuickSort.java │ ├── Selection.java │ ├── Shell.java │ └── Sort.java │ ├── system │ ├── Sys.java │ ├── ThyClass.java │ ├── ThyCommand.java │ ├── ThyEncrypt.java │ ├── ThyEnv.java │ ├── ThyFileReader.java │ ├── ThyFileSystem.java │ ├── ThyHost.java │ ├── ThyProperties.java │ ├── ThyResource.java │ ├── ThyRuntime.java │ ├── ThyStream.java │ └── ThyString.java │ ├── tree │ ├── AVLTree.java │ ├── AbstractTree.java │ ├── BaseBinaryTree.java │ ├── BinarySearchTree.java │ ├── Btree.java │ ├── ListNodeTree.java │ ├── RedBlackTree.java │ ├── node │ │ ├── AbstractBinaryNode.java │ │ ├── AbstractListNode.java │ │ └── Node.java │ └── st │ │ ├── BalancedSearchTree.java │ │ ├── BinarySearchOrderedST.java │ │ ├── LinearProbingHashST.java │ │ ├── ListUnorderedST.java │ │ ├── OrderedST.java │ │ ├── RedBlackBalancedSearchTree.java │ │ └── UnorderedST.java │ └── util │ ├── Base64Utils.java │ ├── BeanMapper.java │ ├── CollectionUtils.java │ ├── DateUtils.java │ ├── DownUtils.java │ ├── GetterUtils.java │ ├── HttpUtils.java │ ├── JsonUtils.java │ ├── ObjectUtils.java │ ├── SeparatorUtils.java │ ├── Sortable.java │ └── SslUtils.java └── test └── java └── com └── geektcp └── common └── mosheh ├── cache ├── CacheBuilderTest.java └── LruCacheTest.java ├── concurrent └── thread │ ├── ScheduleTest.java │ └── executor │ └── TinyExecutorServiceTest.java ├── generator └── GeneratorTest.java ├── socket ├── SocketServerBuilderTest.java ├── nio │ ├── NIOClient.java │ ├── NIOServer.java │ └── NIOServerSimple.java └── simple │ ├── ClientTest.java │ └── ServerTest.java ├── system ├── SysTest.java ├── ThyCommandTest.java ├── ThyEncryptTest.java ├── ThyFileReaderTest.java ├── ThyHostTest.java ├── ThyResourceTest.java ├── ThyStreamTest.java └── ThyStringTest.java ├── tree ├── ListNodeTreeTest.java └── UserDirVo.java └── util ├── Base64UtilsTest.java ├── CollectionUtilsTest.java ├── DownUtilsTest.java ├── FileUtilsTest.java ├── ThyEncryptTest.java └── UserSitePo.java /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven 3 | 4 | # This workflow uses actions that are not certified by GitHub. 5 | # They are provided by a third-party and are governed by 6 | # separate terms of service, privacy policy, and support 7 | # documentation. 8 | 9 | name: Java CI with Maven 10 | 11 | on: 12 | push: 13 | branches: [ "main" ] 14 | pull_request: 15 | branches: [ "main" ] 16 | 17 | jobs: 18 | build: 19 | 20 | runs-on: ubuntu-latest 21 | 22 | steps: 23 | - uses: actions/checkout@v3 24 | - name: Set up JDK 8 25 | uses: actions/setup-java@v3 26 | with: 27 | java-version: '8' 28 | distribution: 'temurin' 29 | cache: maven 30 | - name: Build with Maven 31 | run: mvn clean compile 32 | 33 | # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive 34 | #- name: Update dependency graph 35 | # uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | /*/idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | 27 | ### maven version backup 28 | *.versionsBackup 29 | 30 | ### log4j2 31 | logs 32 | *.log 33 | 34 | ### pom 35 | dependency-reduced-pom.xml 36 | 37 | # dependencies 38 | node_modules 39 | /.pnp 40 | .pnp.js 41 | /blog-web/build 42 | 43 | .idea -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | mosheh 2 | ========== 3 | [![Actions Status](https://github.com/geektcp/mosheh/actions/workflows/maven.yml/badge.svg)](https://github.com/geektcp/mosheh/actions) 4 | [![Security Score](https://snyk.io/test/github/geektcp/mosheh/badge.svg)](https://snyk.io/test/github/geektcp/mosheh) 5 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.geektcp.common/mosheh/badge.svg#)](https://mvnrepository.com/artifact/com.geektcp.common/mosheh) 6 | [![OpenSSF Best Practices](https://bestpractices.coreinfrastructure.org/projects/7038/badge)](https://bestpractices.coreinfrastructure.org/projects/7038) 7 | [![Java Doc](https://img.shields.io/badge/javadoc-6.0.5-brightgreen.svg)](https://javadoc.io/doc/com.geektcp.common/mosheh/latest/index.html) 8 | 9 | ### describe 10 | ``` 11 | mosheh is a common library for Java, which release in maven repository: 12 | https://mvnrepository.com/artifact/com.geektcp.common/mosheh 13 | 14 | this is a tiny project. we can use it to develop big project. 15 | 16 | Furthermore, mosheh offers a convenient API for build many cache, collection, executor. 17 | ``` 18 | 19 | ### maven dependency 20 | ``` 21 | 22 | 23 | com.geektcp.common 24 | mosheh 25 | 1.0.3 26 | 27 | ``` 28 | 29 | 30 | ### example 31 | ```java 32 | package xxx; 33 | 34 | /** 35 | * @author Geektcp 2021/9/23 14:30 36 | */ 37 | @Slf4j 38 | @Service 39 | public class RoomInfoServerImpl extends JpaBase implements RoomInfoServer { 40 | 41 | private LoadingCache> tinyCache = CacheBuilder.newBuilder() 42 | .refreshAfterWrite(7, TimeUnit.SECONDS) 43 | .expireAfterWrite(5, TimeUnit.SECONDS) 44 | .build(new TinyLoader>() { 45 | @Override 46 | public List load(Object key) { 47 | return null; 48 | } 49 | }); 50 | 51 | public List queryRoomInfo(RoomInfoQo qo) { 52 | log.info("qo: {}", JSON.toJSONString(qo)); 53 | 54 | // get result from tiny cache 55 | List cacheResult = tinyCache.get(qo); 56 | if(Objects.nonNull(cacheResult)){ 57 | return cacheResult; 58 | } 59 | 60 | // here is your code, for example: select mysql with JPA or Mybatis 61 | String title = qo.getTitle(); 62 | String id = qo.getId(); 63 | String author = qo.getAuthor(); 64 | 65 | QRoomInfoPo qRoomInfoPo = QRoomInfoPo.roomInfoPo; 66 | JPQLQuery jpqlQuery = jpa.select(qRoomInfoPo).from(qRoomInfoPo); 67 | if(StringUtils.isNotEmpty(title)){ 68 | jpqlQuery.where(qRoomInfoPo.title.like(JQL.likeWrap(title))); 69 | } 70 | if(StringUtils.isNotEmpty(id)){ 71 | jpqlQuery.where(qRoomInfoPo.id.eq(id)); 72 | } 73 | if(StringUtils.isNotEmpty(author)){ 74 | jpqlQuery.where(qRoomInfoPo.author.like(JQL.likeWrap(author))); 75 | } 76 | List poList = jpqlQuery.orderBy(qRoomInfoPo.updateBy.asc()).fetch(); 77 | 78 | if (CollectionUtils.isEmpty(poList)) { 79 | return null; 80 | } 81 | List voList = poList.stream().map((RoomInfoPo po) -> { 82 | RoomInfoVo vo = new RoomInfoVo(); 83 | BeanMapper.map(po, vo); 84 | return vo; 85 | }).collect(Collectors.toList()); 86 | 87 | // add result to tiny cache 88 | tinyCache.put(qo, voList); 89 | 90 | return voList; 91 | } 92 | 93 | } 94 | 95 | ``` 96 | -------------------------------------------------------------------------------- /doc/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | date 4 | mvn clean javadoc:jar deploy -P release,gpg 5 | date 6 | -------------------------------------------------------------------------------- /doc/header/java.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | -------------------------------------------------------------------------------- /doc/header/shell.txt: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | -------------------------------------------------------------------------------- /doc/header/xml.txt: -------------------------------------------------------------------------------- 1 | 18 | -------------------------------------------------------------------------------- /doc/todo.md: -------------------------------------------------------------------------------- 1 | ### cache builder 2 | ``` 3 | use RedBlackTree 4 | ``` 5 | 6 | ### pipeline tool 7 | ``` 8 | use for scheduler system 9 | 10 | ``` 11 | 12 | ### thread lock 13 | 14 | 15 | ### cas tool 16 | 17 | 18 | ### socket builder 19 | 20 | 21 | ### date tool 22 | 23 | 24 | ### rpc cache 25 | ``` 26 | cache cluster 27 | ``` 28 | -------------------------------------------------------------------------------- /release.md: -------------------------------------------------------------------------------- 1 | mosheh Release Notes 2 | ------------------------ 3 | ### 2023/1/28: version 1.0.0.RELEASE 4 | - init 5 | 6 | ### 2023/2/5: version 1.0.1.RELEASE 7 | - add Sys utils 8 | - add IdGenerator 9 | - add test case: com.geektcp.common.mosheh.generator.* 10 | 11 | ### 2023/2/5: version 1.0.2.RELEASE 12 | - add Thy* utils, include manager file ,print log, 13 | - add test case: com.geektcp.common.mosheh.util.* 14 | 15 | ### 2023.2.18: version 1.0.3.RELEASE 16 | - add utils, use shell, 17 | - add host,string,resource file reader util 18 | - add test case: com.geektcp.common.mosheh.system.* 19 | - add executor, safely use multi thread 20 | - add test case: com.geektcp.common.mosheh.concurrent.thread.executor.* 21 | 22 | ### 2023.3.12: version 1.0.4 23 | - add simple local cache, thread safely cache 24 | - add test case: com.geektcp.common.mosheh.cache.* 25 | 26 | ### 2023.3.12: version 1.0.0 27 | - rename: old name mosheh,new name mosheh 28 | 29 | ### 2023.5.29: version 1.0.2 30 | - develop tiny cache 31 | - build some test case for tiny cache 32 | 33 | ### 2023.5.29: version 1.0.2 34 | - add clear feature for tiny cache 35 | - use weak reference to load travel data 36 | - add test case for this version -------------------------------------------------------------------------------- /security.md: -------------------------------------------------------------------------------- 1 | Security Policy 2 | ------------------------ 3 | 4 | ## Supported Versions 5 | 6 | the latest version is guaranteed to be secure. For this purpose, mosheh maintains backwards compatibility. 7 | Please note the [readme of the project](https://github.com/geektcp/mosheh/blob/main/README.md) for further information. 8 | 9 | 10 | ## Reporting a Vulnerability 11 | 12 | Please disclose responsibly by [contacting the maintainer](mailto:geektcp@163.com). After a fix is released, 13 | any vulnerability will be publicly disclosed and registered. As of today, no vulnerabilities are known to exists. 14 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/buffer/BufferBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.buffer; 19 | 20 | 21 | /** 22 | * @author geektcp on 2023/2/2 0:41. 23 | */ 24 | public class BufferBuilder { 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/AbstractCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache; 20 | 21 | /** 22 | * @author geektcp on 2023/3/5 15:13. 23 | */ 24 | public class AbstractCache implements Cache { 25 | 26 | 27 | @Override 28 | public boolean clear() { 29 | return false; 30 | } 31 | 32 | public boolean refresh(K key){ 33 | return false; 34 | } 35 | 36 | public V get(K key){ 37 | 38 | return null; 39 | } 40 | 41 | public boolean delete(K key){ 42 | 43 | return false; 44 | } 45 | 46 | @Override 47 | public boolean put(K k, V v) { 48 | return false; 49 | } 50 | 51 | @Override 52 | public void print(){ 53 | 54 | } 55 | 56 | @Override 57 | public void travel(){ 58 | 59 | } 60 | 61 | @Override 62 | public void start(){ 63 | } 64 | 65 | @Override 66 | public void stop(){ 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/AbstractCacheTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache; 20 | 21 | /** 22 | * @author geektcp on 2023/3/5 15:13. 23 | */ 24 | public abstract class AbstractCacheTree implements Store { 25 | 26 | @Override 27 | public boolean clear() { 28 | return false; 29 | } 30 | 31 | @Override 32 | public boolean put(K nodeId, V v) { 33 | return false; 34 | } 35 | 36 | @Override 37 | public boolean delete(K nodeId) { 38 | return false; 39 | } 40 | 41 | @Override 42 | public int size() { 43 | return 0; 44 | } 45 | 46 | @Override 47 | public V getRoot() { 48 | return null; 49 | } 50 | 51 | @Override 52 | public V fetch(K nodeId) { 53 | return null; 54 | } 55 | 56 | @Override 57 | public V getHeight(K nodeId) { 58 | return null; 59 | } 60 | 61 | @Override 62 | public V getHeight() { 63 | return null; 64 | } 65 | 66 | @Override 67 | public void print() { 68 | 69 | } 70 | 71 | @Override 72 | public void travel() { 73 | 74 | } 75 | 76 | @Override 77 | public void start() { 78 | 79 | } 80 | 81 | @Override 82 | public void stop() { 83 | 84 | } 85 | 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/Cache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache; 20 | 21 | 22 | /** 23 | * @author geektcp on 2019/11/30 0:59. 24 | */ 25 | public interface Cache { 26 | 27 | boolean clear(); 28 | 29 | boolean refresh(K key); 30 | 31 | V get(K key); 32 | 33 | boolean delete(K key); 34 | 35 | boolean put(K k, V v); 36 | 37 | void print(); 38 | 39 | void travel(); 40 | 41 | void start(); 42 | 43 | void stop(); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/Store.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache; 20 | 21 | /** 22 | * @author geektcp on 2023/3/12 19:58. 23 | */ 24 | public interface Store { 25 | 26 | boolean clear(); 27 | 28 | boolean put(K nodeId, V v); 29 | 30 | boolean delete(K nodeId); 31 | 32 | int size(); 33 | 34 | V getRoot(); 35 | 36 | V fetch(K nodeId); 37 | 38 | V getHeight(K nodeId); 39 | 40 | V getHeight(); 41 | 42 | void print(); 43 | 44 | void travel(); 45 | 46 | void start(); 47 | 48 | void stop(); 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/common/WeakCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.common; 20 | 21 | import com.geektcp.common.mosheh.cache.AbstractCache; 22 | import com.geektcp.common.mosheh.cache.Cache; 23 | 24 | import java.util.Map; 25 | import java.util.Objects; 26 | import java.util.WeakHashMap; 27 | import java.util.concurrent.ConcurrentHashMap; 28 | 29 | /** 30 | * @author geektcp on 2019/9/10. 31 | */ 32 | public final class WeakCache extends AbstractCache implements Cache { 33 | 34 | private final int size; 35 | 36 | private final Map eden; 37 | 38 | private final Map longTerm; 39 | 40 | public WeakCache(int size) { 41 | this.size = size; 42 | this.eden = new ConcurrentHashMap<>(size); 43 | this.longTerm = new WeakHashMap<>(size); 44 | } 45 | 46 | @Override 47 | public V get(K k) { 48 | if(Objects.isNull(k)){ 49 | return null; 50 | } 51 | V v = this.eden.get(k); 52 | if (v == null) { 53 | v = this.longTerm.get(k); 54 | if (v != null) 55 | this.eden.put(k, v); 56 | } 57 | return v; 58 | } 59 | 60 | public boolean put(K k, V v) { 61 | if(Objects.isNull(k)){ 62 | return false; 63 | } 64 | if (this.eden.size() >= size) { 65 | this.longTerm.putAll(this.eden); 66 | this.eden.clear(); 67 | } 68 | this.eden.put(k, v); 69 | return true; 70 | } 71 | 72 | @Override 73 | public boolean clear() { 74 | return false; 75 | } 76 | 77 | @Override 78 | public boolean refresh(K key) { 79 | return false; 80 | } 81 | 82 | @Override 83 | public boolean delete(Object key) { 84 | 85 | return false; 86 | } 87 | 88 | 89 | @Override 90 | public void start() { 91 | 92 | } 93 | 94 | @Override 95 | public void stop() { 96 | 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/listener/TinyListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.listener; 20 | 21 | 22 | /** 23 | * @author geektcp on 2023/2/26 16:51. 24 | */ 25 | public interface TinyListener { 26 | 27 | void onRemoval(TinyRemovalNotification var1); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/listener/TinyReferenceEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.listener; 20 | 21 | import org.checkerframework.checker.nullness.qual.Nullable; 22 | 23 | /** 24 | * @author geektcp on 2023/2/26 17:55. 25 | */ 26 | public interface TinyReferenceEntry { 27 | 28 | ValueReference getValueReference(); 29 | 30 | void setValueReference(ValueReference var1); 31 | 32 | @Nullable 33 | TinyReferenceEntry getNext(); 34 | 35 | int getHash(); 36 | 37 | @Nullable 38 | K getKey(); 39 | 40 | long getAccessTime(); 41 | 42 | void setAccessTime(long var1); 43 | 44 | TinyReferenceEntry getNextInAccessQueue(); 45 | 46 | void setNextInAccessQueue(TinyReferenceEntry var1); 47 | 48 | TinyReferenceEntry getPreviousInAccessQueue(); 49 | 50 | void setPreviousInAccessQueue(TinyReferenceEntry var1); 51 | 52 | long getWriteTime(); 53 | 54 | void setWriteTime(long var1); 55 | 56 | TinyReferenceEntry getNextInWriteQueue(); 57 | 58 | void setNextInWriteQueue(TinyReferenceEntry var1); 59 | 60 | TinyReferenceEntry getPreviousInWriteQueue(); 61 | 62 | void setPreviousInWriteQueue(TinyReferenceEntry var1); 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/listener/TinyRemovalCause.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.listener; 20 | 21 | /** 22 | * @author geektcp on 2023/2/26 17:18. 23 | */ 24 | public enum TinyRemovalCause { 25 | 26 | EXPLICIT { 27 | boolean wasEvicted() { 28 | return false; 29 | } 30 | }, 31 | REPLACED { 32 | boolean wasEvicted() { 33 | return false; 34 | } 35 | }, 36 | COLLECTED { 37 | boolean wasEvicted() { 38 | return true; 39 | } 40 | }, 41 | EXPIRED { 42 | boolean wasEvicted() { 43 | return true; 44 | } 45 | }, 46 | SIZE { 47 | boolean wasEvicted() { 48 | return true; 49 | } 50 | }; 51 | 52 | private TinyRemovalCause() { 53 | } 54 | 55 | abstract boolean wasEvicted(); 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/listener/TinyRemovalNotification.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.listener; 20 | 21 | import com.geektcp.common.mosheh.checker.Preconditions; 22 | import org.checkerframework.checker.nullness.qual.Nullable; 23 | 24 | import java.util.AbstractMap; 25 | 26 | /** 27 | * @author geektcp on 2023/2/26 17:16. 28 | */ 29 | public class TinyRemovalNotification extends AbstractMap.SimpleImmutableEntry { 30 | 31 | private final TinyRemovalCause cause; 32 | private static final long serialVersionUID = 0L; 33 | 34 | public static TinyRemovalNotification create(@Nullable K key, @Nullable V value, TinyRemovalCause cause) { 35 | return new TinyRemovalNotification(key, value, cause); 36 | } 37 | 38 | private TinyRemovalNotification(@Nullable K key, @Nullable V value, TinyRemovalCause cause) { 39 | super(key, value); 40 | this.cause = (TinyRemovalCause) Preconditions.checkNotNull(cause); 41 | } 42 | 43 | public TinyRemovalCause getCause() { 44 | return this.cause; 45 | } 46 | 47 | public boolean wasEvicted() { 48 | return this.cause.wasEvicted(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/listener/ValueReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.listener; 20 | 21 | import org.checkerframework.checker.nullness.qual.Nullable; 22 | 23 | import java.lang.ref.ReferenceQueue; 24 | import java.util.concurrent.ExecutionException; 25 | 26 | /** 27 | * @author geektcp on 2023/2/26 18:00. 28 | */ 29 | public interface ValueReference { 30 | @Nullable 31 | V get(); 32 | 33 | V waitForValue() throws ExecutionException; 34 | 35 | int getWeight(); 36 | 37 | @Nullable 38 | TinyReferenceEntry getEntry(); 39 | 40 | ValueReference copyFor(ReferenceQueue var1, @Nullable V var2, TinyReferenceEntry var3); 41 | 42 | void notifyNewValue(@Nullable V var1); 43 | 44 | boolean isLoading(); 45 | 46 | boolean isActive(); 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/loader/CacheLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.loader; 20 | 21 | /** 22 | * @author geektcp on 2023/3/5 14:49. 23 | */ 24 | public interface CacheLoader { 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/loader/TinyLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.loader; 20 | 21 | import com.geektcp.common.mosheh.exception.BaseException; 22 | 23 | import java.util.Map; 24 | 25 | 26 | /** 27 | * @author geektcp on 2023/2/26 17:30. 28 | */ 29 | public abstract class TinyLoader implements CacheLoader { 30 | 31 | protected TinyLoader() { 32 | } 33 | 34 | /** 35 | * generate value 36 | * 37 | * @param k key 38 | * @return value 39 | */ 40 | public abstract V load(K k); 41 | 42 | public Map loadAll(Iterable keys) { 43 | throw new BaseException(); 44 | } 45 | 46 | /* 47 | public ListenableFuture reload(K key, V oldValue) { 48 | Preconditions.checkNotNull(key); 49 | Preconditions.checkNotNull(oldValue); 50 | return Futures.immediateFuture(load(key)); 51 | } 52 | 53 | public static TinyLoader asyncReloading( 54 | final TinyLoader loader, final TinyExecutor executor) { 55 | Preconditions.checkNotNull(loader); 56 | Preconditions.checkNotNull(executor); 57 | return new TinyLoader() { 58 | @Override 59 | public V load(K key) { 60 | return loader.load(key); 61 | } 62 | 63 | @Override 64 | public ListenableFuture reload(final K key, final V oldValue) { 65 | ListenableFutureTask task = 66 | ListenableFutureTask.create(() -> loader.reload(key, oldValue).get()); 67 | executor.execute(task); 68 | return task; 69 | } 70 | 71 | @Override 72 | public Map loadAll(Iterable keys) { 73 | return loader.loadAll(keys); 74 | } 75 | }; 76 | } 77 | */ 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/loading/InvalidateCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.loading; 20 | 21 | 22 | import java.util.Map; 23 | import java.util.concurrent.Callable; 24 | import java.util.concurrent.ExecutionException; 25 | 26 | /** 27 | * @author geektcp on 2021/5/6 16:59. 28 | */ 29 | public interface InvalidateCache extends LoadingCache { 30 | 31 | V getIfPresent( Object var1); 32 | 33 | V get(K var1, Callable var2) throws ExecutionException; 34 | 35 | void putAll(Map var1); 36 | 37 | void invalidate(Object var1); 38 | 39 | void invalidateAll(Iterable var1); 40 | 41 | void invalidateAll(); 42 | 43 | long size(); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/loading/LoadingCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.loading; 20 | 21 | 22 | import com.geektcp.common.mosheh.cache.Cache; 23 | 24 | import java.util.Map; 25 | import java.util.concurrent.ConcurrentMap; 26 | import java.util.concurrent.ExecutionException; 27 | 28 | /** 29 | * @author geektcp on 2023/2/26 17:26. 30 | */ 31 | public interface LoadingCache extends Cache { 32 | 33 | V getUnchecked(K var1); 34 | 35 | Map getAll(Iterable var1) throws ExecutionException; 36 | 37 | ConcurrentMap asMap(); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/local/TinyCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.local; 20 | 21 | import com.geektcp.common.mosheh.cache.Store; 22 | import com.geektcp.common.mosheh.cache.tiny.CacheBuilder; 23 | import com.geektcp.common.mosheh.cache.tiny.loader.TinyLoader; 24 | import com.geektcp.common.mosheh.cache.AbstractCache; 25 | import com.geektcp.common.mosheh.cache.tiny.storage.cache.TreeCacheFactory; 26 | import com.geektcp.common.mosheh.cache.tiny.storage.key.NodeKey; 27 | 28 | import java.util.HashMap; 29 | import java.util.Map; 30 | import java.util.Objects; 31 | 32 | /** 33 | * @author geektcp on 2023/2/26 17:50. 34 | */ 35 | public class TinyCache extends AbstractCache { 36 | 37 | private Store store; 38 | 39 | private Map expireTimeMap = new HashMap<>(); 40 | 41 | public V putIfAbsent(K key, V value) { 42 | return null; 43 | } 44 | 45 | 46 | public TinyCache() { 47 | // build complex cache 48 | this.store = TreeCacheFactory.build().buildCache(); 49 | } 50 | 51 | public TinyCache(CacheBuilder builder, TinyLoader loader) { 52 | 53 | 54 | } 55 | 56 | @Override 57 | public boolean clear() { 58 | return store.clear(); 59 | } 60 | 61 | @Override 62 | public boolean refresh(K k) { 63 | return false; 64 | } 65 | 66 | @Override 67 | public V get(K k) { 68 | if(Objects.isNull(k)){ 69 | return null; 70 | } 71 | return store.fetch(new NodeKey<>(k)); 72 | } 73 | 74 | @Override 75 | public boolean put(K k, V v) { 76 | if(Objects.isNull(k)){ 77 | return false; 78 | } 79 | store.put(new NodeKey<>(k), v); 80 | return true; 81 | } 82 | 83 | @Override 84 | public boolean delete(K k) { 85 | return store.delete(new NodeKey<>(k)); 86 | } 87 | 88 | public void print(){ 89 | store.print(); 90 | } 91 | @Override 92 | public void travel(){ 93 | store.travel(); 94 | } 95 | 96 | @Override 97 | public void start(){ 98 | store.start(); 99 | } 100 | 101 | @Override 102 | public void stop(){ 103 | store.stop(); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/storage/AbstractCacheFactory.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.cache.tiny.storage; 2 | 3 | /** 4 | * @author geektcp on 2023/8/13 0:07. 5 | */ 6 | public abstract class AbstractCacheFactory implements CacheFactory { 7 | 8 | 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/storage/CacheFactory.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.cache.tiny.storage; 2 | 3 | import com.geektcp.common.mosheh.cache.Store; 4 | 5 | /** 6 | * @author geektcp on 2023/8/13 0:04. 7 | */ 8 | public interface CacheFactory { 9 | 10 | Store buildCache(); 11 | 12 | void set(); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/storage/cache/CacheTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.storage.cache; 20 | 21 | import com.geektcp.common.mosheh.cache.Store; 22 | import com.geektcp.common.mosheh.cache.tiny.storage.key.AbstractKey; 23 | import com.geektcp.common.mosheh.tree.RedBlackTree; 24 | 25 | /** 26 | * @author geektcp on 2023/3/12 19:55. 27 | */ 28 | public class CacheTree, V> extends RedBlackTree implements Store { 29 | 30 | public static boolean IS_VALID = true; 31 | 32 | public CacheTree() { 33 | // to do 34 | IS_VALID = true; 35 | this.init(); 36 | 37 | } 38 | 39 | public CacheTree(K k, V v) { 40 | // to do 41 | 42 | } 43 | 44 | 45 | @Override 46 | public boolean clear() { 47 | return super.clear(); 48 | } 49 | 50 | @Override 51 | public boolean put(K k, V v) { 52 | if(!IS_VALID){ 53 | return false; 54 | } 55 | super.add(k, v); 56 | return true; 57 | } 58 | 59 | @Override 60 | public boolean delete(K nodeId) { 61 | return false; 62 | } 63 | 64 | @Override 65 | public int size() { 66 | return 0; 67 | } 68 | 69 | @Override 70 | public V getRoot() { 71 | return null; 72 | } 73 | 74 | @Override 75 | public V fetch(K k) { 76 | return super.get(k); 77 | } 78 | 79 | @Override 80 | public V getHeight(K nodeId) { 81 | return null; 82 | } 83 | 84 | @Override 85 | public V getHeight() { 86 | return null; 87 | } 88 | 89 | public void print(){ 90 | super.print(); 91 | } 92 | 93 | public boolean invalid(){ 94 | IS_VALID = false; 95 | return super.clear(); 96 | } 97 | 98 | @Override 99 | public void start(){ 100 | IS_VALID = true; 101 | } 102 | 103 | @Override 104 | public void stop(){ 105 | IS_VALID = false; 106 | super.clear(); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/storage/cache/TreeCacheFactory.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.cache.tiny.storage.cache; 2 | 3 | import com.geektcp.common.mosheh.cache.Store; 4 | import com.geektcp.common.mosheh.cache.tiny.storage.AbstractCacheFactory; 5 | import com.geektcp.common.mosheh.tree.RedBlackTree; 6 | 7 | /** 8 | * @author geektcp on 2023/8/13 0:08. 9 | */ 10 | public class TreeCacheFactory extends AbstractCacheFactory { 11 | 12 | private Store store; 13 | 14 | 15 | public static TreeCacheFactory build(){ 16 | return new TreeCacheFactory<>(); 17 | } 18 | 19 | @Override 20 | public Store buildCache() { 21 | store = new CacheTree(); 22 | return store; 23 | } 24 | 25 | @Override 26 | public void set() { 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/storage/key/AbstractKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.storage.key; 20 | 21 | /** 22 | * @author geektcp on 2023/7/21 16:12. 23 | */ 24 | public abstract class AbstractKey implements Comparable { 25 | 26 | public Long getId(){ 27 | return null; 28 | } 29 | 30 | public Object getKey(){ 31 | return null; 32 | } 33 | 34 | public int compareTo(K o){ 35 | return 0; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/storage/key/LocalKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.storage.key; 20 | 21 | /** 22 | * @author geektcp on 2023/3/12 20:47. 23 | */ 24 | public class LocalKey extends AbstractKey { 25 | private Long id; 26 | private Object key; 27 | 28 | public LocalKey(Object key) { 29 | this.key = key; 30 | } 31 | 32 | 33 | @Override 34 | public Long getId() { 35 | return this.id; 36 | } 37 | 38 | @Override 39 | public Object getKey() { 40 | return this.key; 41 | } 42 | 43 | public LocalKey build(Object key) { 44 | return new LocalKey(key); 45 | } 46 | 47 | 48 | public int compareTo(K k){ 49 | return 0; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/cache/tiny/storage/key/NodeKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.cache.tiny.storage.key; 20 | 21 | 22 | import com.geektcp.common.mosheh.generator.IdGenerator; 23 | import com.geektcp.common.mosheh.system.Sys; 24 | 25 | import java.util.Objects; 26 | 27 | /** 28 | * @author geektcp on 2023/3/12 20:18. 29 | */ 30 | public class NodeKey extends AbstractKey { 31 | 32 | private Long id; 33 | 34 | private K currentKey; 35 | 36 | @Override 37 | public Long getId() { 38 | return this.id; 39 | } 40 | 41 | @Override 42 | public Object getKey() { 43 | return this.currentKey; 44 | } 45 | 46 | public NodeKey(K k) { 47 | this.id = IdGenerator.getId(); 48 | this.currentKey = k; 49 | } 50 | 51 | public NodeKey build(K k) { 52 | return new NodeKey<>(k); 53 | } 54 | 55 | 56 | @Override 57 | public int compareTo(K k) { 58 | if (Objects.isNull(k)) { 59 | return -1000; 60 | } 61 | if (Objects.isNull(currentKey)) { 62 | return -2000; 63 | } 64 | 65 | try { 66 | NodeKey nodeKey = (NodeKey) k; 67 | if(this.currentKey.equals(nodeKey.getKey())){ 68 | return 0; 69 | } 70 | }catch (Exception e){ 71 | Sys.p("k is not NodeKey type!"); 72 | } 73 | 74 | return -1; 75 | } 76 | 77 | @Override 78 | public boolean equals(Object obj) { 79 | return super.equals(obj); 80 | } 81 | 82 | 83 | @Override 84 | public int hashCode() { 85 | return Objects.hash(currentKey); 86 | } 87 | 88 | 89 | public String toString() { 90 | StringBuilder sb = new StringBuilder(); 91 | sb.append("id: ").append(id) 92 | .append("|") 93 | .append("currentKey: ").append(currentKey); 94 | return sb.toString(); 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/collection/ArrayStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.collection; 20 | 21 | import java.util.Iterator; 22 | 23 | /** 24 | * @author geektcp on 2019/9/28. 25 | */ 26 | public class ArrayStack implements ThyStack { 27 | 28 | private Item[] a = (Item[]) new Object[1]; 29 | 30 | private int N = 0; 31 | 32 | @Override 33 | public ThyStack push(Item item) { 34 | check(); 35 | a[N++] = item; 36 | return this; 37 | } 38 | 39 | @Override 40 | public Item pop() { 41 | if (isEmpty()) { 42 | return null; 43 | } 44 | 45 | Item item = a[--N]; 46 | check(); 47 | a[N] = null; 48 | 49 | return item; 50 | } 51 | 52 | 53 | private void check() { 54 | if (N >= a.length) { 55 | resize(2 * a.length); 56 | 57 | } else if (N > 0 && N <= a.length / 4) { 58 | resize(a.length / 2); 59 | } 60 | } 61 | 62 | private void resize(int size) { 63 | Item[] tmp = (Item[]) new Object[size]; 64 | for (int i = 0; i < N; i++) { 65 | tmp[i] = a[i]; 66 | } 67 | a = tmp; 68 | } 69 | 70 | @Override 71 | public boolean isEmpty() { 72 | return N == 0; 73 | } 74 | 75 | @Override 76 | public int size() { 77 | return N; 78 | } 79 | 80 | @Override 81 | public Iterator iterator() { 82 | return new Iterator() { 83 | private int i = N; 84 | @Override 85 | public boolean hasNext() { 86 | return i > 0; 87 | } 88 | @Override 89 | public Item next() { 90 | return a[--i]; 91 | } 92 | }; 93 | 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/collection/ListQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.collection; 20 | 21 | import java.util.Iterator; 22 | 23 | /** 24 | * @author geektcp on 2019/9/28. 25 | */ 26 | public class ListQueue implements ThyQueue { 27 | 28 | private Node first; 29 | private Node last; 30 | int N = 0; 31 | 32 | private class Node { 33 | Item item; 34 | Node next; 35 | } 36 | 37 | @Override 38 | public boolean isEmpty() { 39 | return N == 0; 40 | } 41 | 42 | @Override 43 | public int size() { 44 | return N; 45 | } 46 | 47 | @Override 48 | public ThyQueue add(Item item) { 49 | Node newNode = new Node(); 50 | newNode.item = item; 51 | newNode.next = null; 52 | if (isEmpty()) { 53 | last = newNode; 54 | first = newNode; 55 | } else { 56 | last.next = newNode; 57 | last = newNode; 58 | } 59 | N++; 60 | return this; 61 | } 62 | 63 | @Override 64 | public Item remove() throws Exception { 65 | if (isEmpty()) { 66 | throw new Exception("queue is empty"); 67 | } 68 | Node node = first; 69 | first = first.next; 70 | N--; 71 | if (isEmpty()) { 72 | last = null; 73 | } 74 | 75 | return node.item; 76 | } 77 | 78 | 79 | @Override 80 | public Iterator iterator() { 81 | return new Iterator() { 82 | Node cur = first; 83 | 84 | @Override 85 | public boolean hasNext() { 86 | return cur != null; 87 | } 88 | 89 | @Override 90 | public Item next() { 91 | Item item = cur.item; 92 | cur = cur.next; 93 | return item; 94 | } 95 | }; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/collection/ListStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.collection; 20 | 21 | import java.util.Iterator; 22 | import java.util.NoSuchElementException; 23 | 24 | /** 25 | * @author geektcp on 2019/9/28. 26 | */ 27 | public class ListStack implements ThyStack { 28 | 29 | private Node top = null; 30 | private int N = 0; 31 | 32 | private class Node { 33 | Item item; 34 | Node next; 35 | } 36 | 37 | @Override 38 | public ThyStack push(Item item) { 39 | Node newTop = new Node(); 40 | newTop.item = item; 41 | newTop.next = top; 42 | top = newTop; 43 | N++; 44 | 45 | return this; 46 | } 47 | 48 | @Override 49 | public Item pop() throws Exception { 50 | if (isEmpty()) { 51 | throw new Exception("stack is empty"); 52 | } 53 | Item item = top.item; 54 | top = top.next; 55 | N--; 56 | 57 | return item; 58 | } 59 | 60 | @Override 61 | public boolean isEmpty() { 62 | return N == 0; 63 | } 64 | 65 | @Override 66 | public int size() { 67 | return N; 68 | } 69 | 70 | @Override 71 | public Iterator iterator() { 72 | return new Iterator() { 73 | private Node cur = top; 74 | 75 | @Override 76 | public boolean hasNext() { 77 | return cur != null; 78 | } 79 | 80 | @Override 81 | public Item next() { 82 | if (!hasNext()) { 83 | throw new NoSuchElementException(); 84 | } 85 | Item item = cur.item; 86 | cur = cur.next; 87 | return item; 88 | } 89 | }; 90 | 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/collection/Lists.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.collection; 20 | 21 | import com.geektcp.common.mosheh.system.Sys; 22 | 23 | import java.lang.reflect.Array; 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | import java.util.Objects; 27 | 28 | @SuppressWarnings("unchecked") 29 | public class Lists { 30 | 31 | public static T[] toArray(List src, Class cls) { 32 | if (Objects.isNull(src)) { 33 | Sys.p("List src is null"); 34 | return (T[]) Array.newInstance(cls, 0); 35 | } 36 | int size = src.size(); 37 | T[] dst = (T[]) Array.newInstance(cls, size); 38 | 39 | for (int i = 0; i < size; i++) { 40 | dst[i] = src.get(i); 41 | } 42 | return dst; 43 | } 44 | 45 | public static T[] toArray(List src) { 46 | Class cls = Object.class; 47 | if (Objects.isNull(src) || src.isEmpty()) { 48 | Sys.p("List src is null or empty"); 49 | return (T[]) Array.newInstance(cls, 0); 50 | } 51 | int size = src.size(); 52 | 53 | { 54 | // get class 55 | boolean isAllNull = false; 56 | for (T checkElement : src) { 57 | if (Objects.nonNull(checkElement)) { 58 | isAllNull = true; 59 | cls = checkElement.getClass(); 60 | break; 61 | } 62 | } 63 | if (isAllNull) { 64 | return (T[]) Array.newInstance(cls, 0); 65 | } 66 | } 67 | 68 | T[] dst = (T[]) Array.newInstance(cls, size); 69 | 70 | for (int i = 0; i < size; i++) { 71 | dst[i] = src.get(i); 72 | } 73 | return dst; 74 | } 75 | 76 | 77 | public static ArrayList newArrayList() { 78 | return new ArrayList(); 79 | } 80 | 81 | 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/collection/ThyQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.collection; 20 | 21 | /** 22 | * @author geektcp on 2019/9/28. 23 | */ 24 | public interface ThyQueue extends Iterable { 25 | 26 | int size(); 27 | 28 | boolean isEmpty(); 29 | 30 | ThyQueue add(Item item); 31 | 32 | Item remove() throws Exception; 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/collection/ThyStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.collection; 20 | 21 | /** 22 | * @author geektcp on 2019/9/28. 23 | */ 24 | public interface ThyStack extends Iterable { 25 | 26 | ThyStack push(Item item); 27 | 28 | Item pop() throws Exception; 29 | 30 | boolean isEmpty(); 31 | 32 | int size(); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/cas/UnSafeBuilder.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.concurrent.cas; 2 | 3 | 4 | /** 5 | * @author geektcp on 2023/2/2 0:59. 6 | */ 7 | public class UnSafeBuilder { 8 | 9 | private UnSafeBuilder(){ 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/lock/Lock.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.concurrent.lock; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | /** 6 | * @author geektcp on 2023/2/2 0:16. 7 | */ 8 | public interface Lock { 9 | 10 | void lock(); 11 | 12 | void unlock(); 13 | 14 | boolean tryLock(); 15 | 16 | boolean tryLock(long time, TimeUnit unit) throws InterruptedException; 17 | 18 | void lockInterrupt() throws InterruptedException; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/lock/LockBuilder.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.concurrent.lock; 2 | 3 | /** 4 | * @author geektcp on 2023/2/2 0:25. 5 | */ 6 | public class LockBuilder { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/lock/SynchronizedLock.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.concurrent.lock; 2 | 3 | /** 4 | * @author geektcp on 2023/2/2 0:25. 5 | */ 6 | public class SynchronizedLock { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/lock/ThyLock.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.concurrent.lock; 2 | 3 | /** 4 | * @author geektcp on 2023/2/2 0:25. 5 | */ 6 | public class ThyLock { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/single/ThreadBuilder.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.concurrent.single; 2 | 3 | /** 4 | * @author geektcp on 2023/2/2 0:35. 5 | */ 6 | public class ThreadBuilder { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/thread/able/ThyCallable.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.concurrent.thread.able; 2 | 3 | import java.util.concurrent.Callable; 4 | 5 | /** 6 | * @author geektcp on 2023/2/7 0:09. 7 | */ 8 | public class ThyCallable implements Callable { 9 | 10 | 11 | @Override 12 | public Object call() throws Exception { 13 | return null; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/thread/able/ThyRunnable.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.concurrent.thread.able; 2 | 3 | /** 4 | * @author geektcp on 2023/2/7 0:09. 5 | */ 6 | public class ThyRunnable implements Runnable{ 7 | 8 | @Override 9 | public void run() { 10 | 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/thread/executor/service/TinyExecutor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.concurrent.thread.executor.service; 19 | 20 | import java.util.concurrent.RejectedExecutionException; 21 | 22 | /** 23 | * @author geektcp on 2023/2/6 22:47. 24 | */ 25 | public interface TinyExecutor { 26 | 27 | /** 28 | * Executes the given command at some time in the future. The command 29 | * may execute in a new thread, in a pooled thread, or in the calling 30 | * thread, at the discretion of the {@code Executor} implementation. 31 | * 32 | * @param command the runnable task 33 | * @throws RejectedExecutionException if this task cannot be 34 | * accepted for execution 35 | * @throws NullPointerException if command is null 36 | */ 37 | void execute(Runnable command); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/thread/executor/service/TinyExecutorService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.concurrent.thread.executor.service; 19 | 20 | 21 | import java.util.Collection; 22 | import java.util.List; 23 | import java.util.concurrent.*; 24 | 25 | /** 26 | * @author geektcp on 2023/2/6 22:47. 27 | */ 28 | public interface TinyExecutorService extends TinyExecutor { 29 | 30 | void shutdown(); 31 | 32 | List shutdownNow(); 33 | 34 | boolean isShutdown(); 35 | 36 | boolean isTerminated(); 37 | 38 | boolean awaitTermination(long timeout, TimeUnit unit) 39 | throws InterruptedException; 40 | 41 | Future submit(Callable task); 42 | 43 | Future submit(Runnable task, T result); 44 | 45 | Future submit(Runnable task); 46 | 47 | List> invokeAll(Collection> tasks) 48 | throws InterruptedException; 49 | 50 | List> invokeAll(Collection> tasks, 51 | long timeout, TimeUnit unit) 52 | throws InterruptedException; 53 | 54 | T invokeAny(Collection> tasks) 55 | throws InterruptedException, ExecutionException; 56 | 57 | T invokeAny(Collection> tasks, 58 | long timeout, TimeUnit unit) 59 | throws InterruptedException, ExecutionException, TimeoutException; 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/thread/executor/service/TinyRejectedHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.concurrent.thread.executor.service; 19 | 20 | import com.geektcp.common.mosheh.concurrent.thread.executor.service.impl.extend.TinyThreadPoolExecutor; 21 | 22 | public interface TinyRejectedHandler { 23 | 24 | void rejectedExecution(Runnable r, TinyThreadPoolExecutor executor); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/thread/executor/service/TinyScheduledService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.concurrent.thread.executor.service; 19 | 20 | import java.util.concurrent.Callable; 21 | import java.util.concurrent.ScheduledFuture; 22 | import java.util.concurrent.TimeUnit; 23 | 24 | /** 25 | * @author geektcp on 2023/2/6 22:47. 26 | */ 27 | public interface TinyScheduledService extends TinyExecutorService { 28 | 29 | ScheduledFuture schedule(Runnable command, 30 | long delay, TimeUnit unit); 31 | 32 | ScheduledFuture schedule(Callable callable, 33 | long delay, TimeUnit unit); 34 | 35 | ScheduledFuture scheduleAtFixedRate(Runnable command, 36 | long initialDelay, 37 | long period, 38 | TimeUnit unit); 39 | 40 | ScheduledFuture scheduleWithFixedDelay(Runnable command, 41 | long initialDelay, 42 | long delay, 43 | TimeUnit unit); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/thread/executor/service/impl/delegated/TinyDelegatedExecutor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.concurrent.thread.executor.service.impl.delegated; 19 | 20 | import com.geektcp.common.mosheh.concurrent.thread.executor.service.TinyExecutorService; 21 | import com.geektcp.common.mosheh.concurrent.thread.executor.service.impl.TinyAbstractService; 22 | 23 | import java.util.Collection; 24 | import java.util.List; 25 | import java.util.concurrent.*; 26 | 27 | /** 28 | * @author geektcp on 2023/2/18 23:18. 29 | * DelegatedExecutorService 30 | */ 31 | public class TinyDelegatedExecutor extends TinyAbstractService { 32 | 33 | private final TinyExecutorService e; 34 | 35 | public TinyDelegatedExecutor(TinyExecutorService executor) { 36 | e = executor; 37 | } 38 | 39 | public void execute(Runnable command) { 40 | e.execute(command); 41 | } 42 | 43 | public void shutdown() { 44 | e.shutdown(); 45 | } 46 | 47 | public List shutdownNow() { 48 | return e.shutdownNow(); 49 | } 50 | 51 | public boolean isShutdown() { 52 | return e.isShutdown(); 53 | } 54 | 55 | public boolean isTerminated() { 56 | return e.isTerminated(); 57 | } 58 | 59 | public boolean awaitTermination(long timeout, TimeUnit unit) 60 | throws InterruptedException { 61 | return e.awaitTermination(timeout, unit); 62 | } 63 | 64 | public Future submit(Runnable task) { 65 | return e.submit(task); 66 | } 67 | 68 | public Future submit(Callable task) { 69 | return e.submit(task); 70 | } 71 | 72 | public Future submit(Runnable task, T result) { 73 | return e.submit(task, result); 74 | } 75 | 76 | public List> invokeAll(Collection> tasks) 77 | throws InterruptedException { 78 | return e.invokeAll(tasks); 79 | } 80 | 81 | public List> invokeAll(Collection> tasks, long timeout, TimeUnit unit) 82 | throws InterruptedException { 83 | return e.invokeAll(tasks, timeout, unit); 84 | } 85 | 86 | public T invokeAny( Collection> tasks) 87 | throws InterruptedException, ExecutionException { 88 | return e.invokeAny(tasks); 89 | } 90 | 91 | public T invokeAny(Collection> tasks, long timeout, TimeUnit unit) 92 | throws InterruptedException, ExecutionException, TimeoutException { 93 | return e.invokeAny(tasks, timeout, unit); 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/thread/executor/service/impl/delegated/TinyFinalizeExecutor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.concurrent.thread.executor.service.impl.delegated; 19 | 20 | import com.geektcp.common.mosheh.concurrent.thread.executor.service.TinyExecutorService; 21 | 22 | /** 23 | * @author geektcp on 2023/2/6 22:47. 24 | */ 25 | public class TinyFinalizeExecutor extends TinyDelegatedExecutor { 26 | 27 | public TinyFinalizeExecutor(TinyExecutorService executor) { 28 | super(executor); 29 | } 30 | 31 | protected void finalize() { 32 | super.shutdown(); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/thread/executor/service/impl/delegated/TinyScheduledExecutor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.concurrent.thread.executor.service.impl.delegated; 19 | 20 | import com.geektcp.common.mosheh.concurrent.thread.executor.service.TinyScheduledService; 21 | 22 | import java.util.concurrent.*; 23 | 24 | /** 25 | * DelegatedScheduledExecutorService 26 | */ 27 | public class TinyScheduledExecutor extends TinyDelegatedExecutor implements TinyScheduledService { 28 | 29 | private final TinyScheduledService e; 30 | 31 | public TinyScheduledExecutor(TinyScheduledService executor) { 32 | super(executor); 33 | e = executor; 34 | } 35 | 36 | public ScheduledFuture schedule(Runnable command, long delay, TimeUnit unit) { 37 | return e.schedule(command, delay, unit); 38 | } 39 | 40 | public ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit) { 41 | return e.schedule(callable, delay, unit); 42 | } 43 | 44 | public ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { 45 | return e.scheduleAtFixedRate(command, initialDelay, period, unit); 46 | } 47 | 48 | public ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { 49 | return e.scheduleWithFixedDelay(command, initialDelay, delay, unit); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/thread/factory/ThyThread.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.concurrent.thread.factory; 2 | 3 | 4 | /** 5 | * @author geektcp on 2023/2/6 23:04. 6 | */ 7 | public class ThyThread extends Thread { 8 | 9 | private boolean isRunning; 10 | 11 | 12 | public boolean isRunning() { 13 | return isRunning; 14 | } 15 | 16 | @Override 17 | public void run() { 18 | this.isRunning = true; 19 | innerRun(); 20 | this.isRunning = false; 21 | } 22 | 23 | private void innerRun() { 24 | // do something 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/concurrent/thread/factory/ThyThreadFactory.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.concurrent.thread.factory; 2 | 3 | import java.util.concurrent.ThreadFactory; 4 | import java.util.concurrent.atomic.AtomicInteger; 5 | 6 | /** 7 | * @author geektcp on 2023/2/6 23:04. 8 | */ 9 | public class ThyThreadFactory implements ThreadFactory { 10 | 11 | private static final AtomicInteger poolNumber = new AtomicInteger(1); 12 | private final ThreadGroup group; 13 | private final AtomicInteger threadNumber = new AtomicInteger(1); 14 | private final String namePrefix; 15 | 16 | ThyThreadFactory() { 17 | SecurityManager s = System.getSecurityManager(); 18 | group = (s != null) ? s.getThreadGroup() : 19 | Thread.currentThread().getThreadGroup(); 20 | namePrefix = "thy-" + poolNumber.getAndIncrement() + "-thread-"; 21 | } 22 | 23 | public Thread newThread(Runnable r) { 24 | Thread t = new Thread(group, r, 25 | namePrefix + threadNumber.getAndIncrement(), 26 | 0); 27 | if (t.isDaemon()) 28 | t.setDaemon(false); 29 | if (t.getPriority() != Thread.NORM_PRIORITY) 30 | t.setPriority(Thread.NORM_PRIORITY); 31 | return t; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/constant/CommonStatus.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.constant; 2 | 3 | /** 4 | * 5 | * 共用 错误码 6 | * @author geektcp 7 | * 2021/7/15 14:30 8 | */ 9 | public enum CommonStatus implements Status { 10 | 11 | SYSTEM_BUSY(-1, "system is busy."), 12 | SYSTEM_TIMEOUT(-2, "system timeout "), 13 | PARAM_EX(-3, "para exception"), 14 | SQL_EX(-4, "sql exception"), 15 | NULL_POINT_EX(-5, "nop exception"), 16 | ILLEGAL_ARGUMENT_EX(-6, "illegal para"), 17 | MEDIA_TYPE_EX(-7, "type exception"), 18 | LOAD_RESOURCES_ERROR(-8, "load error"), 19 | BASE_VALID_PARAM(-9, "valid error"), 20 | OPERATION_EX(-10, "operate exception"), 21 | SERVICE_MAPPER_ERROR(-11, "mapper error"), 22 | CAPTCHA_ERROR(-12, "captcha error"), 23 | JSON_PARSE_ERROR(-13, "json error"), 24 | REQUEST_RETURN_NULL(-14, "request return null"), 25 | BAD_REQUEST(400, "bad request"), 26 | UNAUTHORIZED(401, "unauthorized"), 27 | NOT_FOUND(404, "not found"), 28 | METHOD_NOT_ALLOWED(405, "method not allowed"), 29 | TOO_MANY_REQUESTS(429, "too many requests"), 30 | INTERNAL_SERVER_ERROR(500, "internal server error"), 31 | BAD_GATEWAY(502, "bad gateway"), 32 | GATEWAY_TIMEOUT(504, "gateway timeout"), 33 | REQUIRED_FILE_PARAM_EX(1001, "required file param"), 34 | DATA_SAVE_ERROR(2000, "data save error"), 35 | DATA_UPDATE_ERROR(2001, "date update error"), 36 | TOO_MUCH_DATA_ERROR(2002, "too much data error"), 37 | JWT_BASIC_INVALID(4000, "basic invalid"), 38 | JWT_TOKEN_EXPIRED(4001, "token expired"), 39 | JWT_SIGNATURE(4002, "signature"), 40 | JWT_ILLEGAL_ARGUMENT(4003, "illegal argument"), 41 | JWT_GEN_TOKEN_FAIL(4004, "generate token failed"), 42 | JWT_PARSE_TOKEN_FAIL(4005, "pare token failed"), 43 | JWT_USER_INVALID(4006, "user invalid"), 44 | JWT_USER_ENABLED(4007, "user is forbid !"), 45 | JWT_OFFLINE(4008, "login at local env!"), 46 | UPLOAD_FILE_TYPE_ERROR(4009, "file type error"), 47 | UPLOAD_FILE_ERROR(4010, "save file error"), 48 | 49 | ERROR_LOG_INSERT(5000, "log save error"), 50 | 51 | ; 52 | 53 | 54 | private int code; 55 | private String desc; 56 | 57 | CommonStatus(Integer code, String desc) { 58 | this.code = code; 59 | this.desc = desc; 60 | } 61 | 62 | @Override 63 | public int getCode() { 64 | return code; 65 | } 66 | 67 | @Override 68 | public String getDesc() { 69 | return desc; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/constant/Status.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.constant; 2 | 3 | /** 4 | * @author geektcp on 2018/5/15. 5 | */ 6 | public interface Status { 7 | int getCode(); 8 | 9 | String getDesc(); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/exception/ExecutorException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.exception; 19 | 20 | public class ExecutorException extends Exception { 21 | private static final long serialVersionUID = 7830266012832686185L; 22 | 23 | /** 24 | * Constructs an {@code ExecutorException} with no detail message. 25 | * The cause is not initialized, and may subsequently be 26 | * initialized by a call to {@link #initCause(Throwable) initCause}. 27 | */ 28 | public ExecutorException() { } 29 | 30 | /** 31 | * Constructs an {@code ExecutorException} with the specified detail 32 | * message. The cause is not initialized, and may subsequently be 33 | * initialized by a call to {@link #initCause(Throwable) initCause}. 34 | * 35 | * @param message the detail message 36 | */ 37 | public ExecutorException(String message) { 38 | super(message); 39 | } 40 | 41 | /** 42 | * Constructs an {@code ExecutorException} with the specified detail 43 | * message and cause. 44 | * 45 | * @param message the detail message 46 | * @param cause the cause (which is saved for later retrieval by the 47 | * {@link #getCause()} method) 48 | */ 49 | public ExecutorException(String message, Throwable cause) { 50 | super(message, cause); 51 | } 52 | 53 | /** 54 | * Constructs an {@code ExecutorException} with the specified cause. 55 | * The detail message is set to {@code (cause == null ? null : 56 | * cause.toString())} (which typically contains the class and 57 | * detail message of {@code cause}). 58 | * 59 | * @param cause the cause (which is saved for later retrieval by the 60 | * {@link #getCause()} method) 61 | */ 62 | public ExecutorException(Throwable cause) { 63 | super(cause); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/exception/MoshehException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.exception; 19 | 20 | 21 | /** 22 | * @author geektcp on 2018/5/16. 23 | */ 24 | public class MoshehException extends BaseException { 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/exception/StatusException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.exception; 20 | 21 | 22 | import com.geektcp.common.mosheh.constant.Status; 23 | 24 | /** 25 | * @author geektcp on 2018/5/16. 26 | */ 27 | public class StatusException extends BaseException { 28 | 29 | private Status status; 30 | 31 | public StatusException(Status status) { 32 | this(status, new String[]{}); 33 | } 34 | 35 | public StatusException(Status status, Object... args) { 36 | this(status, null, args); 37 | } 38 | 39 | public StatusException(Status status, Throwable cause) { 40 | super(status.getCode(), status.getDesc(), cause); 41 | this.status = status; 42 | } 43 | 44 | public StatusException(Status status, Throwable cause, Object... args) { 45 | super(status.getCode(), status.getDesc(), cause, args); 46 | this.status = status; 47 | } 48 | 49 | public Status getStatus() { 50 | return status; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/parser/Expression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.parser; 20 | 21 | /** 22 | * @author geektcp on 2023/2/22 1:14. 23 | */ 24 | public interface Expression { 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/parser/GqlParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.parser; 20 | 21 | /** 22 | * @author geektcp on 2023/2/22 1:14. 23 | */ 24 | public class GqlParser { 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/parser/Parser.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.parser; 2 | 3 | /** 4 | * @author geektcp on 2023/2/22 1:14. 5 | */ 6 | public interface Parser { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/parser/SearchParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.parser; 20 | 21 | /** 22 | * @author geektcp on 2023/2/22 1:14. 23 | */ 24 | public class SearchParser { 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/parser/SqlParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.parser; 20 | 21 | /** 22 | * @author geektcp on 2023/2/22 1:14. 23 | */ 24 | public class SqlParser { 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/parser/SyntaxParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.parser; 20 | 21 | /** 22 | * @author geektcp on 2023/2/22 1:14. 23 | */ 24 | public class SyntaxParser { 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/socket/buffer/DataOutputBuffer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.socket.buffer; 19 | 20 | import java.io.*; 21 | 22 | /** 23 | * @author geektcp on 2023/2/22 1:14. 24 | */ 25 | public class DataOutputBuffer extends DataOutputStream { 26 | 27 | private static class Buffer extends ByteArrayOutputStream { 28 | 29 | byte[] getData() { 30 | return buf; 31 | } 32 | int getLength() { 33 | return count; 34 | } 35 | Buffer() { 36 | super(); 37 | } 38 | Buffer(int size) { 39 | super(size); 40 | } 41 | 42 | public void write(DataInput in, int len) throws IOException { 43 | int newCount = count + len; 44 | if (newCount > buf.length) { 45 | byte[] newBuf = new byte[Math.max(buf.length << 1, newCount)]; 46 | System.arraycopy(buf, 0, newBuf, 0, count); 47 | buf = newBuf; 48 | } 49 | in.readFully(buf, count, len); 50 | count = newCount; 51 | } 52 | } 53 | 54 | private Buffer buffer; 55 | 56 | public DataOutputBuffer() { 57 | this(new Buffer()); 58 | } 59 | 60 | public DataOutputBuffer(int size) { 61 | this(new Buffer(size)); 62 | } 63 | 64 | private DataOutputBuffer(Buffer buffer) { 65 | super(buffer); 66 | this.buffer = buffer; 67 | } 68 | 69 | public byte[] getData() { 70 | return buffer.getData(); 71 | } 72 | 73 | public int getLength() { 74 | return buffer.getLength(); 75 | } 76 | 77 | public DataOutputBuffer reset() { 78 | this.written = 0; 79 | buffer.reset(); 80 | return this; 81 | } 82 | 83 | public void write(DataInput in, int length) throws IOException { 84 | buffer.write(in, length); 85 | } 86 | 87 | public void writeTo(OutputStream out) throws IOException { 88 | buffer.writeTo(out); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/socket/builder/MoshehClientBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.socket.builder; 20 | 21 | import com.geektcp.common.mosheh.socket.buffer.DataOutputBuffer; 22 | import com.geektcp.common.mosheh.socket.text.ConnectionBody; 23 | import com.geektcp.common.mosheh.socket.text.ConnectionHeader; 24 | import com.geektcp.common.mosheh.system.Sys; 25 | 26 | import java.io.*; 27 | import java.net.InetSocketAddress; 28 | import java.net.Socket; 29 | import java.net.SocketAddress; 30 | 31 | 32 | /** 33 | * @author geektcp on 2023/2/2 1:04. 34 | */ 35 | public class MoshehClientBuilder { 36 | 37 | private static String SERVER_IP = "127.0.0.1"; 38 | private static int PORT = 10017; 39 | private static Socket socket; 40 | 41 | public static void main(String[] args) { 42 | try { 43 | while (true) { 44 | try { 45 | socket = new Socket(); 46 | SocketAddress remoteAddr = new InetSocketAddress(SERVER_IP, PORT); 47 | socket.connect(remoteAddr, 60000); 48 | socket.setTcpNoDelay(false); 49 | break; 50 | } catch (IOException ie) { 51 | try { 52 | socket.close(); 53 | } catch (IOException e) { 54 | e.printStackTrace(); 55 | } 56 | 57 | socket = null; 58 | Sys.sleep(1000); 59 | } 60 | } 61 | 62 | DataInputStream in = new DataInputStream( 63 | new BufferedInputStream(socket.getInputStream())); 64 | DataOutputStream out = new DataOutputStream( 65 | new BufferedOutputStream(socket.getOutputStream())); 66 | 67 | DataOutputBuffer buf = new DataOutputBuffer(); 68 | ConnectionHeader header = new ConnectionHeader("TLV"); 69 | header.write(buf); 70 | 71 | int bufLen = buf.getLength(); 72 | out.writeInt(bufLen); 73 | out.write(buf.getData(), 0, bufLen); 74 | out.flush(); 75 | 76 | DataOutputBuffer buf2 = new DataOutputBuffer(); 77 | ConnectionBody writeBody = new ConnectionBody("are you ok!!!"); 78 | writeBody.write(buf2); 79 | 80 | bufLen = buf2.getLength(); 81 | out.writeInt(bufLen); 82 | out.write(buf2.getData(), 0, bufLen); 83 | out.flush(); 84 | 85 | ConnectionBody readBody = new ConnectionBody(); 86 | readBody.readFields(in); 87 | out.close(); 88 | in.close(); 89 | socket.close(); 90 | Sys.p("test over!!!"); 91 | }catch (IOException e) { 92 | e.printStackTrace(); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/socket/builder/MoshehServerBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.socket.builder; 20 | 21 | import com.geektcp.common.mosheh.socket.server.MoshehServer; 22 | import com.geektcp.common.mosheh.system.Sys; 23 | 24 | import java.io.IOException; 25 | 26 | /** 27 | * @author geektcp on 2023/2/2 1:04. 28 | */ 29 | public class MoshehServerBuilder { 30 | 31 | private static String SERVER_IP = "127.0.0.1"; 32 | private static int PORT = 10017; 33 | 34 | public static void start() throws IOException { 35 | Sys.p("mosheh Server started !!!"); 36 | 37 | MoshehServer moshehServer = new MoshehServer(SERVER_IP, PORT); 38 | try { 39 | moshehServer.start(); 40 | } catch (IOException e) { 41 | moshehServer.stop(); 42 | throw e; 43 | } 44 | 45 | try { 46 | moshehServer.join(); 47 | } catch (Exception e) { 48 | Sys.p(e.getMessage()); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/socket/comparator/BinaryComparable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.socket.comparator; 19 | /** 20 | * @author geektcp on 2018/2/22 1:14. 21 | */ 22 | public abstract class BinaryComparable implements Comparable { 23 | 24 | public abstract int getLength(); 25 | 26 | public abstract byte[] getBytes(); 27 | 28 | public int compareTo(BinaryComparable other) { 29 | if (this == other) { 30 | return 0; 31 | } 32 | 33 | return WritableComparator.compareBytes(getBytes(), 0, getLength(), 34 | other.getBytes(), 0, other.getLength()); 35 | } 36 | 37 | public int compareTo(byte[] other, int off, int len) { 38 | return WritableComparator.compareBytes(getBytes(), 0, getLength(), 39 | other, off, len); 40 | } 41 | 42 | public boolean equals(Object other) { 43 | if (!(other instanceof BinaryComparable)) { 44 | return false; 45 | } 46 | 47 | BinaryComparable that = (BinaryComparable) other; 48 | if (this.getLength() != that.getLength()) { 49 | return false; 50 | } 51 | 52 | return this.compareTo(that) == 0; 53 | } 54 | 55 | public int hashCode() { 56 | return WritableComparator.hashBytes(getBytes(), getLength()); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/socket/comparator/RawComparator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.socket.comparator; 19 | 20 | import java.util.Comparator; 21 | /** 22 | * @author geektcp on 2018/2/22 1:14. 23 | */ 24 | public interface RawComparator extends Comparator { 25 | 26 | public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2); 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/socket/comparator/Writable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.socket.comparator; 19 | 20 | import java.io.DataInput; 21 | import java.io.DataOutput; 22 | import java.io.IOException; 23 | /** 24 | * @author geektcp on 2018/2/22 1:14. 25 | */ 26 | public interface Writable { 27 | 28 | void write(DataOutput out) throws IOException; 29 | 30 | void readFields(DataInput in) throws IOException; 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/socket/comparator/WritableComparable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.socket.comparator; 19 | /** 20 | * @author geektcp on 2018/2/22 1:14. 21 | */ 22 | public interface WritableComparable extends Writable, Comparable { 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/socket/constant/Status.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.socket.constant; 19 | /** 20 | * @author geektcp on 2018/2/22 1:14. 21 | */ 22 | public enum Status { 23 | SUCCESS(0), 24 | ERROR(1), 25 | FATAL(-1); 26 | 27 | int state; 28 | 29 | private Status(int state) { 30 | this.state = state; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/socket/server/call/MoshehCall.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.socket.server.call; 19 | 20 | import com.geektcp.common.mosheh.socket.server.connection.MoshehConnection; 21 | import com.geektcp.common.mosheh.socket.server.responder.MoshehResponder; 22 | 23 | import java.nio.ByteBuffer; 24 | 25 | /** 26 | * @author geektcp on 2023/4/5 20:57. 27 | */ 28 | 29 | public class MoshehCall { 30 | 31 | String str; 32 | private MoshehConnection connection; 33 | private long timestamp; 34 | 35 | private ByteBuffer response; 36 | 37 | public MoshehCall(String str, MoshehConnection connection, MoshehResponder responder) { 38 | this.str = str; 39 | this.connection = connection; 40 | this.timestamp = System.currentTimeMillis(); 41 | this.response = null; 42 | } 43 | 44 | public synchronized void setResponse(ByteBuffer response) { 45 | this.response = response; 46 | } 47 | 48 | public String toString() { 49 | return connection.toString(); 50 | } 51 | 52 | public MoshehConnection getConnection(){ 53 | return connection; 54 | } 55 | 56 | public long getTimestamp(){ 57 | return timestamp; 58 | } 59 | 60 | public void setTimestamp(long timestamp){ 61 | this.timestamp = timestamp; 62 | } 63 | 64 | public String getStr(){ 65 | return str; 66 | } 67 | 68 | 69 | public ByteBuffer getResponse(){ 70 | return response; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/socket/server/config/MoshehConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.socket.server.config; 19 | 20 | import com.geektcp.common.mosheh.socket.server.connection.MoshehConnection; 21 | import lombok.Data; 22 | 23 | import java.util.Collections; 24 | import java.util.LinkedList; 25 | import java.util.List; 26 | 27 | 28 | /** 29 | * @author geektcp on 2023/4/5 21:13. 30 | */ 31 | @Data 32 | public class MoshehConfig { 33 | 34 | public static String IP = "127.0.0.1"; 35 | public static int PORT = 8090; 36 | 37 | public static int numConnections = 0; 38 | 39 | 40 | public static final int readThreads = 5; 41 | public static final int workerThreads = 10; 42 | public static final int thresholdIdleConnections = 4000; 43 | public static final int maxConnectionsToNuke = 10; 44 | public static final int maxIdleTime = 2000; 45 | 46 | public static final int NIO_BUFFER_LIMIT = 8192; 47 | public static final int INITIAL_RESP_BUF_SIZE = 10240; 48 | 49 | public static final int maxRespSize = 1024 * 1024; 50 | public static final int callQueueCapacity = 4096 * 100; 51 | 52 | 53 | 54 | /////////////////////////////////// 55 | public static volatile boolean running = true; 56 | 57 | 58 | public static List connectionList = Collections.synchronizedList(new LinkedList<>()); 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/socket/text/ConnectionBody.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.socket.text; 20 | 21 | import com.geektcp.common.mosheh.socket.comparator.Writable; 22 | import com.geektcp.common.mosheh.system.Sys; 23 | import lombok.extern.slf4j.Slf4j; 24 | 25 | import java.io.DataInput; 26 | import java.io.DataOutput; 27 | import java.io.IOException; 28 | 29 | /** 30 | * @author geektcp on 2018/2/22 1:14. 31 | */ 32 | public class ConnectionBody implements Writable { 33 | 34 | private String body; 35 | 36 | public ConnectionBody() { 37 | 38 | } 39 | 40 | public ConnectionBody(String body) { 41 | this.body = body; 42 | } 43 | 44 | public void readFields(DataInput in) throws IOException { 45 | body = Text.readString(in); 46 | if (body.isEmpty()) { 47 | body = null; 48 | } else { 49 | Sys.p("The body is: {}", body); 50 | } 51 | } 52 | 53 | public void write(DataOutput out) throws IOException { 54 | Text.writeString(out, (body == null) ? "" : body); 55 | } 56 | 57 | public String getBody() { 58 | return body; 59 | } 60 | 61 | public String toString() { 62 | return body; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/socket/text/ConnectionHeader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.socket.text; 20 | 21 | import com.geektcp.common.mosheh.socket.comparator.Writable; 22 | import com.geektcp.common.mosheh.system.Sys; 23 | 24 | import java.io.DataInput; 25 | import java.io.DataOutput; 26 | import java.io.IOException; 27 | /** 28 | * @author geektcp on 2018/2/22 1:14. 29 | */ 30 | public class ConnectionHeader implements Writable { 31 | 32 | private String protocol; 33 | 34 | public ConnectionHeader() { 35 | 36 | } 37 | 38 | public ConnectionHeader(String protocol) { 39 | this.protocol = protocol; 40 | } 41 | 42 | public void readFields(DataInput in) throws IOException { 43 | protocol = Text.readString(in); 44 | if (protocol.isEmpty()) { 45 | protocol = null; 46 | } else { 47 | Sys.p("The protocol is: {}", protocol); 48 | } 49 | } 50 | 51 | public void write(DataOutput out) throws IOException { 52 | Text.writeString(out, (protocol == null) ? "" : protocol); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/sort/Bubble.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.sort; 20 | 21 | 22 | /** 23 | * @author geektcp on 2019/9/23. 24 | */ 25 | public class Bubble> extends Sort { 26 | 27 | @Override 28 | public void sort(T[] numberArray) { 29 | int N = numberArray.length; 30 | boolean isSorted = false; 31 | for (int i = N - 1; i > 0 && !isSorted; i--) { 32 | isSorted = true; 33 | for (int j = 0; j < i; j++) { 34 | if (less(numberArray[j + 1], numberArray[j])) { 35 | isSorted = false; 36 | swap(numberArray, j, j + 1); 37 | } 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/sort/Heap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.sort; 20 | 21 | /** 22 | * @author geektcp on 2019/9/23. 23 | */ 24 | public class Heap> { 25 | 26 | private T[] heapArray; 27 | private int seq = 0; 28 | 29 | public Heap(int maxSeq) { 30 | this.heapArray = (T[]) new Comparable[maxSeq + 1]; 31 | } 32 | 33 | public boolean isEmpty() { 34 | return seq == 0; 35 | } 36 | 37 | public int size() { 38 | return seq; 39 | } 40 | 41 | private boolean less(int i, int j) { 42 | return heapArray[i].compareTo(heapArray[j]) < 0; 43 | } 44 | 45 | private void swap(int i, int j) { 46 | T t = heapArray[i]; 47 | heapArray[i] = heapArray[j]; 48 | heapArray[j] = t; 49 | } 50 | 51 | private void swim(int k) { 52 | while (k > 1 && less(k / 2, k)) { 53 | swap(k / 2, k); 54 | k = k / 2; 55 | } 56 | } 57 | 58 | private void sink(int k) { 59 | while (2 * k <= seq) { 60 | int j = 2 * k; 61 | if (j < seq && less(j, j + 1)) 62 | j++; 63 | if (!less(k, j)) 64 | break; 65 | swap(k, j); 66 | k = j; 67 | } 68 | } 69 | 70 | public void insert(T v) { 71 | heapArray[++seq] = v; 72 | swim(seq); 73 | } 74 | 75 | public T delMax() { 76 | T max = heapArray[1]; 77 | swap(1, seq--); 78 | heapArray[seq + 1] = null; 79 | sink(1); 80 | return max; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/sort/HeapSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.sort; 20 | 21 | /** 22 | * @author geektcp on 2019/9/23. 23 | */ 24 | public class HeapSort> extends Sort { 25 | 26 | @Override 27 | public void sort(T[] numberArray) { 28 | int N = numberArray.length - 1; 29 | for (int k = N / 2; k >= 1; k--) 30 | sink(numberArray, k, N); 31 | 32 | while (N > 1) { 33 | swap(numberArray, 1, N--); 34 | sink(numberArray, 1, N); 35 | } 36 | } 37 | 38 | private void sink(T[] numberArray, int k, int N) { 39 | while (2 * k <= N) { 40 | int j = 2 * k; 41 | if (j < N && less(numberArray, j, j + 1)) 42 | j++; 43 | if (!less(numberArray, k, j)) 44 | break; 45 | swap(numberArray, k, j); 46 | k = j; 47 | } 48 | } 49 | 50 | private boolean less(T[] numberArray, int i, int j) { 51 | return numberArray[i].compareTo(numberArray[j]) < 0; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/sort/Insertion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.sort; 20 | 21 | /** 22 | * @author geektcp on 2019/9/23. 23 | */ 24 | public class Insertion> extends Sort { 25 | 26 | @Override 27 | public void sort(T[] numberArray) { 28 | int N = numberArray.length; 29 | for (int i = 1; i < N; i++) { 30 | for (int j = i; j > 0 && less(numberArray[j], numberArray[j - 1]); j--) { 31 | swap(numberArray, j, j - 1); 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/sort/MergeSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.sort; 20 | 21 | 22 | /** 23 | * @author geektcp on 2019/9/23. 24 | */ 25 | public abstract class MergeSort> extends Sort { 26 | 27 | protected T[] aux; 28 | 29 | 30 | protected void merge(T[] numberArray, int l, int m, int h) { 31 | 32 | int i = l, j = m + 1; 33 | 34 | for (int k = l; k <= h; k++) { 35 | aux[k] = numberArray[k]; 36 | } 37 | 38 | for (int k = l; k <= h; k++) { 39 | if (i > m) { 40 | numberArray[k] = aux[j++]; 41 | 42 | } else if (j > h) { 43 | numberArray[k] = aux[i++]; 44 | 45 | } else if (aux[i].compareTo(aux[j]) <= 0) { 46 | numberArray[k] = aux[i++]; 47 | 48 | } else { 49 | numberArray[k] = aux[j++]; 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/sort/MergeSortDown.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.sort; 20 | 21 | /** 22 | * @author geektcp on 2019/9/23. 23 | */ 24 | public class MergeSortDown> extends MergeSort { 25 | 26 | @Override 27 | public void sort(T[] numberArray) { 28 | aux = (T[]) new Comparable[numberArray.length]; 29 | sort(numberArray, 0, numberArray.length - 1); 30 | } 31 | 32 | private void sort(T[] numberArray, int l, int h) { 33 | if (h <= l) { 34 | return; 35 | } 36 | int mid = l + (h - l) / 2; 37 | sort(numberArray, l, mid); 38 | sort(numberArray, mid + 1, h); 39 | merge(numberArray, l, mid, h); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/sort/MergeSortUp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.sort; 20 | 21 | /** 22 | * @author geektcp on 2019/9/23. 23 | */ 24 | public class MergeSortUp> extends MergeSort { 25 | 26 | @Override 27 | public void sort(T[] numberArray) { 28 | 29 | int N = numberArray.length; 30 | aux = (T[]) new Comparable[N]; 31 | 32 | for (int sz = 1; sz < N; sz += sz) { 33 | for (int lo = 0; lo < N - sz; lo += sz + sz) { 34 | merge(numberArray, lo, lo + sz - 1, Math.min(lo + sz + sz - 1, N - 1)); 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/sort/QuickSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.sort; 20 | 21 | 22 | import java.util.Arrays; 23 | import java.util.Collections; 24 | import java.util.List; 25 | 26 | /** 27 | * @author geektcp on 2019/9/23. 28 | */ 29 | public class QuickSort> extends Sort { 30 | 31 | @Override 32 | public void sort(T[] numberArray) { 33 | shuffle(numberArray); 34 | sort(numberArray, 0, numberArray.length - 1); 35 | } 36 | 37 | private void sort(T[] numberArray, int l, int h) { 38 | if (h <= l) 39 | return; 40 | int j = partition(numberArray, l, h); 41 | sort(numberArray, l, j - 1); 42 | sort(numberArray, j + 1, h); 43 | } 44 | 45 | private void shuffle(T[] numberArray) { 46 | List list = Arrays.asList(numberArray); 47 | Collections.shuffle(list); 48 | list.toArray(numberArray); 49 | } 50 | 51 | private int partition(T[] numberArray, int l, int h) { 52 | int i = l, j = h + 1; 53 | T v = numberArray[l]; 54 | while (true) { 55 | while (less(numberArray[++i], v) && i != h) ; 56 | while (less(v, numberArray[--j]) && j != l) ; 57 | if (i >= j) 58 | break; 59 | swap(numberArray, i, j); 60 | } 61 | swap(numberArray, l, j); 62 | return j; 63 | } 64 | 65 | public T select(T[] numberArray, int k) { 66 | int l = 0, h = numberArray.length - 1; 67 | while (h > l) { 68 | int j = partition(numberArray, l, h); 69 | 70 | if (j == k) { 71 | return numberArray[k]; 72 | 73 | } else if (j > k) { 74 | h = j - 1; 75 | 76 | } else { 77 | l = j + 1; 78 | } 79 | } 80 | return numberArray[k]; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/sort/Selection.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.sort; 2 | 3 | /** 4 | * @author geektcp on 2019/9/23. 5 | */ 6 | public class Selection> extends Sort { 7 | 8 | @Override 9 | public void sort(T[] numberArray) { 10 | int N = numberArray.length; 11 | for (int i = 0; i < N - 1; i++) { 12 | int min = i; 13 | for (int j = i + 1; j < N; j++) { 14 | if (less(numberArray[j], numberArray[min])) { 15 | min = j; 16 | } 17 | } 18 | swap(numberArray, i, min); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/sort/Shell.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.sort; 20 | 21 | /** 22 | * @author geektcp on 2019/9/23. 23 | */ 24 | public class Shell> extends Sort { 25 | 26 | @Override 27 | public void sort(T[] numberArray) { 28 | 29 | int length = numberArray.length; 30 | int h = 1; 31 | 32 | while (h < length / 3) { 33 | h = 3 * h + 1; // 1, 4, 13, 40, ... 34 | } 35 | 36 | while (h >= 1) { 37 | for (int i = h; i < length; i++) { 38 | for (int j = i; j >= h && less(numberArray[j], numberArray[j - h]); j -= h) { 39 | swap(numberArray, j, j - h); 40 | } 41 | } 42 | h = h / 3; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/sort/Sort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.sort; 20 | 21 | /** 22 | * @author geektcp on 2019/9/23. 23 | */ 24 | public abstract class Sort> { 25 | 26 | public abstract void sort(T[] numberArray); 27 | 28 | protected boolean less(T v, T w) { 29 | return v.compareTo(w) < 0; 30 | } 31 | 32 | protected void swap(T[] a, int i, int j) { 33 | T t = a[i]; 34 | a[i] = a[j]; 35 | a[j] = t; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/system/ThyClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.system; 19 | 20 | import java.util.Objects; 21 | 22 | /** 23 | * @author geektcp on 2018/3/15. 24 | */ 25 | class ThyClass { 26 | 27 | private ThyClass() { 28 | } 29 | 30 | public static Class getClass(String className) { 31 | if (className == null) { 32 | return null; 33 | } 34 | Class cls = null; 35 | try { 36 | cls = Class.forName(className); 37 | } catch (ClassNotFoundException e) { 38 | Sys.p("exception: + " + className + e.getMessage()); 39 | } 40 | return cls; 41 | } 42 | 43 | public static String getClassPath(String className) { 44 | Class cls = getClass(className); 45 | return getClassPath(cls); 46 | } 47 | 48 | public static String getClassPath(Class cls) { 49 | if(Objects.isNull(cls)) { 50 | return null; 51 | } 52 | return cls.getResource("").getPath(); 53 | } 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/system/ThyCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.system; 19 | 20 | import com.geektcp.common.mosheh.collection.Lists; 21 | 22 | import java.io.BufferedReader; 23 | import java.io.File; 24 | import java.io.InputStreamReader; 25 | import java.util.ArrayList; 26 | 27 | /** 28 | * @author geektcp on 2022/2/5. 29 | */ 30 | class ThyCommand { 31 | 32 | private File dir = null; 33 | private ArrayList cmdList = new ArrayList<>(); 34 | private static ThyCommand instance = new ThyCommand(); 35 | 36 | private ThyCommand() { 37 | } 38 | 39 | public static ThyCommand getInstance(){ 40 | return instance; 41 | } 42 | 43 | public ThyCommand runDir(String dir) { 44 | this.dir = new File(dir); 45 | return this; 46 | } 47 | 48 | public ThyCommand program(String command) { 49 | cmdList.add(0, command); 50 | return this; 51 | } 52 | 53 | public ThyCommand arg(String arg) { 54 | cmdList.add(arg); 55 | return this; 56 | } 57 | 58 | public Process start() { 59 | String[] cmdArray = Lists.toArray(cmdList, String.class); 60 | Process process = null; 61 | try { 62 | process = new ProcessBuilder(cmdArray) 63 | .directory(dir) 64 | .start(); 65 | } catch (Exception e) { 66 | Sys.p(e.getMessage()); 67 | } 68 | 69 | return process; 70 | } 71 | 72 | /** 73 | * 74 | * @param cmdArray single command which is String[] type 75 | * @param dir run command dir 76 | * @return result Stream 77 | */ 78 | public static Process execute(String[] cmdArray, File dir) { 79 | Process process = null; 80 | try { 81 | process = new ProcessBuilder(cmdArray) 82 | .directory(dir) 83 | .start(); 84 | } catch (Exception e) { 85 | Sys.p(e.getMessage()); 86 | } 87 | 88 | return process; 89 | } 90 | 91 | public static void printCommandResult(Process process) { 92 | BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 93 | String line = ""; 94 | try { 95 | while ((line = reader.readLine()) != null) { 96 | Sys.p(line); 97 | } 98 | }catch (Exception e){ 99 | Sys.p(e.getMessage()); 100 | } 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/system/ThyEnv.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.system; 19 | 20 | import java.util.Map; 21 | 22 | /** 23 | * @author geektcp on 2023/2/5 1:28. 24 | */ 25 | class ThyEnv { 26 | 27 | private ThyEnv() { 28 | } 29 | 30 | private static Map env = System.getenv(); 31 | 32 | public static String getEnv(String name) { 33 | return env.getOrDefault(name, ""); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/system/ThyProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.system; 19 | 20 | import java.util.Properties; 21 | 22 | /** 23 | * @author geektcp on 2023/2/5 0:08. 24 | */ 25 | class ThyProperties { 26 | 27 | private ThyProperties() { 28 | } 29 | 30 | private static Properties prop = System.getProperties(); 31 | 32 | public static Properties getProperties() { 33 | return prop; 34 | } 35 | 36 | public static String getUserHome() { 37 | return prop.getProperty("user.home"); 38 | } 39 | 40 | public static String getUserDir() { 41 | return prop.getProperty("user.dir"); 42 | } 43 | 44 | public static String getUserName() { 45 | return prop.getProperty("user.name"); 46 | } 47 | 48 | public static String getTmpdir() { 49 | return prop.getProperty("java.io.tmpdir"); 50 | } 51 | 52 | 53 | public static String getUserLanguage() { 54 | return prop.getProperty("user.language"); 55 | } 56 | 57 | public static String getUserTimezone() { 58 | return prop.getProperty("user.timezone"); 59 | } 60 | 61 | public static String getOsName() { 62 | return prop.getProperty("os.name"); 63 | } 64 | 65 | public static boolean isWindows() { 66 | return getOsName().contains("Windows"); 67 | } 68 | 69 | public static boolean isLinux() { 70 | return getOsName().contains("Linux"); 71 | } 72 | 73 | public static String getOsVersion() { 74 | return prop.getProperty("os.version"); 75 | } 76 | 77 | public static String getJdkVersion() { 78 | return prop.getProperty("java.version"); 79 | } 80 | 81 | public static String getFileEncoding() { 82 | return prop.getProperty("file.encoding"); 83 | } 84 | 85 | public static String getOsArch() { 86 | return prop.getProperty("os.arch"); 87 | } 88 | 89 | public static String getJdkArch() { 90 | return prop.getProperty("sun.arch.data.model"); 91 | } 92 | 93 | public static int getOsBit() { 94 | String osArch = getOsArch(); 95 | if (osArch.contains("64")) { 96 | return 64; 97 | } 98 | return 32; 99 | } 100 | 101 | public static int getJdkBit() { 102 | String jdkArch = getJdkArch(); 103 | if (jdkArch.contains("64")) { 104 | return 64; 105 | } 106 | return 32; 107 | } 108 | 109 | public static String getJavaClassPath() { 110 | return prop.getProperty("java.class.path"); 111 | } 112 | 113 | public static String getFileSeparator() { 114 | return prop.getProperty("file.separator"); 115 | } 116 | 117 | 118 | } 119 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/system/ThyResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.system; 19 | 20 | import java.net.URL; 21 | import java.util.Objects; 22 | 23 | /** 24 | * @author geektcp on 2023/2/13 19:58. 25 | */ 26 | class ThyResource { 27 | 28 | private ThyResource() { 29 | } 30 | 31 | public static String getResourceRootPath() { 32 | return getResourceClassPath("/", ThyFileSystem.class); 33 | } 34 | 35 | public static String getResourceClassPath() { 36 | return getResourceClassPath("", ThyFileSystem.class); 37 | } 38 | 39 | public static String getResourceClassPath(String name) { 40 | return getResourceClassPath(name, ThyFileSystem.class); 41 | } 42 | 43 | public static String getResourceClassPath(Class cls) { 44 | return getResourceClassPath("", cls); 45 | } 46 | 47 | public static String getResourceClassPath(String name, Class cls) { 48 | if (Objects.isNull(name) || Objects.isNull(cls)) { 49 | return ""; 50 | } 51 | URL url = cls.getResource(name); 52 | if (Objects.isNull(url)) { 53 | return ""; 54 | } 55 | return url.getPath(); 56 | } 57 | 58 | public static String getResourcePath() { 59 | return getResourcePath(""); 60 | } 61 | 62 | public static String getResourcePath(String name) { 63 | String rootPath = getResourceRootPath(); 64 | if (Objects.isNull(name)) { 65 | return rootPath; 66 | } 67 | if (name.startsWith("/")) { 68 | name = name.substring(1); 69 | } 70 | return rootPath + name; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/system/ThyRuntime.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.system; 19 | 20 | import java.io.IOException; 21 | 22 | /** 23 | * @author geektcp on 2023/2/4 23:47. 24 | */ 25 | class ThyRuntime { 26 | 27 | private ThyRuntime() { 28 | } 29 | 30 | public static long maxMemory() { 31 | return Runtime.getRuntime().maxMemory(); 32 | } 33 | 34 | public static long totalMemory() { 35 | return Runtime.getRuntime().totalMemory(); 36 | } 37 | 38 | public static long freeMemory() { 39 | return Runtime.getRuntime().freeMemory(); 40 | } 41 | 42 | public static int availableProcessors() { 43 | return Runtime.getRuntime().availableProcessors(); 44 | } 45 | 46 | public static Process exec(String cmd) { 47 | Process process = null; 48 | try { 49 | process = Runtime.getRuntime().exec(cmd); 50 | } catch (IOException e) { 51 | Sys.p(e.getMessage()); 52 | } 53 | return process; 54 | } 55 | 56 | public static void gc() { 57 | Runtime.getRuntime().gc(); 58 | } 59 | 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/tree/AVLTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.tree; 20 | 21 | /** 22 | * @author geektcp on 2019/1/21. 23 | */ 24 | public class AVLTree { 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/tree/BaseBinaryTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.tree; 20 | 21 | /** 22 | * @author geektcp on 2019/1/21. 23 | */ 24 | public class BaseBinaryTree extends AbstractTree{ 25 | 26 | @Override 27 | protected Node createNode(int value, Node parent, Node left, Node right){ 28 | 29 | return null; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/tree/BinarySearchTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.tree; 20 | 21 | /** 22 | * @author geektcp on 2019/1/21. 23 | */ 24 | public class BinarySearchTree { 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/tree/Btree.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.tree; 20 | 21 | /** 22 | * @author geektcp on 2019/1/21. 23 | */ 24 | public class Btree { 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/tree/node/AbstractBinaryNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.tree.node; 20 | 21 | import com.geektcp.common.mosheh.tree.RedBlackTree; 22 | 23 | /** 24 | * @author geektcp on 2023/8/3 10:29. 25 | */ 26 | public abstract class AbstractBinaryNode implements Node { 27 | 28 | protected boolean root = false; 29 | 30 | private Node left; 31 | private Node right; 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/tree/node/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.tree.node; 20 | 21 | import java.io.Serializable; 22 | 23 | /** 24 | * @author geektcp on 2023/8/3 10:30. 25 | */ 26 | public interface Node extends Serializable { 27 | 28 | boolean isRoot(); 29 | 30 | 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/tree/st/BinarySearchOrderedST.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.tree.st; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | /** 25 | * @author geektcp on 2019/9/28. 26 | */ 27 | public class BinarySearchOrderedST, Value> implements OrderedST { 28 | 29 | private Key[] keys; 30 | private Value[] values; 31 | private int N = 0; 32 | 33 | public BinarySearchOrderedST(int capacity) { 34 | keys = (Key[]) new Comparable[capacity]; 35 | values = (Value[]) new Object[capacity]; 36 | } 37 | 38 | @Override 39 | public int size() { 40 | return N; 41 | } 42 | 43 | @Override 44 | public int rank(Key key) { 45 | int l = 0, h = N - 1; 46 | while (l <= h) { 47 | int m = l + (h - l) / 2; 48 | int cmp = key.compareTo(keys[m]); 49 | if (cmp == 0) 50 | return m; 51 | else if (cmp < 0) 52 | h = m - 1; 53 | else 54 | l = m + 1; 55 | } 56 | return l; 57 | } 58 | 59 | @Override 60 | public List keys(Key l, Key h) { 61 | int index = rank(l); 62 | List list = new ArrayList<>(); 63 | while (keys[index].compareTo(h) <= 0) { 64 | list.add(keys[index]); 65 | index++; 66 | } 67 | return list; 68 | } 69 | 70 | @Override 71 | public void put(Key key, Value value) { 72 | int index = rank(key); 73 | if (index < N && keys[index].compareTo(key) == 0) { 74 | values[index] = value; 75 | return; 76 | } 77 | for (int j = N; j > index; j--) { 78 | keys[j] = keys[j - 1]; 79 | values[j] = values[j - 1]; 80 | } 81 | keys[index] = key; 82 | values[index] = value; 83 | N++; 84 | } 85 | 86 | @Override 87 | public Value get(Key key) { 88 | int index = rank(key); 89 | if (index < N && keys[index].compareTo(key) == 0) 90 | return values[index]; 91 | return null; 92 | } 93 | 94 | @Override 95 | public Key min() { 96 | return keys[0]; 97 | } 98 | 99 | @Override 100 | public Key max() { 101 | return keys[N - 1]; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/tree/st/ListUnorderedST.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.tree.st; 20 | 21 | /** 22 | * @author geektcp on 2019/9/28. 23 | */ 24 | public class ListUnorderedST implements UnorderedST { 25 | 26 | private Node first; 27 | 28 | private class Node { 29 | Key key; 30 | Value value; 31 | Node next; 32 | 33 | Node(Key key, Value value, Node next) { 34 | this.key = key; 35 | this.value = value; 36 | this.next = next; 37 | } 38 | } 39 | 40 | @Override 41 | public int size() { 42 | int cnt = 0; 43 | Node cur = first; 44 | while (cur != null) { 45 | cnt++; 46 | cur = cur.next; 47 | } 48 | return cnt; 49 | } 50 | 51 | @Override 52 | public void put(Key key, Value value) { 53 | Node cur = first; 54 | while (cur != null) { 55 | if (cur.key.equals(key)) { 56 | cur.value = value; 57 | return; 58 | } 59 | cur = cur.next; 60 | } 61 | first = new Node(key, value, first); 62 | } 63 | 64 | @Override 65 | public void delete(Key key) { 66 | if (first == null) 67 | return; 68 | if (first.key.equals(key)) 69 | first = first.next; 70 | Node pre = first, cur = first.next; 71 | while (cur != null) { 72 | if (cur.key.equals(key)) { 73 | pre.next = cur.next; 74 | return; 75 | } 76 | pre = pre.next; 77 | cur = cur.next; 78 | } 79 | } 80 | 81 | @Override 82 | public Value get(Key key) { 83 | Node cur = first; 84 | while (cur != null) { 85 | if (cur.key.equals(key)) 86 | return cur.value; 87 | cur = cur.next; 88 | } 89 | return null; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/tree/st/OrderedST.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.tree.st; 20 | 21 | import java.util.List; 22 | 23 | /** 24 | * @author geektcp on 2019/9/28. 25 | */ 26 | public interface OrderedST, Value> { 27 | 28 | int size(); 29 | 30 | void put(Key key, Value value); 31 | 32 | Value get(Key key); 33 | 34 | Key min(); 35 | 36 | Key max(); 37 | 38 | int rank(Key key); 39 | 40 | List keys(Key l, Key h); 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/tree/st/RedBlackBalancedSearchTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.tree.st; 20 | 21 | /** 22 | * @author geektcp on 2019/9/28. 23 | */ 24 | public class RedBlackBalancedSearchTree, Value> extends BalancedSearchTree { 25 | 26 | private static final boolean RED = true; 27 | private static final boolean BLACK = false; 28 | 29 | private boolean isRed(Node x) { 30 | return x!=null && x.color == RED; 31 | } 32 | 33 | public Node rotateLeft(Node h) { 34 | Node x = h.right; 35 | h.right = x.left; 36 | x.left = h; 37 | x.color = h.color; 38 | h.color = RED; 39 | x.N = h.N; 40 | recalculateSize(h); 41 | return x; 42 | } 43 | 44 | public Node rotateRight(Node h) { 45 | Node x = h.left; 46 | h.left = x.right; 47 | x.right = h; 48 | x.color = h.color; 49 | h.color = RED; 50 | x.N = h.N; 51 | recalculateSize(h); 52 | return x; 53 | } 54 | 55 | void flipColors(Node h) { 56 | h.color = RED; 57 | h.left.color = BLACK; 58 | h.right.color = BLACK; 59 | } 60 | 61 | @Override 62 | public void put(Key key, Value value) { 63 | root = put(root, key, value); 64 | root.color = BLACK; 65 | } 66 | 67 | private Node put(Node x, Key key, Value value) { 68 | if (x == null) { 69 | Node node = new Node(key, value, 1); 70 | node.color = RED; 71 | return node; 72 | } 73 | int cmp = key.compareTo(x.key); 74 | if (cmp == 0) 75 | x.value = value; 76 | else if (cmp < 0) 77 | x.left = put(x.left, key, value); 78 | else 79 | x.right = put(x.right, key, value); 80 | 81 | if (isRed(x.right) && !isRed(x.left)) 82 | x = rotateLeft(x); 83 | if (isRed(x.left) && isRed(x.left.left)) 84 | x = rotateRight(x); 85 | if (isRed(x.left) && isRed(x.right)) 86 | flipColors(x); 87 | 88 | recalculateSize(x); 89 | return x; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/tree/st/UnorderedST.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.tree.st; 20 | 21 | /** 22 | * @author geektcp on 2019/9/28. 23 | */ 24 | public interface UnorderedST { 25 | 26 | int size(); 27 | 28 | Value get(Key key); 29 | 30 | void put(Key key, Value value); 31 | 32 | void delete(Key key); 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/util/GetterUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.util; 20 | 21 | import java.util.List; 22 | import java.util.Map; 23 | import java.util.Objects; 24 | 25 | /** 26 | * @author geektcp on 2017/12/6. 27 | */ 28 | public class GetterUtils { 29 | 30 | public static String get(String key, Map map) { 31 | return get(key, "", map); 32 | } 33 | 34 | public static String get(String key, String defaultValue, Map map) { 35 | if (map == null || !map.containsKey(key)) { 36 | return defaultValue; 37 | } 38 | return Objects.toString(map.get(key), ""); 39 | } 40 | 41 | @SuppressWarnings("unchecked") 42 | public static List> getListMap(Object obj) { 43 | return (List>) obj; 44 | } 45 | 46 | @SuppressWarnings("unchecked") 47 | public static Map getMap(Object obj) { 48 | return (Map) obj; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/util/HttpUtils.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.util; 2 | 3 | import com.geektcp.common.mosheh.exception.BaseException; 4 | import com.geektcp.common.mosheh.system.Sys; 5 | import java.io.InputStream; 6 | import java.io.OutputStream; 7 | import java.net.HttpURLConnection; 8 | import java.net.URL; 9 | import java.util.Objects; 10 | 11 | /** 12 | * @author geektcp on 2023/9/14 0:12. 13 | */ 14 | public class HttpUtils { 15 | 16 | public static HttpURLConnection buildConnect(String imgUrl, 17 | String method, 18 | Integer connectTimeout, 19 | Integer readTimeout, 20 | Boolean useCache, 21 | Boolean followRedirects) { 22 | try { 23 | SslUtils.ignoreSsl(); 24 | URL url = new URL(imgUrl); 25 | HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); 26 | httpConnection.setRequestMethod(method); 27 | httpConnection.setUseCaches(useCache); 28 | httpConnection.setInstanceFollowRedirects(followRedirects); 29 | httpConnection.setConnectTimeout(connectTimeout); 30 | httpConnection.setReadTimeout(readTimeout); 31 | return httpConnection; 32 | } catch (Exception e) { 33 | throw new BaseException(752, e.getMessage()); 34 | } 35 | } 36 | 37 | public static boolean close(InputStream inStream, OutputStream outStream, HttpURLConnection httpConnection) { 38 | try { 39 | if (Objects.nonNull(inStream)) { 40 | inStream.close(); 41 | } 42 | if (Objects.nonNull(outStream)) { 43 | outStream.close(); 44 | } 45 | if (Objects.nonNull(httpConnection)) { 46 | httpConnection.disconnect(); 47 | } 48 | return true; 49 | } catch (Exception e) { 50 | Sys.p(e.getMessage()); 51 | return false; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/util/JsonUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package com.geektcp.common.mosheh.util; 20 | 21 | import com.alibaba.fastjson.JSON; 22 | import com.alibaba.fastjson.TypeReference; 23 | 24 | import java.util.List; 25 | import java.util.Map; 26 | 27 | /** 28 | * @author geektcp on 2018/4/12. 29 | */ 30 | public class JsonUtils { 31 | 32 | private JsonUtils() { 33 | } 34 | 35 | public static Map toMap(T bean) { 36 | String text = JSON.toJSONString(bean); 37 | return JSON.parseObject(text, new TypeReference>() { 38 | }); 39 | } 40 | 41 | public static List> toListMap(List beanList) { 42 | String text = JSON.toJSONString(beanList); 43 | return JSON.parseObject(text, new TypeReference>>() { 44 | }); 45 | } 46 | 47 | public static Map jsonToMap(String json) { 48 | return JSON.parseObject(json, new TypeReference>() { 49 | }); 50 | } 51 | 52 | public static List> jsonToListMap(String json) { 53 | return JSON.parseObject(json, new TypeReference>>() { 54 | }); 55 | } 56 | 57 | public static List> toListMap(Object obj) { 58 | String json = JSON.toJSONString(obj); 59 | return JSON.parseObject(json, new TypeReference>>() { 60 | }); 61 | } 62 | 63 | public static D copy(S srcBean, Class dstClass) { 64 | return JSON.parseObject(JSON.toJSONString(srcBean), dstClass); 65 | } 66 | 67 | public static String toString(T object) { 68 | return toString(object, true); 69 | } 70 | 71 | public static String toString(T object, boolean isPretty) { 72 | return JSON.toJSONString(object, isPretty); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/util/ObjectUtils.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.util; 2 | 3 | import com.geektcp.common.mosheh.exception.BaseException; 4 | 5 | import java.io.ByteArrayInputStream; 6 | import java.io.ByteArrayOutputStream; 7 | import java.io.ObjectInputStream; 8 | import java.io.ObjectOutputStream; 9 | import java.util.List; 10 | import java.util.Objects; 11 | 12 | /** 13 | * @author geektcp on 2023/8/6 15:53. 14 | */ 15 | public class ObjectUtils { 16 | 17 | public static T deepCopyClass(Object object, Class clazz) { 18 | Object ret = deepCopy(object); 19 | 20 | return clazz.cast(ret); 21 | } 22 | 23 | public static List deepCopyList(Object object, Class> clazz) { 24 | Object ret = deepCopy(object); 25 | 26 | return clazz.cast(ret); 27 | } 28 | 29 | 30 | /* 31 | * para list will be modified when excute recursive inserting, so need deepcopy 32 | * 33 | * */ 34 | public static Object deepCopy(Object obj) { 35 | if(Objects.isNull(obj)){ 36 | return null; 37 | } 38 | try { 39 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 40 | ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); 41 | objectOutputStream.writeObject(obj); 42 | ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); 43 | ObjectInputStream objInputStream = new ObjectInputStream(inputStream); 44 | return objInputStream.readObject(); 45 | } catch (Exception e) { 46 | throw new BaseException(e.getMessage()); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/util/SeparatorUtils.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.util; 2 | 3 | /** 4 | * @author geektcp on 2023/9/14 0:29. 5 | */ 6 | public class SeparatorUtils { 7 | 8 | public static final String SEPARATOR_LOG = "|"; 9 | public static final String SEPARATOR_DIR = "/"; 10 | public static final String SEPARATOR_WORD = ","; 11 | public static final String SEPARATOR_STRING = "_"; 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/util/Sortable.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.util; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * @author geektcp on 2023/8/6 14:00. 7 | */ 8 | public interface Sortable extends Serializable { 9 | 10 | /** 11 | * only a method. the sort field can be any name. 12 | * for example: sorting , id, num etc. 13 | * just return the sort field as result and it is ok. 14 | * 15 | * if someone use lombok, check that sort field type must be Integer type. 16 | * correct example: 17 | * @Data 18 | * public class AnyThingPo implements Sortable { 19 | * private Integer sort; 20 | * } 21 | * 22 | * wrong example: 23 | * @Data 24 | * public class AnyThingPo implements Sortable { 25 | * private int sort; // do not use int type because return type is Integer 26 | * 27 | * @return return Integer type ,not int type. 28 | * 29 | */ 30 | Comparable getSort(); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/geektcp/common/mosheh/util/SslUtils.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.util; 2 | 3 | import com.geektcp.common.mosheh.system.Sys; 4 | import java.security.cert.X509Certificate; 5 | import javax.net.ssl.HostnameVerifier; 6 | import javax.net.ssl.HttpsURLConnection; 7 | import javax.net.ssl.SSLContext; 8 | import javax.net.ssl.SSLSession; 9 | import javax.net.ssl.TrustManager; 10 | import javax.net.ssl.X509TrustManager; 11 | 12 | /** 13 | * @author geektcp on 2023/9/13 21:04. 14 | */ 15 | 16 | public class SslUtils { 17 | 18 | private static final String SSL_KEY = "SSL"; 19 | 20 | private static void trustAllHttpsCertificates() { 21 | TrustManager[] trustAllCerts = new TrustManager[1]; 22 | TrustManager tm = new miTM(); 23 | trustAllCerts[0] = tm; 24 | try { 25 | SSLContext sc = SSLContext.getInstance(SSL_KEY); 26 | sc.init(null, trustAllCerts, null); 27 | HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 28 | } catch (Exception e) { 29 | Sys.p(e.getMessage()); 30 | } 31 | } 32 | 33 | private static class miTM implements TrustManager, X509TrustManager { 34 | public X509Certificate[] getAcceptedIssuers() { 35 | return null; 36 | } 37 | 38 | public void checkServerTrusted(X509Certificate[] certs, String authType) { 39 | } 40 | 41 | public void checkClientTrusted(X509Certificate[] certs, String authType) { 42 | } 43 | } 44 | 45 | public static void ignoreSsl() { 46 | trustAllHttpsCertificates(); 47 | HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { 48 | public boolean verify(String urlHostName, SSLSession session) { 49 | return true; 50 | } 51 | }); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/cache/LruCacheTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.cache; 2 | 3 | 4 | import com.geektcp.common.mosheh.cache.common.LruCache; 5 | import com.geektcp.common.mosheh.system.Sys; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | public class LruCacheTest { 10 | 11 | @Test 12 | public void test() { 13 | LruCache lruCache = new LruCache<>(); 14 | 15 | lruCache.put("aaa", 11); 16 | lruCache.put("bbb", 22); 17 | lruCache.put("ccc", 33); 18 | lruCache.put("ddd", 44); 19 | 20 | Sys.p(lruCache.get("ddd")); 21 | 22 | Assert.assertTrue(true); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/concurrent/thread/ScheduleTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.concurrent.thread; 2 | 3 | import com.geektcp.common.mosheh.system.Sys; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | import java.util.List; 8 | import java.util.Queue; 9 | import java.util.concurrent.*; 10 | 11 | /** 12 | * @author Mr.Tang on 2023/4/23 10:47. 13 | */ 14 | public class ScheduleTest { 15 | 16 | public static void main(String[] args) { 17 | test2(); 18 | } 19 | 20 | private static void test1() { 21 | Sys.p("start"); 22 | Queue queue = new ConcurrentLinkedDeque<>( 23 | Arrays.asList("number 1", "number 2", "number 3", "number 4") 24 | ); 25 | 26 | ScheduledExecutorService pool = Executors.newScheduledThreadPool(1); 27 | for (int i = 0; i < queue.size(); i++) { 28 | ScheduledFuture future = pool.schedule(new Callable() { 29 | @Override 30 | public String call() { 31 | Sys.p(Thread.currentThread().getName() + " " + System.currentTimeMillis() + " current job:" + queue.poll()); 32 | Sys.sleep(2000); 33 | return "callSchedule"; 34 | } 35 | }, 1, TimeUnit.SECONDS); 36 | } 37 | } 38 | 39 | private static void test2() { 40 | Sys.p("start"); 41 | Queue queue = new ConcurrentLinkedDeque<>( 42 | Arrays.asList("number 1", "number 2", "number 3", "number 4") 43 | ); 44 | 45 | ScheduledExecutorService pool = Executors.newScheduledThreadPool(1); 46 | 47 | pool.scheduleAtFixedRate(() -> { 48 | int num = 0; 49 | Sys.p(Thread.currentThread().getName() + " " + System.currentTimeMillis() + " current job:" + queue.poll()); 50 | Sys.sleep(2000); 51 | num++; 52 | Sys.p("num: {}", num); 53 | if (num > 10) { 54 | pool.shutdown(); 55 | } 56 | }, 1, 2, TimeUnit.SECONDS); 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/concurrent/thread/executor/TinyExecutorServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.concurrent.thread.executor; 2 | 3 | import com.geektcp.common.mosheh.generator.IdGenerator; 4 | import com.geektcp.common.mosheh.system.Sys; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.concurrent.Future; 11 | import java.util.concurrent.TimeUnit; 12 | 13 | /** 14 | * @author geektcp on 2023/2/18 22:05. 15 | */ 16 | public class TinyExecutorServiceTest { 17 | 18 | 19 | @Test 20 | public void test() { 21 | ThyExecutor thyExecutor = ThyExecutor.getInstance(); 22 | Future future = thyExecutor.submit(() -> ( 23 | Sys.getResourceClassPath(TinyExecutorServiceTest.class))); 24 | try { 25 | String result = future.get(5, TimeUnit.SECONDS); 26 | Sys.p(result); 27 | } catch (Exception e) { 28 | Sys.p(e.getMessage()); 29 | } 30 | thyExecutor.cleanup(); 31 | Assert.assertTrue(true); 32 | } 33 | 34 | @Test 35 | public void test2() { 36 | ThyExecutor thyExecutor = new ThyExecutor(15); 37 | List> result = new ArrayList<>(); 38 | for (int i = 0; i < 100; i++) { 39 | Future future = thyExecutor.submit(() -> ( 40 | getId() 41 | )); 42 | result.add(future); 43 | } 44 | 45 | result.forEach(future -> { 46 | try { 47 | Long ret = future.get(5, TimeUnit.SECONDS); 48 | Sys.p(ret); 49 | } catch (Exception e) { 50 | Sys.p(e.getMessage()); 51 | } 52 | }); 53 | 54 | // tinyExecutorService.cleanup(); 55 | 56 | Sys.sleep(5); 57 | Assert.assertTrue(true); 58 | } 59 | 60 | @Test 61 | public void test3() { 62 | ThyExecutor thyExecutor = ThyExecutor.getInstance(); 63 | thyExecutor.submit(() -> ( 64 | Sys.getResourceClassPath(TinyExecutorServiceTest.class) 65 | )); 66 | 67 | // tinyExecutorService.cleanup(); 68 | Assert.assertTrue(true); 69 | } 70 | 71 | private Long getId() { 72 | long id = IdGenerator.getId(); 73 | Sys.sleep(1000); 74 | Sys.p("id: {}, thread id: {}", id, Thread.currentThread().getId()); 75 | return id; 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/generator/GeneratorTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.generator; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author geektcp on 2023/2/5 1:55. 8 | */ 9 | public class GeneratorTest { 10 | 11 | @Test 12 | public void base() { 13 | long a = 0; 14 | String c = null; 15 | 16 | a = IdGenerator.getId(); 17 | System.out.println(a); 18 | a = IdGenerator.getId(1, 3); 19 | 20 | System.out.println(a); 21 | IdGenerator.setSeparator("/"); 22 | c = IdGenerator.getId("PRE"); 23 | 24 | System.out.println(c); 25 | IdGenerator.getInstance(10, 0); 26 | 27 | a = IdGenerator.getId(); 28 | 29 | System.out.println(a); 30 | Assert.assertTrue(true); 31 | } 32 | 33 | @Test 34 | public void singleGen() { 35 | for (int i = 0; i < 100; i++) { 36 | System.out.println(IdGenerator.getId()); 37 | } 38 | Assert.assertTrue(true); 39 | } 40 | 41 | @Test 42 | public void multiThread() { 43 | new Thread(() -> { 44 | for (int i = 0; i < 100; i++) { 45 | System.out.println(IdGenerator.getId("A")); 46 | } 47 | }, "A").start(); 48 | 49 | new Thread(() -> { 50 | for (int i = 0; i < 100; i++) { 51 | System.out.println(IdGenerator.getId("B")); 52 | } 53 | }, "A").start(); 54 | 55 | Assert.assertTrue(true); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/socket/SocketServerBuilderTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.socket; 2 | 3 | import com.geektcp.common.mosheh.system.Sys; 4 | 5 | import java.io.*; 6 | import java.net.ServerSocket; 7 | import java.net.Socket; 8 | 9 | /** 10 | * @author geektcp on 2023/4/2 21:56. 11 | */ 12 | public class SocketServerBuilderTest { 13 | 14 | 15 | public static void main(String[] args) { 16 | try { 17 | ServerSocket ss = new ServerSocket(8888); 18 | Sys.p("启动服务器...."); 19 | Socket s = ss.accept(); 20 | Sys.p("客户端:" + s.getInetAddress().getLocalHost() + "已连接到服务器"); 21 | 22 | BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); 23 | //读取客户端发送来的消息 24 | String mess = br.readLine(); 25 | Sys.p("客户端:" + mess); 26 | BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 27 | bw.write(mess + "\n"); 28 | bw.flush(); 29 | } catch (IOException e) { 30 | e.printStackTrace(); 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/socket/nio/NIOServerSimple.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.socket.nio; 2 | 3 | import java.io.IOException; 4 | import java.net.InetSocketAddress; 5 | import java.net.ServerSocket; 6 | import java.nio.ByteBuffer; 7 | import java.nio.channels.SelectionKey; 8 | import java.nio.channels.Selector; 9 | import java.nio.channels.ServerSocketChannel; 10 | import java.nio.channels.SocketChannel; 11 | import java.util.Iterator; 12 | import java.util.Set; 13 | 14 | /** 15 | * @author tanghaiyang on 2019/9/3. 16 | */ 17 | public class NIOServerSimple { 18 | public static void main(String[] args) throws IOException { 19 | Selector selector = Selector.open(); 20 | ServerSocketChannel ssChannel = ServerSocketChannel.open(); 21 | ssChannel.configureBlocking(false); 22 | ssChannel.register(selector, SelectionKey.OP_ACCEPT); 23 | 24 | ServerSocket serverSocket = ssChannel.socket(); 25 | InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8888); 26 | serverSocket.bind(address); 27 | 28 | while (true) { 29 | try { 30 | selector.select(); 31 | Set keys = selector.selectedKeys(); 32 | Iterator keyIterator = keys.iterator(); 33 | while (keyIterator.hasNext()) { 34 | SelectionKey key = keyIterator.next(); 35 | if (key.isAcceptable()) { 36 | ServerSocketChannel ssChannel1 = (ServerSocketChannel) key.channel(); 37 | 38 | // 服务器会为每个新连接创建一个 SocketChannel 39 | SocketChannel sChannel = ssChannel1.accept(); 40 | sChannel.configureBlocking(false); 41 | 42 | // 这个新连接主要用于从客户端读取数据 43 | sChannel.register(selector, SelectionKey.OP_READ); 44 | } else if (key.isReadable()) { 45 | SocketChannel sChannel = (SocketChannel) key.channel(); 46 | System.out.println(readDataFromSocketChannel(sChannel)); 47 | sChannel.close(); 48 | } 49 | keyIterator.remove(); 50 | } 51 | }catch (Exception e){ 52 | System.out.println(e.getMessage()); 53 | break; 54 | } 55 | } 56 | } 57 | 58 | private static String readDataFromSocketChannel(SocketChannel sChannel) throws IOException { 59 | ByteBuffer buffer = ByteBuffer.allocate(1024); 60 | StringBuilder data = new StringBuilder(); 61 | 62 | while (true) { 63 | 64 | buffer.clear(); 65 | int n = sChannel.read(buffer); 66 | if (n == -1) { 67 | break; 68 | } 69 | buffer.flip(); 70 | int limit = buffer.limit(); 71 | char[] dst = new char[limit]; 72 | for (int i = 0; i < limit; i++) { 73 | dst[i] = (char) buffer.get(i); 74 | } 75 | data.append(dst); 76 | buffer.clear(); 77 | } 78 | return data.toString(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/socket/simple/ClientTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package com.geektcp.common.mosheh.socket.simple; 19 | 20 | import com.geektcp.common.mosheh.system.Sys; 21 | import org.junit.Assert; 22 | import org.junit.Test; 23 | 24 | import java.io.IOException; 25 | import java.io.OutputStream; 26 | import java.net.InetSocketAddress; 27 | import java.net.Socket; 28 | 29 | /** 30 | * Created by geektcp on 2018/11/24. 31 | */ 32 | 33 | public class ClientTest { 34 | public static void main(String[] args) throws Exception { 35 | for (int i = 3300; i < 10000; i++) { 36 | Sys.p("start check port: {}", i); 37 | Socket socket = new Socket(); 38 | socket.setSoTimeout(11); 39 | socket.connect(new InetSocketAddress("geektcp.com", i), 200); 40 | Sys.p("port is opened: {}", i); 41 | socket.close(); 42 | 43 | 44 | } 45 | } 46 | 47 | 48 | @Test 49 | public void connect() throws Exception { 50 | Socket socket = new Socket(); 51 | socket.setSoTimeout(11); 52 | socket.connect(new InetSocketAddress("localhost", 999), 200); 53 | Sys.p("port is opened: {}", 999); 54 | 55 | Assert.assertTrue(true); 56 | } 57 | 58 | @Test 59 | public void connectNIO() throws IOException { 60 | Socket socket = new Socket("127.0.0.1", 999); 61 | OutputStream out = socket.getOutputStream(); 62 | String s = "hello world"; 63 | out.write(s.getBytes()); 64 | out.close(); 65 | 66 | Assert.assertTrue(true); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/socket/simple/ServerTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.socket.simple; 2 | 3 | import com.geektcp.common.mosheh.system.Sys; 4 | 5 | import java.io.*; 6 | import java.net.ServerSocket; 7 | import java.net.Socket; 8 | 9 | /** 10 | * Created by geektcp on 2018/11/24. 11 | */ 12 | 13 | public class ServerTest { 14 | 15 | public static void main(String[] args) throws Exception { 16 | int port = 999; 17 | ServerSocket serverSocket = new ServerSocket(port); 18 | Sys.p("start accept ..."); 19 | Socket connection = serverSocket.accept(); 20 | Sys.p("accepting ..."); 21 | InputStream ins = connection.getInputStream(); 22 | Reader reader = new InputStreamReader(ins, "utf-8"); 23 | BufferedReader br = new BufferedReader(reader); 24 | 25 | // StringBuffer sb = new StringBuffer(); 26 | // for(int c = reader.read(); c!=-1; c=reader.read() ){ 27 | // sb.append((char)c); 28 | // 29 | // Sys.p("sb: {}", sb); 30 | // } 31 | 32 | // SelectionKey; 33 | // DefaultSelectorProvider 34 | 35 | String line = null; 36 | while (( line = br.readLine() ) != null){ 37 | Sys.p("line: {}", line); 38 | 39 | if(line.equals("") |line.length()==0){ 40 | break; 41 | } 42 | } 43 | 44 | OutputStream out = connection.getOutputStream(); 45 | Writer writer = new OutputStreamWriter(out, "UTF-8"); 46 | 47 | String str = "sdfsdfsdfs"; 48 | 49 | Sys.p(str); 50 | writer.write("3333333\r\n"); 51 | writer.flush(); 52 | 53 | // out.write((str ).getBytes() ); 54 | // out.flush(); 55 | 56 | Thread.sleep(3000); 57 | connection.close(); 58 | serverSocket.close(); 59 | 60 | } 61 | 62 | 63 | 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/system/SysTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.system; 2 | 3 | import com.geektcp.common.mosheh.util.JsonUtils; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Properties; 8 | 9 | import org.junit.Assert; 10 | import org.junit.Test; 11 | 12 | /** 13 | * @author geektcp on 2023/2/4 23:37. 14 | */ 15 | public class SysTest { 16 | 17 | @Test 18 | public void testPrint() { 19 | Sys.println("aaa"); 20 | Sys.println("123"); 21 | 22 | Assert.assertTrue(true); 23 | } 24 | 25 | @Test 26 | public void testRuntime() { 27 | Sys.p(Sys.availableProcessors()); 28 | Sys.p(Sys.maxMemory()); 29 | Sys.p(Sys.totalMemory()); 30 | 31 | Assert.assertTrue(true); 32 | } 33 | 34 | @Test 35 | public void testProperties() { 36 | Properties prop = Sys.getProperties(); 37 | Sys.p(JsonUtils.toString(prop)); 38 | 39 | Assert.assertTrue(true); 40 | } 41 | 42 | @Test 43 | public void testVar() { 44 | Sys.p(Sys.getResourceRootPath()); 45 | Sys.p(Sys.getUserHome()); 46 | Sys.p(Sys.getFileSeparator()); 47 | Sys.p(Sys.getJdkVersion()); 48 | Sys.p(Sys.getUserHome()); 49 | Sys.p(Sys.getOsName()); 50 | Sys.p(Sys.getUserLanguage()); 51 | Sys.p(Sys.getUserName()); 52 | 53 | Assert.assertTrue(true); 54 | } 55 | 56 | @Test 57 | public void testEnv() { 58 | // windows 59 | Sys.p(Sys.getEnv("JAVA_HOME")); 60 | Sys.p(Sys.getEnv("Path")); 61 | 62 | // linux 63 | Sys.p(Sys.getEnv("PATH")); 64 | Sys.p(Sys.getEnv("HOME")); 65 | Assert.assertTrue(true); 66 | } 67 | 68 | @Test 69 | public void testClass() { 70 | // windows 71 | Sys.p(Sys.getClassPath("com.geektcp.common.mosheh.generator.IdGenerator")); 72 | 73 | Assert.assertTrue(true); 74 | } 75 | 76 | 77 | @Test 78 | public void testList(){ 79 | List list = new ArrayList<>(); 80 | list.add("s1"); 81 | list.add("s2"); 82 | list.add("111"); 83 | list.add("222"); 84 | list.add("333"); 85 | list.add(0,"thy"); 86 | Sys.p(list.get(0)); 87 | Assert.assertTrue(true); 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/system/ThyCommandTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.system; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class ThyCommandTest { 7 | 8 | @Test 9 | public void testPrint() throws Exception { 10 | 11 | Process process = Sys.getCommandBuilder() 12 | .runDir("/tmp") 13 | .program("netstat") 14 | .arg("-a") 15 | .arg("-n") 16 | .arg("-t") 17 | .arg("-p") 18 | .start(); 19 | Sys.printCommandResult(process); 20 | 21 | Assert.assertTrue(true); 22 | } 23 | 24 | 25 | @Test 26 | public void testExec() throws Exception { 27 | Process process = Sys.exec("ls -l"); 28 | Sys.printCommandResult(process); 29 | 30 | Assert.assertTrue(true); 31 | } 32 | 33 | @Test 34 | public void testExec2() throws Exception { 35 | Process process = Sys.getCommandBuilder() 36 | .program("pwd") 37 | .start(); 38 | Sys.printCommandResult(process); 39 | 40 | Assert.assertTrue(true); 41 | } 42 | 43 | @Test 44 | public void testExec3() throws Exception { 45 | Process process = Sys.getCommandBuilder() 46 | .runDir("/tmp") 47 | .program("netstat") 48 | .arg("-a") 49 | .arg("-n") 50 | .arg("-t") 51 | .arg("-p") 52 | .start(); 53 | Sys.printCommandResult(process); 54 | 55 | Assert.assertTrue(true); 56 | } 57 | 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/system/ThyEncryptTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.system; 2 | 3 | 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | /** 8 | * @author geektcp on 2023/2/15 0:27. 9 | */ 10 | public class ThyEncryptTest { 11 | 12 | 13 | @Test 14 | public void test() { 15 | Sys.p(Sys.encrypt("1111")); 16 | Sys.p(Sys.decrypt( 17 | "0ff2aaffd3ecf1b3b24dd04b6046424351b6038c8136286a2738c2ec7d0fc5988b1f6a23da0cd" + 18 | "5a44fb2e74b2bf63dd5c40adf4965ca1df0bd38838b0718b3f287aed7cd945235dc1cc7c8bb" + 19 | "bb3775aee2a9157a5299a2151e4c691fd7f4d1b3e3dad04aa6429d4db67092711158aac1eb6" + 20 | "ec06f2ee0133cd05f7a4b75487939")); 21 | Assert.assertTrue(true); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/system/ThyFileReaderTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.system; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.util.Map; 7 | 8 | public class ThyFileReaderTest { 9 | 10 | @Test 11 | public void test2(){ 12 | Sys.p("111: {}", Sys.getResourcePath()); 13 | Sys.p("222: {}", Sys.getResourceRootPath()); 14 | Sys.p("444: {}", Sys.getResourcePath("dist.json")); 15 | Sys.p("555: {}", Sys.getResourcePath("/aaa/dist.json")); 16 | Sys.p("666: {}", Sys.getResourcePath("ThyFileSystem.class")); 17 | 18 | Assert.assertTrue(true); 19 | } 20 | 21 | @Test 22 | public void read(){ 23 | Map map = Sys.readXmlFile(Sys.getResourcePath("test.xml")); 24 | 25 | 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/system/ThyHostTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.system; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author geektcp on 2023/2/13 20:36. 8 | */ 9 | public class ThyHostTest { 10 | 11 | @Test 12 | public void getIp(){ 13 | Sys.p(ThyHost.getPublicIp()); 14 | Sys.p(ThyHost.getPrivateIp()); 15 | Sys.p(ThyHost.getHostName()); 16 | Sys.p(ThyHost.getLoopbackIp()); 17 | Assert.assertTrue(true); 18 | } 19 | 20 | 21 | @Test 22 | public void getIp2(){ 23 | Sys.p(Sys.getPublicIp()); 24 | Sys.p(Sys.getPrivateIp()); 25 | Sys.p(Sys.getHostName()); 26 | Sys.p(Sys.getLoopbackIp()); 27 | Assert.assertTrue(true); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/system/ThyResourceTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.system; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author geektcp on 2023/2/13 20:00. 8 | */ 9 | public class ThyResourceTest { 10 | 11 | @Test 12 | public void getPath(){ 13 | Sys.p(ThyResource.getResourceClassPath()); 14 | Sys.p(ThyResource.getResourcePath()); 15 | 16 | Sys.p(Sys.getResourceClassPath(ThyResourceTest.class)); 17 | Sys.p(Sys.getResourcePath()); 18 | 19 | Assert.assertTrue(true); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/system/ThyStreamTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.system; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class ThyStreamTest { 7 | 8 | @Test 9 | public void test(){ 10 | String str = "abc: {}|{}/{}"; 11 | int[] indices = new int[3]; 12 | Sys.p(str, "yju", 555); 13 | 14 | Assert.assertTrue(true); 15 | } 16 | 17 | @Test 18 | public void test2(){ 19 | Sys.p("ssss: {}|{}", "ttt", 1111,"ssf","gggg"); 20 | 21 | Assert.assertTrue(true); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/tree/ListNodeTreeTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.tree; 2 | 3 | import com.geektcp.common.mosheh.collection.Lists; 4 | import com.geektcp.common.mosheh.system.Sys; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * @author geektcp on 2023/8/2 22:49. 10 | */ 11 | public class ListNodeTreeTest { 12 | 13 | 14 | public static void main(String[] args) throws Exception { 15 | List voList = Lists.newArrayList(); 16 | 17 | UserDirVo vo1 = new UserDirVo<>(); 18 | vo1.setTitle("xxx"); 19 | vo1.setNodeId("111"); 20 | vo1.setParentNodeId(null); 21 | voList.add(vo1); 22 | 23 | UserDirVo vo2 = new UserDirVo<>(); 24 | vo2.setTitle("xxx"); 25 | vo2.setNodeId("222"); 26 | vo2.setParentNodeId(null); 27 | voList.add(vo2); 28 | 29 | UserDirVo vo3 = new UserDirVo<>(); 30 | vo3.setTitle("xxx"); 31 | vo3.setNodeId("333"); 32 | vo3.setParentNodeId("111"); 33 | voList.add(vo3); 34 | 35 | UserDirVo vo4 = new UserDirVo<>(); 36 | // vo4.setTitle("xxx"); 37 | vo4.setNodeId("444"); 38 | vo4.setParentNodeId("222"); 39 | voList.add(vo4); 40 | 41 | UserDirVo vo5 = new UserDirVo<>(); 42 | vo5.setTitle("xxx"); 43 | vo5.setNodeId("555"); 44 | vo5.setParentNodeId("333"); 45 | voList.add(vo5); 46 | 47 | UserDirVo vo6 = new UserDirVo<>(); 48 | vo6.setTitle("xxx"); 49 | vo6.setNodeId("666"); 50 | vo6.setParentNodeId("444"); 51 | voList.add(vo6); 52 | 53 | UserDirVo vo7 = new UserDirVo<>(); 54 | vo7.setTitle("xxx"); 55 | vo7.setNodeId("777"); 56 | vo7.setParentNodeId("555"); 57 | voList.add(vo7); 58 | 59 | 60 | UserDirVo tree = ListNodeTree.createTree(voList, UserDirVo.class); 61 | 62 | Sys.pretty(tree); 63 | } 64 | 65 | 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/tree/UserDirVo.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.tree; 2 | 3 | import com.geektcp.common.mosheh.tree.node.AbstractListNode; 4 | import lombok.Data; 5 | import lombok.EqualsAndHashCode; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @author Geektcp 2021/9/24 9:57 11 | */ 12 | @Data 13 | @EqualsAndHashCode(callSuper = false) 14 | public class UserDirVo extends AbstractListNode { 15 | 16 | private String title; 17 | 18 | private String type; 19 | 20 | private String url; 21 | 22 | private String description; 23 | 24 | 25 | public String toString() { 26 | return super.toString(); 27 | } 28 | 29 | public boolean add(AbstractListNode childNode) { 30 | return true; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/util/Base64UtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.util; 2 | 3 | import com.geektcp.common.mosheh.system.Sys; 4 | 5 | /** 6 | * @author geektcp on 2023/9/13 21:47. 7 | */ 8 | public class Base64UtilsTest { 9 | public static void main(String[] args) { 10 | String base64Data = Base64Utils.getBase64FromUrl("https://baidu.com/favicon.ico"); 11 | Sys.p(base64Data.length()); 12 | Sys.p(base64Data); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/util/CollectionUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.util; 2 | 3 | import com.geektcp.common.mosheh.collection.Lists; 4 | import com.geektcp.common.mosheh.system.Sys; 5 | 6 | import java.util.List; 7 | 8 | 9 | /** 10 | * @author geektcp on 2023/8/6 14:40. 11 | */ 12 | public class CollectionUtilsTest { 13 | 14 | public static void main(String[] args) { 15 | List list = Lists.newArrayList(); 16 | 17 | UserSitePo po1 = new UserSitePo(); 18 | po1.setName("abc"); 19 | po1.setSort(3); 20 | list.add(po1); 21 | 22 | UserSitePo po2 = new UserSitePo(); 23 | po2.setName("ttttt"); 24 | po2.setSort(8); 25 | list.add(po2); 26 | 27 | UserSitePo po3 = new UserSitePo(); 28 | po3.setName("xxxx"); 29 | po3.setSort(2); 30 | list.add(po3); 31 | 32 | UserSitePo po4 = new UserSitePo(); 33 | po4.setName("addddbc"); 34 | po4.setSort(11); 35 | list.add(po4); 36 | 37 | Class cls = list.getClass(); 38 | List ret = CollectionUtils.sort(list); 39 | Sys.pretty(ret); 40 | List ret1 = CollectionUtils.sort(list, CollectionUtils.SORT_ASC); 41 | Sys.pretty(ret1); 42 | List ret2 = CollectionUtils.sort(list, CollectionUtils.SORT_DESC); 43 | Sys.pretty(ret2); 44 | 45 | } 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/util/DownUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.util; 2 | 3 | import com.geektcp.common.mosheh.system.Sys; 4 | 5 | /** 6 | * @author geektcp on 2023/9/14 0:17. 7 | */ 8 | public class DownUtilsTest { 9 | public static void main(String[] args) { 10 | boolean ret = DownUtils.downFileFromUrl("https://baidu.com/favicon.ico", "aaa.png"); 11 | Sys.p(ret); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/util/FileUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.util; 2 | 3 | /** 4 | * @author geektcp on 2023/2/5 2:30. 5 | */ 6 | public class FileUtilsTest { 7 | } 8 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/util/ThyEncryptTest.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.util; 2 | 3 | import com.geektcp.common.mosheh.system.Sys; 4 | import org.junit.Assert; 5 | 6 | /** 7 | * @author geektcp on 2023/2/15 0:55. 8 | */ 9 | public class ThyEncryptTest { 10 | 11 | 12 | public static void main(String[] args) { 13 | test(); 14 | } 15 | 16 | 17 | public static void test(){ 18 | Sys.p(Sys.encrypt("11111")); 19 | Sys.p(Sys.decrypt("41f552c89c3b81c8c787740e7f43ed4a0e902fa07726975d7ddc365585a81e1bd5c4914de772b9467f6abeb52c551bec3ca0454b440ae852145c843a0b427d33f1cef64734a54d9f34975b5b8706e52f724760b0441900c20c60f7e6b1a9e19c571a719d6be3210bfb24489bf8a0efaed7229d11c5bf2f508b2d37fabb56b194")); 20 | Assert.assertTrue(true); 21 | } 22 | 23 | 24 | public static void test2(){ 25 | Sys.p(Sys.encrypt("11111")); 26 | Sys.p(Sys.decrypt("10604891205ea5b7aa5fdd72808da3d02fec4846e4b9fd4c9d58e4ecb4b0a4a5bc76ffb60e795efd90e580a620aef77da1cd123a9984b5981277181c984aba28c29c303f5a0b7204b9b9956e35260e41441e42a4b53d44ea2c972786dc018e274e05fbc63ba024cddf94998b5f4469aac033982d0187557da5a5061e70553fa8")); 27 | Assert.assertTrue(true); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/com/geektcp/common/mosheh/util/UserSitePo.java: -------------------------------------------------------------------------------- 1 | package com.geektcp.common.mosheh.util; 2 | 3 | import lombok.Data; 4 | import lombok.NoArgsConstructor; 5 | 6 | /** 7 | * @author Geektcp 2021/9/23 14:09 8 | */ 9 | public class UserSitePo implements Sortable { 10 | 11 | private String id; 12 | 13 | private String userBaseId; 14 | 15 | private String url; 16 | 17 | private String name; 18 | 19 | private String description; 20 | 21 | private String logo; 22 | 23 | private String siteLabelId; 24 | 25 | private Integer sort; 26 | 27 | 28 | public Integer getSort(){ 29 | return sort; 30 | } 31 | 32 | public void setName(String name){ 33 | this.name = name; 34 | } 35 | 36 | public String getName(){ 37 | return name; 38 | } 39 | 40 | public void setSort(int sort){ 41 | this.sort = sort; 42 | } 43 | } 44 | --------------------------------------------------------------------------------